public async Task <IActionResult> AddCottage([FromForm] CottageAddUpdateDto cottageAddUpdateDto) { Cottage cottageToAdd = _mapper.Map <Cottage>(cottageAddUpdateDto); var cottageRegion = await _repo.GetRegionById(cottageAddUpdateDto.RegionId); cottageToAdd.Region = cottageRegion; var userIdFromClaims = User.Claims.FirstOrDefault().Value; if (Int32.TryParse(userIdFromClaims, out int currentUser)) { cottageToAdd.ModifiedBy = currentUser; } else { cottageToAdd.ModifiedBy = 0; } _repo.Add <Cottage>(cottageToAdd); var cottageAddedSuccess = await _repo.Commit(); if (!cottageAddedSuccess) { return(StatusCode(500)); } if (cottageAddUpdateDto.PhotoFile != null) { var photoUrl = await SavePhoto(cottageAddUpdateDto.PhotoFile, cottageToAdd.Id, cottageToAdd.Name); if (!String.IsNullOrEmpty(photoUrl)) { cottageToAdd.Photo = photoUrl; } } //add cottage owner entitie(s) List <CottageOwner> cottageOwners = cottageAddUpdateDto.OwnerIds.Select(o => new CottageOwner { CottageId = cottageToAdd.Id, OwnerId = o }).ToList(); _repo.AddCottageOwners(cottageOwners); var cottageOwnersAdded = await _repo.Commit(); if (!cottageOwnersAdded) { return(StatusCode(500)); } return(CreatedAtRoute("GetCottageById", new { id = cottageToAdd.Id }, new { id = cottageToAdd.Id })); }
public async Task <IActionResult> UpdateCottage(int id, [FromForm] CottageAddUpdateDto cottageAddUpdateDto) { Cottage cottageToUpdate = await _repo.GetCottageById(id, false, false); cottageToUpdate.NoOfBedrooms = cottageAddUpdateDto.NoOfBedrooms; cottageToUpdate.MaxGuests = cottageAddUpdateDto.MaxGuests; cottageToUpdate.Location = cottageAddUpdateDto.Location; cottageToUpdate.Description = cottageAddUpdateDto.Description; cottageToUpdate.ModifiedDate = DateTime.Now; var userIdFromClaims = User.Claims.FirstOrDefault().Value; if (Int32.TryParse(userIdFromClaims, out int currentUser)) { cottageToUpdate.ModifiedBy = currentUser; } else { cottageToUpdate.ModifiedBy = 0; } if (cottageAddUpdateDto.PhotoFile != null) { var photoUrl = await SavePhoto(cottageAddUpdateDto.PhotoFile, cottageToUpdate.Id, cottageToUpdate.Name); if (!String.IsNullOrEmpty(photoUrl)) // photo saved successfully { cottageToUpdate.Photo = photoUrl; } } var cottageUpdateSuccess = await _repo.Commit(); if (!cottageUpdateSuccess) { return(StatusCode(500)); } return(NoContent()); }