private HttpResponseMessage SaveProject(CourseProjectData project)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            project.LecturerId = WebSecurity.CurrentUserId;
            
            CpManagementService.SaveProject(project);
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
示例#2
0
        public void SaveProject(CourseProjectData projectData)
        {
            if (!projectData.LecturerId.HasValue)
            {
                throw new ApplicationException("LecturerId cant be empty!");
            }

            if (Context.CourseProjects.Any(x => x.Theme == projectData.Theme && x.SubjectId == projectData.SubjectId))
            {
                throw new ApplicationException("Тема с таким названием уже есть!");
            }

            AuthorizationHelper.ValidateLecturerAccess(Context, projectData.LecturerId.Value);

            CourseProject project;
            if (projectData.Id.HasValue)
            {
                project = Context.CourseProjects
                              .Include(x => x.CourseProjectGroups)
                              .Single(x => x.CourseProjectId == projectData.Id);
            }
            else
            {
                project = new CourseProject();
                Context.CourseProjects.Add(project);
            }

            var currentGroups = project.CourseProjectGroups.ToList();
            var newGroups = projectData.SelectedGroupsIds.Select(x => new CourseProjectGroup { GroupId = x, CourseProjectId = project.CourseProjectId }).ToList();

            var groupsToAdd = newGroups.Except(currentGroups, grp => grp.GroupId);
            var groupsToDelete = currentGroups.Except(newGroups, grp => grp.GroupId);

            foreach (var projectGroup in groupsToAdd)
            {
                project.CourseProjectGroups.Add(projectGroup);
            }

            foreach (var projectGroup in groupsToDelete)
            {
                Context.CourseProjectGroups.Remove(projectGroup);
            }

            project.LecturerId = projectData.LecturerId.Value;
            project.Theme = projectData.Theme;
            project.SubjectId = projectData.SubjectId.Value;
            Context.SaveChanges();
        }