Exemplo n.º 1
0
        public async Task <bool> CreateRole(Role role)
        {
            try
            {
                await _context.Roles.AddAsync(role);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public async Task <CreateProductResponse> CreateProduct(Product product, IFormFile image)
        {
            CreateProductResponse response = new CreateProductResponse();

            try
            {
                if (!image.ImageValidator())
                {
                    response.DidError = true;
                    response.Message  = "تصویر وارد شده معیوب است";
                    return(response);
                }

                //Save Doc Image
                string imgName      = CodeGenerators.GenerateGUID("product_");
                string path         = "wwwroot/Images/Product";
                string productImage = image.SaveImage(imgName, path, null);

                product.ProductImage = productImage;
                product.CreateDate   = DateTime.Now;


                //Add Driver Doc
                await _context.Products.AddAsync(product);

                await _context.SaveChangesAsync();



                response.DidError    = false;
                response.Message     = "عملیات با موفقیت انجام شد";
                response.ReturnValue = product.ProductId;

                return(response);
            }
            catch
            {
                response.DidError = true;
                response.Message  = "Unhandled Exception Occurred";
                return(response);
            }
        }
Exemplo n.º 3
0
        public async Task <bool> AddToCart(int productId, string userPhoneNumber)
        {
            try
            {
                var customer = await _context.Users
                               .SingleOrDefaultAsync(u => u.UserPhoneNumber == userPhoneNumber);

                var product = await _context.Products.FindAsync(productId);

                if (customer == null || product == null)
                {
                    return(false);
                }

                var order = await _context.Orders
                            .SingleOrDefaultAsync(o => o.UserId == customer.UserId && !o.IsFinally);

                if (order == null)
                {
                    // bayad factor jadid ijad shavad
                    await _context.Orders.AddAsync(new Order()
                    {
                        UserId       = customer.UserId,
                        OrderSum     = product.ProductPrice,
                        CreateDate   = DateTime.Now,
                        IsFinally    = false,
                        OrderDetails = new List <OrderDetail>()
                        {
                            new OrderDetail()
                            {
                                ProductId = productId,
                                Count     = 1,
                                Price     = product.ProductPrice
                            }
                        }
                    });
                }
                else
                {
                    // factor baz darad
                    await _context.OrderDetails.AddAsync(new OrderDetail()
                    {
                        OrderId   = order.OrderId,
                        ProductId = productId,
                        Price     = product.ProductPrice,
                        Count     = 1
                    });

                    order.OrderSum += product.ProductPrice;
                    _context.Orders.Update(order);
                }

                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }