public JsonResult GetAllProducts() { ProductApi productApi = new ProductApi(); List <ProductViewModel> result = productApi.GetActive().Where(q => q.ParentProductId == null).ToList(); return(Json(new { data = result }, JsonRequestBehavior.AllowGet)); }
public ActionResult EditProduct(string name, string[] sizeList, string[] colorList, string description, int categoryID, int supplierID, decimal price, int ID) { StringBuilder size = new StringBuilder(); StringBuilder color = new StringBuilder(); for (int i = 0; i < sizeList.Length; i++) { size.Append(sizeList[i]); if (i < sizeList.Length - 1) { size.Append(","); } } for (int i = 0; i < colorList.Length; i++) { color.Append(colorList[i]); if (i < colorList.Length - 1) { color.Append(","); } } List <ProductImageViewModel> listProductImage = new List <ProductImageViewModel>(); ProductImageViewModel productImage = new ProductImageViewModel(); var orderCount = 0; byte[] avatarImage = null; if (Request.Files.Count > 0) { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; int fileSizeInBytes = file.ContentLength; using (var br = new BinaryReader(file.InputStream)) { avatarImage = br.ReadBytes(fileSizeInBytes); } string picUrl = null; if (avatarImage != null) { picUrl = SaveImageToServer(avatarImage); productImage.PicUrl = picUrl; productImage.DisplayOrder = ++orderCount; listProductImage.Add(productImage); } } } ProductApi productApi = new ProductApi(); var product = productApi.GetActive().Where(q => q.ID == ID).FirstOrDefault(); product.Name = name; product.Size = size.ToString(); product.Color = color.ToString(); product.Description = description; product.CategoryID = categoryID; product.Supplier = null; product.SupplierId = supplierID; product.Price = price; product.ProductImages = listProductImage; productApi.EditProduct(product); return(Json(new { success = true, message = "Successfully added!" }, JsonRequestBehavior.AllowGet)); }
public JsonResult setOrder([FromBody] OrderViewModel order, string accessToken) { var orderApi = new OrderApi(); var customerApi = new CustomerApi(); var employeeApi = new EmployeeApi(); var productApi = new ProductApi(); //Get CustomerId from token int number; order.CustomerID = 0; if (Int32.TryParse(getCustomerIdFromToken(accessToken), out number)) { order.CustomerID = number; } //Check customer exist if customerId != 0 (customerId default is 0) if (order.CustomerID != 0 && (order.CustomerID == null || customerApi.GetActive().Where(q => q.ID == order.CustomerID) == null)) { return(Json(new { status = new { success = false, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CHECK_CUSTOMER_FAIL }, data = new { } })); } //Check employee exist if employeeId != 0 (employeeId is 0 for online Order) order.EmployeeID = null; //Check order details if (order.OrderDetails == null || order.OrderDetails.Count() == 0) { return(Json(new { status = new { success = false, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CHECK_ORDER_DETAIL_FAIL }, data = new { } })); } //Calculate order decimal totalAmount = 0; foreach (var orderdetail in order.OrderDetails) { //Check Order detail ProductId not null and product exist if (orderdetail.ProductID == null) { return(Json(new { status = new { success = false, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CHECK_PRODUCT_FAIL }, data = new { } })); } //Check product exist var product = productApi.GetActive().Where(q => q.ID == orderdetail.ProductID).FirstOrDefault(); if (product == null) { return(Json(new { status = new { success = false, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CHECK_PRODUCT_FAIL }, data = new { } })); } //Check Order detail Quantity if (orderdetail.Quantity == null || orderdetail.Quantity < 1) { return(Json(new { status = new { success = false, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CHECK_QUANTITY_FAIL }, data = new { } })); } //Set OrderDetail orderdetail.Price = product.Price; orderdetail.Amount = product.Price * orderdetail.Quantity; //Calculate total amount totalAmount += orderdetail.Amount; } //Create payment List <PaymentViewModel> listPayment = new List <PaymentViewModel>(); PaymentViewModel payment = new PaymentViewModel(); payment.Amount = totalAmount; payment.Type = (int)PaymentTypeEnum.Cash; listPayment.Add(payment); //Create Order order.CheckInDate = DateTime.Now; order.TotalAmount = totalAmount; order.Payments = listPayment; order.InvoiceID = generateInvoiceID(); order.Status = (int)OrderStatusEnum.Finish; var rs = orderApi.CreateOrder(order); if (rs == false) { return(Json(new { status = new { success = false, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CREATE_ORDER_FAIL }, data = new { } })); } return(Json(new { status = new { success = true, status = ConstantManager.STATUS_SUCCESS, message = ConstantManager.MES_CREATE_ORDER_SUCCESS }, data = new { data = new { InvoiceID = order.InvoiceID, Status = order.Status } } })); }