|
|
package mysqldriver
import ( "context" "database/sql" "git.aiterp.net/stufflog/server/internal/xlerrors" "git.aiterp.net/stufflog/server/models" sq "github.com/Masterminds/squirrel" "github.com/jmoiron/sqlx" )
type projectStatusRepository struct { db *sqlx.DB }
func (r *projectStatusRepository) Find(ctx context.Context, projectID string, name string) (*models.ProjectStatus, error) { projectStatus := models.ProjectStatus{} err := r.db.GetContext(ctx, &projectStatus, "SELECT * FROM project_status WHERE project_id=? AND name=?", projectID, name) if err != nil { if err == sql.ErrNoRows { return nil, xlerrors.NotFound("Project status") }
return nil, err }
return &projectStatus, nil }
func (r *projectStatusRepository) List(ctx context.Context, filter models.ProjectStatusFilter) ([]*models.ProjectStatus, error) { q := sq.Select("*").From("project_status") if filter.ProjectID != nil { q = q.Where(sq.Eq{"project_id": *filter.ProjectID}) } if filter.MinStage != nil { q = q.Where(sq.GtOrEq{"stage": *filter.MinStage}) } if filter.MaxStage != nil { q = q.Where(sq.LtOrEq{"stage": *filter.MaxStage}) } q = q.OrderBy("project_id", "stage", "name")
query, args, err := q.ToSql() if err != nil { return nil, err }
statuses := make([]*models.ProjectStatus, 0, 16) err = r.db.SelectContext(ctx, &statuses, query, args...) if err != nil { return nil, err }
return statuses, nil }
func (r *projectStatusRepository) Save(ctx context.Context, status models.ProjectStatus) error { _, err := r.db.NamedExecContext(ctx, ` REPLACE INTO project_status (project_id, name, stage, description) VALUES (:project_id, :name, :stage, :description) `, status)
return err }
func (r *projectStatusRepository) Delete(ctx context.Context, status models.ProjectStatus) error { _, err := r.db.ExecContext(ctx, "DELETE FROM project_status WHERE project_id=? AND name=?", status.ProjectID, status.Name)
return err }
|