public async Task <IActionResult> Add(CalendarVacationViewModel vacationViewModel) { if (vacationViewModel.Id.GetValueOrDefault() != 0) { return(BadRequest("Id must be empty.")); } if (vacationViewModel.Type == VacationType.None) { return(BadRequest("Vacation type can not be empty.")); } if (vacationViewModel.DateFrom > vacationViewModel.DateTo) { return(BadRequest("Date from can not be greater than date to.")); } Employee employee = await _employeeRepository.GetAsync(vacationViewModel.EmployeeId); if (employee == null) { return(NotFound("Employee does not exist.")); } int userId = employee.UserId; int authenticatedUserId = Int32.Parse(User.Claims.Single(x => x.Type == ApplicationClaimTypes.UserId).Value); bool isAdmin = User.IsInRole(Roles.Admin); if (!isAdmin && userId != authenticatedUserId) { return(Unauthorized()); } if (employee.Vacations.Any(x => x.DateFrom <= vacationViewModel.DateTo && vacationViewModel.DateFrom <= x.DateTo && x.Id != vacationViewModel.Id)) { return(BadRequest("Date range intersects with other vacation.")); } Vacation vacation = new Vacation() { EmployeeId = employee.Id.Value, DateFrom = vacationViewModel.DateFrom, DateTo = vacationViewModel.DateTo, Type = vacationViewModel.Type }; vacation.Id = await _vacationRepository.AddAsync(vacation); return(CreatedAtRoute("GetVacation", new { id = vacation.Id }, vacation)); }
public async Task <IActionResult> Get(int id) { if (id == 0) { return(BadRequest("Id can not be empty.")); } Vacation vacation = await _vacationRepository.GetAsync(id); if (vacation == null) { return(NotFound("Vacation not found.")); } CalendarVacationViewModel vacationViewModel = _mapper.Map <CalendarVacationViewModel>(vacation); return(Json(vacation)); }
public async Task <IActionResult> Update(CalendarVacationViewModel vacationViewModel) { if (vacationViewModel.Id == 0) { return(BadRequest("Id can not be null.")); } if (vacationViewModel.Type == VacationType.None) { return(BadRequest("Vacation type can not be empty.")); } if (vacationViewModel.DateFrom > vacationViewModel.DateTo) { return(BadRequest("Date from can not be greater than date to.")); } Employee employee = await _employeeRepository.GetAsync(vacationViewModel.EmployeeId); if (employee == null) { return(NotFound("Employee does not exist.")); } int userId = employee.UserId; int authenticatedUserId = Int32.Parse(User.Claims.Single(x => x.Type == ApplicationClaimTypes.UserId).Value); bool isAdmin = User.IsInRole(Roles.Admin); if (!isAdmin && userId != authenticatedUserId) { return(Unauthorized()); } Vacation existingVacation = await _vacationRepository.GetAsync(vacationViewModel.Id.Value); if (existingVacation == null) { return(NotFound("Vacation entry does not exist.")); } if (employee.Vacations.Any(x => x.DateFrom <= vacationViewModel.DateTo && vacationViewModel.DateFrom <= x.DateTo && x.Id != vacationViewModel.Id)) { return(BadRequest("Date range intersects with other vacation.")); } Vacation vacation = new Vacation() { Id = vacationViewModel.Id.Value, EmployeeId = employee.Id.Value, DateFrom = vacationViewModel.DateFrom, DateTo = vacationViewModel.DateTo, Type = vacationViewModel.Type }; if (!await _vacationRepository.UpdateAsync(vacation)) { return(StatusCode(StatusCodes.Status500InternalServerError)); } return(NoContent()); }