Beispiel #1
0
        public async Task<IHttpActionResult> Get(long id)
        {
            var entity = await _productRepository.GetAsync(id);
            if (entity == null)
            {
                return NotFound();
            }
            var viewModel = new ProductViewModel();
            viewModel.Create(entity);

            return Ok(viewModel);
        }
Beispiel #2
0
        public async Task<IHttpActionResult> GetAll()
        {
            var all = await _productRepository.GetAllAsync();
            var allViewModels = new List<ProductViewModel>();

            foreach (var entity in all)
            {
                var viewModel = new ProductViewModel();
                viewModel.Create(entity);
                allViewModels.Add(viewModel);
            }
            return Ok(allViewModels);
        }
Beispiel #3
0
        public async Task<IHttpActionResult> Put()
        {
            if (!await IsAdmin())
            {
                return StatusCode(HttpStatusCode.Forbidden);
            }

            var image = HttpContext.Current.Request.Files["file"];
            if (image != null)
            {
                if (!image.ContentType.Contains("image"))
                    return BadRequest();
            }
            var id = HttpContext.Current.Request.Form["newProduct[id]"];
            var name = HttpContext.Current.Request.Form["newProduct[name]"];
            var description = HttpContext.Current.Request.Form["newProduct[description]"];
            var cat = HttpContext.Current.Request.Form["newProduct[categoryId]"];
            var reqCost = HttpContext.Current.Request.Form["newProduct[cost]"];
            long categoryId;
            long prodId;
            int cost;

            if (long.TryParse(id, out prodId) == false ||
                String.IsNullOrWhiteSpace(name) ||
                Int32.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 = "";
            if (image != null)
            {
                path = Path.GetRandomFileName() + Path.GetExtension(image.FileName);
                image.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath("~/Img/") + path);
                path = "img/" + path;
            }
            else
            {
                path = HttpContext.Current.Request.Form["newProduct[imagePath]"];
            }

            var product = await _productRepository.GetAsync(prodId);
            product.Name = name;
            product.Description = description;
            product.Cost = cost;
            product.Category = category;
            product.CategoryId = category.Id;
            product.ImagePath = path;


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

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, vm);
        }
Beispiel #4
0
 public async Task<IEnumerable<ProductViewModel>> GetProducts(long categoryId, string findValue, int page)
 {
     const int productsPerPage = 8;
     var category = await _categoryRepository.GetAsync(categoryId);
     var productsPage = await _productRepository.FindProductsInCategoryAsync(findValue, category, page, productsPerPage);
     var viewModelsList = new List<ProductViewModel>();
     foreach (var product in productsPage)
     {
         var viewModel = new ProductViewModel();
         viewModel.Create(product);
         viewModelsList.Add(viewModel);
     }
     return viewModelsList;
 }
Beispiel #5
0
        public async Task<IHttpActionResult> Delete(long id)
        {
            if (!await IsAdmin())
            {
                return StatusCode(HttpStatusCode.Forbidden);
            }

            var entity = await _productRepository.GetAsync(id);
            if (entity == null)
            {
                return NotFound();
            }

            var ordereditems = await _unitOfWork.Orders.FindAsync(o => o.ProductId == id);
            var cartsItems = await _unitOfWork.Carts.FindAsync(o => o.ProductId == id);
            _unitOfWork.Orders.RemoveRange(ordereditems);
            _unitOfWork.Carts.RemoveRange(cartsItems);

            _productRepository.Remove(entity);
            await _unitOfWork.CompleteAsync();
            var viewModel = new ProductViewModel();
            viewModel.Create(entity);

            return Ok(viewModel);

        }