public ActionResult <CategoryReadDto> GetCategoryById(int id) { var item = _repository.GetCategoryById(id); if (item != null) { return(Ok(_mapper.Map <CategoryReadDto>(item))); } else { return(NotFound()); } }
public ActionResult <ItemReadDto> CreateItem(ItemCreateDto itemCreateDto) { var model = new Item() { IdCategory = _repository.GetCategoryById(itemCreateDto.CategoryId), ItemName = itemCreateDto.ItemName, }; try { _repository.CreateItem(model); _repository.SaveChanges(); _logger.LogInformation("Item created by {HttpUser}", HttpContext.User.Identity.Name); } catch (System.Exception ex) { _logger.LogInformation("Item creation failed. Exception:", ex.Message); } var readDto = _mapper.Map <ItemReadDto>(model); return(CreatedAtRoute(nameof(GetItemById), new { Id = readDto.ItemId }, readDto)); }