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.

100 lines
2.4 KiB

5 years ago
  1. package mysqldriver
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "git.aiterp.net/stufflog/server/internal/generate"
  7. "git.aiterp.net/stufflog/server/internal/xlerrors"
  8. "git.aiterp.net/stufflog/server/models"
  9. sq "github.com/Masterminds/squirrel"
  10. "github.com/jmoiron/sqlx"
  11. )
  12. type activityRepository struct {
  13. db *sqlx.DB
  14. }
  15. func (r *activityRepository) Find(ctx context.Context, id string) (*models.Activity, error) {
  16. activity := models.Activity{}
  17. err := r.db.GetContext(ctx, &activity, "SELECT * FROM activity WHERE activity_id=?", id)
  18. if err != nil {
  19. if err == sql.ErrNoRows {
  20. return nil, xlerrors.NotFound("Activity")
  21. }
  22. return nil, err
  23. }
  24. return &activity, nil
  25. }
  26. func (r *activityRepository) List(ctx context.Context, filter models.ActivityFilter) ([]*models.Activity, error) {
  27. q := sq.Select("*").From("activity")
  28. if len(filter.ActivityIDs) > 0 {
  29. q = q.Where(sq.Eq{"activity_id": filter.ActivityIDs})
  30. }
  31. if len(filter.ProjectIDs) > 0 {
  32. q = q.Where(sq.Eq{"project_id": filter.ProjectIDs})
  33. }
  34. query, args, err := q.ToSql()
  35. if err != nil {
  36. return nil, err
  37. }
  38. results := make([]*models.Activity, 0, 16)
  39. err = r.db.SelectContext(ctx, &results, query, args...)
  40. if err != nil {
  41. if err == sql.ErrNoRows {
  42. return []*models.Activity{}, nil
  43. }
  44. return nil, err
  45. }
  46. return results, nil
  47. }
  48. func (r *activityRepository) Insert(ctx context.Context, activity models.Activity) (*models.Activity, error) {
  49. if activity.ProjectID == "" {
  50. return nil, errors.New("missing project id")
  51. }
  52. activity.ID = generate.ActivityID()
  53. _, err := r.db.NamedExecContext(ctx, `
  54. INSERT INTO activity (
  55. activity_id, project_id, name, countable,
  56. unit_is_time, unit_name, unit_value, base_value
  57. ) VALUES (
  58. :activity_id, :project_id, :name, :countable,
  59. :unit_is_time, :unit_name, :unit_value, :base_value
  60. )
  61. `, activity)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return &activity, nil
  66. }
  67. func (r *activityRepository) Save(ctx context.Context, activity models.Activity) error {
  68. _, err := r.db.NamedExecContext(ctx, `
  69. UPDATE activity SET
  70. name=:name,
  71. countable=:countable,
  72. unit_is_time=:unit_is_time,
  73. unit_name=:unit_name,
  74. unit_value=:unit_value,
  75. base_value=:base_value
  76. WHERE activity_id=:activity_id
  77. `, activity)
  78. return err
  79. }
  80. func (r *activityRepository) Delete(ctx context.Context, activity models.Activity) error {
  81. _, err := r.db.ExecContext(ctx, "DELETE FROM activity WHERE activity_id=? LIMIT 1;", activity.ID)
  82. return err
  83. }