public async Task UpsertAsync(ProductionAreas productionAreas)
        {
            if (_context.ProductionAreas.AsNoTracking().Any(x => x.Id == productionAreas.Id))
            {
                _context.Attach(productionAreas);
                _context.Update(productionAreas);
            }
            else
            {
                await _context.AddAsync(productionAreas);
            }

            await _context.SaveChangesAsync();
        }
        public async Task UpsertAsync(Product product)
        {
            if (_context.Products.AsNoTracking().Any(x => x.Id == product.Id))
            {
                _context.Attach(product);
                _context.Update(product);
                //_context.Entry(product).State = EntityState.Modified;
            }
            else
            {
                await _context.AddAsync(product);
            }

            await _context.SaveChangesAsync();
        }
Пример #3
0
        public async Task <Order> Create(DTO.Order order)
        {
            Models.Order orderDB = new Models.Order()
            {
                UserId = Guid.Parse(order.UserId)
            };

            var d = await _StoreContext.AddAsync(orderDB);

            await _StoreContext.SaveChangesAsync();

            order = d.Entity.ToDTO();

            /*
             * foreach (DTO.OrderProduct orderProduct in order.OrderProducts)
             * {
             *  Models.OrderProduct orderProductDB = new Models.OrderProduct()
             *  {
             *      Quantity = orderProduct.Quantity,
             *      ProductId = Guid.Parse(orderProduct.Product.Id),
             *      OrderId = Guid.Parse(order.Id),
             *  };
             *
             *  await _StoreContext.AddAsync(orderProductDB);
             *  await _StoreContext.SaveChangesAsync();
             * }
             */

            return(order);
        }
Пример #4
0
        /// <summary>
        /// Adds a new user to the database.
        /// </summary>
        /// <param name="user">The user to add.</param>
        /// <returns>CreateUserAccountResult detailing the status of the operation.</returns>
        public async Task <CreateUserAccountResult> Add(User user)
        {
            var hashed = StoreApp.Util.Hash.Sha256(user.Password);

            user.Password = hashed;
            if (String.IsNullOrEmpty(user.Login))
            {
                return(CreateUserAccountResult.MissingLogin);
            }
            if (String.IsNullOrEmpty(user.Password))
            {
                return(CreateUserAccountResult.MissingPassword);
            }

            var loginExists = await _context.Users.Where(c => c.Login == user.Login.ToLower()).SingleOrDefaultAsync();

            if (loginExists != null)
            {
                return(CreateUserAccountResult.AccountNameExists);
            }

            await _context.AddAsync(user);

            await _context.SaveChangesAsync();

            return(CreateUserAccountResult.Ok);
        }
Пример #5
0
        private static async Task GenerateTransaction(StoreContext context)
        {
            if (!context.TransactionMDs.Any())
            {
                var dataFromSJsonFile = File.ReadAllText("../Backend/Data/SeedData/trans_examples.json");
                var data = JsonSerializer.Deserialize <List <TransactionMD> >(dataFromSJsonFile);

                foreach (var item in data)
                {
                    await context.AddAsync(item);
                }
            }
        }
Пример #6
0
        public async Task <Order> Create(DTO.Order order)
        {
            Models.Order orderDB = new Models.Order()
            {
                UserId = Guid.Parse(order.UserId)
            };

            var d = await _StoreContext.AddAsync(orderDB);

            await _StoreContext.SaveChangesAsync();

            order = d.Entity.ToDTO();

            return(order);
        }
Пример #7
0
            private async Task <Domain.Marca> ObterMarcas(Command request)
            {
                if (!request.Id.HasValue)
                {
                    var marca = new Domain.Marca();

                    await _storeContext.AddAsync(marca);

                    return(marca);
                }

                return(await _storeContext
                       .Set <Domain.Marca>()
                       .FirstOrDefaultAsync(e => e.Id == request.Id));
            }
Пример #8
0
            private async Task <Usuario> GetPerson(Command request)
            {
                if (!request.Id.HasValue)
                {
                    var person = new Usuario();

                    await _storeContext.AddAsync(person);

                    return(person);
                }

                return(await _storeContext
                       .Set <Usuario>()
                       .FirstOrDefaultAsync(e => e.Id == request.Id));
            }
        public static async Task SeedProductAsync(StoreContext context, UserManager <User> userManager)
        {
            int           counter   = 0;
            List <string> userNames = new List <string>();

            userNames.Add("Yaser01");
            userNames.Add("Yaser02");
            userNames.Add("Yaser03");

            if (!context.Products.Any())
            {
                var productData = await File.ReadAllTextAsync("../Services/Seeds/Data/Product.json");

                var products = JsonSerializer.Deserialize <List <Product> >(productData);
                foreach (var product in products)
                {
                    var productForAdd = new Product()
                    {
                        Name             = product.Name.ToLower(),
                        ShortDescription = product.ShortDescription,
                        LongDescription  = product.LongDescription,
                        Price            = product.Price,
                        DateAdded        = DateTime.UtcNow,
                        CategoryId       = product.CategoryId
                    };

                    var user = await userManager.FindByNameAsync(userNames[counter]);

                    counter++;
                    productForAdd.UserId = user.Id;

                    productForAdd.User = await context.Users
                                         .FirstOrDefaultAsync(x => x.Id == productForAdd.UserId);

                    productForAdd.Category = await context.Categories
                                             .FirstOrDefaultAsync(c => c.Id == productForAdd.CategoryId);

                    await context.AddAsync(productForAdd);

                    await context.SaveChangesAsync();

                    if (counter == userNames.Count)
                    {
                        counter = 0;
                    }
                }
            }
        }
Пример #10
0
        public async Task <DTO.User> Create(DTO.UserCredentials user)
        {
            Models.User userDb = new Models.User()
            {
                Email    = user.Email,
                FullName = user.FullName,
                Gender   = user.Gender,
                Password = user.Password,
                Role     = user.Role
            };

            var d = await _StoreContext.AddAsync(userDb);

            await _StoreContext.SaveChangesAsync();

            return(d.Entity.ToDTO());
        }
Пример #11
0
        public async Task SetLeagueLadderPending(string league)
        {
            var exists = await LeagueLadderExists(league);

            if (!exists)
            {
                var storeModel = new LadderStoreModel()
                {
                    Name     = league,
                    Started  = DateTime.MinValue,
                    Finished = DateTime.MinValue,
                    Ladder   = new List <LadderPlayerModel>(),
                    Running  = false
                };

                await _store.AddAsync(storeModel);

                await _store.SaveChangesAsync();
            }
        }
        public static async Task SeedProductLinkSubCategoriesAsync(StoreContext context)
        {
            if (!context.productAndSubCategories.Any())
            {
                var productAndSubCategoryData = await File.ReadAllTextAsync("../Services/Seeds/Data/LinkProductAndSubCategory.json");

                var data = JsonSerializer.Deserialize <List <ProductAndSubCategory> >(productAndSubCategoryData);
                foreach (var item in data)
                {
                    var productLinkSubCategory = new ProductAndSubCategory()
                    {
                        ProductId     = item.ProductId,
                        SubCategoryId = item.SubCategoryId
                    };
                    await context.AddAsync(productLinkSubCategory);

                    await context.SaveChangesAsync();
                }
            }
        }
Пример #13
0
        public static async Task SeedCategoryAsync(StoreContext context)
        {
            if (!context.Categories.Any())
            {
                var categoriesData = await File.ReadAllTextAsync(@"../Services/Seeds/Data/Category.json");

                var categories = JsonSerializer.Deserialize <List <Category> >(categoriesData);
                foreach (var category in categories)
                {
                    var categoryForAdd = new Category
                    {
                        Name     = category.Name,
                        ImageUrl = category.ImageUrl
                    };
                    await context.AddAsync(categoryForAdd);

                    await context.SaveChangesAsync();
                }
            }
        }
        public static async Task SeedSubCategory(StoreContext context)
        {
            if (!context.SubCategories.Any())
            {
                var subCategoriesData = await File.ReadAllTextAsync("../Services/Seeds/Data/SubCategory.json");

                var subCategories = JsonSerializer.Deserialize <List <SubCategory> >(subCategoriesData);
                foreach (var subCategory in subCategories)
                {
                    var subCategoryForAdd = new SubCategory
                    {
                        Name       = subCategory.Name.ToLower(),
                        CategoryId = subCategory.CategoryId
                    };

                    await context.AddAsync(subCategoryForAdd);

                    await context.SaveChangesAsync();
                }
            }
        }
Пример #15
0
        public static async Task SeedVIPAds(StoreContext context)
        {
            if (!context.VIPAds.Any())
            {
                var vipAdsData = await File.ReadAllTextAsync("../Services/Seeds/Data/VIPAds.json");

                var vipAds = JsonSerializer.Deserialize <List <VIPAd> >(vipAdsData);
                foreach (var vipAd in vipAds)
                {
                    var vipAdForAdd = new VIPAd
                    {
                        Name      = vipAd.Name.ToLower(),
                        ImageUrl  = vipAd.ImageUrl,
                        DateAdded = DateTime.UtcNow
                    };

                    await context.AddAsync(vipAdForAdd);

                    await context.SaveChangesAsync();
                }
            }
        }
        public static async Task SeedProductImages(StoreContext context)
        {
            if (!context.ProductImages.Any())
            {
                var imagesData = await File.ReadAllTextAsync("../Services/Seeds/Data/ProductImages.json");

                var images = JsonSerializer.Deserialize <List <ProductImage> >(imagesData);
                foreach (var image in images)
                {
                    var imageForAdd = new ProductImage
                    {
                        ProductId   = image.ProductId,
                        ImageUrl    = image.ImageUrl,
                        IsMainPhoto = image.IsMainPhoto,
                        DateAdded   = DateTime.UtcNow
                    };

                    await context.AddAsync(imageForAdd);

                    await context.SaveChangesAsync();
                }
            }
        }
Пример #17
0
        public async Task <IActionResult> Payment(string stripeEmail, string stripeToken)
        {
            var customers = new Stripe.CustomerService();
            var charges   = new Stripe.ChargeService();
            var user      = _userManager.GetUserAsync(User).Result;

            int.TryParse(HttpContext.Request.Cookies["counter"], out int counter);
            var orderProducts = await GetOrderProductsList(counter);

            decimal price = 0;

            foreach (var item in orderProducts)
            {
                var stock = await _context.Stock.FirstAsync(x => x.Id == item.StockId);

                stock.Qty -= item.Qty;
                for (int i = 0; i < item.Qty; i++)
                {
                    price += item.Product.Price;
                }

                if (stock.Qty == 0)
                {
                    stock.IsLastElementOrdered = true;
                }

                _context.Update(stock);
                await _context.OrderProducts.AddAsync(item);
            }

            var customer = customers.Create(new Stripe.CustomerCreateOptions
            {
                Email   = stripeEmail,
                Source  = stripeToken,
                Name    = user.FirstName + " " + user.LastName,
                Address = new Stripe.AddressOptions {
                    City = user.City, Country = "Poland", PostalCode = user.PostCode, Line1 = user.Address1, Line2 = user.Address2
                },
                Phone = user.PhoneNumber,
            });

            var charge = charges.Create(new Stripe.ChargeCreateOptions
            {
                Amount       = Convert.ToInt64(price * 100),
                Description  = "Sample Charge",
                Currency     = "PLN",
                Customer     = customer.Id,
                ReceiptEmail = stripeEmail,
            });

            System.Diagnostics.Debug.WriteLine($"Zamówienie użytkownika {user.UserName} o id = {user.   Id}");


            //można to zrobić mapperem ale trzeba go dokładnie skonfigurować
            var order = new Order
            {
                FirstName     = user.FirstName,
                LastName      = user.LastName,
                Email         = user.Email,
                Address1      = user.Address1,
                Address2      = user.Address2,
                City          = user.City,
                PostCode      = user.PostCode,
                OrderProducts = orderProducts,
                OrderDate     = DateTime.Now,
                UserId        = user.Id,
                ChargeId      = charge.Id
            };

            await _context.AddAsync(order);

            // czyszczenie koszyka
            HttpContext.Response.Cookies.Delete("counter");

            // todo dodanie zamówienia do tabeli orders i przypisywać je danemu użytkownikowi

            await _context.SaveChangesAsync();

            System.Diagnostics.Debug.WriteLine("Poprawne zamowienie");

            return(RedirectToAction("OrdersHistory", "Account"));
        }
 public async Task AddAsync(T entity)
 {
     await _storeContext.AddAsync(entity);
 }