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.

104 lines
2.4 KiB

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