Пример #1
0
        public async Task <IActionResult> UpdateList(int listId, [FromBody] APIShoppingList shoppingListNewName, CancellationToken ct)
        {
            var currentUser  = HttpContext.User;
            var userclaim    = currentUser.Claims.First();
            var userId       = Guid.Parse(userclaim.Value);
            var shoppingUser = await _shoppingUserRepository.GetEntityAsync(userId, ct);

            if (shoppingUser == null)
            {
                return(BadRequest(new { error = "Could not find shopping user" }));
            }

            var shoppingList = await _shoppingListRepository.GetEntityAsync(listId, ct);

            if (shoppingList == null)
            {
                return(BadRequest(new { error = $"Error updating shopping list with new name {shoppingListNewName.Name}, Could not find shopping list" }));
            }

            shoppingList.Name = shoppingListNewName.Name;

            var updatedList = await _shoppingListRepository.UpdateEntity(shoppingList, ct);

            if (updatedList == null)
            {
                return(BadRequest(new { error = $"Error updating shopping list with new name {shoppingListNewName.Name}" }));
            }

            return(Ok($"Shopping list updated with new name \"{updatedList.Name}\""));
        }
Пример #2
0
        public async Task <IActionResult> PostShoppingList([FromBody] APIShoppingList shoppingList, CancellationToken ct)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { error = "Model state is not valid" }));
            }

            var currentUser  = HttpContext.User;
            var userclaim    = currentUser.Claims.First();
            var userId       = Guid.Parse(userclaim.Value);
            var shoppingUser = await _shoppingUserRepository.GetEntityAsync(userId, ct);

            if (shoppingUser != null)
            {
                ShoppingListEntity list = new ShoppingListEntity
                {
                    Name           = shoppingList.Name,
                    ShoppingUserId = shoppingUser.Id,
                    StoreId        = shoppingUser.HomeStoreId,
                    TimeOfCreation = DateTimeOffset.UtcNow
                };

                var listAdded = await _shoppingListRepository.AddShoppingList(list, ct);

                if (listAdded.Item1)
                {
                    shoppingUser.ShoppingLists.Add(list);
                    var user = await _shoppingUserRepository.UpdateEntity(shoppingUser, ct);

                    return(Ok(new ShoppingList {
                        Id = list.Id, Items = new List <ShoppingListItem>(), Name = list.Name, TimeOfCreation = list.TimeOfCreation, TotalCost = 0, TotalItems = 0
                    }));
                }
                else
                {
                    return(BadRequest(new { error = listAdded.Item2 }));
                }
            }

            return(BadRequest(new { error = "Could not find shopping user" }));
        }