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
|
|
@ -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