Exemplo n.º 1
0
        public ActionResult Post([FromBody] NewProductDTO value)
        {
            int      id   = _context.GetMaxProductId() + 1;
            Category cat  = _context.GetCategoryByName(value.Category);
            Person   pers = _context.GetPerson(value.PersonEmail);

            if (pers == null)
            {
                return(BadRequest(value.PersonEmail));
            }

            float pric = 0;

            try
            {
                pric = float.Parse(value.Price, CultureInfo.InvariantCulture.NumberFormat);
            }
            catch
            {
                return(BadRequest(value.Price));
            }

            _context.Add(new WebshopData.Models.Product
            {
                Id          = id,
                ItemName    = value.ItemName,
                Description = value.Description,
                Price       = pric,
                UploadTime  = DateTime.Now,
                Category    = cat,
                Person      = pers
            });

            return(Ok());
        }
Exemplo n.º 2
0
 public ActionResult Submit([FromBody] NewProductDTO newProduct)
 {
     //checkoutManager.SaveCart(new CartItemDTO
     //{
     //    SKU = Guid.NewGuid().ToString(),
     //    Description  = newProduct.Description,
     //    Price = newProduct.UnitPrice,
     //    Quantity = 1
     //});
     return(RedirectToAction("Cart", "Home"));
 }
        public async Task <IActionResult> AddNewProduct([FromBody] NewProductDTO dto)
        {
            var command = new AddNewProductCommand(dto.Name, dto.Price, dto.Stock);
            var result  = await CartSystem.AddNewProduct(command);

            if (result.IsSuccessful)
            {
                return(Ok());
            }
            return(BadRequest(result.Message));
        }
Exemplo n.º 4
0
 public Task <IActionResult> AddProduct([FromBody] NewProductDTO data)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 5
0
        public async Task <IHttpActionResult> NewProduct(NewProductDTO newProductDTO)
        {
            string userName = User.Identity.Name;
            User   user     = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault();

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Company       company       = user.Company;
            Product       product       = new Product();
            ProductUpdate productUpdate = new ProductUpdate();

            Regex regex = new Regex("[^a-zA-Z0-9_-]");

            product.URI = regex.Replace(newProductDTO.Name, "").ToLower();

            if (product.URI.Length == 0)
            {
                product.URI = null;
            }
            else if (company != null && db.Products.Where(_product => _product.URI == product.URI && _product.CompanyID == company.ID).SingleOrDefault() != null)

            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            if (company != null)
            {
                product.CompanyID = company.ID;
                product.Company   = company;
                product.User      = null;
            }
            else if (user.IsIndividual.HasValue && user.IsIndividual.Value)
            {
                product.UserId  = user.Id;
                product.User    = user;
                product.Company = null;
            }
            else
            {
                CompanyMember companyMember = await db.CompanyMembers.Where(cm => cm.UserID == user.Id).FirstOrDefaultAsync();

                if (companyMember != null)
                {
                    product.CompanyID = companyMember.CompanyID;
                    product.UserId    = user.Id;
                    product.Company   = null;
                }
            }

            product.Name        = newProductDTO.Name;
            product.Description = newProductDTO.Description;
            product.Privacy     = newProductDTO.Privacy;
            product.Logo        = newProductDTO.Logo;

            if (newProductDTO.Privacy == ProductPrivacy.VisibleToSelectedGroups)
            {
                foreach (CompanyFollowerGroupDTO companyFollowerGroupDto in newProductDTO.GroupsVisibleTo)
                {
                    product.GroupsVisibleTo.Add(db.CompanyFollowerGroups.Where(followerGroup => followerGroup.ID == companyFollowerGroupDto.ID && followerGroup.CompanyID == company.ID).SingleOrDefault());
                }
            }

            product = db.Products.Add(product);
            productUpdate.UserID     = user.Id;
            productUpdate.User       = user;
            productUpdate.ProductID  = product.ID;
            productUpdate.Product    = product;
            productUpdate.DateTime   = DateTime.Now;
            productUpdate.UpdateType = UpdateType.ProductAdded;
            db.ProductUpdates.Add(productUpdate);
            await db.SaveChangesAsync();

            return(Ok(Mapper.Map <Product, ProductDTO>(product)));
        }
Exemplo n.º 6
0
 public Task <bool> AddNewProduct(NewProductDTO data)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 7
0
        public IActionResult AddProduct(NewProductDTO newProduct)
        {
            string username = this.HttpContext.Session.GetString(SessionConstant.UserNameSession);

            if (string.IsNullOrEmpty(username))
            {
                return(RedirectToAction("Login", "Account"));
            }

            var suppliers  = this._supplierRepository.GetSuppliers();
            var categories = this._categoryRepository.GetCategories();

            ViewData["Suppliers"]  = suppliers;
            ViewData["Categories"] = categories;

            if (!ModelState.IsValid)
            {
                return(View("AddNew", newProduct));
            }

            bool isExisting = this._productRepository.IsExistingProductCodeOrName(
                newProduct.Serial, newProduct.Name, newProduct.Id
                );

            if (isExisting)
            {
                ModelState.AddModelError("", "Mã sản phẩm hoặc tên sản phẩm đã tồn tại.");
                return(View("AddNew", newProduct));
            }

            var    productInDb  = this._mapper.Map <Product>(newProduct);
            bool   uploadFileOk = false;
            string fileName     = "";

            if (newProduct.PictureFile == null || newProduct.PictureFile.FileName == "")
            {
                productInDb.Picture = "default.gif";
                uploadFileOk        = true;
            }
            else
            {
                fileName = WebUtility.HtmlEncode(
                    Path.GetFileName(newProduct.PictureFile.FileName));

                if ((newProduct.PictureFile.ContentType.ToLower() != "image/jpg") &&
                    (newProduct.PictureFile.ContentType.ToLower() != "image/jpeg") &&
                    (newProduct.PictureFile.ContentType.ToLower() != "image/pjpeg") &&
                    (newProduct.PictureFile.ContentType.ToLower() != "image/gif") &&
                    (newProduct.PictureFile.ContentType.ToLower() != "image/x-png") &&
                    (newProduct.PictureFile.ContentType.ToLower() != "image/png"))
                {
                    ModelState.AddModelError("",
                                             $"File upload ({fileName}) không phải file ảnh,.");
                    return(View("AddNew", newProduct));
                }

                if (newProduct.PictureFile.Length == 0)
                {
                    ModelState.AddModelError("",
                                             $"File upload ({fileName}) rỗng.");
                    return(View("AddNew", newProduct));
                }
                else if (newProduct.PictureFile.Length > 1048576)
                {
                    ModelState.AddModelError("",
                                             $"File upload ({fileName}) vượt quá 1 MB.");
                    return(View("AddNew", newProduct));
                }

                var filePath = Path.Combine(this._env.WebRootPath, "uploads", fileName);
                using (FileStream fs = new FileStream(filePath, FileMode.Create))
                {
                    newProduct.PictureFile.CopyTo(fs);
                }

                uploadFileOk = true;
            }

            if (uploadFileOk == false)
            {
                TempData["Fail"] = "Có lỗi trong quá trình upload file. !";
                return(View("AddNew", newProduct));
            }

            if (string.IsNullOrEmpty(productInDb.Picture))
            {
                productInDb.Picture = fileName;
            }

            productInDb.CreatedBy   = "admin";
            productInDb.CreatedDate = DateTime.Now;
            productInDb.UpdatedBy   = "admin";
            productInDb.UpdatedDate = DateTime.Now;

            bool isSuccess = this._productRepository.AddProduct(productInDb);

            if (isSuccess)
            {
                TempData["Success"] = "Cập nhật dữ liệu thành công !";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["Fail"] = "Cập nhật dữ liệu thất bại !";
                return(View("AddNew", newProduct));
            }
        }