public IActionResult SetProjectRepository([FromRoute] Guid project_id, [FromBody] ProjectRepositorySetupM model) { try { JwtClaimM jwt_claim = _jwtAuth.GetClaims(Request); _permission.EnsureProjectManager(jwt_claim.UserId, project_id); return(Created("", _projectRepository.SetProjectRepository(jwt_claim.UserId, project_id, model))); } catch (Exception e) { return(GetError(e)); } }
public IList <ProjectRepositoryM> SetProjectRepository(Guid user_id, Guid project_id, ProjectRepositorySetupM model) { try { if (!("Server".Equals(model.Side) || "Client".Equals(model.Side) || "Database".Equals(model.Side))) { throw BadRequest("Side value must be 'Server' or 'Client'!"); } if (!_collaborator.Any(c => c.Account.UserId.Equals(user_id) && c.RepositoryId.Equals(model.RepositoryId) && c.AccountId.Equals(c.OwnerId))) { throw NotFound(model.RepositoryId, "repository id"); } ProjectRepository project_repository = _projectRepository.GetOne(pr => pr.ProjectId.Equals(project_id) && pr.Side.Equals(model.Side)); var project = _projectRepository.Where(pr => pr.RepositoryId.Equals(model.RepositoryId) && pr.Side.Equals(model.Side)) .Select(pr => new { pr.Project.Id, pr.Project.Name }).FirstOrDefault(); if (project_repository == null) { if (project != null) { throw BadRequest("This repository has been set to project " + project.Name + "!"); } else { _projectRepository.Add(new ProjectRepository { ProjectId = project_id, RepositoryId = model.RepositoryId, Side = model.Side }); SaveChanges(); } } else { if (project != null) { if (!project.Id.Equals(project_id)) { throw BadRequest("This repository has been set to project " + project.Name + "!"); } } if (!project_repository.RepositoryId.Equals(model.RepositoryId)) { _projectRepository.Remove(project_repository); _projectRepository.Add(new ProjectRepository { ProjectId = project_id, RepositoryId = model.RepositoryId, Side = model.Side }); SaveChanges(); } } return(GetProjectRepositories(project_id)); } catch (Exception e) { throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while set project repository!", e, DateTime.Now, "Server", "Service_ProjectRepository_SetProjectRepository"); } }