1
0
Fork 0

feat: add postgres repository for training

This commit is contained in:
Vojtěch Mareš 2024-06-23 10:35:38 +02:00
parent 8c277ef692
commit ff7e320481
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
19 changed files with 1277 additions and 414 deletions

View file

@ -238,8 +238,8 @@ func (h *APIHandlers) ListTrainingDates(ctx context.Context, req ListTrainingDat
Address: td.Address,
Capacity: td.Capacity,
Price: Price{
Amount: td.Price.Amount.String(),
Currency: td.Price.Currency,
Amount: td.PriceAmount.String(),
Currency: td.PriceCurrency,
},
}
}
@ -258,7 +258,7 @@ func (h *APIHandlers) CreateTrainingDate(ctx context.Context, req CreateTraining
}}, nil
}
price := money.Price{
price := money.Money{
Amount: amount,
Currency: req.Body.Price.Currency,
}
@ -274,14 +274,15 @@ func (h *APIHandlers) CreateTrainingDate(ctx context.Context, req CreateTraining
}
td := training.TrainingDate{
Date: req.Body.Date.Time,
StartTime: startTime,
Days: req.Body.Days,
IsOnline: req.Body.IsOnline,
Location: req.Body.Location,
Address: req.Body.Address,
Capacity: req.Body.Capacity,
Price: price,
Date: req.Body.Date.Time,
StartTime: startTime,
Days: req.Body.Days,
IsOnline: req.Body.IsOnline,
Location: req.Body.Location,
Address: req.Body.Address,
Capacity: req.Body.Capacity,
PriceAmount: price.Amount,
PriceCurrency: price.Currency,
}
err = h.trainingDateRepository.Create(req.TrainingID, &td)
@ -304,8 +305,8 @@ func (h *APIHandlers) CreateTrainingDate(ctx context.Context, req CreateTraining
Address: td.Address,
Capacity: td.Capacity,
Price: Price{
Amount: td.Price.Amount.String(),
Currency: td.Price.Currency,
Amount: td.PriceAmount.String(),
Currency: td.PriceCurrency,
},
}, nil
}
@ -357,8 +358,8 @@ func (h *APIHandlers) GetTrainingDate(ctx context.Context, req GetTrainingDateRe
Address: td.Address,
Capacity: td.Capacity,
Price: Price{
Amount: td.Price.Amount.String(),
Currency: td.Price.Currency,
Amount: td.PriceAmount.String(),
Currency: td.PriceCurrency,
},
}, nil
}
@ -374,7 +375,7 @@ func (h *APIHandlers) UpdateTrainingDate(ctx context.Context, req UpdateTraining
}}, nil
}
price := money.Price{
price := money.Money{
Amount: amount,
Currency: req.Body.Price.Currency,
}
@ -390,15 +391,16 @@ func (h *APIHandlers) UpdateTrainingDate(ctx context.Context, req UpdateTraining
}
td := training.TrainingDate{
ID: req.TrainingDateID,
Date: req.Body.Date.Time,
StartTime: startTime,
Days: req.Body.Days,
IsOnline: req.Body.IsOnline,
Location: req.Body.Location,
Address: req.Body.Address,
Capacity: req.Body.Capacity,
Price: price,
ID: req.TrainingDateID,
Date: req.Body.Date.Time,
StartTime: startTime,
Days: req.Body.Days,
IsOnline: req.Body.IsOnline,
Location: req.Body.Location,
Address: req.Body.Address,
Capacity: req.Body.Capacity,
PriceAmount: price.Amount,
PriceCurrency: price.Currency,
}
err = h.trainingDateRepository.Update(&td)
@ -421,8 +423,8 @@ func (h *APIHandlers) UpdateTrainingDate(ctx context.Context, req UpdateTraining
Address: td.Address,
Capacity: td.Capacity,
Price: Price{
Amount: td.Price.Amount.String(),
Currency: td.Price.Currency,
Amount: td.PriceAmount.String(),
Currency: td.PriceCurrency,
},
}, nil
}
@ -463,8 +465,8 @@ func (h *APIHandlers) ListAllUpcomingTrainingDates(ctx context.Context, req List
Address: td.Address,
Capacity: td.Capacity,
Price: Price{
Amount: td.Price.Amount.String(),
Currency: td.Price.Currency,
Amount: td.PriceAmount.String(),
Currency: td.PriceCurrency,
},
}
}
@ -496,8 +498,8 @@ func (h *APIHandlers) ListTrainingUpcomingDates(ctx context.Context, req ListTra
Address: td.Address,
Capacity: td.Capacity,
Price: Price{
Amount: td.Price.Amount.String(),
Currency: td.Price.Currency,
Amount: td.PriceAmount.String(),
Currency: td.PriceCurrency,
},
}
}
@ -529,8 +531,8 @@ func (h *APIHandlers) ListTrainingDateAttendees(ctx context.Context, req ListTra
HasPaid: a.HasPaid,
HasAttended: a.HasAttended,
Bill: Price{
Amount: a.Bill.Amount.String(),
Currency: a.Bill.Currency,
Amount: a.BillAmount.String(),
Currency: a.BillCurrency,
},
}
}
@ -576,15 +578,16 @@ func (h *APIHandlers) CreateTrainingDateAttendee(ctx context.Context, req Create
}
ta := training.TrainingDateAttendee{
Name: req.Body.Name,
Email: string(req.Body.Email),
Phone: req.Body.Phone,
Company: req.Body.Company,
Position: req.Body.Position,
IsStudent: *req.Body.IsStudent,
HasPaid: false,
HasAttended: false,
Bill: td.Price,
Name: req.Body.Name,
Email: string(req.Body.Email),
Phone: req.Body.Phone,
Company: req.Body.Company,
Position: req.Body.Position,
IsStudent: *req.Body.IsStudent,
HasPaid: false,
HasAttended: false,
BillAmount: td.PriceAmount,
BillCurrency: td.PriceCurrency,
}
err = h.trainingDateAttendeeRepository.Create(req.TrainingDateID, &ta)
@ -608,8 +611,8 @@ func (h *APIHandlers) CreateTrainingDateAttendee(ctx context.Context, req Create
HasAttended: ta.HasAttended,
HasPaid: ta.HasPaid,
Bill: Price{
Amount: ta.Bill.Amount.String(),
Currency: ta.Bill.Currency,
Amount: ta.BillAmount.String(),
Currency: ta.BillCurrency,
},
}, nil
}
@ -714,8 +717,8 @@ func (h *APIHandlers) GetTrainingDateAttendee(ctx context.Context, req GetTraini
HasAttended: ta.HasAttended,
HasPaid: ta.HasPaid,
Bill: Price{
Amount: ta.Bill.Amount.String(),
Currency: ta.Bill.Currency,
Amount: ta.BillAmount.String(),
Currency: ta.BillCurrency,
},
}, nil
}
@ -803,8 +806,8 @@ func (h *APIHandlers) UpdateTrainingDateAttendee(ctx context.Context, req Update
HasAttended: ta.HasAttended,
HasPaid: ta.HasPaid,
Bill: Price{
Amount: ta.Bill.Amount.String(),
Currency: ta.Bill.Currency,
Amount: ta.BillAmount.String(),
Currency: ta.BillCurrency,
},
}, nil
}
@ -887,8 +890,8 @@ func (h *APIHandlers) UpdateTrainingDateAttendeePayment(ctx context.Context, req
HasAttended: ta.HasAttended,
HasPaid: ta.HasPaid,
Bill: Price{
Amount: ta.Bill.Amount.String(),
Currency: ta.Bill.Currency,
Amount: ta.BillAmount.String(),
Currency: ta.BillCurrency,
},
}, nil
}
@ -971,8 +974,8 @@ func (h *APIHandlers) UpdateTrainingDateAttendeeAttendance(ctx context.Context,
HasAttended: ta.HasAttended,
HasPaid: ta.HasPaid,
Bill: Price{
Amount: ta.Bill.Amount.String(),
Currency: ta.Bill.Currency,
Amount: ta.BillAmount.String(),
Currency: ta.BillCurrency,
},
}, nil
}

View file

@ -3,6 +3,7 @@ package server
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
"time"
fiberzap "github.com/gofiber/contrib/fiberzap/v2"
@ -16,13 +17,15 @@ type Server struct {
port int
logger *zap.Logger
apiHandlers *APIHandlers
pool *pgxpool.Pool
}
func NewServer(apiHandlers *APIHandlers, port int, logger *zap.Logger) *Server {
func NewServer(apiHandlers *APIHandlers, port int, logger *zap.Logger, pool *pgxpool.Pool) *Server {
return &Server{
apiHandlers: apiHandlers,
port: port,
logger: logger,
pool: pool,
}
}
@ -54,6 +57,11 @@ func (s *Server) Run(ctx context.Context) {
panic(err)
}
// gracefully shutdown/close database pool
if s.pool != nil {
s.pool.Close()
}
s.logger.Info("HTTP server shut down gracefully.", zap.Duration("duration", time.Since(shutdownBegan)))
}

View file

@ -338,7 +338,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -384,7 +384,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -447,7 +447,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -510,7 +510,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -584,7 +584,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -599,7 +599,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -614,7 +614,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -663,7 +663,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -705,7 +705,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -765,7 +765,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -782,7 +782,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},
@ -842,7 +842,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -856,7 +856,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},
@ -909,7 +909,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -923,7 +923,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},
@ -986,7 +986,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -1000,7 +1000,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},
@ -1046,7 +1046,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -1064,7 +1064,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},
@ -1123,7 +1123,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -1141,7 +1141,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},
@ -1200,7 +1200,7 @@ func TestServer(t *testing.T) {
IsOnline: false,
Location: "NYC",
StartTime: date,
Price: money.Price{
Price: money.Money{
Amount: decimal.NewFromInt(200),
Currency: "EUR",
},
@ -1216,7 +1216,7 @@ func TestServer(t *testing.T) {
Position: "Software Engineer",
Phone: "+420 123 456 789",
IsStudent: false,
Bill: money.Price{
Bill: money.Money{
Amount: td.Price.Amount,
Currency: td.Price.Currency,
},