Esempio n. 1
0
        public async Task <IActionResult> CreateHouse([FromBody] HouseBindingModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var houseMate = _houseMateService.GetHouseMate(model.CreatorId);

                    if (houseMate.HouseId != 0)
                    {
                        throw new Exception("houseMate already is a member of a house");
                    }

                    //first add house to db
                    House house = new House();
                    ParseHouseFields(house, model);
                    //TODO: I dont need to return thisI don't think... somehow EF/.NET brings the new guy along...
                    House houseAdded = _houseService.AddHouse(house);

                    //Add houseId to houseMate and save in database
                    houseMate.HouseId = houseAdded.Id;
                    await _houseMateService.UpdateHouseMate(houseMate);

                    return(Ok(houseAdded));
                }
                catch (Exception)
                {
                    throw new Exception("there was an error adding the house");
                }
            }

            return(BadRequest("Shitty request"));
        }
        public async Task <IActionResult> UpdateHouseMate([FromBody] UpdateHouseMateBindingModel model)
        {
            //This method is currently used to update HouseId when a housemate joins a House
            if (ModelState.IsValid)
            {
                try
                {
                    HouseMate houseMate = GetCurrentHouseMate();

                    if (houseMate.HouseId != 0)
                    {
                        throw new Exception("houseMate already is a member of a house");
                    }

                    ParseHouseMateFields(houseMate, model);
                    await _houseMateService.UpdateHouseMate(houseMate);

                    return(Ok(houseMate));
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return(BadRequest("There was a problem updating the HouseMate"));
        }