public async Task <IActionResult> Update(UpdateGroupDTO model) { var group = await _formContext.Groups.FindAsync(model.Id); if (group == null) { return(BadRequest()); } if (model.ParentId.HasValue) { if (!IsParentValid(group, model.ParentId.Value)) { return(BadRequest(ModelState)); } } var groupExists = await _formContext.Groups.AnyAsync(g => g.GroupName == model.GroupName && g.Id != model.Id); if (groupExists) { ModelState.AddModelError("GroupName", "Group with given name already exists"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } group.GroupName = model.GroupName; group.Description = model.Description; group.ParentId = model.ParentId; try { _formContext.Groups.Update(group); await _formContext.SaveChangesAsync(); return(Ok()); } catch (Exception) { return(StatusCode((int)HttpStatusCode.InternalServerError)); } }
public async Task <IActionResult> EditGroup(UpdateGroupDTO model) { try { var groupInDb = await db.Groups.AsTracking().SingleOrDefaultAsync(g => g.GroupId == model.Id); if (groupInDb == null) { return(BadRequest()); } if (model.ParentGroupId.HasValue) // Parent group is passed { if (!IsParentValid(groupInDb, model.ParentGroupId.Value)) { return(BadRequest(ModelState)); } } var groupWithNameExists = await db.Groups.AnyAsync(gr => gr.Name == model.Name && gr.GroupId != model.Id); if (groupWithNameExists) { ModelState.AddModelError("Name", "Group with given name already exists"); return(Conflict(ModelState)); } groupInDb.Name = model.Name; groupInDb.ParentId = model.ParentGroupId; groupInDb.Description = model.Description; await db.SaveChangesAsync(); return(NoContent()); } catch (Exception) { return(StatusCode((int)HttpStatusCode.InternalServerError)); } }