feat(locations): now working
This commit is contained in:
@ -33,11 +33,18 @@ func (client TestAiClient) GetImageInfo(imageName string, imageData []byte) (Ima
|
|||||||
func GetAiClient() (AiClient, error) {
|
func GetAiClient() (AiClient, error) {
|
||||||
mode := os.Getenv("MODE")
|
mode := os.Getenv("MODE")
|
||||||
if mode == "TESTING" {
|
if mode == "TESTING" {
|
||||||
|
address := "10 Downing Street"
|
||||||
|
|
||||||
return TestAiClient{
|
return TestAiClient{
|
||||||
ImageInfo: ImageInfo{
|
ImageInfo: ImageInfo{
|
||||||
Tags: []string{"tag"},
|
Tags: []string{"tag"},
|
||||||
Links: []string{"links"},
|
Links: []string{"links"},
|
||||||
Text: []string{"text"},
|
Text: []string{"text"},
|
||||||
|
Locations: []model.Locations{{
|
||||||
|
ID: uuid.Nil,
|
||||||
|
Name: "London",
|
||||||
|
Address: &address,
|
||||||
|
}},
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -63,6 +70,7 @@ func main() {
|
|||||||
linkModel := models.NewLinkModel(db)
|
linkModel := models.NewLinkModel(db)
|
||||||
tagModel := models.NewTagModel(db)
|
tagModel := models.NewTagModel(db)
|
||||||
textModel := models.NewTextModel(db)
|
textModel := models.NewTextModel(db)
|
||||||
|
locationModel := models.NewLocationModel(db)
|
||||||
userModel := models.NewUserModel(db)
|
userModel := models.NewUserModel(db)
|
||||||
|
|
||||||
listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) {
|
listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) {
|
||||||
@ -135,6 +143,13 @@ func main() {
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = locationModel.SaveToImage(ctx, userImage.ImageID, imageInfo.Locations)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to save location")
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,27 +3,76 @@ package models
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log"
|
||||||
"screenmark/screenmark/.gen/haystack/haystack/model"
|
"screenmark/screenmark/.gen/haystack/haystack/model"
|
||||||
. "screenmark/screenmark/.gen/haystack/haystack/table"
|
. "screenmark/screenmark/.gen/haystack/haystack/table"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LocationModel struct {
|
type LocationModel struct {
|
||||||
dbPool *sql.DB
|
dbPool *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m LocationModel) Save(ctx context.Context, location model.Locations) (model.Locations, error) {
|
// This looks stupid
|
||||||
|
func getValues(location model.Locations) []any {
|
||||||
|
arr := make([]any, 0)
|
||||||
|
|
||||||
|
if location.Address != nil {
|
||||||
|
arr = append(arr, *location.Address)
|
||||||
|
} else {
|
||||||
|
arr = append(arr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
if location.Coordinates != nil {
|
||||||
|
arr = append(arr, *location.Coordinates)
|
||||||
|
} else {
|
||||||
|
arr = append(arr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
if location.Description != nil {
|
||||||
|
arr = append(arr, *location.Description)
|
||||||
|
} else {
|
||||||
|
arr = append(arr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m LocationModel) Save(ctx context.Context, locations []model.Locations) (model.Locations, error) {
|
||||||
insertLocationStmt := Locations.
|
insertLocationStmt := Locations.
|
||||||
INSERT(Locations.Name, Locations.Address, Locations.Coordinates, Locations.Description).
|
INSERT(Locations.Name, Locations.Address, Locations.Coordinates, Locations.Description)
|
||||||
VALUES(location.Name, location.Address, location.Coordinates, location.Description).
|
|
||||||
RETURNING(Locations.AllColumns)
|
for _, location := range locations {
|
||||||
|
insertLocationStmt = insertLocationStmt.VALUES(location.Name, getValues(location)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
insertLocationStmt = insertLocationStmt.RETURNING(Locations.AllColumns)
|
||||||
|
|
||||||
|
log.Println(insertLocationStmt.DebugSql())
|
||||||
|
|
||||||
insertedLocation := model.Locations{}
|
insertedLocation := model.Locations{}
|
||||||
|
err := insertLocationStmt.QueryContext(ctx, m.dbPool, &insertedLocation)
|
||||||
err := insertLocationStmt.QueryContext(ctx, m.dbPool, &insertLocationStmt)
|
|
||||||
|
|
||||||
return insertedLocation, err
|
return insertedLocation, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m LocationModel) SaveToImage(ctx context.Context, imageId uuid.UUID, locations []model.Locations) error {
|
||||||
|
location, err := m.Save(ctx, locations)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
insertImageLocationStmt := ImageLocations.
|
||||||
|
INSERT(ImageLocations.ImageID, ImageLocations.LocationID).
|
||||||
|
VALUES(imageId, location.ID)
|
||||||
|
|
||||||
|
_, err = insertImageLocationStmt.ExecContext(ctx, m.dbPool)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func NewLocationModel(db *sql.DB) LocationModel {
|
func NewLocationModel(db *sql.DB) LocationModel {
|
||||||
return LocationModel{dbPool: db}
|
return LocationModel{dbPool: db}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
. "screenmark/screenmark/.gen/haystack/haystack/table"
|
. "screenmark/screenmark/.gen/haystack/haystack/table"
|
||||||
|
|
||||||
. "github.com/go-jet/jet/v2/postgres"
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,12 +11,15 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"screenmark/screenmark/.gen/haystack/haystack/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImageInfo struct {
|
type ImageInfo struct {
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Text []string `json:"text"`
|
Text []string `json:"text"`
|
||||||
Links []string `json:"links"`
|
Links []string `json:"links"`
|
||||||
|
|
||||||
|
Locations []model.Locations `json:"locations"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseFormat struct {
|
type ResponseFormat struct {
|
||||||
|
@ -63,7 +63,7 @@ CREATE TABLE haystack.locations (
|
|||||||
|
|
||||||
CREATE TABLE haystack.image_locations (
|
CREATE TABLE haystack.image_locations (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
location_id UUID NOT NULL REFERENCES haystack.image_locations (id),
|
location_id UUID NOT NULL REFERENCES haystack.locations (id),
|
||||||
image_id UUID NOT NULL REFERENCES haystack.image (id)
|
image_id UUID NOT NULL REFERENCES haystack.image (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user