public async Task <IActionResult> Add([FromBody] ReservationToCreateDto dto) { var result = await repo.AddReservation(dto); if (result) { return(Ok()); } return(BadRequest()); }
public async Task <bool> AddReservation(ReservationToCreateDto dto) { var apm = await context.Apartments.Include(x => x.Host).FirstOrDefaultAsync(x => x.Name == dto.Apartment); var user = await context.Guests.Include(x => x.Reservations).FirstOrDefaultAsync(x => x.Username == dto.Guest); if (apm != null && user != null) { if (CheckForDates(apm.Dates, dto.StartDate, dto.Days)) { var newApmDates = RemoveDates(apm.Dates, dto.StartDate, dto.Days); apm.Dates = newApmDates; await context.SaveChangesAsync(); var totalPrice = CalaculatePrice(dto.StartDate, dto.Days, apm.Price); var res = new Reservation() { Apartment = apm, ApartmentId = apm.Id, Date = ConvertSingle(dto.StartDate), Guest = user, GuestId = user.Id, Number = dto.Days, Status = ReservationStatus.Created, TotalPrice = totalPrice }; user.Reservations.Add(res); await context.SaveChangesAsync(); return(true); } return(false); } return(false); }