improovements
This commit is contained in:
parent
fa7f791873
commit
4205922617
6 changed files with 65 additions and 29 deletions
|
|
@ -9,7 +9,7 @@ import (
|
|||
)
|
||||
|
||||
func (a *App) listProjects(ctx context.Context, userID string) ([]Project, error) {
|
||||
rows, err := a.db.QueryContext(ctx, `SELECT p.id, p.user_id, p.name, p.slug, p.description, p.repo_url, p.repo_private, p.service_name, p.route_host, p.workspace_id, w.name, w.unix_user, p.deploy_token, p.provision_state, p.provision_error, p.db_name, p.db_user, p.db_password, p.created_at
|
||||
rows, err := a.db.QueryContext(ctx, `SELECT p.id, p.user_id, p.name, p.slug, p.description, p.repo_url, p.repo_private, p.service_name, p.route_host, p.workspace_id, w.name, w.unix_user, p.deploy_token, p.provision_state, p.provision_error, p.last_deployed_at, p.last_deployed_commit, p.db_name, p.db_user, p.db_password, p.created_at
|
||||
FROM projects p
|
||||
JOIN workspaces w ON w.id = p.workspace_id
|
||||
WHERE p.user_id=$1 ORDER BY p.created_at DESC`, userID)
|
||||
|
|
@ -20,9 +20,14 @@ func (a *App) listProjects(ctx context.Context, userID string) ([]Project, error
|
|||
var out []Project
|
||||
for rows.Next() {
|
||||
var p Project
|
||||
if err := rows.Scan(&p.ID, &p.UserID, &p.Name, &p.Slug, &p.Description, &p.RepoURL, &p.RepoPrivate, &p.ServiceName, &p.RouteHost, &p.WorkspaceID, &p.Workspace, &p.UnixUser, &p.DeployToken, &p.ProvisionState, &p.ProvisionError, &p.DBName, &p.DBUser, &p.DBPassword, &p.CreatedAt); err != nil {
|
||||
var deployedAt sql.NullTime
|
||||
if err := rows.Scan(&p.ID, &p.UserID, &p.Name, &p.Slug, &p.Description, &p.RepoURL, &p.RepoPrivate, &p.ServiceName, &p.RouteHost, &p.WorkspaceID, &p.Workspace, &p.UnixUser, &p.DeployToken, &p.ProvisionState, &p.ProvisionError, &deployedAt, &p.LastDeployedCommit, &p.DBName, &p.DBUser, &p.DBPassword, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if deployedAt.Valid {
|
||||
t := deployedAt.Time.UTC()
|
||||
p.LastDeployedAt = &t
|
||||
}
|
||||
p.WebhookURL = a.deployWebhookURL(p.ID, p.DeployToken)
|
||||
p.AppURL = a.appURL(p.RouteHost)
|
||||
out = append(out, p)
|
||||
|
|
@ -55,15 +60,20 @@ func (a *App) createProject(ctx context.Context, user User, workspaceID int64, n
|
|||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
err = tx.QueryRowContext(ctx, `INSERT INTO projects (user_id, workspace_id, name, slug, description, repo_url, repo_private, service_name, route_host, deploy_token, provision_state, provision_error) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,'pending','') RETURNING id, user_id, name, slug, description, repo_url, repo_private, service_name, route_host, workspace_id, deploy_token, provision_state, provision_error, db_name, db_user, db_password, created_at`,
|
||||
var createdDeployedAt sql.NullTime
|
||||
err = tx.QueryRowContext(ctx, `INSERT INTO projects (user_id, workspace_id, name, slug, description, repo_url, repo_private, service_name, route_host, deploy_token, provision_state, provision_error) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,'pending','') RETURNING id, user_id, name, slug, description, repo_url, repo_private, service_name, route_host, workspace_id, deploy_token, provision_state, provision_error, last_deployed_at, last_deployed_commit, db_name, db_user, db_password, created_at`,
|
||||
user.Username, workspaceID, name, slug, description, repoURL, private, serviceName, routeHost, deployToken,
|
||||
).Scan(&p.ID, &p.UserID, &p.Name, &p.Slug, &p.Description, &p.RepoURL, &p.RepoPrivate, &p.ServiceName, &p.RouteHost, &p.WorkspaceID, &p.DeployToken, &p.ProvisionState, &p.ProvisionError, &p.DBName, &p.DBUser, &p.DBPassword, &p.CreatedAt)
|
||||
).Scan(&p.ID, &p.UserID, &p.Name, &p.Slug, &p.Description, &p.RepoURL, &p.RepoPrivate, &p.ServiceName, &p.RouteHost, &p.WorkspaceID, &p.DeployToken, &p.ProvisionState, &p.ProvisionError, &createdDeployedAt, &p.LastDeployedCommit, &p.DBName, &p.DBUser, &p.DBPassword, &p.CreatedAt)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate key") {
|
||||
return Project{}, errors.New("project with this name already exists")
|
||||
}
|
||||
return Project{}, err
|
||||
}
|
||||
if createdDeployedAt.Valid {
|
||||
t := createdDeployedAt.Time.UTC()
|
||||
p.LastDeployedAt = &t
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Project{}, err
|
||||
}
|
||||
|
|
@ -144,15 +154,20 @@ func (a *App) provisionProject(ctx context.Context, p *Project) error {
|
|||
|
||||
func (a *App) getProject(ctx context.Context, userID string, projectID int64) (Project, error) {
|
||||
var p Project
|
||||
err := a.db.QueryRowContext(ctx, `SELECT p.id, p.user_id, p.name, p.slug, p.description, p.repo_url, p.repo_private, p.service_name, p.route_host, p.workspace_id, w.name, w.unix_user, p.deploy_token, p.provision_state, p.provision_error, p.db_name, p.db_user, p.db_password, p.created_at
|
||||
var deployedAt sql.NullTime
|
||||
err := a.db.QueryRowContext(ctx, `SELECT p.id, p.user_id, p.name, p.slug, p.description, p.repo_url, p.repo_private, p.service_name, p.route_host, p.workspace_id, w.name, w.unix_user, p.deploy_token, p.provision_state, p.provision_error, p.last_deployed_at, p.last_deployed_commit, p.db_name, p.db_user, p.db_password, p.created_at
|
||||
FROM projects p JOIN workspaces w ON w.id=p.workspace_id WHERE p.user_id=$1 AND p.id=$2`, userID, projectID).
|
||||
Scan(&p.ID, &p.UserID, &p.Name, &p.Slug, &p.Description, &p.RepoURL, &p.RepoPrivate, &p.ServiceName, &p.RouteHost, &p.WorkspaceID, &p.Workspace, &p.UnixUser, &p.DeployToken, &p.ProvisionState, &p.ProvisionError, &p.DBName, &p.DBUser, &p.DBPassword, &p.CreatedAt)
|
||||
Scan(&p.ID, &p.UserID, &p.Name, &p.Slug, &p.Description, &p.RepoURL, &p.RepoPrivate, &p.ServiceName, &p.RouteHost, &p.WorkspaceID, &p.Workspace, &p.UnixUser, &p.DeployToken, &p.ProvisionState, &p.ProvisionError, &deployedAt, &p.LastDeployedCommit, &p.DBName, &p.DBUser, &p.DBPassword, &p.CreatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Project{}, errors.New("project not found")
|
||||
}
|
||||
return Project{}, err
|
||||
}
|
||||
if deployedAt.Valid {
|
||||
t := deployedAt.Time.UTC()
|
||||
p.LastDeployedAt = &t
|
||||
}
|
||||
p.WebhookURL = a.deployWebhookURL(p.ID, p.DeployToken)
|
||||
p.AppURL = a.appURL(p.RouteHost)
|
||||
return p, nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue