package db import ( "context" "encoding/json" "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"` Amenities pgtype.Text `json:"amenities"` RestaurantMenu pgtype.Text `json:"restaurant_menu"` 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, Amenities: arg.Amenities, }) if arg.LocationType == LocationTypeCulinary && arg.RestaurantMenu.Valid { type menuItemInput struct { Name string `json:"name"` Price int32 `json:"price"` Category string `json:"category"` Description string `json:"description"` } var items []menuItemInput if err := json.Unmarshal([]byte(arg.RestaurantMenu.String), &items); err != nil { return err } for _, item := range items { _, err := q.CreateMenuItems(ctx, CreateMenuItemsParams{ LocationID: location_id, Name: item.Name, Price: item.Price, Category: item.Category, Description: item.Description, IsAvailable: true, SubmittedBy: arg.SubmittedBy, }) if err != nil { return err } } } 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 }