Example #1
0
        public CommunicationResponse DeleteHousehold([FromBody] DeleteHouseholdRequest deleteHouseholdRequest)
        {
            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);
                }

                string     sessionId = Request.Headers["Authorization"].ToString();
                ActiveUser user      = _userService.GetUserInformationFromAuthHeader(sessionId);
                if (user.HouseId == 0)
                {
                    response.AddError("You must belong to a household", ErrorCode.USER_NOT_IN_HOUSEHOLD);
                    return(response);
                }

                _billRepository.DeleteHouseholdBills(user.HouseId);
                _shoppingRepository.DeleteHouseholdShoppingItems(user.HouseId);
                _toDoRepository.DeleteHouseholdToDoTask(user.HouseId);

                if (deleteHouseholdRequest.KeepHousehold == false)
                {
                    _houseRepository.DeleteHousehold(user.HouseId);
                    _userService.DeleteSession(sessionId);
                    _userService.UpdateHouseholdForUser(sessionId, -1);
                }
                response.Notifications = new List <string>
                {
                    $"The data for the household (ID: {user.HouseId}) have been deleted"
                };
            }
            catch (ErrorCodeException exception)
            {
                response.AddError($"An unexpected exception occured: {exception}", deleteHouseholdRequest, exception.Code);
            }
            catch (Exception exception)
            {
                response.AddError($"An unexpected exception occured: {exception}", deleteHouseholdRequest);
            }

            return(response);
        }