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.

124 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/Masterminds/squirrel"
  6. "github.com/gissleh/stufflog/internal/slerrors"
  7. "github.com/gissleh/stufflog/models"
  8. "github.com/jmoiron/sqlx"
  9. )
  10. type projectRepository struct {
  11. db *sqlx.DB
  12. }
  13. func (r *projectRepository) Find(ctx context.Context, id string) (*models.Project, error) {
  14. res := models.Project{}
  15. err := r.db.GetContext(ctx, &res, "SELECT * FROM project WHERE project_id=$1", id)
  16. if err != nil {
  17. if err == sql.ErrNoRows {
  18. return nil, slerrors.NotFound("Log")
  19. }
  20. return nil, err
  21. }
  22. return &res, nil
  23. }
  24. func (r *projectRepository) List(ctx context.Context, filter models.ProjectFilter) ([]*models.Project, error) {
  25. sq := squirrel.Select("*").From("project").PlaceholderFormat(squirrel.Dollar)
  26. sq = sq.Where(squirrel.Eq{"user_id": filter.UserID})
  27. if filter.IDs != nil {
  28. sq = sq.Where(squirrel.Eq{"project_id": filter.IDs})
  29. }
  30. if filter.Active != nil {
  31. if filter.IncludeSemiActive {
  32. sq = sq.Where(squirrel.Or{
  33. squirrel.Eq{"active": *filter.Active},
  34. squirrel.Eq{"status_tag": []string{
  35. "to do", "background", "on hold", "progress",
  36. }},
  37. })
  38. } else {
  39. sq = sq.Where(squirrel.Eq{"active": *filter.Active})
  40. }
  41. }
  42. if filter.Expiring {
  43. sq = sq.Where("end_time IS NOT NULL")
  44. }
  45. if filter.Favorite != nil {
  46. sq = sq.Where(squirrel.Eq{"favorite": *filter.Favorite})
  47. }
  48. sq = sq.OrderBy("active", "created_time DESC")
  49. query, args, err := sq.ToSql()
  50. if err != nil {
  51. return nil, err
  52. }
  53. res := make([]*models.Project, 0, 8)
  54. err = r.db.SelectContext(ctx, &res, query, args...)
  55. if err != nil {
  56. if err == sql.ErrNoRows {
  57. return res, nil
  58. }
  59. return nil, err
  60. }
  61. return res, nil
  62. }
  63. func (r *projectRepository) Insert(ctx context.Context, project models.Project) error {
  64. _, err := r.db.NamedExecContext(ctx, `
  65. INSERT INTO project(
  66. project_id, user_id, name, description, icon, active, created_time, end_time, status_tag, favorite
  67. ) VALUES (
  68. :project_id, :user_id, :name, :description, :icon, :active, :created_time, :end_time, :status_tag, :favorite
  69. )
  70. `, &project)
  71. if err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. func (r *projectRepository) Update(ctx context.Context, project models.Project) error {
  77. _, err := r.db.NamedExecContext(ctx, `
  78. UPDATE project SET
  79. name = :name,
  80. description = :description,
  81. icon = :icon,
  82. active = :active,
  83. end_time = :end_time,
  84. status_tag = :status_tag,
  85. favorite = :favorite
  86. WHERE project_id=:project_id
  87. `, &project)
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func (r *projectRepository) Delete(ctx context.Context, project models.Project) error {
  94. _, err := r.db.ExecContext(ctx, `DELETE FROM project WHERE project_id=$1`, project.ID)
  95. if err != nil {
  96. if err == sql.ErrNoRows {
  97. return slerrors.NotFound("Project")
  98. }
  99. return err
  100. }
  101. _, err = r.db.ExecContext(ctx, `DELETE FROM task_link WHERE project_id=$1`, project.ID)
  102. if err != nil && err != sql.ErrNoRows {
  103. return err
  104. }
  105. return nil
  106. }