public async Task <ActionResult <IEnumerable <ItemList> > > GetItemLists([FromQuery] GetListRequest request)
        {
            var itemList = await _context.ItemLists.Where(il => il.AccessCode == request.AccessCode).FirstAsync();

            itemList.Items = await _context.Items.Where(i => i.ListId == itemList.Id).ToListAsync();

            var items     = itemList.Items.Select(i => new { i.Id, i.Name, i.IsDone });
            var isStarred = false;

            try
            {
                var userId = UserIdGetter.getIdFromToken(Request, _context);
                isStarred = _context.UserLists.Where(ul => ul.ListId == itemList.Id)
                            .Select(ul => ul.UserId)
                            .Contains(userId);
            }
            catch {
            }
            if (itemList.IsPublic || itemList.ListPassword == request.ListPassword)
            {
                return(Ok(new SuccessfulGetListResponse()
                {
                    Name = itemList.Name,
                    AccessCode = itemList.AccessCode,
                    IsPublic = itemList.IsPublic,
                    Items = items,
                    IsStarred = isStarred
                }));
            }
            return(BadRequest("Wrong list access code or password"));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> GetStarred()
        {
            try
            {
                /*var tokenBearerEmail = TokenBearerValueGetter.getValue(Request, "email");
                 * var userId = await _context.Users.Where(u => u.Email == tokenBearerEmail)
                 *                               .Select(u => u.Id)
                 *                               .FirstAsync();*/
                var userId          = UserIdGetter.getIdFromToken(Request, _context);
                var starredListsIds = await _context.UserLists.Where(ul => ul.UserId == userId)
                                      .Select(ul => ul.ListId)
                                      .ToListAsync();

                var starredLists = await _context.ItemLists.Where(il => starredListsIds.Contains(il.Id))
                                   .Select(il => new { il.Name, il.AccessCode })
                                   .ToListAsync();

                return(Ok(starredLists));
            }
            catch {
                return(BadRequest("Nothing found"));
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult> DeleteStarred(StarListRequest request)
        {
            var list = await _context.ItemLists.Where(il => il.AccessCode == request.AccessCode).FirstAsync();

            var listAccessCode = list.AccessCode;

            if (listAccessCode != request.AccessCode)
            {
                return(BadRequest("Invalid request: pass"));
            }
            try
            {
                var userId           = UserIdGetter.getIdFromToken(Request, _context);
                var userListToDelete = await _context.UserLists.Where(ul => ul.ListId == list.Id && ul.UserId == userId).FirstAsync();

                _context.UserLists.Remove(userListToDelete);
                await _context.SaveChangesAsync();

                return(Ok("Unstarred a list"));
            }
            catch {
                return(BadRequest("Not logged in"));
            }
        }