hiling_go/db/sqlc/tx_location.go

61 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-10-03 19:44:31 +07:00
package db
import (
"context"
2024-02-06 11:55:25 +07:00
"github.com/jackc/pgx/v5/pgtype"
2023-10-03 19:44:31 +07:00
)
type CreateLocationTxParams struct {
Address string `json:"address"`
Name string `json:"name"`
SubmittedBy int32 `json:"submitted_by"`
LocationType LocationType `json:"location_type"`
RegencyID int16 `json:"regency_id"`
2024-02-06 11:55:25 +07:00
GoogleMapsLink pgtype.Text `json:"google_maps_link"`
2023-10-03 19:44:31 +07:00
IsDeleted bool `json:"is_deleted"`
2024-03-04 16:09:55 +07:00
ApprovedBy pgtype.Int4 `json:"approved_by"`
2023-10-03 19:44:31 +07:00
Thumbnail []CreateImageParams `json:"thumbnails"`
}
func (store *SQLStore) CreateLocationTx(ctx context.Context, arg CreateLocationTxParams) error {
err := store.execTx(ctx, func(q *Queries) error {
var err error
location_id, err := q.CreateLocation(ctx, CreateLocationParams{
Address: arg.Address,
Name: arg.Name,
SubmittedBy: arg.SubmittedBy,
LocationType: arg.LocationType,
RegencyID: arg.RegencyID,
GoogleMapsLink: arg.GoogleMapsLink,
IsDeleted: arg.IsDeleted,
ApprovedBy: arg.ApprovedBy,
})
if err != nil {
return err
}
if len(arg.Thumbnail) > 0 {
err := q.CreateImage(ctx, arg.Thumbnail)
if err != nil {
return err
}
err = q.UpdateLocationThumbnail(ctx, UpdateLocationThumbnailParams{
2024-02-06 11:55:25 +07:00
Thumbnail: pgtype.Text{Valid: true, String: arg.Thumbnail[0].ImageUrl},
2023-10-03 19:44:31 +07:00
ID: location_id,
})
if err != nil {
return err
}
}
return nil
})
return err
}