public IHttpActionResult GetCategory(int id) { Category categoryTmp = db.Categories.Find(id); CategoryDetailsDto category = new CategoryDetailsDto { ID = categoryTmp.ID, Name = categoryTmp.Name, Description = categoryTmp.Description, PurchaseItems = categoryTmp.PurchaseItems.Select(x => new PurchaseItemDetailsDto { ID = x.ID, Amount = x.Amount, Date = x.Date, Comment = x.Comment, CategoryID = x.CategoryID, PurchaseLogID = x.PurchaseLogID }).ToList() }; if (category == null) { return(NotFound()); } return(Ok(category)); }
public IHttpActionResult DeleteCategory(int id) { Category category = db.Categories.Find(id); if (category == null) { return(NotFound()); } db.Categories.Remove(category); db.SaveChanges(); CategoryDetailsDto response = new CategoryDetailsDto { ID = category.ID, Name = category.Name, Description = category.Description, PurchaseItems = category.PurchaseItems.Select(x => new PurchaseItemDetailsDto { ID = x.ID, Amount = x.Amount, Date = x.Date, Comment = x.Comment, CategoryID = x.CategoryID, PurchaseLogID = x.PurchaseLogID }).ToList() }; return(Ok(response)); }
public IHttpActionResult PostCategory(CategoryDetailsDto category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Categories.Add(new Category { ID = category.ID, Name = category.Name, Description = category.Description, PurchaseItems = category.PurchaseItems.Select(x => new PurchaseItem { ID = x.ID, Amount = x.Amount, Date = x.Date, Comment = x.Comment, CategoryID = x.CategoryID, PurchaseLogID = x.PurchaseLogID }).ToList() }); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = category.ID }, category)); }
public IHttpActionResult PutCategory(int id, CategoryDetailsDto category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != category.ID) { return(BadRequest()); } Category currentCategory = new Category { ID = category.ID, Name = category.Name, Description = category.Description, PurchaseItems = category.PurchaseItems.Select(x => new PurchaseItem { ID = x.ID, Amount = x.Amount, Date = x.Date, Comment = x.Comment, CategoryID = x.CategoryID, PurchaseLogID = x.PurchaseLogID }).ToList() }; db.Entry(currentCategory).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IDataResult<CategoryDetailsDto> GetCategoryDetailsById(int categoryId) { var category = _categoryDao.Get(c => c.Id == categoryId); var subcategory = _categoryDao.Get(c => c.ParentCategoryId == category.Id); var categoryDetailsDto = new CategoryDetailsDto { CategoryId = category.Id, CategoryName = category.Name, CategoryDescription = category.Description, }; if (subcategory != null) { categoryDetailsDto.ParentCategoryId = subcategory.Id; categoryDetailsDto.ParentCategoryName = subcategory.Name; } return new SuccessDataResult<CategoryDetailsDto>(categoryDetailsDto); }
public CategoryDetailsDto GetCategoryDetailById(int id, string urlStr) { CategoryDetailsDto targetCategoryItemDetail = _categoriesRepository.GetCategoryDetailItemById(id, urlStr); return(targetCategoryItemDetail); }