예제 #1
0
        public async Task <Project> UpdateProjectData(Guid projectId, ProjectUpdateFieldsModel updatedFields, Guid designerId)
        {
            Project project = await _projectRepository.GetProjectById(projectId);

            if (project == null)
            {
                _logger.LogError("Project with Id {0} not found", projectId.ToString());
                throw new ApiException(404, "Project with Id " + projectId.ToString() + " not found");
            }
            if (project.Designer.Id != designerId)
            {
                _logger.LogError("User with id {0} is unauthorized to modify project with id {1}", designerId.ToString(), project.Id);
                throw new ApiException(401, $"Unauthorized to modify the project with id {project.Id}");
            }

            Project updatedProject = null;
            User    client;

            if (updatedFields.ClientUsername != null)
            {
                client = await _userRepository.GetUserByUsername(updatedFields.ClientUsername);

                if (client == null)
                {
                    _logger.LogError("User with Username {0} not found", updatedFields.ClientUsername);
                    throw new ApiException(404, "User with Username " + updatedFields.ClientUsername + " not found");
                }

                try
                {
                    updatedProject = await _projectRepository.UpdateProjectClient(projectId, client);
                }
                catch (Exception e)
                {
                    throw new ApiException(500, e.Message);
                }
            }
            if (updatedFields.Name != null)
            {
                try
                {
                    updatedProject = await _projectRepository.UpdateProjectData(projectId, updatedFields);
                }
                catch (Exception e)
                {
                    throw new ApiException(500, e.Message);
                }
            }
            if (updatedProject == null)
            {
                throw new ApiException(400, "Client username and project fields are both null");
            }

            return(updatedProject);
        }
예제 #2
0
        public async Task <IActionResult> UpdateProjectData(Guid id, [FromBody] ProjectUpdateFieldsModel updatedFields)
        {
            _logger.LogInformation("Update project data request for project with id {0}\n\n", id.ToString());

            IDictionary <string, object> payload;

            try
            {
                var accessToken = Request.Headers["Bearer"];
                payload = Authorize(accessToken);
            }
            catch (ApiException e)
            {
                return(Unauthorized(new UnauthorizedError(e.Message)));
            }

            try
            {
                Guid    designerId = Guid.Parse(payload["userId"].ToString());
                Project project    = await _projectService.UpdateProjectData(id, updatedFields, designerId);

                return(Ok(project));
            }
            catch (ApiException e)
            {
                if (e.StatusCode == 404)
                {
                    return(NotFound(new NotFoundError(e.Message)));
                }
                if (e.StatusCode == 401)
                {
                    return(Unauthorized(new UnauthorizedError(e.Message)));
                }
                if (e.StatusCode == 400)
                {
                    return(BadRequest(new BadRequestError(e.Message)));
                }

                return(StatusCode(StatusCodes.Status500InternalServerError, new InternalServerError(e.Message)));
            }
        }
예제 #3
0
        public virtual async Task <Project> UpdateProjectData(Guid projectId, ProjectUpdateFieldsModel updatedFields)
        {
            Project projectToBeUpdated = await _context.Projects.FirstOrDefaultAsync(p => p.Id == projectId);

            if (projectToBeUpdated == null)
            {
                _logger.LogError("Project with Id {0} could not be found in the database", projectId);
                throw new Exception($"Project with Id {projectId} could not be found in the database");
            }

            projectToBeUpdated.Name      = updatedFields.Name;
            projectToBeUpdated.UpdatedAt = DateTime.UtcNow;

            var result = await _context.SaveChangesAsync();

            if (result == 0)
            {
                _logger.LogError("Server error! Project with Id {0} could not be updated\n\n", projectToBeUpdated.Id);
                throw new Exception($"Server error! Project {projectToBeUpdated.Id} could not be updated");
            }
            _logger.LogInformation("Project with Id {0} updated into database\n\n", projectToBeUpdated.Id);
            return(projectToBeUpdated);
        }