From c3795dba6a67afd6f453edda2041786e2f0cf74e Mon Sep 17 00:00:00 2001 From: Vojtech Mares Date: Sun, 23 Jun 2024 13:13:44 +0200 Subject: [PATCH] refactor(training): use golang style of naming (do not repeat pacakge name in struct/func names) --- api/v1/openapi.yaml | 6 +- cmd/seed/main.go | 2 +- internal/bootstrap/bootstrap.go | 8 +-- internal/faker/faker.go | 20 +++--- internal/server/api.gen.go | 78 ++++++++++++------------ internal/server/api.go | 26 ++++---- internal/server/server_test.go | 94 ++++++++++++++--------------- migrations/001_trainings.up.sql | 2 +- pkg/training/inmemory_repository.go | 72 +++++++++++----------- pkg/training/model.go | 32 +++++----- pkg/training/postgres_repository.go | 88 +++++++++++++-------------- pkg/training/repository.go | 36 +++++------ 12 files changed, 232 insertions(+), 232 deletions(-) diff --git a/api/v1/openapi.yaml b/api/v1/openapi.yaml index 1aa4e3d..1b8e7a0 100644 --- a/api/v1/openapi.yaml +++ b/api/v1/openapi.yaml @@ -548,7 +548,7 @@ components: TrainingID: type: integer minimum: 1 - x-go-type: training.TrainingID + x-go-type: training.ID x-go-type-import: path: gitlab.mareshq.com/hq/yggdrasil/pkg/training @@ -654,7 +654,7 @@ components: TrainingDateID: type: integer minimum: 1 - x-go-type: training.TrainingDateID + x-go-type: training.DateID x-go-type-import: path: gitlab.mareshq.com/hq/yggdrasil/pkg/training @@ -707,7 +707,7 @@ components: TrainingDateAttendeeID: type: integer minimum: 1 - x-go-type: training.TrainingDateAttendeeID + x-go-type: training.AttendeeID 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 69790b1..b7007aa 100644 --- a/cmd/seed/main.go +++ b/cmd/seed/main.go @@ -53,7 +53,7 @@ func main() { } trainingRepo := training.NewPostgresTrainingRepository(pool) - trainingDateRepo := training.NewPostgresTrainingDateRepository(pool) + trainingDateRepo := training.NewPostgresDateRepository(pool) //trainingDateAttendeeRepo := training.NewPostgresTrainingDateAttendeeRepository(pool) f := faker.NewFaker(trainingRepo, trainingDateRepo) diff --git a/internal/bootstrap/bootstrap.go b/internal/bootstrap/bootstrap.go index 4ca44fc..0203a36 100644 --- a/internal/bootstrap/bootstrap.go +++ b/internal/bootstrap/bootstrap.go @@ -57,8 +57,8 @@ func Bootstrap(logger *zap.Logger) *server.Server { } trainingRepo := training.NewPostgresTrainingRepository(pool) - trainingDateRepo := training.NewPostgresTrainingDateRepository(pool) - trainingDateAttendeeRepo := training.NewPostgresTrainingDateAttendeeRepository(pool) + trainingDateRepo := training.NewPostgresDateRepository(pool) + trainingDateAttendeeRepo := training.NewPostgresAttendeeRepository(pool) apiHandlers := server.NewAPIHandlers(trainingRepo, trainingDateRepo, trainingDateAttendeeRepo) @@ -78,8 +78,8 @@ func BootstrapInMemory(logger *zap.Logger) *server.Server { } trainingRepository := training.NewInMemoryTrainingRepository() - trainingDateRepository := training.NewInMemoryTrainingDateRepository() - trainingDateAttendeeRepository := training.NewInMemoryTrainingDateAttendeeRepository() + trainingDateRepository := training.NewInMemoryDateRepository() + trainingDateAttendeeRepository := training.NewInMemoryAttendeeRepository() f := faker.NewFaker(trainingRepository, trainingDateRepository) if err := f.GenerateFakeData(); err != nil { diff --git a/internal/faker/faker.go b/internal/faker/faker.go index aa83591..adf7380 100644 --- a/internal/faker/faker.go +++ b/internal/faker/faker.go @@ -10,11 +10,11 @@ import ( ) type Faker struct { - trainingRepository training.TrainingRepository - trainingDateRepository training.TrainingDateRepository + trainingRepository training.Repository + trainingDateRepository training.DateRepository } -func NewFaker(trainingRepository training.TrainingRepository, trainingDateRepository training.TrainingDateRepository) *Faker { +func NewFaker(trainingRepository training.Repository, trainingDateRepository training.DateRepository) *Faker { return &Faker{ trainingRepository: trainingRepository, trainingDateRepository: trainingDateRepository, @@ -27,7 +27,7 @@ func (f *Faker) GenerateFakeData() error { Name: "Kubernetes", Days: 2, Description: "Kubernetes", - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(450), Currency: money.USD, @@ -54,7 +54,7 @@ func (f *Faker) GenerateFakeData() error { Name: "Terraform", Days: 1, Description: "Terraform", - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: money.USD, @@ -81,7 +81,7 @@ func (f *Faker) GenerateFakeData() error { Name: "RKE2", Days: 1, Description: "RKE2", - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: money.USD, @@ -108,7 +108,7 @@ func (f *Faker) GenerateFakeData() error { Name: "GitHub Actions", Days: 1, Description: "GitHub Actions", - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: money.USD, @@ -135,7 +135,7 @@ func (f *Faker) GenerateFakeData() error { Name: "GitLab CI", Days: 1, Description: "GitLab CI", - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: money.USD, @@ -162,7 +162,7 @@ func (f *Faker) GenerateFakeData() error { Name: "Prometheus", Days: 2, Description: "Prometheus", - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(450), Currency: money.USD, @@ -230,7 +230,7 @@ func (f *Faker) GenerateFakeData() error { } } - td := training.TrainingDate{ + td := training.Date{ Date: date, StartTime: date.Truncate(24 * time.Hour), Days: t.Days, diff --git a/internal/server/api.gen.go b/internal/server/api.gen.go index 14b2924..0690e9c 100644 --- a/internal/server/api.gen.go +++ b/internal/server/api.gen.go @@ -173,7 +173,7 @@ type TrainingDateAttendeeAttendanceUpdateRequest struct { } // TrainingDateAttendeeID defines model for TrainingDateAttendeeID. -type TrainingDateAttendeeID = training.TrainingDateAttendeeID +type TrainingDateAttendeeID = training.AttendeeID // TrainingDateAttendeePaymentUpdateRequest defines model for TrainingDateAttendeePaymentUpdateRequest. type TrainingDateAttendeePaymentUpdateRequest struct { @@ -181,10 +181,10 @@ type TrainingDateAttendeePaymentUpdateRequest struct { } // TrainingDateID defines model for TrainingDateID. -type TrainingDateID = training.TrainingDateID +type TrainingDateID = training.DateID // TrainingID defines model for TrainingID. -type TrainingID = training.TrainingID +type TrainingID = training.ID // TrainingPrice defines model for TrainingPrice. type TrainingPrice struct { @@ -2336,44 +2336,44 @@ func (sh *strictHandler) ListTrainingUpcomingDates(ctx *fiber.Ctx, trainingID Tr // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+xczXLjuBF+FRSSW2hRk92t3dIpjj3ZdWUzdjx2Dpn4AJMtETMkwAFAe1QuPUlOeZfk", - "vVIASIqkQJG0JEv26jQaCWx8/d9oNP2EA56knAFTEk+ecEoESUCBMP+7EYQyymbnRMGpUsBCgItz/UsI", + "H4sIAAAAAAAC/+xc3XLbuBV+FQzauzKi0t2d3dFV3Tjd9XQbu47di6a+gMkjEQkJMABoR+PRk/Sq79K+", + "VwcASZEUKJL6sWSvrqJI4MF3/g8ODv2EA56knAFTEk+ecEoESUCBMP+7EYQyymbnRMGZUsBCgItz/UsI", "MhA0VZQzPCnXIb0QFSvRxTn2MNULUqIi7GFGEsATrNxkPSzga0YFhHiiRAYelkEECdH7/V7AFE/w7/wl", - "XN/+Kv0WlIuFV2OgG3gfvFvC2cC3FlsXrC1AMnAWmopMOZNgtH/BFAhG4vdCcKG/CDhTwJT+SNI0pgHR", - "UP1U8PsYkj98lhr3U8+tr+xT56AIjaXdvi6AYn8EBsDCwxfsgcQ0vGBppvaHymBAVINYQvvA1V94xsJ9", - "wfrAFZpqAAWkRWEDRpdnAogCl6dcw9cMpMGZCp6CUNSqX8MgbK4/qnmqDU4qQdlMcwsJobH+ZcpFQhSe", - "5N94q0up/KiyMJdECFOSxQpPpiSWUK6+5zwGwvRya9uOLdOIs5ZfuKRWCis/LqqO8ckS90qslqRXcloh", - "dVdi4/efIVB6n3UytG5jlB3Hl1M8+bReoR/g0UUIL7ymGu5pHHebBw30szgiMicV9hN3ROQVoT0X23XP", - "DMVVPdAQe5axJYI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Nm6xCGq+gV5Pv0HUv2Zh/Odick9vdDo0ymRwWJFV2/3BqJdWeW4ZGAeCbWmvu+nqeZQ4S50", + "bIEjghg8lnpuUfPCq7uIn+VXXG/KW8lWj2m78ty3A3VetTpU9GvuTVXTRwWvyPJqtPZ9t+zr85Y79co6", + "oiZg2VeJT8v724WtjWOwlV9di+fm+4ozV2ewW7LUckkt0dytKP37NXPFFlB4QJFb1hFZWsP93I47u6Oh", + "0wsqkwo7Ft7uPMY1TeFMOKpygXwgpfwMqr9G0syhkXrttb1Sdp/b3FV1r9w23huIHrktM49smdsOZFeW", + "3b6mtTac+t15sZkMjzMutE/OrEmfpfyOJmMuE2VXqdOvoj23PYGjCxvt8+cHLYtrXY114cM44BHXx6ho", + "hW0WEJZf2S7LgLJra4vzeq8uOkADizUtrWOs2Ayw7cq2oxD+Xoq9Ls88upDeKP56KLdHBXgo/e67bhyc", + "AMZ7BdI3AbyuIrLTRDdJHD4phqZ7F5nlmPUrimTdo+R9YxpaCvQI6tUSTL1mXVd+9C9cy3vYVxHxul8c", + "Pnjpu3KD3hkBC/3vphY+kDW3V9Alf3uJiH750lqfuq72ItzrLfLc7/s5DNEsOM7w6Kr9lp6S628P9vTk", + "/nsgA09vBwq7w56ozrNscP4rtXG0B8ES4fYnwleg0L2Hm+Ep71jjy04OmS/TZJ7jmLpR0TZ+FkCDi7ZX", + "eH7t5QZ7SrN+dSh+oKMtB+9/6y435LWEl+x8pOSpeG3rFbhjzslqM8DF9zN7Z/EuxkDPzF/0OLllz3dg", + "XrJPppah35ZDpqWBD/fGAfN1zr868SLmCdx/J+PFzBU4pvA65yg1FfOesFVKg00ekBiF8AAxT62/mLW1", + "WemJ78d6XcSlmnw3Ho+NvvLNmhQvC2uRiNzzTNVmBPNx6iW+xd3i/wEAAP//jH42mm5VAAA=", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/internal/server/api.go b/internal/server/api.go index 2635bbb..332e13c 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -13,12 +13,12 @@ import ( ) type APIHandlers struct { - trainingRepository training.TrainingRepository - trainingDateRepository training.TrainingDateRepository - trainingDateAttendeeRepository training.TrainingDateAttendeeRepository + trainingRepository training.Repository + trainingDateRepository training.DateRepository + trainingDateAttendeeRepository training.AttendeeRepository } -func NewAPIHandlers(trainingRepository training.TrainingRepository, trainingDateRepository training.TrainingDateRepository, trainingDateAttendeeRepository training.TrainingDateAttendeeRepository) *APIHandlers { +func NewAPIHandlers(trainingRepository training.Repository, trainingDateRepository training.DateRepository, trainingDateAttendeeRepository training.AttendeeRepository) *APIHandlers { return &APIHandlers{ trainingRepository: trainingRepository, trainingDateRepository: trainingDateRepository, @@ -60,7 +60,7 @@ func (h *APIHandlers) ListTrainings(ctx context.Context, req ListTrainingsReques } func (h *APIHandlers) CreateTraining(ctx context.Context, req CreateTrainingRequestObject) (CreateTrainingResponseObject, error) { - pricing := make([]training.TrainingPrice, len(req.Body.Pricing)) + pricing := make([]training.Price, len(req.Body.Pricing)) for idx, p := range req.Body.Pricing { amount, err := decimal.NewFromString(p.Amount) if err != nil { @@ -72,10 +72,10 @@ func (h *APIHandlers) CreateTraining(ctx context.Context, req CreateTrainingRequ }}, nil } - pricing[idx] = training.TrainingPrice{ + pricing[idx] = training.Price{ Amount: amount, Currency: p.Currency, - Type: training.TrainingPriceType(p.Type), + Type: training.PriceType(p.Type), } } @@ -169,7 +169,7 @@ func (h *APIHandlers) GetTraining(ctx context.Context, req GetTrainingRequestObj } func (h *APIHandlers) UpdateTraining(ctx context.Context, req UpdateTrainingRequestObject) (UpdateTrainingResponseObject, error) { - pricing := make([]training.TrainingPrice, len(req.Body.Pricing)) + pricing := make([]training.Price, len(req.Body.Pricing)) for idx, p := range req.Body.Pricing { amount, err := decimal.NewFromString(p.Amount) if err != nil { @@ -181,10 +181,10 @@ func (h *APIHandlers) UpdateTraining(ctx context.Context, req UpdateTrainingRequ }}, nil } - pricing[idx] = training.TrainingPrice{ + pricing[idx] = training.Price{ Amount: amount, Currency: p.Currency, - Type: training.TrainingPriceType(p.Type), + Type: training.PriceType(p.Type), } } t := training.Training{ @@ -273,7 +273,7 @@ func (h *APIHandlers) CreateTrainingDate(ctx context.Context, req CreateTraining }}, nil } - td := training.TrainingDate{ + td := training.Date{ Date: req.Body.Date.Time, StartTime: startTime, Days: req.Body.Days, @@ -390,7 +390,7 @@ func (h *APIHandlers) UpdateTrainingDate(ctx context.Context, req UpdateTraining }}, nil } - td := training.TrainingDate{ + td := training.Date{ ID: req.TrainingDateID, Date: req.Body.Date.Time, StartTime: startTime, @@ -577,7 +577,7 @@ func (h *APIHandlers) CreateTrainingDateAttendee(ctx context.Context, req Create } } - ta := training.TrainingDateAttendee{ + ta := training.Attendee{ Name: req.Body.Name, Email: string(req.Body.Email), Phone: req.Body.Phone, diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 7162ccc..bf239a7 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -80,8 +80,8 @@ func doDelete(t *testing.T, app *fiber.App, rawURL string) (*http.Response, erro func TestServer(t *testing.T) { //var err error trainingRepo := training.NewInMemoryTrainingRepository() - trainingDateRepo := training.NewInMemoryTrainingDateRepository() - trainingDateAttendeeRepo := training.NewInMemoryTrainingDateAttendeeRepository() + trainingDateRepo := training.NewInMemoryDateRepository() + trainingDateAttendeeRepo := training.NewInMemoryAttendeeRepository() handlers := NewAPIHandlers(trainingRepo, trainingDateRepo, trainingDateAttendeeRepo) app := NewTestingServer(handlers) @@ -142,7 +142,7 @@ func TestServer(t *testing.T) { Name: "Testing Training", Description: "This is a test training", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -166,7 +166,7 @@ func TestServer(t *testing.T) { Name: "Testing Training", Description: "This is a test training", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -204,7 +204,7 @@ func TestServer(t *testing.T) { Name: "Testing Training", Description: "This is a test training", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -243,7 +243,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -293,7 +293,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -318,7 +318,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -330,7 +330,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -362,7 +362,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -374,7 +374,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -423,7 +423,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -435,7 +435,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 0, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -482,7 +482,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -496,7 +496,7 @@ func TestServer(t *testing.T) { upcomingTrainingCount := 5 for i := -4; i <= 5; i++ { date := now.AddDate(0, 0, i) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -524,7 +524,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date 1", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -538,7 +538,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date 2", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -552,7 +552,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date 3", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -568,7 +568,7 @@ func TestServer(t *testing.T) { upcomingTrainingCount := 5 for i := -4; i <= upcomingTrainingCount; i++ { date := now.AddDate(0, 0, i) - td1 := &training.TrainingDate{ + td1 := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -581,7 +581,7 @@ func TestServer(t *testing.T) { } _ = handlers.trainingDateRepository.Create(tr1.ID, td1) - td2 := &training.TrainingDate{ + td2 := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -594,7 +594,7 @@ func TestServer(t *testing.T) { } _ = handlers.trainingDateRepository.Create(tr2.ID, td2) - td3 := &training.TrainingDate{ + td3 := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -628,7 +628,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -641,7 +641,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -668,7 +668,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -681,7 +681,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -726,7 +726,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -739,7 +739,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -753,9 +753,9 @@ func TestServer(t *testing.T) { _ = handlers.trainingDateRepository.Create(tr.ID, td) attendeeCount := 9 - attendees := make([]training.TrainingDateAttendee, attendeeCount) + attendees := make([]training.Attendee, attendeeCount) for i := range attendeeCount { - nAtd := &training.TrainingDateAttendee{ + nAtd := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", @@ -799,7 +799,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -812,7 +812,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -825,7 +825,7 @@ func TestServer(t *testing.T) { } _ = handlers.trainingDateRepository.Create(tr.ID, td) - attendee := &training.TrainingDateAttendee{ + attendee := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", @@ -862,7 +862,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -875,7 +875,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -888,7 +888,7 @@ func TestServer(t *testing.T) { } _ = handlers.trainingDateRepository.Create(tr.ID, td) - attendee := &training.TrainingDateAttendee{ + attendee := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", @@ -935,7 +935,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -948,7 +948,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -961,7 +961,7 @@ func TestServer(t *testing.T) { } _ = handlers.trainingDateRepository.Create(tr.ID, td) - attendee := &training.TrainingDateAttendee{ + attendee := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", @@ -991,7 +991,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -1004,7 +1004,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -1021,7 +1021,7 @@ func TestServer(t *testing.T) { Paid: true, } - attendee := &training.TrainingDateAttendee{ + attendee := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", @@ -1064,7 +1064,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -1077,7 +1077,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -1094,7 +1094,7 @@ func TestServer(t *testing.T) { Attended: true, } - attendee := &training.TrainingDateAttendee{ + attendee := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", @@ -1137,7 +1137,7 @@ func TestServer(t *testing.T) { Name: "Testing Training for training date", Description: "This is a test training for training date", Days: 1, - Pricing: []training.TrainingPrice{ + Pricing: []training.Price{ { Amount: decimal.NewFromInt(200), Currency: "EUR", @@ -1150,7 +1150,7 @@ func TestServer(t *testing.T) { date := time.Date(2024, time.May, 1, 9, 0, 0, 0, time.UTC) - td := &training.TrainingDate{ + td := &training.Date{ Address: "Test Address 123, NYC", Capacity: 12, Date: date, @@ -1165,7 +1165,7 @@ func TestServer(t *testing.T) { attendeeCount := 9 for range attendeeCount { - nAtd := &training.TrainingDateAttendee{ + nAtd := &training.Attendee{ Name: "John Doe", Email: "john.doe@example.com", Company: "Acme Inc.", diff --git a/migrations/001_trainings.up.sql b/migrations/001_trainings.up.sql index 0a2d5c9..0ec66b9 100644 --- a/migrations/001_trainings.up.sql +++ b/migrations/001_trainings.up.sql @@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS training.dates ( CONSTRAINT allowed_currencies CHECK (price_currency IN ('USD', 'EUR', 'CZK')) ); -CREATE TABLE IF NOT EXISTS training.date_attendees ( +CREATE TABLE IF NOT EXISTS training.attendees ( id SERIAL PRIMARY KEY, date_id SERIAL REFERENCES training.dates(id), name VARCHAR(255) NOT NULL, diff --git a/pkg/training/inmemory_repository.go b/pkg/training/inmemory_repository.go index 7b3c673..5d7c978 100644 --- a/pkg/training/inmemory_repository.go +++ b/pkg/training/inmemory_repository.go @@ -6,14 +6,14 @@ import ( ) type InMemoryTrainingRepository struct { - trainings map[TrainingID]Training + trainings map[ID]Training lock sync.RWMutex ai int } func NewInMemoryTrainingRepository() *InMemoryTrainingRepository { return &InMemoryTrainingRepository{ - trainings: make(map[TrainingID]Training), + trainings: make(map[ID]Training), ai: 1, } } @@ -28,7 +28,7 @@ func (r *InMemoryTrainingRepository) Create(training *Training) error { return nil } -func (r *InMemoryTrainingRepository) FindByID(id TrainingID) (*Training, error) { +func (r *InMemoryTrainingRepository) FindByID(id ID) (*Training, error) { r.lock.RLock() defer r.lock.RUnlock() @@ -58,7 +58,7 @@ func (r *InMemoryTrainingRepository) Update(training *Training) error { return nil } -func (r *InMemoryTrainingRepository) Delete(id TrainingID) error { +func (r *InMemoryTrainingRepository) Delete(id ID) error { r.lock.Lock() defer r.lock.Unlock() @@ -71,23 +71,23 @@ func (r *InMemoryTrainingRepository) Delete(id TrainingID) error { return nil } -type InMemoryTrainingDateRepository struct { - trainingDates map[TrainingDateID]TrainingDate - trainingToDates map[TrainingID][]TrainingDateID +type InMemoryDateRepository struct { + trainingDates map[DateID]Date + trainingToDates map[ID][]DateID lock sync.RWMutex ai int } -func NewInMemoryTrainingDateRepository() *InMemoryTrainingDateRepository { - return &InMemoryTrainingDateRepository{ - trainingDates: make(map[TrainingDateID]TrainingDate), - trainingToDates: make(map[TrainingID][]TrainingDateID), +func NewInMemoryDateRepository() *InMemoryDateRepository { + return &InMemoryDateRepository{ + trainingDates: make(map[DateID]Date), + trainingToDates: make(map[ID][]DateID), ai: 1, } } -func (r *InMemoryTrainingDateRepository) Create(trainingID TrainingID, trainingDate *TrainingDate) error { +func (r *InMemoryDateRepository) Create(trainingID ID, trainingDate *Date) error { r.lock.Lock() defer r.lock.Unlock() @@ -100,7 +100,7 @@ func (r *InMemoryTrainingDateRepository) Create(trainingID TrainingID, trainingD return nil } -func (r *InMemoryTrainingDateRepository) FindByID(id TrainingDateID) (*TrainingDate, error) { +func (r *InMemoryDateRepository) FindByID(id DateID) (*Date, error) { r.lock.RLock() defer r.lock.RUnlock() @@ -111,11 +111,11 @@ func (r *InMemoryTrainingDateRepository) FindByID(id TrainingDateID) (*TrainingD return &date, nil } -func (r *InMemoryTrainingDateRepository) FindAll() ([]TrainingDate, error) { +func (r *InMemoryDateRepository) FindAll() ([]Date, error) { r.lock.RLock() defer r.lock.RUnlock() - dates := make([]TrainingDate, len(r.trainingDates)) + dates := make([]Date, len(r.trainingDates)) for _, date := range r.trainingDates { dates = append(dates, date) } @@ -123,11 +123,11 @@ func (r *InMemoryTrainingDateRepository) FindAll() ([]TrainingDate, error) { return dates, nil } -func (r *InMemoryTrainingDateRepository) FindAllByTrainingID(trainingID TrainingID) ([]TrainingDate, error) { +func (r *InMemoryDateRepository) FindAllByTrainingID(trainingID ID) ([]Date, error) { r.lock.RLock() defer r.lock.RUnlock() - dates := make([]TrainingDate, 0) + dates := make([]Date, 0) for _, id := range r.trainingToDates[trainingID] { dates = append(dates, r.trainingDates[id]) } @@ -135,12 +135,12 @@ func (r *InMemoryTrainingDateRepository) FindAllByTrainingID(trainingID Training return dates, nil } -func (r *InMemoryTrainingDateRepository) FindUpcomingByTrainingID(trainingID TrainingID) ([]TrainingDate, error) { +func (r *InMemoryDateRepository) FindUpcomingByTrainingID(trainingID ID) ([]Date, error) { r.lock.RLock() defer r.lock.RUnlock() now := time.Now() - var dates []TrainingDate + var dates []Date for _, id := range r.trainingToDates[trainingID] { date := r.trainingDates[id] if date.Date.After(now) { @@ -151,7 +151,7 @@ func (r *InMemoryTrainingDateRepository) FindUpcomingByTrainingID(trainingID Tra return dates, nil } -func (r *InMemoryTrainingDateRepository) Update(trainingDate *TrainingDate) error { +func (r *InMemoryDateRepository) Update(trainingDate *Date) error { r.lock.Lock() defer r.lock.Unlock() @@ -161,7 +161,7 @@ func (r *InMemoryTrainingDateRepository) Update(trainingDate *TrainingDate) erro return nil } -func (r *InMemoryTrainingDateRepository) Delete(id TrainingDateID) error { +func (r *InMemoryDateRepository) Delete(id DateID) error { r.lock.Lock() defer r.lock.Unlock() @@ -182,23 +182,23 @@ func (r *InMemoryTrainingDateRepository) Delete(id TrainingDateID) error { return nil } -type InMemoryTrainingDateAttendeeRepository struct { - attendees map[TrainingDateAttendeeID]TrainingDateAttendee - dateToAttendees map[TrainingDateID][]TrainingDateAttendeeID +type InMemoryAttendeeRepository struct { + attendees map[AttendeeID]Attendee + dateToAttendees map[DateID][]AttendeeID lock sync.RWMutex ai int } -func NewInMemoryTrainingDateAttendeeRepository() *InMemoryTrainingDateAttendeeRepository { - return &InMemoryTrainingDateAttendeeRepository{ - attendees: make(map[TrainingDateAttendeeID]TrainingDateAttendee), - dateToAttendees: make(map[TrainingDateID][]TrainingDateAttendeeID), +func NewInMemoryAttendeeRepository() *InMemoryAttendeeRepository { + return &InMemoryAttendeeRepository{ + attendees: make(map[AttendeeID]Attendee), + dateToAttendees: make(map[DateID][]AttendeeID), ai: 1, } } -func (r *InMemoryTrainingDateAttendeeRepository) Create(trainingDateID TrainingDateID, attendee *TrainingDateAttendee) error { +func (r *InMemoryAttendeeRepository) Create(trainingDateID DateID, attendee *Attendee) error { r.lock.Lock() defer r.lock.Unlock() @@ -212,7 +212,7 @@ func (r *InMemoryTrainingDateAttendeeRepository) Create(trainingDateID TrainingD return nil } -func (r *InMemoryTrainingDateAttendeeRepository) FindByID(id TrainingDateAttendeeID) (*TrainingDateAttendee, error) { +func (r *InMemoryAttendeeRepository) FindByID(id AttendeeID) (*Attendee, error) { r.lock.RLock() defer r.lock.RUnlock() @@ -223,11 +223,11 @@ func (r *InMemoryTrainingDateAttendeeRepository) FindByID(id TrainingDateAttende return &attendee, nil } -func (r *InMemoryTrainingDateAttendeeRepository) FindAll() ([]TrainingDateAttendee, error) { +func (r *InMemoryAttendeeRepository) FindAll() ([]Attendee, error) { r.lock.RLock() defer r.lock.RUnlock() - attendees := make([]TrainingDateAttendee, len(r.attendees)) + attendees := make([]Attendee, len(r.attendees)) for _, attendee := range r.attendees { attendees = append(attendees, attendee) } @@ -235,11 +235,11 @@ func (r *InMemoryTrainingDateAttendeeRepository) FindAll() ([]TrainingDateAttend return attendees, nil } -func (r *InMemoryTrainingDateAttendeeRepository) FindAllByTrainingDateID(trainingDateID TrainingDateID) ([]TrainingDateAttendee, error) { +func (r *InMemoryAttendeeRepository) FindAllByTrainingDateID(trainingDateID DateID) ([]Attendee, error) { r.lock.RLock() defer r.lock.RUnlock() - attendees := make([]TrainingDateAttendee, 0) + attendees := make([]Attendee, 0) for _, id := range r.dateToAttendees[trainingDateID] { attendees = append(attendees, r.attendees[id]) } @@ -247,7 +247,7 @@ func (r *InMemoryTrainingDateAttendeeRepository) FindAllByTrainingDateID(trainin return attendees, nil } -func (r *InMemoryTrainingDateAttendeeRepository) Update(attendee *TrainingDateAttendee) error { +func (r *InMemoryAttendeeRepository) Update(attendee *Attendee) error { r.lock.Lock() defer r.lock.Unlock() @@ -257,7 +257,7 @@ func (r *InMemoryTrainingDateAttendeeRepository) Update(attendee *TrainingDateAt return nil } -func (r *InMemoryTrainingDateAttendeeRepository) Delete(id TrainingDateAttendeeID) error { +func (r *InMemoryAttendeeRepository) Delete(id AttendeeID) error { r.lock.Lock() defer r.lock.Unlock() diff --git a/pkg/training/model.go b/pkg/training/model.go index d225c57..e9f9a39 100644 --- a/pkg/training/model.go +++ b/pkg/training/model.go @@ -6,35 +6,35 @@ import ( "time" ) -type TrainingID = int +type ID = int type Training struct { - ID TrainingID + ID ID Name string Days int8 Description string - Pricing []TrainingPrice `db:"-"` + Pricing []Price `db:"-"` } -type TrainingPrice struct { +type Price struct { Amount decimal.Decimal Currency money.Currency - Type TrainingPriceType + Type PriceType } -type TrainingPriceType string +type PriceType string const ( - OpenTrainingPrice TrainingPriceType = "OPEN" - CorporateTrainingPrice TrainingPriceType = "CORPORATE" + OpenTrainingPrice PriceType = "OPEN" + CorporateTrainingPrice PriceType = "CORPORATE" ) -type TrainingDateID = int +type DateID = int -type TrainingDate struct { - trainingID TrainingID +type Date struct { + trainingID ID - ID TrainingDateID + ID DateID Date time.Time StartTime time.Time Days int8 @@ -46,12 +46,12 @@ type TrainingDate struct { PriceCurrency money.Currency `db:"price_currency"` } -type TrainingDateAttendeeID = int +type AttendeeID = int -type TrainingDateAttendee struct { - trainingDateID TrainingDateID +type Attendee struct { + trainingDateID DateID - ID TrainingDateAttendeeID + ID AttendeeID Name string Email string Phone string diff --git a/pkg/training/postgres_repository.go b/pkg/training/postgres_repository.go index 7c91f3f..0b00291 100644 --- a/pkg/training/postgres_repository.go +++ b/pkg/training/postgres_repository.go @@ -66,7 +66,7 @@ func (r *PostgresTrainingRepository) Create(training *Training) error { return nil } -func (r *PostgresTrainingRepository) FindByID(id TrainingID) (*Training, error) { +func (r *PostgresTrainingRepository) FindByID(id ID) (*Training, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -91,12 +91,12 @@ func (r *PostgresTrainingRepository) FindByID(id TrainingID) (*Training, error) } defer rows.Close() - training.Pricing = make([]TrainingPrice, 0) - training.Pricing, err = pgx.CollectRows[TrainingPrice](rows, func(row pgx.CollectableRow) (TrainingPrice, error) { - var price TrainingPrice + training.Pricing = make([]Price, 0) + training.Pricing, err = pgx.CollectRows[Price](rows, func(row pgx.CollectableRow) (Price, error) { + var price Price err := row.Scan(&price.Amount, &price.Currency, &price.Type) if err != nil { - return TrainingPrice{}, err + return Price{}, err } return price, nil }) @@ -132,7 +132,7 @@ func (r *PostgresTrainingRepository) FindAll() ([]Training, error) { return Training{}, queryErr } - training.Pricing, scanErr = pgx.CollectRows(priceRows, pgx.RowToStructByName[TrainingPrice]) + training.Pricing, scanErr = pgx.CollectRows(priceRows, pgx.RowToStructByName[Price]) if scanErr != nil { return Training{}, scanErr } @@ -198,7 +198,7 @@ func (r *PostgresTrainingRepository) Update(training *Training) error { return nil } -func (r *PostgresTrainingRepository) Delete(id TrainingID) error { +func (r *PostgresTrainingRepository) Delete(id ID) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -233,15 +233,15 @@ func (r *PostgresTrainingRepository) Delete(id TrainingID) error { return nil } -type PostgresTrainingDateRepository struct { +type PostgresDateRepository struct { pg *pgxpool.Pool } -func NewPostgresTrainingDateRepository(pg *pgxpool.Pool) *PostgresTrainingDateRepository { - return &PostgresTrainingDateRepository{pg: pg} +func NewPostgresDateRepository(pg *pgxpool.Pool) *PostgresDateRepository { + return &PostgresDateRepository{pg: pg} } -func (r *PostgresTrainingDateRepository) Create(trainingID TrainingID, trainingDate *TrainingDate) error { +func (r *PostgresDateRepository) Create(trainingID ID, trainingDate *Date) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -266,11 +266,11 @@ func (r *PostgresTrainingDateRepository) Create(trainingID TrainingID, trainingD return nil } -func (r *PostgresTrainingDateRepository) FindByID(id TrainingDateID) (*TrainingDate, error) { +func (r *PostgresDateRepository) FindByID(id DateID) (*Date, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - var trainingDate TrainingDate + var trainingDate Date err := r.pg.QueryRow(ctx, `SELECT * FROM training.dates WHERE id = $1`, id). Scan( &trainingDate.ID, @@ -292,7 +292,7 @@ func (r *PostgresTrainingDateRepository) FindByID(id TrainingDateID) (*TrainingD return &trainingDate, nil } -func (r *PostgresTrainingDateRepository) FindAll() ([]TrainingDate, error) { +func (r *PostgresDateRepository) FindAll() ([]Date, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -302,9 +302,9 @@ func (r *PostgresTrainingDateRepository) FindAll() ([]TrainingDate, error) { } defer rows.Close() - var trainingDates []TrainingDate + var trainingDates []Date for rows.Next() { - var trainingDate TrainingDate + var trainingDate Date err := rows.Scan( &trainingDate.ID, &trainingDate.trainingID, @@ -328,7 +328,7 @@ func (r *PostgresTrainingDateRepository) FindAll() ([]TrainingDate, error) { return trainingDates, nil } -func (r *PostgresTrainingDateRepository) FindAllByTrainingID(trainingID TrainingID) ([]TrainingDate, error) { +func (r *PostgresDateRepository) FindAllByTrainingID(trainingID ID) ([]Date, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -338,9 +338,9 @@ func (r *PostgresTrainingDateRepository) FindAllByTrainingID(trainingID Training } defer rows.Close() - var trainingDates []TrainingDate + var trainingDates []Date for rows.Next() { - var trainingDate TrainingDate + var trainingDate Date err := rows.Scan( &trainingDate.ID, &trainingDate.trainingID, @@ -364,7 +364,7 @@ func (r *PostgresTrainingDateRepository) FindAllByTrainingID(trainingID Training return trainingDates, nil } -func (r *PostgresTrainingDateRepository) FindUpcomingByTrainingID(trainingID TrainingID) ([]TrainingDate, error) { +func (r *PostgresDateRepository) FindUpcomingByTrainingID(trainingID ID) ([]Date, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -377,9 +377,9 @@ func (r *PostgresTrainingDateRepository) FindUpcomingByTrainingID(trainingID Tra } defer rows.Close() - var trainingDates []TrainingDate + var trainingDates []Date for rows.Next() { - var trainingDate TrainingDate + var trainingDate Date err := rows.Scan( &trainingDate.ID, &trainingDate.trainingID, @@ -403,7 +403,7 @@ func (r *PostgresTrainingDateRepository) FindUpcomingByTrainingID(trainingID Tra return trainingDates, nil } -func (r *PostgresTrainingDateRepository) Update(trainingDate *TrainingDate) error { +func (r *PostgresDateRepository) Update(trainingDate *Date) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -419,7 +419,7 @@ func (r *PostgresTrainingDateRepository) Update(trainingDate *TrainingDate) erro return nil } -func (r *PostgresTrainingDateRepository) Delete(id TrainingDateID) error { +func (r *PostgresDateRepository) Delete(id DateID) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -431,20 +431,20 @@ func (r *PostgresTrainingDateRepository) Delete(id TrainingDateID) error { return nil } -type PostgresTrainingDateAttendeeRepository struct { +type PostgresAttendeeRepository struct { pg *pgxpool.Pool } -func NewPostgresTrainingDateAttendeeRepository(pg *pgxpool.Pool) *PostgresTrainingDateAttendeeRepository { - return &PostgresTrainingDateAttendeeRepository{pg: pg} +func NewPostgresAttendeeRepository(pg *pgxpool.Pool) *PostgresAttendeeRepository { + return &PostgresAttendeeRepository{pg: pg} } -func (r *PostgresTrainingDateAttendeeRepository) Create(trainingDateID TrainingDateID, attendee *TrainingDateAttendee) error { +func (r *PostgresAttendeeRepository) Create(trainingDateID DateID, attendee *Attendee) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() 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) + INSERT INTO training.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). @@ -456,14 +456,14 @@ func (r *PostgresTrainingDateAttendeeRepository) Create(trainingDateID TrainingD return nil } -func (r *PostgresTrainingDateAttendeeRepository) FindByID(id TrainingDateAttendeeID) (*TrainingDateAttendee, error) { +func (r *PostgresAttendeeRepository) FindByID(id AttendeeID) (*Attendee, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - var attendee TrainingDateAttendee + var attendee Attendee err := r.pg.QueryRow(ctx, ` SELECT id, date_id, name, email, phone, company, position, is_student, has_paid, has_attended, bill_amount, bill_currency - FROM training.date_attendees + FROM training.attendees WHERE id = $1 `, id).Scan(&attendee.ID, &attendee.trainingDateID, &attendee.Name, &attendee.Email, &attendee.Phone, &attendee.Company, &attendee.Position, &attendee.IsStudent, &attendee.HasPaid, &attendee.HasAttended, &attendee.BillAmount, &attendee.BillCurrency) if err != nil { @@ -473,22 +473,22 @@ func (r *PostgresTrainingDateAttendeeRepository) FindByID(id TrainingDateAttende return &attendee, nil } -func (r *PostgresTrainingDateAttendeeRepository) FindAll() ([]TrainingDateAttendee, error) { +func (r *PostgresAttendeeRepository) FindAll() ([]Attendee, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() rows, err := r.pg.Query(ctx, ` SELECT id, date_id, name, email, phone, company, position, is_student, has_paid, has_attended, bill_amount, bill_currency - FROM training.date_attendees + FROM training.attendees `) if err != nil { return nil, err } defer rows.Close() - var attendees []TrainingDateAttendee + var attendees []Attendee for rows.Next() { - var attendee TrainingDateAttendee + var attendee Attendee err := rows.Scan(&attendee.ID, &attendee.trainingDateID, &attendee.Name, &attendee.Email, &attendee.Phone, &attendee.Company, &attendee.Position, &attendee.IsStudent, &attendee.HasPaid, &attendee.HasAttended, &attendee.BillAmount, &attendee.BillCurrency) if err != nil { return nil, err @@ -500,13 +500,13 @@ func (r *PostgresTrainingDateAttendeeRepository) FindAll() ([]TrainingDateAttend return attendees, nil } -func (r *PostgresTrainingDateAttendeeRepository) FindAllByTrainingDateID(trainingDateID TrainingDateID) ([]TrainingDateAttendee, error) { +func (r *PostgresAttendeeRepository) FindAllByTrainingDateID(trainingDateID DateID) ([]Attendee, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() rows, err := r.pg.Query(ctx, ` SELECT id, date_id, name, email, phone, company, position, is_student, has_paid, has_attended, bill_amount, bill_currency - FROM training.date_attendees + FROM training.attendees WHERE date_id = $1 `, trainingDateID) if err != nil { @@ -514,9 +514,9 @@ func (r *PostgresTrainingDateAttendeeRepository) FindAllByTrainingDateID(trainin } defer rows.Close() - var attendees []TrainingDateAttendee + var attendees []Attendee for rows.Next() { - var attendee TrainingDateAttendee + var attendee Attendee err := rows.Scan(&attendee.ID, &attendee.trainingDateID, &attendee.Name, &attendee.Email, &attendee.Phone, &attendee.Company, &attendee.Position, &attendee.IsStudent, &attendee.HasPaid, &attendee.HasAttended, &attendee.BillAmount, &attendee.BillCurrency) if err != nil { return nil, err @@ -528,12 +528,12 @@ func (r *PostgresTrainingDateAttendeeRepository) FindAllByTrainingDateID(trainin return attendees, nil } -func (r *PostgresTrainingDateAttendeeRepository) Update(attendee *TrainingDateAttendee) error { +func (r *PostgresAttendeeRepository) Update(attendee *Attendee) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() _, err := r.pg.Exec(ctx, ` - UPDATE training.date_attendees + UPDATE training.attendees SET name = $1, email = $2, phone = $3, company = $4, position = $5, is_student = $6, has_paid = $7, has_attended = $8, bill_amount = $9, bill_currency = $10 WHERE id = $10 `, attendee.Name, attendee.Email, attendee.Phone, attendee.Company, attendee.Position, attendee.IsStudent, attendee.HasPaid, attendee.HasAttended, attendee.BillAmount, attendee.BillCurrency, attendee.ID) @@ -544,11 +544,11 @@ func (r *PostgresTrainingDateAttendeeRepository) Update(attendee *TrainingDateAt return nil } -func (r *PostgresTrainingDateAttendeeRepository) Delete(id TrainingDateAttendeeID) error { +func (r *PostgresAttendeeRepository) Delete(id AttendeeID) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - _, err := r.pg.Exec(ctx, `DELETE FROM training.date_attendees WHERE id = $1`, id) + _, err := r.pg.Exec(ctx, `DELETE FROM training.attendees WHERE id = $1`, id) if err != nil { return err } diff --git a/pkg/training/repository.go b/pkg/training/repository.go index 0e78c35..00de3ef 100644 --- a/pkg/training/repository.go +++ b/pkg/training/repository.go @@ -1,28 +1,28 @@ package training -type TrainingRepository interface { +type Repository interface { Create(training *Training) error - FindByID(id TrainingID) (*Training, error) + FindByID(id ID) (*Training, error) FindAll() ([]Training, error) Update(training *Training) error - Delete(id TrainingID) error + Delete(id ID) error } -type TrainingDateRepository interface { - Create(trainingID TrainingID, trainingDate *TrainingDate) error - FindByID(id TrainingDateID) (*TrainingDate, error) - FindAll() ([]TrainingDate, error) - FindAllByTrainingID(trainingID TrainingID) ([]TrainingDate, error) - FindUpcomingByTrainingID(trainingID TrainingID) ([]TrainingDate, error) - Update(trainingDate *TrainingDate) error - Delete(id TrainingDateID) error +type DateRepository interface { + Create(trainingID ID, trainingDate *Date) error + FindByID(id DateID) (*Date, error) + FindAll() ([]Date, error) + FindAllByTrainingID(trainingID ID) ([]Date, error) + FindUpcomingByTrainingID(trainingID ID) ([]Date, error) + Update(trainingDate *Date) error + Delete(id DateID) error } -type TrainingDateAttendeeRepository interface { - Create(trainingDateID TrainingDateID, attendee *TrainingDateAttendee) error - FindByID(id TrainingDateAttendeeID) (*TrainingDateAttendee, error) - FindAll() ([]TrainingDateAttendee, error) - FindAllByTrainingDateID(trainingDateID TrainingDateID) ([]TrainingDateAttendee, error) - Update(attendee *TrainingDateAttendee) error - Delete(id TrainingDateAttendeeID) error +type AttendeeRepository interface { + Create(trainingDateID DateID, attendee *Attendee) error + FindByID(id AttendeeID) (*Attendee, error) + FindAll() ([]Attendee, error) + FindAllByTrainingDateID(trainingDateID DateID) ([]Attendee, error) + Update(attendee *Attendee) error + Delete(id AttendeeID) error }