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.

109 lines
2.6 KiB

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. sq = sq.Where(squirrel.Eq{"active": *filter.Active})
  32. }
  33. if filter.Expiring {
  34. sq = sq.Where("end_time IS NOT NULL")
  35. }
  36. if filter.Favorite != nil {
  37. sq = sq.Where(squirrel.Eq{"favorite": *filter.Favorite})
  38. }
  39. sq = sq.OrderBy("active", "created_time DESC")
  40. query, args, err := sq.ToSql()
  41. if err != nil {
  42. return nil, err
  43. }
  44. res := make([]*models.Project, 0, 8)
  45. err = r.db.SelectContext(ctx, &res, query, args...)
  46. if err != nil {
  47. if err == sql.ErrNoRows {
  48. return res, nil
  49. }
  50. return nil, err
  51. }
  52. return res, nil
  53. }
  54. func (r *projectRepository) Insert(ctx context.Context, project models.Project) error {
  55. _, err := r.db.NamedExecContext(ctx, `
  56. INSERT INTO project(
  57. project_id, user_id, name, description, icon, active, created_time, end_time, status_tag, favorite
  58. ) VALUES (
  59. :project_id, :user_id, :name, :description, :icon, :active, :created_time, :end_time, :status_tag, :favorite
  60. )
  61. `, &project)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func (r *projectRepository) Update(ctx context.Context, project models.Project) error {
  68. _, err := r.db.NamedExecContext(ctx, `
  69. UPDATE project SET
  70. name = :name,
  71. description = :description,
  72. icon = :icon,
  73. active = :active,
  74. end_time = :end_time,
  75. status_tag = :status_tag,
  76. favorite = :favorite
  77. WHERE project_id=:project_id
  78. `, &project)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func (r *projectRepository) Delete(ctx context.Context, project models.Project) error {
  85. _, err := r.db.ExecContext(ctx, `DELETE FROM project WHERE project_id=$1`, project.ID)
  86. if err != nil {
  87. if err == sql.ErrNoRows {
  88. return slerrors.NotFound("Project")
  89. }
  90. return err
  91. }
  92. return nil
  93. }