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(); }
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 })); }
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()); }
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); }
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()); }
public async Task <ActionResult <Employee> > Post(Employee employee) { db.Employees.Add(employee); await db.SaveChangesAsync(); return(CreatedAtAction("GetEmployees", new { id = employee.id }, employee)); }
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()); }
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)); }
public async Task DeleteAmmount(int id) { var ammount = _ctx.Ammounts.FirstOrDefault(p => p.ProductId == id); _ctx.Ammounts.Remove(ammount); await _ctx.SaveChangesAsync(); }
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()); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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")); }
// 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(); }
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); }
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()); }
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); }
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)); }
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); }
public void Add(SysLog item) { using (var db = new ApplicationDb()) { db.SysLogs.Add(item); db.SaveChangesAsync().Wait(); } }
public void Add(SysLog item) { using (var db = new ApplicationDb()) { db.SysLogs.Add(item); db.SaveChangesAsync().Wait(); } }
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(); } }