Пример #1
0
        public Task Update(Product entity)
        {
            var updatedEntity = _repository.FirstOrDefault(e => e.Id == entity.Id);
            if (updatedEntity == null)
                return Task.FromResult(0);


            _repository.Remove(updatedEntity);
            updatedEntity.Name = entity.Name;
            updatedEntity.Category = entity.Category;
            updatedEntity.CategoryId = entity.CategoryId;
            updatedEntity.Cost = entity.Cost;
            updatedEntity.Description = entity.Description;
            updatedEntity.ImagePath = entity.ImagePath;

            _repository.Add(updatedEntity);
            return Task.FromResult(0);
        }
Пример #2
0
 public void Remove(Product entity)
 {
     _repository.Remove(entity);
 }
Пример #3
0
        public async Task<IHttpActionResult> Post()
        {
            if (!await IsAdmin())
            {
                return StatusCode(HttpStatusCode.Forbidden);
            }

            var image = HttpContext.Current.Request.Files["file"];
            if (image == null || !image.ContentType.Contains("image"))
            {
                return BadRequest();
            }


            var name = HttpContext.Current.Request.Form["newProduct[name]"];
            var description = HttpContext.Current.Request.Form["newProduct[description]"];
            var cat = HttpContext.Current.Request.Form["newProduct[category]"];
            var reqCost = HttpContext.Current.Request.Form["newProduct[cost]"];
            long categoryId;
            long cost;

            if (String.IsNullOrWhiteSpace(name) ||
                long.TryParse(reqCost, out cost) == false ||
                long.TryParse(cat, out categoryId) == false)
            {
                return BadRequest();
            }

            var category = await _categoryRepository.GetAsync(categoryId);
            if (category == null)
            {
                return BadRequest();
            }


            string path = Path.GetRandomFileName() + Path.GetExtension(image.FileName);
            image.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath("~/Img/") + path);

            var product = new Product()
            {
                Name = name,
                Description = description,
                Cost = cost,
                Category = category,
                CategoryId = category.Id,
                ImagePath = "img/" + path

            };

            _productRepository.Add(product);
            await _unitOfWork.CompleteAsync();
            ProductViewModel vm = new ProductViewModel();
            vm.Create(product);

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, vm);

        }
Пример #4
0
 public void Add(Product entity)
 {
     _repository.Add(entity);
 }
Пример #5
0
 public void Create(Product product)
 {
     _product = product;
 }