Example #1
0
        public Task Update(Category 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.Products = entity.Products;
            
            _repository.Add(updatedEntity);
            return Task.FromResult(0);
        }
        public void TestInit()
        {

            #region products and categories Init

            var catFirst = new Category()
            {
                Id = 1,
                Name = "FirstCat"
            };
            var catSecond = new Category()
            {
                Id = 2,
                Name = "SecondCat"
            };

            var productFirst = new Product()
            {
                Id = 1,
                Name = "lalala",
                CategoryId = catFirst.Id,
                Category = catFirst
            };

            var productSecond = new Product()
            {
                Id = 2,
                Name = "abc",
                CategoryId = catSecond.Id,
                Category = catSecond
            };

            catFirst.Products.Add(productFirst);
            catSecond.Products.Add(productSecond);

            #endregion
            

            var products = new List<Product> {productFirst, productSecond};
            var categories = new List<Category>() {catFirst, catSecond};

            _productRepositoryTest = new ProductRepositoryTest(products);
            _categoryRepositoryTest = new CategoryRepositoryTest(categories);

            _controller = new ProductsController(_productRepositoryTest, _categoryRepositoryTest);
        }
Example #3
0
        public async Task<IEnumerable<Product>> GetProductsPageByCategoryAsync(Category category = null, int pageIndex = 1, int pageSize = 10)
        {

            if (category != null)
            {
                return await Task.Run(() => _repository
                    .Where(p => p.CategoryId == category.Id)
                    .OrderBy(p => p.Id)
                    .Skip((pageIndex - 1) * pageSize)
                    .Take(pageSize)
                    .ToList());
            }
            return await Task.Run(() => _repository
                .OrderBy(p => p.Id)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList());

        }
Example #4
0
        public async Task<IHttpActionResult> Put(long id, Category entity)
        {
            var user = HttpContext.Current.User as ClaimsPrincipal;
            bool isAdmin = await _unitOfWork.Users.IsAdmin(user);

            if (!isAdmin)
            {
                return StatusCode(HttpStatusCode.Forbidden); //forbidden
            }

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

            if (id != entity.Id)
            {
                return BadRequest();
            }

            await _repository.Update(entity);
            await _unitOfWork.CompleteAsync();
            return StatusCode(HttpStatusCode.NoContent);
        }
Example #5
0
 public async Task<IEnumerable<Product>> FindProductsInCategoryAsync(string searchText, Category category = null, int pageIndex = 1, int pageSize = 10)
 {
     if (category != null)
     {
         return await Task.Run(() => _repository
             .Where(p => p.CategoryId == category.Id && p.Name.Contains(searchText))
             .OrderBy(p => p.Id)
             .Skip((pageIndex - 1) * pageSize)
             .Take(pageSize)
             .ToList());
     }
     return await Task.Run(() => _repository
         .Where(p => p.Name.Contains(searchText))
         .OrderBy(p => p.Id)
         .Skip((pageIndex - 1) * pageSize)
         .Take(pageSize)
         .ToList());
 }
Example #6
0
        public async Task<int> ProductsByCategoryCountAsync(Category category = null)
        {
            if (category != null)
                await Task.Run(() => _repository.Count(p => p.CategoryId == category.Id));

            return await Task.Run(() => _repository.Count);
        }
Example #7
0
 public void Create(Category category)
 {
     _category = category;
 }
Example #8
0
 public void Remove(Category entity)
 {
     _repository.Remove(entity);
 }
Example #9
0
 public void Add(Category entity)
 {
     _repository.Add(entity);
 }
Example #10
0
        public async Task<IHttpActionResult> Post(Category entity)
        {
            var user = HttpContext.Current.User as ClaimsPrincipal;
            bool isAdmin = await _unitOfWork.Users.IsAdmin(user);

            if (!isAdmin)
            {
                return Unauthorized();
            }

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

            _repository.Add(entity);
            await _unitOfWork.CompleteAsync();
            var viewModel = new CategoryViewModel();
            viewModel.Create(entity);

            return CreatedAtRoute("DefaultApi", new { id = entity.Id }, viewModel);
        }