feat(tags): creating and getting user tags
This commit is contained in:
@ -13,6 +13,6 @@ import (
|
|||||||
|
|
||||||
type ImageTags struct {
|
type ImageTags struct {
|
||||||
ID uuid.UUID `sql:"primary_key"`
|
ID uuid.UUID `sql:"primary_key"`
|
||||||
Tag string
|
TagID uuid.UUID
|
||||||
ImageID uuid.UUID
|
ImageID uuid.UUID
|
||||||
}
|
}
|
||||||
|
18
backend/.gen/haystack/haystack/model/user_tags.go
Normal file
18
backend/.gen/haystack/haystack/model/user_tags.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// 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
|
||||||
|
}
|
@ -18,7 +18,7 @@ type imageTagsTable struct {
|
|||||||
|
|
||||||
// Columns
|
// Columns
|
||||||
ID postgres.ColumnString
|
ID postgres.ColumnString
|
||||||
Tag postgres.ColumnString
|
TagID postgres.ColumnString
|
||||||
ImageID postgres.ColumnString
|
ImageID postgres.ColumnString
|
||||||
|
|
||||||
AllColumns postgres.ColumnList
|
AllColumns postgres.ColumnList
|
||||||
@ -61,10 +61,10 @@ func newImageTagsTable(schemaName, tableName, alias string) *ImageTagsTable {
|
|||||||
func newImageTagsTableImpl(schemaName, tableName, alias string) imageTagsTable {
|
func newImageTagsTableImpl(schemaName, tableName, alias string) imageTagsTable {
|
||||||
var (
|
var (
|
||||||
IDColumn = postgres.StringColumn("id")
|
IDColumn = postgres.StringColumn("id")
|
||||||
TagColumn = postgres.StringColumn("tag")
|
TagIDColumn = postgres.StringColumn("tag_id")
|
||||||
ImageIDColumn = postgres.StringColumn("image_id")
|
ImageIDColumn = postgres.StringColumn("image_id")
|
||||||
allColumns = postgres.ColumnList{IDColumn, TagColumn, ImageIDColumn}
|
allColumns = postgres.ColumnList{IDColumn, TagIDColumn, ImageIDColumn}
|
||||||
mutableColumns = postgres.ColumnList{TagColumn, ImageIDColumn}
|
mutableColumns = postgres.ColumnList{TagIDColumn, ImageIDColumn}
|
||||||
)
|
)
|
||||||
|
|
||||||
return imageTagsTable{
|
return imageTagsTable{
|
||||||
@ -72,7 +72,7 @@ func newImageTagsTableImpl(schemaName, tableName, alias string) imageTagsTable {
|
|||||||
|
|
||||||
//Columns
|
//Columns
|
||||||
ID: IDColumn,
|
ID: IDColumn,
|
||||||
Tag: TagColumn,
|
TagID: TagIDColumn,
|
||||||
ImageID: ImageIDColumn,
|
ImageID: ImageIDColumn,
|
||||||
|
|
||||||
AllColumns: allColumns,
|
AllColumns: allColumns,
|
||||||
|
@ -16,5 +16,6 @@ func UseSchema(schema string) {
|
|||||||
ImageText = ImageText.FromSchema(schema)
|
ImageText = ImageText.FromSchema(schema)
|
||||||
UserImages = UserImages.FromSchema(schema)
|
UserImages = UserImages.FromSchema(schema)
|
||||||
UserImagesToProcess = UserImagesToProcess.FromSchema(schema)
|
UserImagesToProcess = UserImagesToProcess.FromSchema(schema)
|
||||||
|
UserTags = UserTags.FromSchema(schema)
|
||||||
Users = Users.FromSchema(schema)
|
Users = Users.FromSchema(schema)
|
||||||
}
|
}
|
||||||
|
81
backend/.gen/haystack/haystack/table/user_tags.go
Normal file
81
backend/.gen/haystack/haystack/table/user_tags.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
//
|
||||||
|
// 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,
|
||||||
|
}
|
||||||
|
}
|
78
backend/models/tags.go
Normal file
78
backend/models/tags.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"screenmark/screenmark/.gen/haystack/haystack/model"
|
||||||
|
. "screenmark/screenmark/.gen/haystack/haystack/table"
|
||||||
|
|
||||||
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Raw dogging SQL is kinda based though?
|
||||||
|
//
|
||||||
|
// | nO, usE OrM!!
|
||||||
|
//
|
||||||
|
// | RAW - RAW
|
||||||
|
// | SQL | \ SQL
|
||||||
|
// | GOOD | \ GOOD
|
||||||
|
// | - -
|
||||||
|
// | -- --
|
||||||
|
// | -- --
|
||||||
|
// | ---- IQ ----
|
||||||
|
func getNonExistantTags(userId uuid.UUID, tags []string) ([]string, error) {
|
||||||
|
stmt, err := db.Prepare(`WITH given_tags
|
||||||
|
AS (SELECT given_tags.tag FROM (VALUES ('a'), ('b'), ('c')) AS given_tags (tag))
|
||||||
|
SELECT given_tags.tag
|
||||||
|
FROM given_tags
|
||||||
|
LEFT OUTER JOIN haystack.user_tags ON haystack.user_tags.tag = a.tag
|
||||||
|
where user_tags.tag is null;`)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
rows, err := stmt.Query()
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
nonExistantTags := make([]string, 0)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var tag string
|
||||||
|
rows.Scan(&tag)
|
||||||
|
|
||||||
|
nonExistantTags = append(nonExistantTags, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nonExistantTags, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateTags(userId uuid.UUID, tags []string) error {
|
||||||
|
tagsToInsert, err := getNonExistantTags(userId, tags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt := UserTags.INSERT(UserTags.UserID, UserTags.Tag)
|
||||||
|
|
||||||
|
for _, tag := range tagsToInsert {
|
||||||
|
stmt = stmt.VALUES(UUID(userId), tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = stmt.Exec(db)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTags(userId uuid.UUID) ([]model.UserTags, error) {
|
||||||
|
stmt := UserTags.SELECT(UserTags.AllColumns).WHERE(UserTags.UserID.EQ(UUID(userId)))
|
||||||
|
|
||||||
|
userTags := []model.UserTags{}
|
||||||
|
|
||||||
|
err := stmt.Query(db, &userTags)
|
||||||
|
|
||||||
|
return userTags, err
|
||||||
|
}
|
@ -31,7 +31,7 @@ CREATE TABLE haystack.user_images (
|
|||||||
|
|
||||||
CREATE TABLE haystack.user_tags (
|
CREATE TABLE haystack.user_tags (
|
||||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
tag VARCHAR(32) NOT NULL,
|
tag VARCHAR(32) UNIQUE NOT NULL,
|
||||||
user_id uuid NOT NULL REFERENCES haystack.users (id)
|
user_id uuid NOT NULL REFERENCES haystack.users (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user