Exemple #1
0
        public async Task <IActionResult> Update([FromBody] UserViewModel userViewModel)
        {
            var destination = await dbContext.Users
                              .Include(u => u.UserSubscriptions)
                              .Include(u => u.UserCourseTypes)
                              .SingleOrDefaultAsync(u => u.Id == userViewModel.Id);

            if (destination == null)
            {
                return(NotFound());
            }

            var subscriptions = await dbContext.Subscriptions.Include(subscription => subscription.CourseType).ToListAsync();

            var source = userViewModel.MapToUserEntity(subscriptions);

            destination.FirstName   = source.FirstName;
            destination.LastName    = source.LastName;
            destination.PhoneNumber = source.PhoneNumber;
            destination.Birthdate   = source.Birthdate;
            destination.CustomerId  = source.CustomerId;

            destination.UserSubscriptions.Clear();
            destination.UserCourseTypes.Clear();
            dbContext.Users.Update(destination);
            await dbContext.SaveChangesAsync();

            destination.UserSubscriptions.AddRange(source.UserSubscriptions);
            destination.UserCourseTypes.AddRange(source.UserCourseTypes);

            var resendActivation = destination.Email != userViewModel.Email;

            if (!destination.EmailConfirmed)
            {
                await userManager.SetUserNameAsync(destination, userViewModel.Email);

                await userManager.SetEmailAsync(destination, userViewModel.Email);
            }

            await dbContext.SaveChangesAsync();

            if (resendActivation)
            {
                await SendActivationMail(userViewModel.Email, destination);
            }

            return(new NoContentResult());
        }
Exemple #2
0
        public async Task <IActionResult> New([FromBody] ScheduleViewModel scheduleViewModel)
        {
            var currentUserId = GetCurrentUserId();
            var timeTable     = await dbContext.Timetables
                                .Include(t => t.Location)
                                .Include(t => t.Course)
                                .SingleAsync(t => t.Id == scheduleViewModel.TimetableId);

            var alreadySubscribedSchedules = await dbContext.Schedules
                                             .Include(s => s.Timetable)
                                             .Where(s => s.Timetable.Id == scheduleViewModel.TimetableId &&
                                                    s.Date.Date == scheduleViewModel.Date.Date)
                                             .CountAsync();

            var meta = new Dictionary <string, string>
            {
                { "user id", currentUserId.ToString() },
                { "timetable id", scheduleViewModel.TimetableId.ToString() },
                { "date", scheduleViewModel.Date.ToShortDateString() }
            };

            if (alreadySubscribedSchedules >= timeTable.Course.Capacity)
            {
                telemetryClient.TrackTrace("Course has reached it's capacity", SeverityLevel.Warning, meta);
                return(BadRequest());
            }

            var courseTypeId = timeTable.Course.CourseTypeId;
            var user         = await dbContext.Users
                               .Include(u => u.UserCourseTypes)
                               .Include(u => u.UserSubscriptions)
                               .ThenInclude(us => us.Subscription)
                               .SingleAsync(u => u.Id == currentUserId);

            if (user.UserCourseTypes.All(uc => uc.CourseTypeId != courseTypeId))
            {
                telemetryClient.TrackTrace("User does not have any subscriptions for this coursetype",
                                           SeverityLevel.Warning, meta);
                return(BadRequest());
            }

            if (await dbContext.Schedules.SingleOrDefaultAsync(s => s.UserId == currentUserId &&
                                                               s.TimetableId == scheduleViewModel.TimetableId &&
                                                               s.Date.Date == scheduleViewModel.Date.Date) != null)
            {
                telemetryClient.TrackTrace("User is trying to send a schedule for the second time",
                                           SeverityLevel.Warning, meta);
                return(BadRequest());
            }

            var count        = dbContext.Schedules.Count(s => s.UserId == currentUserId && s.Date >= DateTime.Today);
            var subscription = user.UserSubscriptions.Single(us => us.Subscription.CourseTypeId == courseTypeId);
            var type         = (int)subscription.Subscription.Type;

            if (type != 0 && count >= type * 4)
            {
                meta.Add("schedules count", count.ToString());
                telemetryClient.TrackTrace("User has reached it's maximum capacity for the coming 4 weeks",
                                           SeverityLevel.Warning, meta);
                return(BadRequest());
            }

            var newSchedule = scheduleViewModel.ToEntity(currentUserId, timeTable);
            await dbContext.Schedules.AddAsync(newSchedule);

            await dbContext.SaveChangesAsync();

            return(NoContent());
        }