diff --git a/api/v1/openapi.yaml b/api/v1/openapi.yaml index 30479a0..1aa4e3d 100644 --- a/api/v1/openapi.yaml +++ b/api/v1/openapi.yaml @@ -546,8 +546,8 @@ components: - id TrainingID: - type: string - format: uuid + type: integer + minimum: 1 x-go-type: training.TrainingID x-go-type-import: path: gitlab.mareshq.com/hq/yggdrasil/pkg/training @@ -652,8 +652,8 @@ components: - id TrainingDateID: - type: string - format: uuid + type: integer + minimum: 1 x-go-type: training.TrainingDateID x-go-type-import: path: gitlab.mareshq.com/hq/yggdrasil/pkg/training @@ -705,8 +705,8 @@ components: - attended TrainingDateAttendeeID: - type: string - format: uuid + type: integer + minimum: 1 x-go-type: training.TrainingDateAttendeeID x-go-type-import: path: gitlab.mareshq.com/hq/yggdrasil/pkg/training diff --git a/cmd/seed/main.go b/cmd/seed/main.go index 74cb272..69790b1 100644 --- a/cmd/seed/main.go +++ b/cmd/seed/main.go @@ -5,7 +5,6 @@ import ( pgxDeciaml "github.com/jackc/pgx-shopspring-decimal" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" - pgxUUID "github.com/vgarvardt/pgx-google-uuid/v5" "gitlab.mareshq.com/hq/yggdrasil/internal/bootstrap" "gitlab.mareshq.com/hq/yggdrasil/internal/faker" "gitlab.mareshq.com/hq/yggdrasil/pkg/training" @@ -44,7 +43,6 @@ func main() { } pgxConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { - pgxUUID.Register(conn.TypeMap()) pgxDeciaml.Register(conn.TypeMap()) return nil } diff --git a/go.mod b/go.mod index 85670d2..6ce8c7e 100644 --- a/go.mod +++ b/go.mod @@ -7,14 +7,12 @@ require ( github.com/gofiber/contrib/fiberzap/v2 v2.1.3 github.com/gofiber/contrib/swagger v1.1.2 github.com/gofiber/fiber/v2 v2.52.4 - github.com/google/uuid v1.6.0 github.com/jackc/pgx-shopspring-decimal v0.0.0-20220624020537-1d36b5a1853e github.com/jackc/pgx/v5 v5.6.0 github.com/oapi-codegen/fiber-middleware v1.0.2 github.com/oapi-codegen/runtime v1.1.1 github.com/shopspring/decimal v1.4.0 github.com/stretchr/testify v1.9.0 - github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0 go.uber.org/zap v1.27.0 ) @@ -34,6 +32,7 @@ require ( github.com/go-openapi/swag v0.23.0 // indirect github.com/go-openapi/validate v0.24.0 // indirect github.com/google/go-cmp v0.6.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/gorilla/mux v1.8.1 // indirect github.com/invopop/yaml v0.3.1 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect diff --git a/go.sum b/go.sum index 927e17b..0d78b89 100644 --- a/go.sum +++ b/go.sum @@ -111,8 +111,6 @@ github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8 github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= -github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0 h1:EhPtK0mgrgaTMXpegE69hvoSOVC1Ahk8+QJ9B8b+OdU= -github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0/go.mod h1:5LtFrNEkgzxHvXPO9eOvcXsSn9/KeKYgx9kjeI2oXQI= go.mongodb.org/mongo-driver v1.15.1 h1:l+RvoUOoMXFmADTLfYDm7On9dRm7p4T80/lEQM+r7HU= go.mongodb.org/mongo-driver v1.15.1/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index 0d3332d..4ca44fc 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -5,7 +5,6 @@ import ( pgxDeciaml "github.com/jackc/pgx-shopspring-decimal" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" - pgxUUID "github.com/vgarvardt/pgx-google-uuid/v5" "gitlab.mareshq.com/hq/yggdrasil/internal/faker" "gitlab.mareshq.com/hq/yggdrasil/internal/server" "gitlab.mareshq.com/hq/yggdrasil/pkg/training" @@ -48,7 +47,6 @@ func Bootstrap(logger *zap.Logger) *server.Server { } pgxConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { - pgxUUID.Register(conn.TypeMap()) pgxDeciaml.Register(conn.TypeMap()) return nil } diff --git a/internal/server/api.gen.go b/internal/server/api.gen.go index bf6f52f..14b2924 100644 --- a/internal/server/api.gen.go +++ b/internal/server/api.gen.go @@ -2356,24 +2356,24 @@ var swaggerSpec = []string{ "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", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 01502a7..7162ccc 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -3,8 +3,8 @@ package server import ( "bytes" "encoding/json" + "fmt" "github.com/gofiber/fiber/v2" - "github.com/google/uuid" "github.com/oapi-codegen/runtime/types" "github.com/shopspring/decimal" "github.com/stretchr/testify/assert" @@ -128,7 +128,7 @@ func TestServer(t *testing.T) { }) t.Run("Training not found", func(t *testing.T) { - rr, _ := doGet(t, app, "/v1/trainings/97b0e954-14f3-4908-98fa-271f505056d3") + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d", 9999999)) assert.Equal(t, http.StatusNotFound, rr.StatusCode) var trainingError NotFoundError @@ -190,7 +190,7 @@ func TestServer(t *testing.T) { }, } - rr, _ := doPut(t, app, "/v1/trainings/"+tr.ID.String(), updTr) + rr, _ := doPut(t, app, fmt.Sprintf("/v1/trainings/%d", tr.ID), updTr) assert.Equal(t, http.StatusOK, rr.StatusCode) var trr Training @@ -215,7 +215,7 @@ func TestServer(t *testing.T) { _ = handlers.trainingRepository.Create(tr) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d", tr.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var trr Training @@ -223,10 +223,10 @@ func TestServer(t *testing.T) { assert.NoError(t, err, "error unmarshalling response") assert.Equal(t, tr.ID, trr.Id) - drr, _ := doDelete(t, app, "/v1/trainings/"+tr.ID.String()) + drr, _ := doDelete(t, app, fmt.Sprintf("/v1/trainings/%d", tr.ID)) assert.Equal(t, http.StatusNoContent, drr.StatusCode) - rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String()) + rr, _ = doGet(t, app, fmt.Sprintf("/v1/trainings/%d", tr.ID)) assert.Equal(t, http.StatusNotFound, rr.StatusCode) var trainingError NotFoundError @@ -271,7 +271,7 @@ func TestServer(t *testing.T) { }, } - rr, err := doPost(t, app, "/v1/trainings/"+tr.ID.String()+"/dates", newTrainingDate) + rr, err := doPost(t, app, fmt.Sprintf("/v1/trainings/%d/dates", tr.ID), newTrainingDate) assert.Equal(t, http.StatusCreated, rr.StatusCode) @@ -304,7 +304,7 @@ func TestServer(t *testing.T) { _ = handlers.trainingRepository.Create(tr) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/97b0e954-14f3-4908-98fa-271f505056d3") + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d", tr.ID, 999999)) assert.Equal(t, http.StatusNotFound, rr.StatusCode) var trainingError NotFoundError @@ -331,17 +331,15 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } trainingCount := 5 @@ -350,7 +348,7 @@ func TestServer(t *testing.T) { _ = handlers.trainingDateRepository.Create(tr.ID, td) } - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates") + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates", tr.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var trainingDates []TrainingDate @@ -377,17 +375,15 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) @@ -406,7 +402,7 @@ func TestServer(t *testing.T) { }, } - rr, _ := doPut(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String(), updTd) + rr, _ := doPut(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d", tr.ID, td.ID), updTd) assert.Equal(t, http.StatusOK, rr.StatusCode) var tdrr TrainingDate @@ -440,22 +436,20 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d", tr.ID, td.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var tdrr TrainingDate @@ -463,10 +457,10 @@ func TestServer(t *testing.T) { assert.NoError(t, err, "error unmarshalling response") assert.Equal(t, td.ID, tdrr.Id) - drr, _ := doDelete(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()) + drr, _ := doDelete(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d", tr.ID, td.ID)) assert.Equal(t, http.StatusNoContent, drr.StatusCode) - rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()) + rr, _ = doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d", tr.ID, td.ID)) assert.Equal(t, http.StatusNotFound, rr.StatusCode) var trainingError NotFoundError @@ -474,7 +468,7 @@ func TestServer(t *testing.T) { assert.NoError(t, err, "error getting response", err) assert.Equal(t, http.StatusNotFound, trainingError.Status) - lrr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates") + lrr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates", tr.ID)) assert.Equal(t, http.StatusOK, lrr.StatusCode) var trainingDates []TrainingDate @@ -503,22 +497,20 @@ func TestServer(t *testing.T) { for i := -4; i <= 5; i++ { date := now.AddDate(0, 0, i) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) } - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/upcoming-dates") + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/upcoming-dates", tr.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var trainingDatesReturned []TrainingDate @@ -577,47 +569,41 @@ func TestServer(t *testing.T) { for i := -4; i <= upcomingTrainingCount; i++ { date := now.AddDate(0, 0, i) td1 := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr1.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr1.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr1.ID, td1) td2 := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr2.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr2.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr2.ID, td2) td3 := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr3.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr3.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr3.ID, td3) } @@ -656,21 +642,19 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+uuid.New().String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, 99999999)) assert.Equal(t, http.StatusNotFound, rr.StatusCode) var notFound NotFoundError @@ -698,17 +682,15 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) @@ -721,7 +703,7 @@ func TestServer(t *testing.T) { IsStudent: new(bool), } - rr, _ := doPost(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees", newTrainingDateAttendee) + rr, _ := doPost(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees", tr.ID, td.ID), newTrainingDateAttendee) assert.Equal(t, http.StatusCreated, rr.StatusCode) var attendeeResponse TrainingDateAttendee @@ -733,8 +715,8 @@ func TestServer(t *testing.T) { assert.Equal(t, newTrainingDateAttendee.Position, attendeeResponse.Position) assert.Equal(t, newTrainingDateAttendee.Phone, attendeeResponse.Phone) assert.Equal(t, newTrainingDateAttendee.IsStudent, attendeeResponse.IsStudent) - assert.Equal(t, td.Price.Amount.String(), attendeeResponse.Bill.Amount) - assert.Equal(t, td.Price.Currency, attendeeResponse.Bill.Currency) + assert.Equal(t, td.PriceAmount.String(), attendeeResponse.Bill.Amount) + assert.Equal(t, td.PriceCurrency, attendeeResponse.Bill.Currency) assert.Equal(t, false, attendeeResponse.HasPaid) assert.Equal(t, false, attendeeResponse.HasAttended) }) @@ -758,17 +740,15 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) @@ -776,24 +756,22 @@ func TestServer(t *testing.T) { attendees := make([]training.TrainingDateAttendee, attendeeCount) for i := range attendeeCount { nAtd := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: false, - HasAttended: false, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: false, + HasAttended: false, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, nAtd) attendees[i] = *nAtd } - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees") + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees", tr.ID, td.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var attendeesResponse ListTrainingDateAttendeesResponse @@ -809,8 +787,8 @@ func TestServer(t *testing.T) { assert.Equal(t, attendees[i].Position, attendee.Position) assert.Equal(t, attendees[i].IsStudent, *attendee.IsStudent) assert.Equal(t, attendees[i].Phone, attendee.Phone) - assert.Equal(t, attendees[i].Bill.Amount.String(), attendee.Bill.Amount) - assert.Equal(t, attendees[i].Bill.Currency, attendee.Bill.Currency) + assert.Equal(t, attendees[i].BillAmount.String(), attendee.Bill.Amount) + assert.Equal(t, attendees[i].BillCurrency, attendee.Bill.Currency) assert.Equal(t, attendees[i].HasPaid, attendee.HasPaid) assert.Equal(t, attendees[i].HasAttended, attendee.HasAttended) } @@ -835,38 +813,34 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) attendee := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: false, - HasAttended: false, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: false, + HasAttended: false, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, attendee) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) var resultTrainingDateAttendee TrainingDateAttendee err := json.NewDecoder(rr.Body).Decode(&resultTrainingDateAttendee) @@ -877,8 +851,8 @@ func TestServer(t *testing.T) { assert.Equal(t, attendee.Position, resultTrainingDateAttendee.Position) assert.Equal(t, attendee.IsStudent, *resultTrainingDateAttendee.IsStudent) assert.Equal(t, attendee.Phone, resultTrainingDateAttendee.Phone) - assert.Equal(t, attendee.Bill.Amount.String(), resultTrainingDateAttendee.Bill.Amount) - assert.Equal(t, attendee.Bill.Currency, resultTrainingDateAttendee.Bill.Currency) + assert.Equal(t, attendee.BillAmount.String(), resultTrainingDateAttendee.Bill.Amount) + assert.Equal(t, attendee.BillCurrency, resultTrainingDateAttendee.Bill.Currency) assert.Equal(t, attendee.HasPaid, resultTrainingDateAttendee.HasPaid) assert.Equal(t, attendee.HasAttended, resultTrainingDateAttendee.HasAttended) }) @@ -902,33 +876,29 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) attendee := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: false, - HasAttended: false, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: false, + HasAttended: false, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, attendee) @@ -942,7 +912,7 @@ func TestServer(t *testing.T) { IsStudent: new(bool), } - rr, _ := doPut(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String(), newTrainingDateAttendee) + rr, _ := doPut(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID), newTrainingDateAttendee) assert.Equal(t, http.StatusOK, rr.StatusCode) var attendeeResponse UpdateTrainingDateAttendeeResponse @@ -954,8 +924,8 @@ func TestServer(t *testing.T) { assert.Equal(t, newTrainingDateAttendee.Position, attendeeResponse.Position) assert.Equal(t, newTrainingDateAttendee.Phone, attendeeResponse.Phone) assert.Equal(t, newTrainingDateAttendee.IsStudent, attendeeResponse.IsStudent) - assert.Equal(t, td.Price.Amount.String(), attendeeResponse.Bill.Amount) - assert.Equal(t, td.Price.Currency, attendeeResponse.Bill.Currency) + assert.Equal(t, td.PriceAmount.String(), attendeeResponse.Bill.Amount) + assert.Equal(t, td.PriceCurrency, attendeeResponse.Bill.Currency) assert.Equal(t, attendee.HasPaid, attendeeResponse.HasPaid) assert.Equal(t, attendee.HasAttended, attendeeResponse.HasAttended) }) @@ -979,44 +949,40 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) attendee := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: false, - HasAttended: false, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: false, + HasAttended: false, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, attendee) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) - rr, _ = doDelete(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ = doDelete(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusNoContent, rr.StatusCode) - rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ = doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusNotFound, rr.StatusCode) }) @@ -1039,17 +1005,15 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) @@ -1058,23 +1022,21 @@ func TestServer(t *testing.T) { } attendee := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: !paid.Paid, - HasAttended: false, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: !paid.Paid, + HasAttended: false, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, attendee) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var attendeeResponse TrainingDateAttendee @@ -1082,14 +1044,14 @@ func TestServer(t *testing.T) { assert.NoError(t, err, "error unmarshalling response") assert.Equal(t, !paid.Paid, attendeeResponse.HasPaid) - rr, _ = doPut(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()+"/paid", paid) + rr, _ = doPut(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d/paid", tr.ID, td.ID, attendee.ID), paid) assert.Equal(t, http.StatusOK, rr.StatusCode) err = json.NewDecoder(rr.Body).Decode(&attendeeResponse) assert.NoError(t, err, "error unmarshalling response") assert.Equal(t, paid.Paid, attendeeResponse.HasPaid) - rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ = doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) err = json.NewDecoder(rr.Body).Decode(&attendeeResponse) @@ -1116,17 +1078,15 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) @@ -1135,23 +1095,21 @@ func TestServer(t *testing.T) { } attendee := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: false, - HasAttended: !attended.Attended, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: false, + HasAttended: !attended.Attended, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, attendee) - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var attendeeResponse TrainingDateAttendee @@ -1159,14 +1117,14 @@ func TestServer(t *testing.T) { assert.NoError(t, err, "error unmarshalling response") assert.Equal(t, !attended.Attended, attendeeResponse.HasAttended) - rr, _ = doPut(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()+"/attended", attended) + rr, _ = doPut(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d/attended", tr.ID, td.ID, attendee.ID), attended) assert.Equal(t, http.StatusOK, rr.StatusCode) err = json.NewDecoder(rr.Body).Decode(&attendeeResponse) assert.NoError(t, err, "error unmarshalling response") assert.Equal(t, attended.Attended, attendeeResponse.HasAttended) - rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/"+attendee.ID.String()) + rr, _ = doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/%d", tr.ID, td.ID, attendee.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) err = json.NewDecoder(rr.Body).Decode(&attendeeResponse) @@ -1193,40 +1151,36 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) td := &training.TrainingDate{ - Address: "Test Address 123, NYC", - Capacity: 12, - Date: date, - Days: tr.Days, - IsOnline: false, - Location: "NYC", - StartTime: date, - Price: money.Money{ - Amount: decimal.NewFromInt(200), - Currency: "EUR", - }, + Address: "Test Address 123, NYC", + Capacity: 12, + Date: date, + Days: tr.Days, + IsOnline: false, + Location: "NYC", + StartTime: date, + PriceAmount: decimal.NewFromInt(200), + PriceCurrency: "EUR", } _ = handlers.trainingDateRepository.Create(tr.ID, td) attendeeCount := 9 for range attendeeCount { nAtd := &training.TrainingDateAttendee{ - Name: "John Doe", - Email: "john.doe@example.com", - Company: "Acme Inc.", - Position: "Software Engineer", - Phone: "+420 123 456 789", - IsStudent: false, - Bill: money.Money{ - Amount: td.Price.Amount, - Currency: td.Price.Currency, - }, - HasPaid: false, - HasAttended: false, + Name: "John Doe", + Email: "john.doe@example.com", + Company: "Acme Inc.", + Position: "Software Engineer", + Phone: "+420 123 456 789", + IsStudent: false, + BillAmount: td.PriceAmount, + BillCurrency: td.PriceCurrency, + HasPaid: false, + HasAttended: false, } _ = handlers.trainingDateAttendeeRepository.Create(td.ID, nAtd) } - rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String()+"/dates/"+td.ID.String()+"/attendees/count") + rr, _ := doGet(t, app, fmt.Sprintf("/v1/trainings/%d/dates/%d/attendees/count", tr.ID, td.ID)) assert.Equal(t, http.StatusOK, rr.StatusCode) var attendeesResponse GetTrainingDateAttendeeCountResponse diff --git a/migrations/001_trainings.up.sql b/migrations/001_trainings.up.sql index acc9f85..0a2d5c9 100644 --- a/migrations/001_trainings.up.sql +++ b/migrations/001_trainings.up.sql @@ -3,14 +3,14 @@ BEGIN; CREATE SCHEMA IF NOT EXISTS training; CREATE TABLE IF NOT EXISTS training.trainings ( - id UUID PRIMARY KEY, + id SERIAL PRIMARY KEY, name varchar(255) NOT NULL, description text NOT NULL, days smallint NOT NULL ); CREATE TABLE IF NOT EXISTS training.prices ( - training_id UUID REFERENCES training.trainings(id), + training_id SERIAL REFERENCES training.trainings(id), amount NUMERIC(10,4) NOT NULL, currency VARCHAR(3) NOT NULL, CONSTRAINT positive_amount CHECK (amount >= 0), @@ -21,8 +21,8 @@ CREATE TABLE IF NOT EXISTS training.prices ( ); CREATE TABLE IF NOT EXISTS training.dates ( - ID UUID PRIMARY KEY, - training_id UUID REFERENCES training.trainings(id), + ID SERIAL PRIMARY KEY, + training_id SERIAL REFERENCES training.trainings(id), date DATE NOT NULL, start_time TIME NOT NULL, days SMALLINT NOT NULL, @@ -37,8 +37,8 @@ CREATE TABLE IF NOT EXISTS training.dates ( ); CREATE TABLE IF NOT EXISTS training.date_attendees ( - id UUID PRIMARY KEY, - date_id UUID REFERENCES training.dates(id), + id SERIAL PRIMARY KEY, + date_id SERIAL REFERENCES training.dates(id), name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, phone VARCHAR(20) NOT NULL, diff --git a/pkg/training/inmemory_repository.go b/pkg/training/inmemory_repository.go index 6b697ca..7b3c673 100644 --- a/pkg/training/inmemory_repository.go +++ b/pkg/training/inmemory_repository.go @@ -8,11 +8,13 @@ import ( type InMemoryTrainingRepository struct { trainings map[TrainingID]Training lock sync.RWMutex + ai int } func NewInMemoryTrainingRepository() *InMemoryTrainingRepository { return &InMemoryTrainingRepository{ trainings: make(map[TrainingID]Training), + ai: 1, } } @@ -20,7 +22,8 @@ func (r *InMemoryTrainingRepository) Create(training *Training) error { r.lock.Lock() defer r.lock.Unlock() - training.ID = NewTrainingID() + training.ID = r.ai + r.ai++ r.trainings[training.ID] = *training return nil } @@ -73,12 +76,14 @@ type InMemoryTrainingDateRepository struct { trainingToDates map[TrainingID][]TrainingDateID lock sync.RWMutex + ai int } func NewInMemoryTrainingDateRepository() *InMemoryTrainingDateRepository { return &InMemoryTrainingDateRepository{ trainingDates: make(map[TrainingDateID]TrainingDate), trainingToDates: make(map[TrainingID][]TrainingDateID), + ai: 1, } } @@ -86,7 +91,8 @@ func (r *InMemoryTrainingDateRepository) Create(trainingID TrainingID, trainingD r.lock.Lock() defer r.lock.Unlock() - trainingDate.ID = NewTrainingDateID() + trainingDate.ID = r.ai + r.ai++ trainingDate.trainingID = trainingID r.trainingDates[trainingDate.ID] = *trainingDate @@ -181,12 +187,14 @@ type InMemoryTrainingDateAttendeeRepository struct { dateToAttendees map[TrainingDateID][]TrainingDateAttendeeID lock sync.RWMutex + ai int } func NewInMemoryTrainingDateAttendeeRepository() *InMemoryTrainingDateAttendeeRepository { return &InMemoryTrainingDateAttendeeRepository{ attendees: make(map[TrainingDateAttendeeID]TrainingDateAttendee), dateToAttendees: make(map[TrainingDateID][]TrainingDateAttendeeID), + ai: 1, } } @@ -194,7 +202,8 @@ func (r *InMemoryTrainingDateAttendeeRepository) Create(trainingDateID TrainingD r.lock.Lock() defer r.lock.Unlock() - attendee.ID = NewTrainingDateAttendeeID() + attendee.ID = r.ai + r.ai++ attendee.trainingDateID = trainingDateID r.attendees[attendee.ID] = *attendee diff --git a/pkg/training/model.go b/pkg/training/model.go index 6b1a3e6..d225c57 100644 --- a/pkg/training/model.go +++ b/pkg/training/model.go @@ -4,15 +4,9 @@ import ( "github.com/shopspring/decimal" "gitlab.mareshq.com/hq/yggdrasil/internal/money" "time" - - "github.com/google/uuid" ) -type TrainingID = uuid.UUID - -func NewTrainingID() TrainingID { - return uuid.Must(uuid.NewV7()) -} +type TrainingID = int type Training struct { ID TrainingID @@ -35,11 +29,7 @@ const ( CorporateTrainingPrice TrainingPriceType = "CORPORATE" ) -type TrainingDateID = uuid.UUID - -func NewTrainingDateID() TrainingDateID { - return uuid.Must(uuid.NewV7()) -} +type TrainingDateID = int type TrainingDate struct { trainingID TrainingID @@ -56,11 +46,7 @@ type TrainingDate struct { PriceCurrency money.Currency `db:"price_currency"` } -type TrainingDateAttendeeID = uuid.UUID - -func NewTrainingDateAttendeeID() TrainingDateAttendeeID { - return uuid.Must(uuid.NewV7()) -} +type TrainingDateAttendeeID = int type TrainingDateAttendee struct { trainingDateID TrainingDateID diff --git a/pkg/training/postgres_repository.go b/pkg/training/postgres_repository.go index c475e22..7c91f3f 100644 --- a/pkg/training/postgres_repository.go +++ b/pkg/training/postgres_repository.go @@ -19,20 +19,18 @@ func (r *PostgresTrainingRepository) Create(training *Training) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - training.ID = NewTrainingID() - - tx, err := r.pg.Begin(ctx) - if err != nil { - return err + tx, txErr := r.pg.Begin(ctx) + if txErr != nil { + return txErr } - _, err = tx.Exec(ctx, ` - INSERT INTO training.trainings (id, name, description, days) - VALUES ($1, $2, $3, $4) - `, training.ID, training.Name, training.Description, training.Days) - - if err != nil { - return err + queryErr := tx.QueryRow(ctx, ` + INSERT INTO training.trainings (name, description, days) + VALUES ($1, $2, $3) + RETURNING id + `, training.Name, training.Description, training.Days).Scan(&training.ID) + if queryErr != nil { + return queryErr } queryBatch := &pgx.Batch{} @@ -44,6 +42,7 @@ func (r *PostgresTrainingRepository) Create(training *Training) error { `, training.ID, price.Amount, price.Currency, price.Type) } + var err error batch := tx.SendBatch(ctx, queryBatch) defer func() { if e := batch.Close(); e != nil { @@ -246,12 +245,9 @@ func (r *PostgresTrainingDateRepository) Create(trainingID TrainingID, trainingD ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - trainingDate.ID = NewTrainingDateID() - - _, err := r.pg.Exec(ctx, ` - INSERT INTO training.dates (id, training_id, date, start_time, days, is_online, location, address, capacity, price_amount, price_currency) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`, - trainingDate.ID, + err := r.pg.QueryRow(ctx, ` + INSERT INTO training.dates (training_id, date, start_time, days, is_online, location, address, capacity, price_amount, price_currency) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id`, trainingID, trainingDate.Date, trainingDate.StartTime, @@ -262,7 +258,7 @@ func (r *PostgresTrainingDateRepository) Create(trainingID TrainingID, trainingD trainingDate.Capacity, trainingDate.PriceAmount, trainingDate.PriceCurrency, - ) + ).Scan(&trainingDate.ID) if err != nil { return err } @@ -447,12 +443,12 @@ func (r *PostgresTrainingDateAttendeeRepository) Create(trainingDateID TrainingD ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - attendee.ID = NewTrainingDateAttendeeID() - - _, err := r.pg.Exec(ctx, ` - INSERT INTO training.date_attendees (id, date_id, name, email, phone, company, position, is_student, has_paid, has_attended, bill_amount, bill_currency) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) - `, attendee.ID, trainingDateID, attendee.Name, attendee.Email, attendee.Phone, attendee.Company, attendee.Position, attendee.IsStudent, attendee.HasPaid, attendee.HasAttended, attendee.BillAmount, attendee.BillCurrency) + err := r.pg.QueryRow(ctx, ` + INSERT INTO training.date_attendees (date_id, name, email, phone, company, position, is_student, has_paid, has_attended, bill_amount, bill_currency) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + RETURNING id + `, trainingDateID, attendee.Name, attendee.Email, attendee.Phone, attendee.Company, attendee.Position, attendee.IsStudent, attendee.HasPaid, attendee.HasAttended, attendee.BillAmount, attendee.BillCurrency). + Scan(&attendee.ID) if err != nil { return err }