public async Task <GenericObjectResponse <RoomResponse> > AddRoom(CreateRoomPayload payload, long?userId) { try { Logger.Debug($"Attempting to create new room for user {userId}"); RoomResponse roomResponse = null; RoomValidationService service = new RoomValidationService(); GenericStatusMessage validationResult = service.ValidateRoomData(payload); if (!validationResult.Success) { return(new GenericObjectResponse <RoomResponse>(validationResult.Message)); } using (ReservationDataContext context = new ReservationDataContext()) { Room room = ConvertRoomFromPayload(payload, userId); context.Rooms.Add(room); context.SaveChanges(); roomResponse = ConvertRoomToResponse(room, userId); } Logger.Debug($"Attempting to create working hours for room {roomResponse.Id}"); WorkingHoursManipulationService workingHoursService = new WorkingHoursManipulationService(); GenericObjectResponse <List <WorkingHoursPayload> > workingHours = workingHoursService.ChangeWorkingHoursForRoom(roomResponse.Id, payload.WorkingHours); if (!workingHours.Status.Success) { Logger.Error($"failed to create working hours for room {roomResponse.Id}, Deleting created room."); using (ReservationDataContext context = new ReservationDataContext()) { Room room = context.Rooms.Single(x => x.Id == roomResponse.Id); context.Rooms.Remove(room); context.SaveChanges(); } return(new GenericObjectResponse <RoomResponse>($"Failed to add room due to faulty working hours: {workingHours.Status.Message}")); } roomResponse.WorkingHours = workingHours.Object; Logger.Debug($"Room created for user {userId}."); LatLon latLon = await AddLatLonForRoom(roomResponse.Id); if (latLon != null) { roomResponse.Lat = latLon.Lat; roomResponse.Lon = latLon.Lon; } return(new GenericObjectResponse <RoomResponse>(roomResponse)); } catch (DbEntityValidationException e) { string exceptionMessage = e.EntityValidationErrors.FirstOrDefault()?.ValidationErrors.FirstOrDefault()?.ErrorMessage; Logger.Error($"Failed to create room. Error: '{exceptionMessage}'"); return(new GenericObjectResponse <RoomResponse>("Failed to add the room, please contact support.")); } }