private HttpResponseMessage SaveProject(DiplomProjectData project) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } project.LecturerId = WebSecurity.CurrentUserId; DpManagementService.SaveProject(project); return new HttpResponseMessage(HttpStatusCode.OK); }
public void SaveProject(DiplomProjectData projectData) { if (!projectData.LecturerId.HasValue) { throw new ApplicationException("LecturerId cant be empty!"); } if (Context.DiplomProjects.Any(x => x.Theme == projectData.Theme)) { throw new ApplicationException("Тема с таким названием уже есть!"); } AuthorizationHelper.ValidateLecturerAccess(Context, projectData.LecturerId.Value); DiplomProject project; if (projectData.Id.HasValue) { project = Context.DiplomProjects .Include(x => x.DiplomProjectGroups) .Single(x => x.DiplomProjectId == projectData.Id); } else { project = new DiplomProject(); Context.DiplomProjects.Add(project); } var currentGroups = project.DiplomProjectGroups.ToList(); var newGroups = projectData.SelectedGroupsIds.Select(x => new DiplomProjectGroup { GroupId = x, DiplomProjectId = project.DiplomProjectId }).ToList(); var groupsToAdd = newGroups.Except(currentGroups, grp => grp.GroupId); var groupsToDelete = currentGroups.Except(newGroups, grp => grp.GroupId); foreach (var projectGroup in groupsToAdd) { project.DiplomProjectGroups.Add(projectGroup); } foreach (var projectGroup in groupsToDelete) { Context.DiplomProjectGroups.Remove(projectGroup); } project.LecturerId = projectData.LecturerId.Value; project.Theme = projectData.Theme; Context.SaveChanges(); }