public async Task <IActionResult> CreateCoachLesson([Required, FromBody] CoachLessonCreateDTO coachLessonDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int currentUserID = this.User.GetUserId().Value; coachLessonDTO.DateStart = coachLessonDTO.DateStart.Value.ToLocalTime(); coachLessonDTO.DateEnd = coachLessonDTO.DateEnd.Value.ToLocalTime(); if (!_coachLessonService.IsTimeAvailable(currentUserID, coachLessonDTO.DateStart.Value, coachLessonDTO.DateEnd.Value)) { return(StatusCode((int)HttpStatusCode.Conflict)); } TimeSpan span = coachLessonDTO.DateEnd.Value.Subtract(coachLessonDTO.DateStart.Value); if ((span.TotalMinutes % coachLessonDTO.Time != 0)) { ModelState.AddModelError("Time", "Nie da się poprawnie rozłożyć lekcji w danym przedziale czasowym."); return(BadRequest(ModelState)); } if ((span.TotalMinutes / coachLessonDTO.Time) > 8) { ModelState.AddModelError("DateEnd", "Nie można utworzyć więcej niż 8 lekcji za jednym razem. Zmniejsz przedział czasowy lub zwiększ czas lekcji."); return(BadRequest(ModelState)); } try { await _lessonSubjectService.GetAsync(coachLessonDTO.LessonSubjectId); } catch (IdDoesNotExistException) { ModelState.AddModelError("LessonSubjectId", "Podany przedmiot lekcji nie istnieje."); return(BadRequest(ModelState)); } foreach (var levelId in coachLessonDTO.LessonLevels) { if (!_lessonLevelRepository.Query().Any(x => x.Id == levelId)) { ModelState.AddModelError("LessonLevels", "Przynajmniej jeden poziom nie istnieje."); return(BadRequest(ModelState)); } } await _coachLessonService.CreateCoachLesson(coachLessonDTO, currentUserID); return(StatusCode((int)HttpStatusCode.Created)); }
public async Task <IActionResult> Get([FromRoute] int id) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var subject = await subjectService.GetAsync(id); return(Ok(subject)); } catch (IdDoesNotExistException) { return(NotFound()); } catch (Exception e) { logger.Fatal(e, $"GET LessonSubjects/{id}"); return(InternalServerError()); } }