示例#1
0
        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 CreateCoachLesson(CoachLessonCreateDTO coachLessonDTO, int currentUserID)
        {
            Address address = _mapper.Map <Address>(coachLessonDTO.Address);

            address.CoachId = currentUserID;
            Address dbAddress = _addressRepository.Query()
                                .Where(add => add.CoachId == currentUserID)
                                .Where(add => add.Latitude == address.Latitude && add.Longitude == address.Longitude && add.City == address.City && add.Street == address.Street).FirstOrDefault();

            address = dbAddress == null
                ? await _addressRepository.AddAsync(address)
                : address = dbAddress;

            List <CoachLesson> coachLessonList = new List <CoachLesson>();

            DateTime          currentDate = coachLessonDTO.DateStart.Value;
            ICollection <int> levels      = coachLessonDTO.LessonLevels;

            while (currentDate.AddMinutes(coachLessonDTO.Time).Subtract(coachLessonDTO.DateEnd.Value).TotalMinutes <= 0)
            {
                CoachLesson coachLesson = _mapper.Map <CoachLesson>(coachLessonDTO);
                coachLesson.DateStart      = currentDate;
                currentDate                = currentDate.AddMinutes(coachLessonDTO.Time);
                coachLesson.DateEnd        = currentDate;
                coachLesson.AddressId      = address.Id;
                coachLesson.LessonStatusId = (int)LessonStatuses.WaitingForStudents;

                coachLessonList.Add(coachLesson);
            }

            foreach (var item in coachLessonList)
            {
                ICollection <CoachLessonLevel> mappedLevels = new List <CoachLessonLevel>();
                foreach (var level in levels)
                {
                    CoachLessonLevel a = new CoachLessonLevel {
                        CoachLessonId = item.Id, LessonLevelId = level
                    };
                    mappedLevels.Add(a);
                }
                item.LessonLevels = mappedLevels;
                item.CoachId      = currentUserID;
                await _coachLessonRepository.AddAsync(item);
            }
        }