private List <Night> NewDatesForCottage(List <DateTime> datesToAddList, AddBookingDaysDto allocateDaysDto, int currentUser) { var daysToAdd = new List <Night>(); foreach (var date in datesToAddList) { Night newDay = new Night { BookingDate = date, BookingStatus = BookingStatus.Available, Price = allocateDaysDto.Price, ModifiedBy = currentUser }; daysToAdd.Add(newDay); } return(daysToAdd); }
public async Task <IActionResult> AddBookingDaysToCalendar(int id, AddBookingDaysDto allocateDaysDto) { if (!DatesValid(allocateDaysDto.StartDate, allocateDaysDto.EndDate)) { return(BadRequest("Dates not valid")); } var cottageToUpdate = await _repo.GetCottageWithCalendar(id); if (cottageToUpdate == null) { return(NotFound()); } var datesToAddList = CreateDatesForAddList(allocateDaysDto.StartDate, allocateDaysDto.EndDate).ToList(); if (DateAlreadyAllocated(datesToAddList, cottageToUpdate.Calendar)) { return(BadRequest("One or more dates in this range have already been allocated")); } var userIdFromClaims = User.Claims.FirstOrDefault().Value; int currentUser; if (!Int32.TryParse(userIdFromClaims, out currentUser)) { currentUser = 0; } var datesToAdd = NewDatesForCottage(datesToAddList, allocateDaysDto, currentUser); datesToAdd.ForEach(d => cottageToUpdate.Calendar.Add(d)); if (await _repo.Commit()) { var cottageWithUpdatedCalendar = _mapper.Map <CottageDto>(cottageToUpdate); return(Ok(cottageWithUpdatedCalendar)); } else { return(StatusCode(500)); } }