public async Task CreateLessonAsync(LessonCreateDTO lessonCreateDTO) { var lesson = _mapper.Map <Lesson>(lessonCreateDTO); var coachLesson = await _coachLessonRepository.GetByIdAsync(lessonCreateDTO.CoachLessonId); lesson.Date = coachLesson.DateStart; lesson.NumberOfHours = (float)(coachLesson.DateEnd - coachLesson.DateStart).TotalHours; lesson.LessonStatusId = (int)LessonStatuses.Reserved; await _lessonRepository.AddAsync(lesson); coachLesson.LessonStatusId = (int)LessonStatuses.Reserved; await _coachLessonRepository.UpdateAsync(coachLesson); // Symulacja powiadomienia 'Wiadomosc od uzytkownika system' var student = await _userRepository.GetByIdAsync(lesson.StudentId); var studentFirstName = student.FirstName; var studentLastNamePrefix = student.LastName.Trim(); studentLastNamePrefix = studentLastNamePrefix.Length > 1 ? studentLastNamePrefix.First().ToString().ToUpper() + "." : ""; var content = $"Użytkownik {studentFirstName} {studentLastNamePrefix} zapisał się na Twoją lekcję ({coachLesson.Subject.Name} - {coachLesson.DateStart.ToString("yyyy-MM-dd HH:mm")})."; var message = new Message { Content = content, OwnerId = 0, RecipientId = coachLesson.CoachId, DateOfSending = DateTime.Now }; await _messageService.AddMessageAsync(message); }
public async Task <IActionResult> CreateLesson([Required, FromBody] LessonCreateDTO lessonCreateDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = this.User.GetUserId().Value; var coachLessonId = lessonCreateDTO.CoachLessonId; lessonCreateDTO.StudentId = currentUserId; // Custom Validation if (!_coachLessonService.IsCoachLessonExists(coachLessonId)) { return(NotFound()); } if (_coachLessonService.IsUserOwnerOfCoachLesson(coachLessonId, currentUserId)) { ModelState.AddModelError("CoachLessonId", "Nie można zapisać się na swoją lekcję."); return(BadRequest(ModelState)); } if (!_coachLessonService.IsCoachLessonAvailable(coachLessonId)) { ModelState.AddModelError("CoachLessonId", "Nie można zapisać się tę lekcję."); return(BadRequest(ModelState)); } if (_coachLessonService.IsUserAlreadySignedUpForCoachLesson(coachLessonId, currentUserId)) { ModelState.AddModelError("CoachLessonId", "Jesteś już zapisany na tę lekcję."); return(BadRequest(ModelState)); } try { await _lessonService.CreateLessonAsync(lessonCreateDTO); } catch (Exception ex) { _logger.Error(ex, "Error during Lesson creation"); return(StatusCode((int)HttpStatusCode.InternalServerError)); } return(StatusCode((int)HttpStatusCode.Created)); }