feat: saving AI information to database

This commit is contained in:
2025-02-24 21:00:05 +00:00
parent ee0587a16b
commit b99432c202
8 changed files with 80 additions and 118 deletions

View File

@ -12,6 +12,7 @@ import (
)
type ImageTags struct {
TagID uuid.UUID
ID uuid.UUID `sql:"primary_key"`
Tag string
ImageID uuid.UUID
}

View File

@ -1,18 +0,0 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package model
import (
"github.com/google/uuid"
)
type UserTags struct {
ID uuid.UUID `sql:"primary_key"`
Tag string
UserID uuid.UUID
}

View File

@ -17,7 +17,8 @@ type imageTagsTable struct {
postgres.Table
// Columns
TagID postgres.ColumnString
ID postgres.ColumnString
Tag postgres.ColumnString
ImageID postgres.ColumnString
AllColumns postgres.ColumnList
@ -59,17 +60,19 @@ func newImageTagsTable(schemaName, tableName, alias string) *ImageTagsTable {
func newImageTagsTableImpl(schemaName, tableName, alias string) imageTagsTable {
var (
TagIDColumn = postgres.StringColumn("tag_id")
IDColumn = postgres.StringColumn("id")
TagColumn = postgres.StringColumn("tag")
ImageIDColumn = postgres.StringColumn("image_id")
allColumns = postgres.ColumnList{TagIDColumn, ImageIDColumn}
mutableColumns = postgres.ColumnList{TagIDColumn, ImageIDColumn}
allColumns = postgres.ColumnList{IDColumn, TagColumn, ImageIDColumn}
mutableColumns = postgres.ColumnList{TagColumn, ImageIDColumn}
)
return imageTagsTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
TagID: TagIDColumn,
ID: IDColumn,
Tag: TagColumn,
ImageID: ImageIDColumn,
AllColumns: allColumns,

View File

@ -14,6 +14,5 @@ func UseSchema(schema string) {
ImageTags = ImageTags.FromSchema(schema)
ImageText = ImageText.FromSchema(schema)
UserImages = UserImages.FromSchema(schema)
UserTags = UserTags.FromSchema(schema)
Users = Users.FromSchema(schema)
}

View File

@ -1,81 +0,0 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package table
import (
"github.com/go-jet/jet/v2/postgres"
)
var UserTags = newUserTagsTable("haystack", "user_tags", "")
type userTagsTable struct {
postgres.Table
// Columns
ID postgres.ColumnString
Tag postgres.ColumnString
UserID postgres.ColumnString
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
}
type UserTagsTable struct {
userTagsTable
EXCLUDED userTagsTable
}
// AS creates new UserTagsTable with assigned alias
func (a UserTagsTable) AS(alias string) *UserTagsTable {
return newUserTagsTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new UserTagsTable with assigned schema name
func (a UserTagsTable) FromSchema(schemaName string) *UserTagsTable {
return newUserTagsTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new UserTagsTable with assigned table prefix
func (a UserTagsTable) WithPrefix(prefix string) *UserTagsTable {
return newUserTagsTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new UserTagsTable with assigned table suffix
func (a UserTagsTable) WithSuffix(suffix string) *UserTagsTable {
return newUserTagsTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newUserTagsTable(schemaName, tableName, alias string) *UserTagsTable {
return &UserTagsTable{
userTagsTable: newUserTagsTableImpl(schemaName, tableName, alias),
EXCLUDED: newUserTagsTableImpl("", "excluded", ""),
}
}
func newUserTagsTableImpl(schemaName, tableName, alias string) userTagsTable {
var (
IDColumn = postgres.StringColumn("id")
TagColumn = postgres.StringColumn("tag")
UserIDColumn = postgres.StringColumn("user_id")
allColumns = postgres.ColumnList{IDColumn, TagColumn, UserIDColumn}
mutableColumns = postgres.ColumnList{TagColumn, UserIDColumn}
)
return userTagsTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
ID: IDColumn,
Tag: TagColumn,
UserID: UserIDColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
}
}

View File

@ -61,7 +61,11 @@ func main() {
return
}
log.Printf("Info: %+v\n", imageInfo)
log.Println("Finished processing image " + parameters.Extra)
models.SaveImageTags(parameters.Extra, imageInfo.Tags)
models.SaveImageLinks(parameters.Extra, imageInfo.Links)
models.SaveImageTexts(parameters.Extra, imageInfo.Tags)
}()
}
}()
@ -155,10 +159,10 @@ func main() {
return
}
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, string(jsonUserImage))
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
})
log.Println("Listening and serving on port 3040.")

View File

@ -34,12 +34,71 @@ func GetImage(imageId string) (model.UserImages, error) {
return images[0], err
}
func GetUserImages(userId string) ([]model.UserImages, error) {
type UserImagesWithInfo struct {
model.UserImages
Tags []model.ImageTags
Links []model.ImageLinks
Text []model.ImageText
}
func GetUserImages(userId string) ([]UserImagesWithInfo, error) {
id := uuid.MustParse(userId)
stmt := UserImages.SELECT(UserImages.ID, UserImages.ImageName).WHERE(UserImages.UserID.EQ(UUID(id)))
images := []model.UserImages{}
images := []UserImagesWithInfo{}
err := stmt.Query(db, &images)
return images, err
}
func SaveImageTags(imageId string, tags []string) ([]model.ImageTags, error) {
id := uuid.MustParse(imageId)
stmt := ImageTags.INSERT(ImageTags.ImageID, ImageTags.Tag)
for _, t := range tags {
stmt = stmt.VALUES(id, t)
}
stmt.RETURNING(ImageTags.AllColumns)
imageTags := []model.ImageTags{}
err := stmt.Query(db, &imageTags)
return imageTags, err
}
func SaveImageLinks(imageId string, links []string) ([]model.ImageLinks, error) {
id := uuid.MustParse(imageId)
stmt := ImageTags.INSERT(ImageLinks.ImageID, ImageLinks.Link)
for _, t := range links {
stmt = stmt.VALUES(id, t)
}
stmt.RETURNING(ImageLinks.AllColumns)
imageLinks := []model.ImageLinks{}
err := stmt.Query(db, &imageLinks)
return imageLinks, err
}
func SaveImageTexts(imageId string, texts []string) ([]model.ImageText, error) {
id := uuid.MustParse(imageId)
stmt := ImageTags.INSERT(ImageText.ImageID, ImageText.ImageText)
for _, t := range texts {
stmt = stmt.VALUES(id, t)
}
stmt.RETURNING(ImageText.AllColumns)
imageTags := []model.ImageText{}
err := stmt.Query(db, &imageTags)
return imageTags, err
}

View File

@ -18,14 +18,9 @@ CREATE TABLE haystack.user_images (
user_id uuid NOT NULL REFERENCES haystack.users (id)
);
CREATE TABLE haystack.user_tags (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
tag TEXT NOT NULL,
user_id uuid NOT NULL REFERENCES haystack.users (id)
);
CREATE TABLE haystack.image_tags (
tag_id UUID NOT NULL REFERENCES haystack.user_tags (id),
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
tag TEXT NOT NULL,
image_id UUID NOT NULL REFERENCES haystack.user_images (id)
);