public Apartment EditApartment(EditApartmentInput input) { var existingApartment = _apartmentRepository.Get(input.Id); if (existingApartment == null) { throw new UserFriendlyException("This apartment doesn't exist"); } //For each room in input, we verify it exists in the apartment or we add it foreach (var inputRoom in input.Rooms) { var existingRoom = _roomRepository.FirstOrDefault(r => r.Id == inputRoom.Id); if (existingRoom == null) { var newRoom = _roomService.CreateNewRoom(new CreateNewRoomInput { Room = inputRoom }); existingApartment.Rooms.Add(newRoom); } } //We remove rooms that are not included in the edit input, as we assume they are not in the apartment anymore foreach (var existingRoom in existingApartment.Rooms) { if (!input.Rooms.Any(r => r.Id == existingRoom.Id)) { _roomService.DeleteRoom(existingRoom.Id); } } //We verify there is still at least one room in the apartment if (existingApartment.Rooms.Count == 0) { throw new UserFriendlyException("An apartment must have at least one room"); } existingApartment.Name = input.Name; existingApartment.Street = input.Street; existingApartment.ZipCode = input.Name; existingApartment.City = input.Name; _apartmentRepository.Update(existingApartment); return(existingApartment); }
public async Task <IActionResult> DeleteRoom(Guid id) { await _roomAppService.DeleteRoom(id); return(Ok()); }