public async Task <IActionResult> AddUserNewVacation(int id, NewVacationDTO newVacation) { if (newVacation.FromDate > newVacation.ToDate || newVacation.FromDate.DayOfYear < DateTime.Now.DayOfYear) { return(Content("Błędna data urlopu")); } var totalVacDays = (newVacation.ToDate - newVacation.FromDate).Days + 1; var daysLeft = await _repo.GetDaysLeft(id, newVacation.IdAbsence, totalVacDays); if (daysLeft < totalVacDays) { return(Content("Nie można udzielić urlopu. Brak dni do wybrania")); } if (await _repo.CheckIfOverlapping(id, newVacation)) { return(Content("Użytkownik ma już w tym terminie zaplanowany urlop")); } if (await _repo.CheckIfOverlapping(newVacation.UserForReplacentId, newVacation)) { return(Content("Użytkownik na zastepstwo ma w tym terminie zaplanowany urlop")); } var newVac = await _repo.AddNewVacation(id, newVacation); var vacToReturn = _mapper.Map <VacationDTO>(newVac); return(Ok(vacToReturn)); }
public async Task <Vacation> AddNewVacation(int userId, NewVacationDTO newVacation) { var user = await GetUser(userId); var absence = await _context.Absence .FirstOrDefaultAsync(x => x.IdAbsence == newVacation.IdAbsence); var newVac = new Vacation { FromDate = newVacation.FromDate, ToDate = newVacation.ToDate, IdUserVacNavigation = user, IdAbsenceVacNavigation = absence }; var vacLeft = await _context.Leftvacationdays .Where(x => x.IdUser == userId) .Where(x => x.IdAbsence == newVacation.IdAbsence) .FirstOrDefaultAsync(); var totalVacDays = (newVacation.ToDate - newVacation.FromDate).Days + 1; vacLeft.LeftDays -= totalVacDays; //zastepstwo var userForReplacemt = await GetUser(newVacation.UserForReplacentId); var replacement = await _context.Absence .FirstOrDefaultAsync(x => x.IdAbsence == 9); var newReplacement = new Vacation { FromDate = newVacation.FromDate, ToDate = newVacation.ToDate, IdUserVacNavigation = userForReplacemt, IdAbsenceVacNavigation = replacement }; await _context.Vacation.AddAsync(newVac); await _context.Vacation.AddAsync(newReplacement); await _context.SaveChangesAsync(); return(newVac); }
public async Task <bool> CheckIfOverlapping(int userId, NewVacationDTO newVacation) { var UserVacList = await _context.Vacation .Where(u => u.IdUserVac == userId) .ToListAsync(); var check = UserVacList .Any(d => newVacation.FromDate.IsBewteenTwoDates(d.FromDate, d.ToDate) || newVacation.ToDate.IsBewteenTwoDates(d.FromDate, d.ToDate) || ((d.ToDate).IsBewteenTwoDates(newVacation.FromDate, newVacation.ToDate) && (d.FromDate).IsBewteenTwoDates(newVacation.FromDate, newVacation.ToDate) )); return(check); }