public async Task <Response <GetCategoryViewModel> > Create(GetCategoryViewModel categoryViewModel) { var category = _mapper.Map <Category>(categoryViewModel); await _categoryRepository.AddAsync(category); return(new Response <GetCategoryViewModel>(categoryViewModel)); }
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)); }
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)); }
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); }
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)); }
public async Task <IActionResult> Save(GetCategoryViewModel category) { return(Ok(await _categories.Create(category))); }