public async Task<IHttpActionResult> Get(long id) { var entity = await _repository.GetAsync(id); if (entity == null) { return NotFound(); } var viewModel = new CategoryViewModel(); viewModel.Create(entity); return Ok(viewModel); }
public async Task<IHttpActionResult> GetAll() { var all = await _repository.GetAllAsync(); var allViewModels = new List<CategoryViewModel>(); foreach (var entity in all) { var viewModel = new CategoryViewModel(); viewModel.Create(entity); allViewModels.Add(viewModel); } return Ok(allViewModels); }
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); }
public async Task<IHttpActionResult> Delete(long id) { var user = HttpContext.Current.User as ClaimsPrincipal; bool isAdmin = await _unitOfWork.Users.IsAdmin(user); if (!isAdmin) { return Unauthorized(); } var entity = await _repository.GetAsync(id); if (entity == null) { return NotFound(); } //var productsInCategory = _unitOfWork.Products.FindAsync(c => c.CategoryId == id); _repository.Remove(entity); await _unitOfWork.CompleteAsync(); var viewModel = new CategoryViewModel(); viewModel.Create(entity); return Ok(viewModel); }