public async Task <AddApiKeyResponse> Handle(AddApiKeyRequest request, CancellationToken cancellationToken) { var existing = await _storeDbContext .ApiKeys .Where(ak => ak.Name == request.Name) .SingleOrDefaultAsync(); if (existing == null) { var apiKey = new ApiKeyEntity( request.Name, ApiKeyGenerator.CreateApiKey(), request.ValidTo ?? DateTime.UtcNow.AddYears(1)); _storeDbContext.Add(apiKey); await _storeDbContext.SaveChangesAsync(cancellationToken); return(new AddApiKeyResponse() { ApiKeyId = apiKey.Id, ApiKey = apiKey.Key }); } Log.ApiKeyAlreadyExist(_logger, request.Name); throw new InvalidOperationException($"A ApiKey with name {request.Name} already exist."); }
public async Task <string> Handle(AddFeatureRequest request, CancellationToken cancellationToken) { var product = await _storeDbContext .Products .Where(p => p.Name == request.ProductName) .SingleOrDefaultAsync(cancellationToken); if (product != null) { var existingFeature = await _storeDbContext .Features .Where(f => f.Name == request.Name && f.ProductEntityId == product.Id) .SingleOrDefaultAsync(cancellationToken); if (existingFeature == null) { var feature = new FeatureEntity(product.Id, request.Name, request.Archived, request.Description); _storeDbContext.Add(feature); await _storeDbContext.SaveChangesAsync(cancellationToken); return(feature.Name); } else { Log.FeatureNameAlreadyExist(_logger, request.Name); throw new InvalidOperationException($"A feature with the same name already exist on the store."); } } else { Log.ProductNotExist(_logger, request.ProductName); throw new InvalidOperationException($"The product with id {request.ProductName} does not exist in the store."); } }
public async Task <Order> CreateOrder(Order order) { _context.Add(order); await _context.SaveChangesAsync(); return(order); }
public int CreateOrderDetail(OrderDetail orderDetail) { List <OrderDetail> orderDetails = _context.OrderDetails.ToList(); OrderDetail FindOrderDetail = orderDetails.Find(el => el.OrderId == orderDetail.OrderId && el.ProductId == orderDetail.ProductId); Product product = _context.Products.FirstOrDefault(el => el.Id == orderDetail.ProductId); if (orderDetails.Contains(FindOrderDetail)) { FindOrderDetail.Quantity += orderDetail.Quantity; FindOrderDetail.UnitPrice += CalculateMoney(product.PricePerUnit, orderDetail.Discount, orderDetail.Quantity); _context.Update(FindOrderDetail); } else { orderDetail.UnitPrice = CalculateMoney(product.PricePerUnit, orderDetail.Discount, orderDetail.Quantity); _context.Add(orderDetail); } return(_context.SaveChanges()); }
public ActionResult CreateOrder([FromBody] Order order) { if (ModelState.IsValid) { order.OrderId = 0; order.Shipped = false; //не доверяем информации о сумме заказа, присланного с клиента order.Payment.Total = GetPrice(order.Goods); ProcessPayment(order.Payment); if (order.Payment.AuthCode != null) { _context.Add(order); _context.SaveChanges(); return(Ok(new { orderId = order.OrderId, authCode = order.Payment.AuthCode, amount = order.Payment.Total })); } else { return(BadRequest("Платёж отклонён")); } } return(BadRequest(ModelState)); }
/// <summary> /// Creates carts for the databse /// </summary> /// <param name="cart"></param> /// <returns></returns> public async Task <Cart> CreateCart(Cart cart) { _context.Add(cart); await _context.SaveChangesAsync(); return(cart); }
public async Task <string> Handle(AddProductRequest request, CancellationToken cancellationToken) { var existing = await _storeDbContext .Products .Where(p => p.Name == request.Name) .SingleOrDefaultAsync(cancellationToken); if (existing == null) { var product = new ProductEntity(request.Name, request.Description); product.Deployments.Add(new DeploymentEntity( productEntityId: product.Id, name: request.DefaultDeploymentName ?? EsquioConstants.DEFAULT_DEPLOYMENT_NAME, byDefault: true)); _storeDbContext.Add(product); await _storeDbContext .SaveChangesAsync(cancellationToken); return(product.Name); } Log.ProductAlreadyExist(_logger, request.Name); throw new InvalidOperationException("A product with the same name already exist in the store."); }
/// <summary> /// Adds the product asynchronous. /// </summary> /// <param name="UserId">The user identifier.</param> /// <param name="ProductId">The product identifier.</param> /// <param name="quantity">The quantity.</param> /// <returns></returns> public async Task <Basket> AddProductAsync(string UserId, int ProductId, int quantity = 1) { Basket basket = await GetProductInBasket(UserId, ProductId); if (basket != null) { basket.Quantity += quantity; } else { basket = new Basket() { ProductID = ProductId, UserID = UserId, Quantity = quantity }; _storeDbContext.Add(basket); } try { await _storeDbContext.SaveChangesAsync(); return(basket); } catch (DbUpdateConcurrencyException ex) { throw ex; } }
public async Task <IActionResult> Create([Bind("Name,Price,Id")] Power power) { if (ModelState.IsValid) { _context.Add(power); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(power)); }
public async Task <IActionResult> Create([Bind("PaymentId,PaymentType,Amount,IsCleared")] Payment payment) { if (ModelState.IsValid) { _context.Add(payment); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(payment)); }
public async Task <IActionResult> Create([Bind("Id,Name,Age,Email")] Signup @signup) { if (ModelState.IsValid) { _context.Add(@signup); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(@signup)); }
public async Task <IActionResult> Create([Bind("SalesPersonId,FirstName,LastName,Phone,HireDate,IsActive")] SalesPerson salesPerson) { if (ModelState.IsValid) { _context.Add(salesPerson); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(salesPerson)); }
public async Task <IActionResult> Create([Bind("Id,Name,Description")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Create([Bind("ModelSeries,CoreFrequence,Memory,MemoryType,MemoryFrequence,CategoryName,Id,Vendor,Year,Price,Description")] Gpu gpu) { if (ModelState.IsValid) { _context.Add(gpu); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(gpu)); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Email,Phone")] Customer customer) { if (ModelState.IsValid) { _context.Add(customer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(customer)); }
public async Task <IActionResult> Create([Bind("Id,status")] Status status) { if (ModelState.IsValid) { _context.Add(status); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(status)); }
public async Task <IActionResult> Create([Bind("PrductId,ProductName,Description,StandardPrice,UnitType,OnHand,IsOrdered,BackOrdered")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <AddApiKeyResponse> Handle(AddApiKeyRequest request, CancellationToken cancellationToken) { var existing = await _storeDbContext .ApiKeys .Where(ak => ak.Name == request.Name) .SingleOrDefaultAsync(); if (existing == null) { var key = ApiKeyGenerator.CreateApiKey(); var apiKey = new ApiKeyEntity( name: request.Name, key: key, validTo: request.ValidTo ?? DateTime.UtcNow.AddYears(1)); var apiKeyPermissions = new PermissionEntity() { SubjectId = key, Kind = SubjectType.Application, ApplicationRole = Enum.Parse <ApplicationRole>(request.ActAs, ignoreCase: true) }; _storeDbContext .Add(apiKeyPermissions); _storeDbContext .Add(apiKey); await _storeDbContext.SaveChangesAsync(cancellationToken); return(new AddApiKeyResponse() { Name = apiKey.Name, Key = key }); } Log.ApiKeyAlreadyExist(_logger, request.Name); throw new InvalidOperationException($"A ApiKey with name {request.Name} already exist."); }
public async Task <IActionResult> Create([Bind("Id,CustomerId")] Cart cart) { if (ModelState.IsValid) { _context.Add(cart); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Id", cart.CustomerId); return(View(cart)); }
public async Task <IActionResult> Create([Bind("CustomerId,LastName,FirstName,StoreId,Phone,Email,Address,City,State,FullName")] Customers customers) { if (ModelState.IsValid) { _context.Add(customers); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["StoreId"] = new SelectList(_context.Stores, "StoreId", "StoreId", customers.StoreId); return(View(customers)); }
public async Task <IActionResult> Create([Bind("Id,Name,Description,ProductionDate,ExpireDate,Amount,Price,ImageName,ImagePath,CategoryID,isoffer")] Item item) { if (ModelState.IsValid) { _context.Add(item); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategoryID"] = new SelectList(_context.Categories, "Id", "Id", item.CategoryID); return(View(item)); }
public async Task <IActionResult> Create([Bind("Id,Quantity,CartID,ItemId")] CartItems cartItems) { if (ModelState.IsValid) { _context.Add(cartItems); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ItemId"] = new SelectList(_context.Carts, "Id", "Id", cartItems.ItemId); ViewData["ItemId"] = new SelectList(_context.Items, "Id", "Id", cartItems.ItemId); return(View(cartItems)); }
public async Task <IActionResult> Create([Bind("Id,Quantity,Price,ItemId,OrderId")] OrderItems orderItems) { if (ModelState.IsValid) { _context.Add(orderItems); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ItemId"] = new SelectList(_context.Items, "Id", "Id", orderItems.ItemId); ViewData["OrderId"] = new SelectList(_context.Orders, "Id", "Id", orderItems.OrderId); return(View(orderItems)); }
public async Task <IActionResult> Create([Bind("OrderId,CustomerId,ProductOrdersId,StoreId,Date,Quantities,TotalPrice")] Order order) { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CustomerId"] = new SelectList(_context.Customers, "CustomerId", "FirstName", order.CustomerId); ViewData["StoreId"] = new SelectList(_context.Stores, "StoreId", "City", order.StoreId); return(View(order)); }
public async Task <IActionResult> Create([Bind("ProductOrderId,ProductId,OrderId,Quantity")] ProductOrders productOrders) { if (ModelState.IsValid) { _context.Add(productOrders); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["OrderId"] = new SelectList(_context.Orders, "OrderId", "OrderId", productOrders.OrderId); ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "Name", productOrders.ProductId); return(View(productOrders)); }
public async Task <IActionResult> Create([Bind("Id,ItemId,OfferId")] Offer offer) { if (ModelState.IsValid) { _context.Add(offer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ItemId"] = new SelectList(_context.Items, "Id", "Id", offer.ItemId); ViewData["OfferId"] = new SelectList(_context.Items, "Id", "Id", offer.OfferId); return(View(offer)); }
public async Task <IActionResult> CreateReviewPost(CreateReviewViewModel model) { var order = await _context.Orders.Include(o => o.OrderedProducts).ThenInclude(op => op.Product) .FirstOrDefaultAsync(o => o.ID == model.OrderID); var product = order.OrderedProducts.FirstOrDefault(op => op.ProductID == model.ProductID).Product; try { if (ModelState.IsValid) { ProductReview review = await _context.ProductReviews.FirstOrDefaultAsync(r => r.OrderID == order.ID && r.ProductID == product.ID); if (review == null) { review = new ProductReview() { Order = order, Product = product, Rating = model.Rating, DateAdded = DateTime.Now.Date, Description = model.Description }; _context.Add(review); } else { review.Rating = model.Rating; review.DateAdded = DateTime.Now.Date; review.Description = model.Description; _context.Update(review); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Details))); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } model.Order = order; model.Product = product; return(View(model)); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (ProductModel.ImageFile != null && ProductModel.ImageFile.Length > 0) { ProductModel.ImageUrl = await _fileUploader.Upload(ProductModel.ImageFile, "products"); } _dbContext.Add(ProductModel.ToEntity()); await _dbContext.SaveChangesAsync(); return(RedirectToPage("Index")); }
/// <summary> /// Adds the inputted cartproduct to the user's cart. If the item is already in the user's cart, adds to the quantity instead. /// </summary> /// <param name="cartProduct">The product to add to the user's current cart</param> /// <returns>Task result</returns> public async Task AddProductToCart(CartProduct cartProduct) { var cartProducts = await GetAllProductsForCartById(cartProduct.CartID); foreach (var cartItem in cartProducts) { if (cartItem.ProductID == cartProduct.ProductID) { await UpdateProductQuantity(cartItem.ID, cartItem.Quantity + cartProduct.Quantity); return; } } _storeContext.Add(cartProduct); await _storeContext.SaveChangesAsync(); }
public async Task <IActionResult> Create(ModelForCreate model) { if (ModelState.IsValid) { Address address = new Address() { ProvinceId = model.Province, DistrictId = model.District, WardId = model.Ward, HouseNum = model.HouseNumber }; _context.Add(address); await _context.SaveChangesAsync(); ApplicationUser User = new ApplicationUser() { Avatar = AvatarPathForUser(model.iformfile_path), FullName = model.FullName, PhoneNumber = model.PhoneNum, Email = model.Email, UserName = model.Email, AddressId = address.Id }; var result = await _userManager.CreateAsync(User, model.Password); address.ApplicationUserId = User.Id; await _context.SaveChangesAsync(); if (result.Succeeded) { await _signInManager.SignInAsync(User, false); return(RedirectToAction("Index", "Account")); } else { foreach (var item in result.Errors) { ModelState.AddModelError("", item.Description); } } } return(View()); }