stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. package mysqldriver
  2. import (
  3. "context"
  4. "database/sql"
  5. "git.aiterp.net/stufflog/server/internal/slerrors"
  6. "git.aiterp.net/stufflog/server/models"
  7. sq "github.com/Masterminds/squirrel"
  8. "github.com/jmoiron/sqlx"
  9. )
  10. type projectStatusRepository struct {
  11. db *sqlx.DB
  12. }
  13. func (r *projectStatusRepository) Find(ctx context.Context, projectID string, name string) (*models.ProjectStatus, error) {
  14. projectStatus := models.ProjectStatus{}
  15. err := r.db.GetContext(ctx, &projectStatus, "SELECT * FROM project_status WHERE project_id=? AND name=?", projectID, name)
  16. if err != nil {
  17. if err == sql.ErrNoRows {
  18. return nil, slerrors.NotFound("Project status")
  19. }
  20. return nil, err
  21. }
  22. return &projectStatus, nil
  23. }
  24. func (r *projectStatusRepository) List(ctx context.Context, filter models.ProjectStatusFilter) ([]*models.ProjectStatus, error) {
  25. q := sq.Select("*").From("project_status")
  26. if filter.ProjectID != nil {
  27. q = q.Where(sq.Eq{"project_id": *filter.ProjectID})
  28. }
  29. if filter.MinStage != nil {
  30. q = q.Where(sq.GtOrEq{"stage": *filter.MinStage})
  31. }
  32. if filter.MaxStage != nil {
  33. q = q.Where(sq.LtOrEq{"stage": *filter.MaxStage})
  34. }
  35. q = q.OrderBy("project_id", "stage", "name")
  36. query, args, err := q.ToSql()
  37. if err != nil {
  38. return nil, err
  39. }
  40. statuses := make([]*models.ProjectStatus, 0, 16)
  41. err = r.db.SelectContext(ctx, &statuses, query, args...)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return statuses, nil
  46. }
  47. func (r *projectStatusRepository) Save(ctx context.Context, status models.ProjectStatus) error {
  48. _, err := r.db.NamedExecContext(ctx, `
  49. REPLACE INTO project_status (project_id, name, stage, description)
  50. VALUES (:project_id, :name, :stage, :description)
  51. `, status)
  52. return err
  53. }
  54. func (r *projectStatusRepository) Delete(ctx context.Context, status models.ProjectStatus) error {
  55. _, err := r.db.ExecContext(ctx, "DELETE FROM project_status WHERE project_id=? AND name=?", status.ProjectID, status.Name)
  56. return err
  57. }