public async Task <IActionResult> PutCategory([FromRoute] int id, [FromBody] Category category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != category.Id) { return(BadRequest()); } _context.Entry(category).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutContentfile([FromRoute] int id, [FromBody] ContentFileDto contentfile) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != contentfile.Id) { return(BadRequest()); } var contentFile = await _context.Contentfile .Include(e => e.Filecategory).ThenInclude(e => e.Category) .Include(e => e.Filesubcategory).ThenInclude(e => e.SubCategory) .FirstOrDefaultAsync(d => d.Id == id); if (contentFile == null) { return(NotFound()); } // For Category var oldArray = contentFile.Filecategory.Select(o => o.Category.Name).ToList(); oldArray.Sort(); string oldKwds = string.Join(";", oldArray); contentfile.Categories.Sort(); string newKwds = string.Join(";", contentfile.Categories); if (newKwds != oldKwds) { // Update data contentFile.Filecategory.Clear(); foreach (var keywd in contentfile.Categories) { var ck = await _context.Category.FirstOrDefaultAsync(k => k.Name.ToLower() == keywd.ToLower()); if (ck == null) { throw new Exception("Category not found: " + keywd); } contentFile.Filecategory.Add(new Filecategory { File = contentFile, Category = ck }); } } // For SubCategory var oldSub = contentFile.Filesubcategory.Select(o => o.SubCategory.Name).ToList(); oldSub.Sort(); string oldSubj = string.Join(";", oldSub); contentfile.Subcategories.Sort(); string newSubj = string.Join(";", contentfile.Subcategories); if (oldSubj != newSubj) { // Update data contentFile.Filesubcategory.Clear(); foreach (var subj in contentfile.Subcategories) { var ck = await _context.Subcategory.FirstOrDefaultAsync(k => k.Name.ToLower() == subj.ToLower()); if (ck == null) { throw new Exception("Subcategory not found: " + subj); } contentFile.Filesubcategory.Add(new FileSubcategory { File = contentFile, SubCategory = ck }); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ContentfileExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }