public async Task <bool> EditSubCategoryAsync(EditSubCategoryDto subCategory) { var subCategoryEdit = await _dbContext.SubCategories.FindAsync(subCategory.Id); subCategoryEdit.Name = subCategory.Name; return(await _dbContext.SaveChangesAsync() > 0); }
public async Task <bool> IsDuplicateSubCategoryAsync(int categoryId, EditSubCategoryDto subCategory) { return(await _dbContext.SubCategories.AnyAsync(c => c.Name.Equals(subCategory.Name, StringComparison.InvariantCultureIgnoreCase) && c.Id != subCategory.Id && c.IsActive && c.CategoryId == categoryId)); }
public async Task <IActionResult> EditSubCategory([FromRoute] int categoryId, [FromBody] EditSubCategoryDto subCategory) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!await _categoryRepository.CategoryExistsAsync(categoryId)) { return(NotFound()); } if (!await _categoryRepository.SubCategoryExistsAsync(categoryId, subCategory.Id)) { ModelState.AddModelError("subCategory", "Sub category not found"); return(BadRequest(ModelState)); } if (await _categoryRepository.IsDuplicateSubCategoryAsync(categoryId, subCategory)) { ModelState.AddModelError("subCategory", "Sub category already exists"); return(BadRequest(ModelState)); } var wasSubCategoryEdited = await _categoryRepository.EditSubCategoryAsync(subCategory); if (wasSubCategoryEdited) { return(Ok()); } return(NoContent()); } catch (Exception e) { _logger.LogCritical($"PUT {Route}/{categoryId}/subcategories - {e.GetType().Name} - {e.Message} - {e.StackTrace}"); return(StatusCode(500, "An error ocurred in server")); } }