예제 #1
0
        public async Task <Response <GetCategoryViewModel> > Create(GetCategoryViewModel categoryViewModel)
        {
            var category = _mapper.Map <Category>(categoryViewModel);
            await _categoryRepository.AddAsync(category);

            return(new Response <GetCategoryViewModel>(categoryViewModel));
        }
예제 #2
0
        public async Task <Response> GetCategoryByIdAsync(int id)
        {
            var category = await _context.Category.FirstOrDefaultAsync(x => x.Id == id);

            var vm = new GetCategoryViewModel
            {
                Id   = category.Id,
                Name = category.Name,
                Icon = category.Icon
            };

            _context.SaveChanges();
            return(Response.Ok(vm));
        }
예제 #3
0
        public async Task <ActionResult <IEnumerable <GetCategoryViewModel> > > GetCategory()
        {
            //var categories = await _context.Category.ToListAsync();
            var categories = await _context.Category.Include(c => c.Products).ToListAsync();

            ICollection <GetCategoryViewModel> getCategories = new List <GetCategoryViewModel>();

            foreach (var category in categories)
            {
                ICollection <Guid> productIds = null;
                if (category.Products != null)
                {
                    productIds =
                        category.Products.Select(categoryProduct => categoryProduct.ProductId).ToList();
                }

                var getCategory = new GetCategoryViewModel(category);
                getCategories.Add(getCategory);
            }
            return(Ok(getCategories));
        }
예제 #4
0
        public async Task <ActionResult <GetCategoryViewModel> > GetCategory(Guid id)
        {
            var category = await _context.Category
                           .Include(c => c.Products)
                           .FirstOrDefaultAsync(c => c.CategoryId == id);

            if (category == null)
            {
                return(NotFound());
            }

            ICollection <Guid> products = new List <Guid>();

            /*
             * foreach (var VARIABLE in category.Products)
             * {
             *  products.Add(VARIABLE.ProductId);
             * }
             */

            var getCategory = new GetCategoryViewModel(category);

            return(getCategory);
        }
예제 #5
0
        public async Task <ActionResult <Category> > PostCategory(PostCategoryViewModel model)
        {
            var category = new Category()
            {
                CategoryId     = Guid.NewGuid(),
                Name           = model.Name,
                ProductCounter = 0
            };

            _context.Category.Add(category);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }


            var getCategoryViewModel = new GetCategoryViewModel(category);

            return(CreatedAtAction("GetCategory", new { id = category.CategoryId }, getCategoryViewModel));
        }
예제 #6
0
 public async Task <IActionResult> Save(GetCategoryViewModel category)
 {
     return(Ok(await _categories.Create(category)));
 }