コード例 #1
0
        public async Task  AddToCart(Product product, int quantity)
        {
            //setting Id

            //retrieve product from database
            //ShoppingCartId = GetCartId();

            var cartItems = await
                            _ctx.CartItems.FirstOrDefaultAsync(
                c => c.Product.Id == product.Id &&
                c.CartId == ShoppingCartId);

            //create new product if no cart item exists
            if (cartItems == null)
            {
                cartItems = new ShoppingCartItem
                {
                    CartId   = ShoppingCartId,
                    Product  = product,
                    Quantity = quantity,
                };
                _ctx.CartItems.Add(cartItems);
            }
            else
            {
                cartItems.Quantity++;
            }
            await _ctx.SaveChangesAsync();
        }
コード例 #2
0
        public async Task <IActionResult> AddToCart([Bind("id,ProductName,Price,Discount")] Cart cart,
                                                    string proName, double?price, int?discount, long postId)
        {
            string id = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (id == null)
            {
                TempData["msg"] = "انت غير مسجل معنا";
                return(RedirectToAction("PostView", new { id = postId }));
            }
            if (!string.IsNullOrEmpty(proName) && price != null)
            {
                if (!GetCartProductName(proName, id))
                {
                    cart.ProductName = proName;
                    cart.Price       = price;
                    cart.Discount    = discount;
                    cart.UserId      = id;

                    db.Add(cart);
                    await db.SaveChangesAsync();

                    cartCount = CartCount();
                }
                else
                {
                    TempData["msg"] = "اسم المنتج: (" + proName + ") موجود مسبقا بقائمتك";
                }
            }

            return(RedirectToAction("PostView", new { id = postId }));
        }
コード例 #3
0
        public async Task <IActionResult> PutWagon(int id, Wagon wagon)
        {
            if (id != wagon.WagonId)
            {
                return(BadRequest());
            }

            _context.Entry(wagon).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WagonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #4
0
        public async Task <bool> DeleteUser(List <string> ids)
        {
            if (ids.Count < 1)
            {
                return(false);
            }
            var i = 0;

            foreach (string id in ids)
            {
                var user = await _db.Users.FirstOrDefaultAsync(x => x.Id == id);

                if (user == null)
                {
                    return(false);
                }
                _db.Users.Remove(user);
                i++;
            }
            if (i > 0)
            {
                await _db.SaveChangesAsync();
            }
            return(true);
        }
コード例 #5
0
        public async Task <IActionResult> PutDock(int id, Dock dock)
        {
            if (id != dock.DockId)
            {
                return(BadRequest());
            }

            _context.Entry(dock).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DockExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #6
0
        public async Task <ActionResult <Employee> > Post(Employee employee)
        {
            db.Employees.Add(employee);
            await db.SaveChangesAsync();

            return(CreatedAtAction("GetEmployees", new { id = employee.id }, employee));
        }
コード例 #7
0
        public async Task <IActionResult> PutHaul(int id, Haul haul)
        {
            if (id != haul.HaulId)
            {
                return(BadRequest());
            }

            _context.Entry(haul).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HaulExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #8
0
        public async Task <IActionResult> Edit(string id, [Bind("id,RoleId,UserId")] UserRole userRole)
        {
            if (id != userRole.id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(userRole);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserRoleExists(userRole.id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            ViewData["RoleId"] = new SelectList(_context.AppRoles, "id", "RoleName");
            ViewData["UserId"] = new SelectList(_context.AppUsers, "id", "UserName");
            return(View(userRole));
        }
コード例 #9
0
        public async Task DeleteAmmount(int id)
        {
            var ammount = _ctx.Ammounts.FirstOrDefault(p => p.ProductId == id);

            _ctx.Ammounts.Remove(ammount);
            await _ctx.SaveChangesAsync();
        }
コード例 #10
0
        public async Task <IActionResult> PutEmployee(int id, Employee employee)
        {
            if (id != employee.id)
            {
                return(BadRequest());
            }

            _context.Entry(employee).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #11
0
        public async Task <IActionResult> Create([Bind("id,catName")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
コード例 #12
0
        public async Task <IActionResult> Create([Bind("id,ProductName,Price,Discount")] Cart cart)
        {
            if (ModelState.IsValid)
            {
                _context.Add(cart);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(cart));
        }
コード例 #13
0
        public async Task <IActionResult> Create([Bind("id,firstName,lastName,UserName,Email,Address,Country,Zip")] BillingAddress billingAddress)
        {
            if (ModelState.IsValid)
            {
                _context.Add(billingAddress);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(billingAddress));
        }
コード例 #14
0
        public async Task <IActionResult> Create([Bind("ID,TransactionID,Type,ProductID,ProductNetPrice,ProductGrossPrice,Date")] Transaction transaction)
        {
            if (ModelState.IsValid)
            {
                _context.Add(transaction);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(transaction));
        }
コード例 #15
0
        public async Task <IActionResult> Create([Bind("ID,FirstName,LastName,ClientEmail,Street,Town,PostalCode,Voivodeship,Country,ownerID")] ClientsViewModel client)
        {
            if (ModelState.IsValid)
            {
                client.ownerID = HttpContext.Session.GetObjectFromJson <int>("ownerID");
                _context.Add(ClientMapper.MapViewToClient(client));
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(client));
        }
コード例 #16
0
        public async Task <IActionResult> Create(EmployeesViewModel employee)
        {
            if (ModelState.IsValid)
            {
                employee.ownerID = HttpContext.Session.GetObjectFromJson <int>("ownerID");
                _context.Add(EmployeeMapper.MapViewToEmployee(employee));
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(employee));
        }
コード例 #17
0
        public async Task <ActionResult> Create([Bind(Include = "Id,Name,Category")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
コード例 #18
0
        public async Task <IActionResult> Create([Bind("ID,Name,Trade,CompanyEmail,Street,Town,PostalCode,Voivodeship,Country,OwnerID")] CompaniesViewModel company)
        {
            if (ModelState.IsValid)
            {
                company.ownerID = HttpContext.Session.GetObjectFromJson <int>("ownerID");
                _context.Add(CompanyMapper.MapViewToCompany(company));
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(company));
        }
コード例 #19
0
        public async Task <IActionResult> Create([Bind("id,SubCatName,catId")] SubCategory subCategory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(subCategory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["catId"] = new SelectList(_context.Categories, "id", "catName", subCategory.catId);
            return(View(subCategory));
        }
コード例 #20
0
        public async Task <IActionResult> Create([Bind("ID,Name,Description,NetPrice,GrossPrice,Quantity")] ProductsViewModel product)
        {
            if (ModelState.IsValid)
            {
                product.ownerID    = HttpContext.Session.GetObjectFromJson <int>("ownerID");
                product.GrossPrice = product.NetPrice * 1.23M;
                _context.Add(ProductMapper.MapViewToProduct(product));
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
コード例 #21
0
        public async Task <IActionResult> Create([Bind("id,cardType,cardName,cardNumber,expiration,cvv,cartId,billingId")] Payment payment)
        {
            if (ModelState.IsValid)
            {
                _context.Add(payment);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["cartId"]    = new SelectList(_context.Carts, "id", "id", payment.cartId);
            ViewData["billingId"] = new SelectList(_context.BillingAddresses, "id", "Address", payment.billingId);
            return(View(payment));
        }
コード例 #22
0
        public async Task <IActionResult> SaveEmail(int id)
        {
            int   ownerId = HttpContext.Session.GetObjectFromJson <int>("ownerID");
            Email msg     = HttpContext.Session.GetItemOfSessionList <Email>(string.Format("ReceivedEmails-{0}", ownerId), id);

            msg.OwnerID = ownerId;
            msg.Saved   = true;
            _context.Add(msg);
            await _context.SaveChangesAsync();

            es.SetEmailAsRead(msg.Uid);
            HttpContext.Session.RemoveFromSessionList <Email>(string.Format("ReceivedEmails-{0}", ownerId), id);
            return(RedirectToAction("ReceiveEmails"));
        }
コード例 #23
0
        // Register Settings **************************************
        public async Task InsertUserSetting()
        {
            UserSetting userSetting = new UserSetting();

            userSetting.isEmailConfirm     = true;
            userSetting.isRegisterOpen     = true;
            userSetting.MinimumPassLength  = 1;
            userSetting.MaxPassLength      = 25;
            userSetting.isDigit            = false;
            userSetting.isUpper            = false;
            userSetting.SendWelcomeMessage = false;
            db.Add(userSetting);
            await db.SaveChangesAsync();
        }
コード例 #24
0
        public async Task RemoveItems(int id)
        {
            var items = _ctx.Images.Where(p => p.ProductId == id);

            _ctx.RemoveRange(items);
            await _ctx.SaveChangesAsync();
        }
        public async Task <Order> AddOrderAsync(Order model)
        {
            if (model != null)
            {
                var order = new Order
                {
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    Address     = model.Address,
                    City        = model.City,
                    phoneNumber = model.phoneNumber,
                    Email       = model.Email,
                    cartId      = model.cartId
                };
                var user = await _manager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    order.userId = user.Id;
                }

                await _db.orders.AddAsync(order);

                await _db.SaveChangesAsync();

                return(order);
            }
            return(null);
        }
コード例 #26
0
        public async Task <IActionResult> addOrder(Order model)
        {
            if (model.cartId == null)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                var order = await _repo.AddOrderAsync(model);

                OrderProduct orderProduct = new OrderProduct
                {
                    OrderId = order.Id
                };

                var basket = await _basketrepo.GetBasketAsync(order.cartId);

                var products = await _db.products.ToListAsync();

                foreach (var item in products)
                {
                    if (item.ProductId == basket.items.FirstOrDefault().Id)
                    {
                        orderProduct.ProductId = item.ProductId;
                        item.Amount           -= basket.items.FirstOrDefault().Quantity;
                        //await _db.SaveChangesAsync();
                    }
                }
                _db.orderProducts.Add(orderProduct);
                await _db.SaveChangesAsync();

                return(Ok());
            }
            return(BadRequest());
        }
コード例 #27
0
        public async Task Can_insertProductPageIntoDbViaUrl_productData()
        {
            //Arrange
            await using var context = new ApplicationDb(ContextOptions);
            Seed();
            var browsingContextServiceMock = new Mock <IBrowsingContextService>();

            browsingContextServiceMock.Setup(service =>
                                             service.OpenPageAsync("https://prom.ua/p1367019485-rozumni-smart-godinnik.html?"))
            .ReturnsAsync(await MockOpenPageAsync());

            var controller    = new ProductService(context, browsingContextServiceMock.Object);
            var targetProduct = context.Products.FirstOrDefault();

            if (targetProduct != null)
            {
                targetProduct.Url = @"https://prom.ua/p1367019485-rozumni-smart-godinnik.html?";
            }
            await context.SaveChangesAsync();

            //Act
            var result = await controller.InsertProductPageIntoDb(targetProduct?.Url);

            //Assert
            Assert.NotNull(result);

            Assert.Equal("Розумні смарт годинник Smart watch DM08", result.Title);
        }
コード例 #28
0
        public async Task <IActionResult> Create([Bind("id,Title,PostContent,PostImg,Auther,PostDate,PostViews,PostLike,LikeUserName,SubId,IsPublish,ProductName,Price,Discount")] Post post, IFormFile img)
        {
            ViewBag.msg = string.Empty;
            string id = User.FindFirst(ClaimTypes.Name)?.Value;

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

            string newFileName = string.Empty;

            if (img != null && img.Length > 0)
            {
                newFileName = img.FileName;
                if (IsImageValidate(newFileName))
                {
                    string filename = Path.Combine(host.WebRootPath + "/images/Post", newFileName);
                    await img.CopyToAsync(new FileStream(filename, FileMode.Create));
                }
                else
                {
                    ViewBag.msg = "الملفات المسموح بها : png, jpeg, jpg, gif, bmp";
                    return(View());
                }
            }

            try
            {
                post.Auther       = id;
                post.LikeUserName = "";
                post.PostDate     = DateTime.Now;
                post.PostImg      = newFileName;
                post.PostLike     = 0;
                post.PostViews    = 0;

                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch { }

            ViewData["SubId"] = new SelectList(_context.SubCategories, "id", "SubCatName", post.SubId);
            return(View(post));
        }
コード例 #29
0
        public async Task <Category> AddCategoryAsync(Category model)
        {
            if (model != null)
            {
                var category = new Category
                {
                    categoryName    = model.categoryName,
                    Num_of_products = model.Num_of_products
                };
                await _db.categories.AddAsync(category);

                await _db.SaveChangesAsync();

                return(category);
            }
            return(null);
        }
コード例 #30
0
 public void Add(SysLog item)
 {
     using (var db = new ApplicationDb())
     {
         db.SysLogs.Add(item);
         db.SaveChangesAsync().Wait();
     }
 }
コード例 #31
0
ファイル: SysLogService.cs プロジェクト: b9502032/MySite
 public void Add(SysLog item)
 {
     using (var db = new ApplicationDb())
     {
         db.SysLogs.Add(item);
         db.SaveChangesAsync().Wait();
     }
 }
コード例 #32
0
ファイル: SysLogService.cs プロジェクト: b9502032/MySite
 public void DeleteExpiredData()
 {
     using (var db = new ApplicationDb())
     {
         //只保留一定数量的日志,根据web.config中的设置值,默认单位:天。
         if (ConfigurationManager.AppSettings["LogValidity"] != null)
         {
             double logValidity = Convert.ToDouble(ConfigurationManager.AppSettings["LogValidity"]);
             DateTime createddatetime = DateTime.Now.AddDays(-logValidity);
             foreach (SysLog item in db.SysLogs.Where(a => a.CreatedDate < createddatetime))
             {
                 db.SysLogs.Remove(item);
             }
         }
         db.SaveChangesAsync().Wait();
     }
 }