public void Create(ProductForSellerDto productForSellerDto, byte[] avatar)
        {
            var product = new Product();

            product.Name        = productForSellerDto.Name;
            product.Description = productForSellerDto.Description;
            product.ProducerID  = productForSellerDto.ProducerID;
            product.CategoryID  = productForSellerDto.CategoryID;
            product.Price       = productForSellerDto.Price;
            product.Quantity    = productForSellerDto.Quantity;
            product.Status      = productForSellerDto.Status;
            product.SellerID    = productForSellerDto.SellerID;
            foreach (var specificationValueDto in productForSellerDto.SpecificationValuesDtos)
            {
                var specificationValue = this.Context.Set <SpecificationValue>().SingleOrDefault(s => s.ProductSpecificationID == specificationValueDto.SpecificationID &&
                                                                                                 s.Value == specificationValueDto.Value);

                if (specificationValue == null)
                {
                    throw new Exception("specification value not found");
                }

                product.SpecificationValues.Add(specificationValue);
            }

            product.AvatarOfProduct        = new AvatarOfProduct();
            product.AvatarOfProduct.Avatar = avatar;

            this.Context.Set <Product>().Add(product);
        }
Пример #2
0
        public ProductForSellerDto Update(ProductForSellerDto productForSellerDto)
        {
            var product = this._unitOfWork.Products.Get(productForSellerDto.ProductID);

            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (!IsAuthorized(product.SellerID))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            productForSellerDto.UpdateModel(product);

            product.SpecificationValues.Clear();

            foreach (var specificationValueDto in productForSellerDto.SpecificationValuesDtos)
            {
                var specificationValue = this._unitOfWork.SpecificationValues.SingleOrDefault(s => s.ProductSpecificationID == specificationValueDto.SpecificationID &&
                                                                                              s.Value == specificationValueDto.Value);
                if (specificationValue == null)
                {
                    throw new Exception("Can not find this specification or value");
                }

                product.SpecificationValues.Add(specificationValue);
            }

            this._unitOfWork.Complete();

            var productInDb = this._unitOfWork.Products.SingleOrDefault(p => p.ID == product.ID);

            //Load related objects
            this._unitOfWork.Categories.Load(c => c.ID == productInDb.CategoryID);
            this._unitOfWork.Producers.Load(p => p.ID == productInDb.ProducerID);

            return(new ProductForSellerDto(productInDb));
        }
Пример #3
0
        public ProductForSellerDto Create()
        {
            var productForSellerDto = new ProductForSellerDto();

            var form = HttpContext.Current.Request.Form;

            //Get data
            productForSellerDto.Name                    = form["name"].ToString();
            productForSellerDto.Description             = form["description"].ToString();
            productForSellerDto.ProducerID              = int.Parse(form["producerID"].ToString());
            productForSellerDto.CategoryID              = int.Parse(form["categoryID"].ToString());
            productForSellerDto.Price                   = int.Parse(form["price"].ToString());
            productForSellerDto.Quantity                = int.Parse(form["quantity"].ToString());
            productForSellerDto.Status                  = Boolean.Parse(form["status"]);
            productForSellerDto.SellerID                = int.Parse(form["sellerID"]);
            productForSellerDto.SpecificationValuesDtos = JsonConvert.DeserializeObject <List <SpecificationValueDto> >(form.Get("specificationValuesDtos"));

            //Get image file
            var          httpPostedFile = HttpContext.Current.Request.Files["imageFile"];
            BinaryReader reader         = new BinaryReader(httpPostedFile.InputStream);
            var          imageFile      = reader.ReadBytes(httpPostedFile.ContentLength);

            if (!IsAuthorized(productForSellerDto.SellerID))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            this._unitOfWork.Products.Create(productForSellerDto, imageFile);
            this._unitOfWork.Complete();

            var product = this._unitOfWork.Products.SingleOrDefault(p => p.Name == productForSellerDto.Name);

            //Load related objects
            this._unitOfWork.Categories.Load(c => c.ID == product.CategoryID);
            this._unitOfWork.Producers.Load(p => p.ID == product.ProducerID);


            return(new ProductForSellerDto(product));
        }
Пример #4
0
        public ProductForSellerDto Delete(int productID)
        {
            var product = this._unitOfWork.Products.SingleOrDefault(p => p.ID == productID);

            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (!IsAuthorized(product.SellerID))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            var productDto = new ProductForSellerDto(product);

            product.SpecificationValues.Clear();

            this._unitOfWork.AvatarOfProducts.Remove(product.AvatarOfProduct);
            this._unitOfWork.Products.Remove(product); // after deletion, this product no longer has category and producer
            this._unitOfWork.Complete();

            return(productDto);
        }