public async Task <ActionResult <RoomToAddDto> > PostRoom(RoomToAddDto roomToAdd) { var responsetoken = JsonConvert.DeserializeObject <ResponseToken>(roomToAdd.Token); Customer currentCustomer = _context.Customers.FirstOrDefault(c => c.IdentityId == responsetoken.Id); if (roomToAdd.Photos is null) { return(BadRequest("please provide a photo")); } foreach (var photo in roomToAdd.Photos) { photo.RoomId = roomToAdd.Id; } foreach (var equipment in roomToAdd.RoomEquipments) { equipment.RoomId = roomToAdd.Id; } foreach (var amenitie in roomToAdd.RoomAmenitiesForDisabled) { amenitie.RoomId = roomToAdd.Id; } foreach (var activitie in roomToAdd.RoomActivities) { activitie.RoomId = roomToAdd.Id; } roomToAdd.RoomAddress.RoomId = roomToAdd.Id; var newRoom = new Room() { Id = roomToAdd.Id, Name = roomToAdd.Name, Area = roomToAdd.Area, Capacity = roomToAdd.Capacity, Description = roomToAdd.Description, ParkingSpace = roomToAdd.ParkingSpace, Photos = roomToAdd.Photos .Select(x => new Photo() { PhotoUrl = x.PhotoUrl, Id = x.Id, IsMain = x.IsMain, RoomId = x.RoomId }) .ToList(), Price = roomToAdd.Price, Rating = 1, RoomActivities = roomToAdd.RoomActivities, RoomAddress = roomToAdd.RoomAddress, RoomEquipments = roomToAdd.RoomEquipments, RoomAmenitiesForDisabled = roomToAdd.RoomAmenitiesForDisabled, RoomArrangementsCapabilitiesDescription = roomToAdd.RoomArrangementsCapabilitiesDescription, Customer = currentCustomer }; if (!ModelState.IsValid) { return(BadRequest("Invalid data")); } _context.Rooms.Add(newRoom); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (RoomExists(newRoom.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtAction("GetRoom", new { id = newRoom.Id }, newRoom)); }
public async Task <ActionResult <RoomToAddDto> > PostRoom(RoomToAddDto roomToAdd) { foreach (var photo in roomToAdd.Photos) { photo.RoomId = roomToAdd.Id; } foreach (var equipment in roomToAdd.RoomEquipments) { equipment.RoomId = roomToAdd.Id; } foreach (var amenitie in roomToAdd.RoomAmenitiesForDisabled) { amenitie.RoomId = roomToAdd.Id; } foreach (var activitie in roomToAdd.RoomActivities) { activitie.RoomId = roomToAdd.Id; } roomToAdd.RoomAddress.RoomId = roomToAdd.Id; var newRoom = new Room() { Id = roomToAdd.Id, Name = roomToAdd.Name, Area = roomToAdd.Area, Capacity = roomToAdd.Capacity, Description = roomToAdd.Description, ParkingSpace = roomToAdd.ParkingSpace, Photos = roomToAdd.Photos .Select(x => new Photo() { PhotoUrl = x.PhotoUrl, Id = x.Id, IsMain = x.IsMain, RoomId = x.RoomId }) .ToList(), Price = roomToAdd.Price, Rating = 1, RoomActivities = roomToAdd.RoomActivities, RoomAddress = roomToAdd.RoomAddress, RoomEquipments = roomToAdd.RoomEquipments, RoomAmenitiesForDisabled = roomToAdd.RoomAmenitiesForDisabled }; if (!ModelState.IsValid) { return(BadRequest("Invalid data")); } _context.Rooms.Add(newRoom); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (RoomExists(newRoom.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtAction("GetRoom", new { id = newRoom.Id }, newRoom)); }