Esempio n. 1
0
        // POST api/projectroles
        public IHttpActionResult Post([FromBody] ProjectRoleStandaloneViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    return(BadRequest(ModelState));
                }

                var currentUser = GetCurrentUser();

                ValidateViewModel(viewModel, _repository, currentUser);

                if (ModelState.IsValid)
                {
                    var projectRole = viewModel.GetModel(currentUser);

                    _repository.SaveProjectRole(projectRole);

                    var uriString = Url.Link("DefaultApi", new { controller = "ProjectRoles", id = projectRole.ProjectRoleId });

                    return(Created(uriString, new { ProjectRoleId = projectRole.ProjectRoleId, Name = projectRole.Name }));
                }
                else
                {
                    return(Error(ModelState));
                }
            }
            catch (Exception exc)
            {
                return(InternalServerError(exc));
            }
        }
Esempio n. 2
0
        // PUT api/projectroles/5
        public IHttpActionResult Put(int id, [FromBody] ProjectRoleStandaloneViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    return(BadRequest(ModelState));
                }

                var projectRole = _repository.GetProjectRole(id);

                var currentUser = GetCurrentUser();
                if (projectRole.Project.UserId != currentUser.UserId)
                {
                    return(Forbidden("You can only update project roles for the current user."));
                }

                ValidateViewModel(viewModel, _repository, currentUser);

                if (ModelState.IsValid)
                {
                    viewModel.UpdateModel(projectRole, currentUser);

                    _repository.SaveProjectRole(projectRole);

                    return(NoContent());
                }
                else
                {
                    return(Error(ModelState));
                }
            }
            catch (Exception exc)
            {
                return(InternalServerError(exc));
            }
        }