Пример #1
0
        public async Task <JsonResult> Start()
        {
            var id      = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var pomidor = context.Pomidors.FirstOrDefault(x => x.UserID == id);

            if (pomidor == null)
            {
                pomidor = new Pomidor()
                {
                    UserID = id
                };
                pomidor.Start(25);
                context.Pomidors.Add(pomidor);
                await context.SaveChangesAsync();
            }
            else
            {
                pomidor.Start(25);
                context.Update(pomidor);
                await context.SaveChangesAsync();
            }
            return(new JsonResult(new TimerJSON {
                Rest = pomidor.Rest.TotalMilliseconds, IsPomidor = true
            }));
        }
Пример #2
0
        public async Task <IActionResult> Buy(int id)
        {
            var typeOfPet = await context.TypeOfPets.FirstOrDefaultAsync(x => x.ID == id);

            var user = await userManager.GetUserAsync(User);

            if (typeOfPet != null && user.Money >= typeOfPet.Price &&
                context.Pets.Count(x => x.UserID == user.Id && x.TypeID == id) == 0)
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    using (var appTransaction = appContext.Database.BeginTransaction())
                    {
                        context.Pets.Add(new Pet {
                            Type = typeOfPet, UserID = user.Id
                        });
                        await context.SaveChangesAsync();

                        user.Money -= typeOfPet.Price;
                        await userManager.UpdateAsync(user);

                        appTransaction.Commit();
                        transaction.Commit();
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
Пример #3
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var user = await userManager.FindByNameAsync(User.Identity.Name);

            var primaryPet = context.Pets.Include(x => x.Type).FirstOrDefault(x => x.ID == user.PrimaryPet);

            if (primaryPet == null)
            {
                primaryPet = new Pet {
                    Type = context.TypeOfPets.First(), Level = 0, UserID = user.Id
                };
                context.Pets.Add(primaryPet);
                await context.SaveChangesAsync();

                user.PrimaryPet = primaryPet.ID;
                await userManager.UpdateAsync(user);
            }

            return(View(primaryPet));
        }