コード例 #1
0
            public IEndpointHelperSetup AddShoppingItem(string name = null)
            {
                AddShoppingItemRequest request = new AddShoppingItemRequest
                {
                    Name    = name ?? "DEVELOPMENT TESTING SHOPPING ITEM",
                    ItemFor = new List <int> {
                        5
                    }
                };
                var requestBody = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
                var result      = _fakeSever.PostAsync("/Api/v2/Shopping", requestBody).Result;

                var responseBody = result.Content.ReadAsStringAsync().Result;

                return(this);
            }
コード例 #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);
        }