public async Task <IActionResult> Create([FromBody] CategoryEditModel category) { if (ModelState.IsValid) { CategoryResultModel response = await this.categoryService.UpdateCategory(category.Id, category.Name, category.Description); if (!response.Success) { FailedResponseModel badResponse = new FailedResponseModel() { Errors = response.Errors }; return(BadRequest(badResponse)); } CategorySuccessResponseModel successResponse = new CategorySuccessResponseModel() { Name = response.Name }; return(Ok(successResponse)); } return(BadRequest(new FailedResponseModel { Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage)) })); }
public async Task <CategoryResultModel> UpdateCategory(Guid id, string name, string description) { CategoryResultModel result = new CategoryResultModel(); Category category = await this.dbContext.Categories.FirstOrDefaultAsync(p => p.Id == id); if (category == null) { result.Success = false; result.Errors = new[] { "Category with this Id not exist." }; return(result); } if (category.Name != name && await this.dbContext.Categories.AnyAsync(p => p.Name.ToLower().Trim() == name.ToLower().Trim() && p.Id != id)) { result.Success = false; result.Errors = new[] { "Category with this name already exist." }; return(result); } this.dbContext.Categories.Attach(category); category.Name = name.Trim(); category.Description = description.Trim(); await this.dbContext.SaveChangesAsync(); result.Name = category.Name; result.Success = true; return(result); }
public async Task <CategoryResultModel> CreateCategory(string name, string description) { CategoryResultModel result = new CategoryResultModel(); if (await this.dbContext.Categories.AnyAsync(p => p.Name.ToLower().Trim() == name.ToLower().Trim())) { result.Success = false; result.Errors = new[] { "Category with this name already exist." }; return(result); } Category category = new Category { Id = Guid.NewGuid(), Name = name.Trim(), Description = description.Trim() }; await this.dbContext.Categories.AddAsync(category); await this.dbContext.SaveChangesAsync(); result.Name = category.Name; result.Success = true; return(result); }
public async Task <IActionResult> Delete(Guid id) { CategoryResultModel response = await this.categoryService.DeleteCategory(id); if (!response.Success) { FailedResponseModel badResponse = new FailedResponseModel() { Errors = response.Errors }; return(BadRequest(badResponse)); } CategorySuccessResponseModel successResponse = new CategorySuccessResponseModel() { Name = response.Name }; return(Ok(successResponse)); }
public async Task <CategoryResultModel> DeleteCategory(Guid id) { CategoryResultModel result = new CategoryResultModel(); Category category = await this.dbContext.Categories.FirstOrDefaultAsync(p => p.Id == id); if (category == null) { result.Success = false; result.Errors = new[] { "Category with this Id not exist." }; return(result); } this.dbContext.Categories.Remove(category); await this.dbContext.SaveChangesAsync(); result.Name = category.Name; result.Success = true; return(result); }