Example #1
0
        public async Task <LearningDay> AddNewLearningDayAsync(CreateNewLearningDay learningDayToCreate)
        {
            var user = await _schedulearnContext.Users.FindAsync(learningDayToCreate.UserId);

            if (user == null)
            {
                throw new NotFoundException(Error_UserNotFound.ReplaceArgs(learningDayToCreate.UserId));
            }

            var topic = await _schedulearnContext.Topics.FindAsync(learningDayToCreate.TopicId);

            if (topic == null)
            {
                throw new NotFoundException(Error_TopicNotFound.ReplaceArgs(learningDayToCreate.TopicId));
            }

            CheckLimits(user, learningDayToCreate.DateFrom, learningDayToCreate.TimezoneMinutes);

            var newLearningDay = learningDayToCreate.CreateLearningDay();

            await _schedulearnContext.LearningDays.AddAsync(newLearningDay);

            await _schedulearnContext.SaveChangesAsync();

            await _schedulearnContext.Entry(newLearningDay).ReloadAsync();

            return(newLearningDay);
        }
        public async Task <ActionResult <LearningDayWithUser> > PostNewLearningDay(CreateNewLearningDay learningDayToCreate)
        {
            System.Diagnostics.Debug.WriteLine($"PostNewLearningDay: User {learningDayToCreate.UserId} creating learning day for topic {learningDayToCreate.TopicId}");
            var newLearningDay = await _learningDayService.AddNewLearningDayAsync(learningDayToCreate);

            var learningDayWithUser = new LearningDayWithUser(newLearningDay);

            return(CreatedAtAction(nameof(GetLearningDay), new { id = learningDayWithUser.Id }, learningDayWithUser));
        }