contract.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Code generated by fkit v3.11.0. DO NOT EDIT.
  2. //
  3. // Source: schema/employee/contract.fkit
  4. // Regenerate with: go run ./tools/fkitgen ./schema/employee
  5. package employee
  6. import (
  7. "context"
  8. "database/sql"
  9. "time"
  10. )
  11. // ContractRow is the generated row type for table contract.
  12. type ContractRow struct {
  13. ID string
  14. EmployeeID string
  15. Kind string
  16. Currency string
  17. RateCents int64
  18. PeriodRateCents int64
  19. StartsOn time.Time
  20. EndsOn time.Time
  21. CreatedAt time.Time
  22. UpdatedAt time.Time
  23. }
  24. // ContractCreateRequest is the generated create payload for table contract.
  25. type ContractCreateRequest struct {
  26. EmployeeID string `json:"employeeId"`
  27. Kind string `json:"kind"`
  28. Currency string `json:"currency"`
  29. RateCents int64 `json:"rateCents"`
  30. PeriodRateCents int64 `json:"periodRateCents"`
  31. StartsOn time.Time `json:"startsOn"`
  32. }
  33. // CreateContract inserts one contract row.
  34. func CreateContract(ctx context.Context, db *sql.DB, req ContractCreateRequest) (ContractRow, error) {
  35. const q = `INSERT INTO contract (employee_id, kind, currency, rate_cents, period_rate_cents, starts_on)
  36. VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`
  37. return scanContract(db.QueryRowContext(ctx, q, req.EmployeeID, req.Kind, req.Currency,
  38. req.RateCents, req.PeriodRateCents, req.StartsOn))
  39. }
  40. // GetContract selects one contract row by primary key.
  41. func GetContract(ctx context.Context, db *sql.DB, id string) (ContractRow, error) {
  42. const q = `SELECT * FROM contract WHERE id = $1`
  43. return scanContract(db.QueryRowContext(ctx, q, id))
  44. }
  45. // DeleteContract removes one contract row.
  46. func DeleteContract(ctx context.Context, db *sql.DB, id string) error {
  47. const q = `DELETE FROM contract WHERE id = $1`
  48. _, err := db.ExecContext(ctx, q, id)
  49. return err
  50. }
  51. // ListContractsForEmployee selects contract rows for one employee.
  52. func ListContractsForEmployee(ctx context.Context, db *sql.DB, employeeID string) ([]ContractRow, error) {
  53. const q = `SELECT * FROM contract WHERE employee_id = $1 ORDER BY starts_on DESC`
  54. rows, err := db.QueryContext(ctx, q, employeeID)
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer rows.Close()
  59. var out []ContractRow
  60. for rows.Next() {
  61. var r ContractRow
  62. if err := rows.Scan(&r.ID, &r.EmployeeID, &r.Kind, &r.Currency, &r.RateCents,
  63. &r.PeriodRateCents, &r.StartsOn, &r.EndsOn, &r.CreatedAt, &r.UpdatedAt); err != nil {
  64. return nil, err
  65. }
  66. out = append(out, r)
  67. }
  68. return out, rows.Err()
  69. }
  70. func scanContract(row *sql.Row) (ContractRow, error) {
  71. var r ContractRow
  72. err := row.Scan(&r.ID, &r.EmployeeID, &r.Kind, &r.Currency, &r.RateCents,
  73. &r.PeriodRateCents, &r.StartsOn, &r.EndsOn, &r.CreatedAt, &r.UpdatedAt)
  74. return r, err
  75. }