feat: update

This commit is contained in:
Rogee
2025-05-23 21:43:49 +08:00
parent f13ef4388e
commit 409a2e8304
9 changed files with 21 additions and 126 deletions

View File

@@ -75,7 +75,7 @@ func (up *uploads) PostUploadedAction(ctx fiber.Ctx, body *PostUploadedForm) err
Hash: body.Md5, Hash: body.Md5,
Path: filepath.Join(UPLOAD_PATH, body.Md5+filepath.Ext(body.OriginalName)), Path: filepath.Join(UPLOAD_PATH, body.Md5+filepath.Ext(body.OriginalName)),
} }
if err := model.MediasModel().Create(ctx.Context(), m); err != nil { if err := m.Create(ctx.Context()); err != nil {
return err return err
} }

View File

@@ -89,12 +89,11 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx
// create a new media record for the image // create a new media record for the image
imageMedia := &model.Medias{ imageMedia := &model.Medias{
CreatedAt: time.Now(), Name: name,
Name: name, MimeType: "image/jpeg",
MimeType: "image/jpeg", Size: fileSize,
Size: fileSize, Path: w.oss.GetSavePath(filename),
Path: w.oss.GetSavePath(filename), Hash: fileMd5,
Hash: fileMd5,
Metas: fields.ToJson(fields.MediaMetas{ Metas: fields.ToJson(fields.MediaMetas{
ParentHash: media.Hash, ParentHash: media.Hash,
}), }),
@@ -110,7 +109,7 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx
log.Errorf("Error removing original file: %v", err) log.Errorf("Error removing original file: %v", err)
} }
if err := model.MediasModel().Create(ctx, imageMedia); err != nil { if err := imageMedia.Create(ctx); err != nil {
log.Errorf("Error creating media record: %v", err) log.Errorf("Error creating media record: %v", err)
return errors.Wrap(err, "failed to create media record") return errors.Wrap(err, "failed to create media record")
} }

View File

@@ -91,12 +91,11 @@ func (w *VideoStoreShortWorker) Work(ctx context.Context, job *Job[VideoStoreSho
// save to db and relate to master // save to db and relate to master
mediaModel := &model.Medias{ mediaModel := &model.Medias{
CreatedAt: time.Now(), Name: "[试听] " + media.Name,
Name: "[试听]" + media.Name, MimeType: media.MimeType,
MimeType: media.MimeType, Size: fileSize,
Size: fileSize, Path: filePath,
Path: filePath, Hash: fileMd5,
Hash: fileMd5,
Metas: fields.ToJson(fields.MediaMetas{ Metas: fields.ToJson(fields.MediaMetas{
ParentHash: media.Hash, ParentHash: media.Hash,
Short: true, Short: true,
@@ -112,7 +111,7 @@ func (w *VideoStoreShortWorker) Work(ctx context.Context, job *Job[VideoStoreSho
} }
log.Infof("pending create media record %s", job.Args.FilePath) log.Infof("pending create media record %s", job.Args.FilePath)
if err := model.MediasModel().Create(ctx, mediaModel); err != nil { if err := mediaModel.Create(ctx); err != nil {
log.Errorf("Error saving media record: %v data: %+v", err, mediaModel) log.Errorf("Error saving media record: %v data: %+v", err, mediaModel)
return err return err
} }

View File

@@ -97,12 +97,12 @@ func (m *Medias) BatchCreate(ctx context.Context, models []*Medias) error {
return nil return nil
} }
func (m *Medias) Create(ctx context.Context, medias *Medias) error { func (m *Medias) Create(ctx context.Context) error {
medias.CreatedAt = time.Now() m.CreatedAt = time.Now()
stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(medias).RETURNING(table.Medias.AllColumns) stmt := table.Medias.INSERT(table.Medias.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
m.log().Infof("sql: %s", stmt.DebugSql()) m.log().Infof("sql: %s", stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, medias); err != nil { if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().Errorf("error creating media item: %v", err) m.log().Errorf("error creating media item: %v", err)
return err return err
} }

View File

@@ -132,7 +132,7 @@ func (s *MediasTestSuite) Test_Create() {
Path: "path/to/media.pdf", Path: "path/to/media.pdf",
} }
err := MediasModel().Create(context.Background(), model) err := model.Create(context.Background())
Convey("Create should not return an error", func() { Convey("Create should not return an error", func() {
So(err, ShouldBeNil) So(err, ShouldBeNil)
}) })
@@ -165,7 +165,7 @@ func (s *MediasTestSuite) Test_Page() {
Path: "path/to/media.pdf", Path: "path/to/media.pdf",
} }
err := MediasModel().Create(context.Background(), model) err := model.Create(context.Background())
So(err, ShouldBeNil) So(err, ShouldBeNil)
} }
@@ -208,7 +208,7 @@ func (s *MediasTestSuite) Test_CreateGetID() {
Path: "path/to/media.pdf", Path: "path/to/media.pdf",
} }
err := MediasModel().Create(context.Background(), model) err := model.Create(context.Background())
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(model.ID, ShouldNotBeEmpty) So(model.ID, ShouldNotBeEmpty)

View File

@@ -1,19 +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 (
"time"
)
type Migrations struct {
ID int32 `sql:"primary_key" json:"id"`
VersionID int64 `json:"version_id"`
IsApplied bool `json:"is_applied"`
Tstamp time.Time `json:"tstamp"`
}

View File

@@ -1,84 +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 Migrations = newMigrationsTable("public", "migrations", "")
type migrationsTable struct {
postgres.Table
// Columns
ID postgres.ColumnInteger
VersionID postgres.ColumnInteger
IsApplied postgres.ColumnBool
Tstamp postgres.ColumnTimestamp
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
}
type MigrationsTable struct {
migrationsTable
EXCLUDED migrationsTable
}
// AS creates new MigrationsTable with assigned alias
func (a MigrationsTable) AS(alias string) *MigrationsTable {
return newMigrationsTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new MigrationsTable with assigned schema name
func (a MigrationsTable) FromSchema(schemaName string) *MigrationsTable {
return newMigrationsTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new MigrationsTable with assigned table prefix
func (a MigrationsTable) WithPrefix(prefix string) *MigrationsTable {
return newMigrationsTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new MigrationsTable with assigned table suffix
func (a MigrationsTable) WithSuffix(suffix string) *MigrationsTable {
return newMigrationsTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newMigrationsTable(schemaName, tableName, alias string) *MigrationsTable {
return &MigrationsTable{
migrationsTable: newMigrationsTableImpl(schemaName, tableName, alias),
EXCLUDED: newMigrationsTableImpl("", "excluded", ""),
}
}
func newMigrationsTableImpl(schemaName, tableName, alias string) migrationsTable {
var (
IDColumn = postgres.IntegerColumn("id")
VersionIDColumn = postgres.IntegerColumn("version_id")
IsAppliedColumn = postgres.BoolColumn("is_applied")
TstampColumn = postgres.TimestampColumn("tstamp")
allColumns = postgres.ColumnList{IDColumn, VersionIDColumn, IsAppliedColumn, TstampColumn}
mutableColumns = postgres.ColumnList{VersionIDColumn, IsAppliedColumn, TstampColumn}
)
return migrationsTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
ID: IDColumn,
VersionID: VersionIDColumn,
IsApplied: IsAppliedColumn,
Tstamp: TstampColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
}
}

View File

@@ -11,7 +11,6 @@ package table
// this method only once at the beginning of the program. // this method only once at the beginning of the program.
func UseSchema(schema string) { func UseSchema(schema string) {
Medias = Medias.FromSchema(schema) Medias = Medias.FromSchema(schema)
Migrations = Migrations.FromSchema(schema)
Orders = Orders.FromSchema(schema) Orders = Orders.FromSchema(schema)
Posts = Posts.FromSchema(schema) Posts = Posts.FromSchema(schema)
UserPosts = UserPosts.FromSchema(schema) UserPosts = UserPosts.FromSchema(schema)

View File

@@ -6,6 +6,7 @@ ignores:
- river_client_queue - river_client_queue
- river_job_state - river_job_state
- river_queue - river_queue
- migrations
model: model:
- migrations - migrations
- user_posts - user_posts