Beispiel #1
0
 public async Task <IActionResult> Index(EditUserSettingsRequest editUserSettingsRequest)
 {
     if (ModelState.IsValid)
     {
         await userSettingsService.EditSettingsAsync(User, editUserSettingsRequest);
     }
     return(View(await userSettingsService.BuildUserSettingsViewModel(User)));
 }
        public async Task <UserSettingsService> SetUserSettingsAsync(string email, List <bool> DaysOfShopping, byte NumberOfWeeksInCycle)
        {
            var user = await userMgr.FindByEmailAsync(email);

            EditUserSettingsRequest userSettingsReq = new EditUserSettingsRequest();

            userSettingsReq.DaysOfShopping       = DaysOfShopping;
            userSettingsReq.NumberOfWeeksInCycle = NumberOfWeeksInCycle;

            var userSettings = mapper.Map <EditUserSettingsRequest, UserSettingsService>(userSettingsReq);
            await applicationDbContext.AddAsync(userSettingsReq);

            return(userSettings);
        }
        public async Task EditSettingsAsync(ClaimsPrincipal user, EditUserSettingsRequest editUserSettingsRequest)
        {
            var appUser = await userService.FindUserByNameOrEmailAsync(user.Identity.Name);

            appUser.NumberOfWeeksInCycle = editUserSettingsRequest.NumberOfWeeksInCycle;
            var daysOfShopping = await GetDaysOfShoppingAsync(appUser);

            for (byte i = 1; i <= 7; i++)
            {
                if (editUserSettingsRequest.DaysOfShopping[i - 1])
                {
                    var day = await applicationDbContext.ShoppingDaysOfWeeks.FirstOrDefaultAsync(s => s.DaysOfShopping == i && s.User == appUser);

                    if (day == null)
                    {
                        ShoppingDaysOfWeek shoppingDaysOfWeek = new ShoppingDaysOfWeek()
                        {
                            DaysOfShopping = (i),
                            User           = appUser
                        };
                        applicationDbContext.ShoppingDaysOfWeeks.Add(shoppingDaysOfWeek);
                    }
                }
                else
                {
                    if (daysOfShopping.Count != 0)
                    {
                        if (daysOfShopping[i - 1])
                        {
                            var day = await applicationDbContext.ShoppingDaysOfWeeks.FirstOrDefaultAsync(s => s.DaysOfShopping == i && s.User == appUser);

                            if (day != null)
                            {
                                applicationDbContext.ShoppingDaysOfWeeks.Remove(day);
                                await applicationDbContext.SaveChangesAsync();
                            }
                        }
                    }
                }
            }
            await applicationDbContext.SaveChangesAsync();
        }