Skip to content

Commit

Permalink
Final with http app on golang
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Nov 16, 2024
1 parent 7005648 commit 31a3817
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
15 changes: 7 additions & 8 deletions examples/golang/cource/06h_http/internals/app/db/cars_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type userCar struct {
UserId int64 `db:"userid"`
Name string
Rank string
CarId int64 `db:"carid"`
CarId int64 `db:"id"`
Brand string
Colour string
LicensePlate string
Expand All @@ -46,7 +46,7 @@ func NewCarsStorage(pool *pgxpool.Pool) *CarsStorage {
}

func (storage *CarsStorage) GetCarsList(userIdFilter int64, brandFilter string, colourFilter string, licenseFilter string) []models.Car {
query := "SELECT users.id AS userid, users.name, users.rank, c.id as carid, c.brand, c.colour, c.licens_plate " +
query := "SELECT users.id AS userid, users.name, users.rank, c.id as carid, c.brand, c.colour, c.license_plate " +
"FROM users JOIN cars c on users.id = c.user_id WHERE 1=1"
placeholderNum := 1
args := make([]interface{}, 0)
Expand Down Expand Up @@ -104,40 +104,39 @@ func (storage *CarsStorage) CreateCar(car models.Car) error {
ctx := context.Background()
tx, err := storage.databasePool.Begin(ctx)
defer func() {
err = tx.Rollback(ctx)
err = tx.Rollback(context.Background())
if err != nil {
log.Errorln(err)
}
}()

query := "SELECT id FROM users WHERE id = $1"
id := -1

err = pgxscan.Get(ctx, tx, &id, query, car.Owner.Id)
if err != nil {
log.Errorln(err)
err = tx.Rollback(ctx)
err = tx.Rollback(context.Background())
if err != nil {
log.Errorln(err)
}
return err
}

if id == -1 {
return errors.New("User not found.")
return errors.New("user not found")
}

insertQuery := "INSERT INTO cars(user_id, colour, brand, license_plate) VALUES ($1, $2, $3, $4)"

_, err = tx.Exec(ctx, insertQuery, car.Owner.Id, car.Colour, car.Brand, car.LicensePlate)
if err != nil {
log.Errorln(err)
err = tx.Rollback(ctx)
err = tx.Rollback(context.Background())
if err != nil {
log.Errorln(err)
}
}
err = tx.Commit(ctx)
err = tx.Commit(context.Background())
if err != nil {
log.Errorln(err)
}
Expand Down
5 changes: 2 additions & 3 deletions examples/golang/cource/06h_http/internals/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (server *AppServer) Serve() {
}
defer database.Close()

carsStorage := db.NewCarsStorage(server.db)
usersStorage := db.NewUsersStorage(server.db)
carsStorage := db.NewCarsStorage(database)
usersStorage := db.NewUsersStorage(database)

carsProcessor := processors.NewCarsProcessor(carsStorage)
usersProcessor := processors.NewUsersProcessor(usersStorage)
Expand All @@ -62,7 +62,6 @@ func (server *AppServer) Serve() {
if err != nil {
log.Fatalln(err)
}

return
}

Expand Down

0 comments on commit 31a3817

Please sign in to comment.