package models import ( "context" "database/sql" "screenmark/screenmark/.gen/haystack/haystack/model" . "screenmark/screenmark/.gen/haystack/haystack/table" . "github.com/go-jet/jet/v2/postgres" "github.com/google/uuid" ) type LocationModel struct { dbPool *sql.DB } func (m LocationModel) List(ctx context.Context, userId uuid.UUID) ([]model.Locations, error) { listLocationsStmt := SELECT(Locations.AllColumns). FROM( Locations. INNER_JOIN(UserLocations, UserLocations.LocationID.EQ(Locations.ID)), ). WHERE(UserLocations.UserID.EQ(UUID(userId))) locations := []model.Locations{} err := listLocationsStmt.QueryContext(ctx, m.dbPool, &locations) return locations, err } func (m LocationModel) Save(ctx context.Context, userId uuid.UUID, location model.Locations) (model.Locations, error) { insertLocationStmt := Locations. INSERT(Locations.Name, Locations.Address, Locations.Description). VALUES(location.Name, location.Address, location.Description). RETURNING(Locations.AllColumns) insertedLocation := model.Locations{} err := insertLocationStmt.QueryContext(ctx, m.dbPool, &insertedLocation) if err != nil { return model.Locations{}, err } insertUserLocationStmt := UserLocations. INSERT(UserLocations.UserID, UserLocations.LocationID). VALUES(userId, insertedLocation.ID) _, err = insertUserLocationStmt.ExecContext(ctx, m.dbPool) return insertedLocation, err } func (m LocationModel) SaveToImage(ctx context.Context, imageId uuid.UUID, locationId uuid.UUID) (model.ImageLocations, error) { insertImageLocationStmt := ImageLocations. INSERT(ImageLocations.ImageID, ImageLocations.LocationID). VALUES(imageId, locationId). RETURNING(ImageLocations.AllColumns) imageLocation := model.ImageLocations{} err := insertImageLocationStmt.QueryContext(ctx, m.dbPool, &imageLocation) return imageLocation, err } func NewLocationModel(db *sql.DB) LocationModel { return LocationModel{dbPool: db} }