Exemplo n.º 1
0
        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));
        }
        private IActionResult SaveProject(DiplomProjectData project)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            project.LecturerId = /*todo #auth WebSecurity.CurrentUserId*/ 2;
            DpManagementService.SaveProject(project);
            return(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();
        }
Exemplo n.º 4
0
 public HttpResponseMessage Put([FromBody] DiplomProjectData project)
 {
     return(SaveProject(project));
 }
 public IActionResult Put([FromBody] DiplomProjectData project)
 {
     return(SaveProject(project));
 }