60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"database/sql"
|
||
|
)
|
||
|
|
||
|
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 sql.NullString `json:"google_maps_link"`
|
||
|
IsDeleted bool `json:"is_deleted"`
|
||
|
ApprovedBy sql.NullInt32 `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: sql.NullString{Valid: true, String: arg.Thumbnail[0].ImageUrl},
|
||
|
ID: location_id,
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
return err
|
||
|
}
|