hiling_go/db/sqlc/tx_location.go
2024-05-22 11:26:15 +07:00

61 lines
1.5 KiB
Go
Executable File

package db
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
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"`
GoogleMapsLink pgtype.Text `json:"google_maps_link"`
IsDeleted bool `json:"is_deleted"`
ApprovedBy pgtype.Int4 `json:"approved_by"`
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{
Thumbnail: pgtype.Text{Valid: true, String: arg.Thumbnail[0].ImageUrl},
ID: location_id,
})
if err != nil {
return err
}
}
return nil
})
return err
}