Ejemplo n.º 1
0
        public CommunicationResponse UpdateShoppingItem([FromBody] UpdateShoppingItemRequest shoppingRequest)
        {
            var response = new CommunicationResponse();

            try
            {
                if (_userService.AuthenticateSession(Request.Headers["Authorization"].ToString()) == false)
                {
                    response.AddError("The authorization credentails were invalid", ErrorCode.SESSION_INVALID);
                    return(response);
                }

                ShoppingValidator.CheckIfValidItem(shoppingRequest);
                _shoppingRepository.UpdateItem(shoppingRequest);

                response.Notifications = new List <string>
                {
                    $"The shopping item '{shoppingRequest.Name}' has been updated"
                };
            }
            catch (ErrorCodeException exception)
            {
                response.AddError($"An unexpected exception occured: {exception}", shoppingRequest, exception.Code);
            }
            catch (Exception exception)
            {
                response.AddError($"An unexpected exception occured: {exception}", shoppingRequest);
            }

            return(response);
        }
Ejemplo n.º 2
0
        public CommunicationResponse AddShoppingItem([FromBody] AddShoppingItemRequest shoppingRequest)
        {
            var response = new CommunicationResponse();

            try
            {
                if (_userService.AuthenticateSession(Request.Headers["Authorization"].ToString()) == false)
                {
                    response.AddError("The authorization credentails were invalid", ErrorCode.SESSION_INVALID);
                    return(response);
                }

                ActiveUser user = _userService.GetUserInformationFromAuthHeader(Request.Headers["Authorization"].ToString());
                if (user.HouseId == 0)
                {
                    response.AddError("You must belong to a household to add shopping items", ErrorCode.USER_NOT_IN_HOUSEHOLD);
                    return(response);
                }
                ShoppingItem item = new ShoppingItem
                {
                    ItemFor   = shoppingRequest.ItemFor,
                    Added     = DateTime.Now,
                    AddedBy   = user.PersonId,
                    Name      = shoppingRequest.Name,
                    Purchased = false
                };
                ShoppingValidator.CheckIfValidItem(item);
                _shoppingRepository.AddItem(item, user.HouseId);

                response.Notifications = new List <string>
                {
                    $"The shopping item '{shoppingRequest.Name}' has been added"
                };
            }
            catch (ErrorCodeException exception)
            {
                response.AddError($"An unexpected exception occured: {exception}", shoppingRequest, exception.Code);
            }
            catch (Exception exception)
            {
                response.AddError($"An unexpected exception occured: {exception}", shoppingRequest);
            }

            return(response);
        }