refactor: use serial as id instead of uuid
This commit is contained in:
parent
18b033add1
commit
370531a9a5
11 changed files with 319 additions and 381 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue