181 lines
5.4 KiB
Go
181 lines
5.4 KiB
Go
// Code generated by the atomctl ; DO NOT EDIT.
|
|
// Code generated by the atomctl ; DO NOT EDIT.
|
|
// Code generated by the atomctl ; DO NOT EDIT.
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
. "github.com/go-jet/jet/v2/postgres"
|
|
"github.com/samber/lo"
|
|
log "github.com/sirupsen/logrus"
|
|
"time"
|
|
)
|
|
|
|
// conds
|
|
func (m *Posts) CondNotDeleted() Cond {
|
|
return func(cond BoolExpression) BoolExpression {
|
|
return cond.AND(tblPosts.DeletedAt.IS_NULL())
|
|
}
|
|
}
|
|
|
|
func (m *Posts) CondDeleted() Cond {
|
|
return func(cond BoolExpression) BoolExpression {
|
|
return cond.AND(tblPosts.DeletedAt.IS_NOT_NULL())
|
|
}
|
|
}
|
|
|
|
func (m *Posts) CondID(id int64) Cond {
|
|
return func(cond BoolExpression) BoolExpression {
|
|
return cond.AND(tblPosts.ID.EQ(Int(id)))
|
|
}
|
|
}
|
|
|
|
// funcs
|
|
func (m *Posts) log() *log.Entry {
|
|
return log.WithField("model", "Posts")
|
|
}
|
|
|
|
func (m *Posts) Create(ctx context.Context) error {
|
|
m.CreatedAt = time.Now()
|
|
m.UpdatedAt = time.Now()
|
|
|
|
stmt := tblPosts.INSERT(tblPosts.MutableColumns).MODEL(m).RETURNING(tblPosts.AllColumns)
|
|
m.log().WithField("func", "Create").Info(stmt.DebugSql())
|
|
|
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
m.log().WithField("func", "Create").Errorf("error creating Posts item: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "Create").Infof("Posts item created successfully")
|
|
return nil
|
|
}
|
|
|
|
func (m *Posts) BatchCreate(ctx context.Context, models []*Posts) error {
|
|
stmt := tblPosts.INSERT(tblPosts.MutableColumns).MODELS(models)
|
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
|
|
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
|
m.log().WithField("func", "Create").Errorf("error creating Posts item: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "BatchCreate").Infof("Posts items created successfully")
|
|
return nil
|
|
}
|
|
func (m *Posts) Delete(ctx context.Context) error {
|
|
stmt := tblPosts.UPDATE().SET(tblPosts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblPosts.ID.EQ(Int(m.ID)))
|
|
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
|
|
|
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
m.log().WithField("func", "SoftDelete").Errorf("error soft deleting Posts item: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "SoftDelete").Infof("Posts item soft deleted successfully")
|
|
return nil
|
|
}
|
|
|
|
// BatchDelete
|
|
func (m *Posts) BatchDelete(ctx context.Context, ids []int64) error {
|
|
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
|
return Int64(id)
|
|
})
|
|
|
|
stmt := tblPosts.UPDATE().SET(tblPosts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblPosts.ID.IN(condIds...))
|
|
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
|
|
|
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
m.log().WithField("func", "BatchSoftDelete").Errorf("error soft deleting Posts items: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "BatchSoftDelete").WithField("ids", ids).Infof("Posts items soft deleted successfully")
|
|
return nil
|
|
}
|
|
|
|
func (m *Posts) ForceDelete(ctx context.Context) error {
|
|
stmt := tblPosts.DELETE().WHERE(tblPosts.ID.EQ(Int(m.ID)))
|
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
|
|
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
|
m.log().WithField("func", "Delete").Errorf("error deleting Posts item: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "Delete").Infof("Posts item deleted successfully")
|
|
return nil
|
|
}
|
|
|
|
func (m *Posts) BatchForceDelete(ctx context.Context, ids []int64) error {
|
|
condIds := lo.Map(ids, func(id int64, _ int) Expression {
|
|
return Int64(id)
|
|
})
|
|
|
|
stmt := tblPosts.DELETE().WHERE(tblPosts.ID.IN(condIds...))
|
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
|
|
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
|
m.log().WithField("func", "BatchForceDelete").Errorf("error deleting Posts items: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "BatchForceDelete").WithField("ids", ids).Infof("Posts items deleted successfully")
|
|
return nil
|
|
}
|
|
|
|
func (m *Posts) Update(ctx context.Context) error {
|
|
m.UpdatedAt = time.Now()
|
|
|
|
stmt := tblPosts.UPDATE(tblPostsUpdateMutableColumns).MODEL(m).WHERE(tblPosts.ID.EQ(Int(m.ID))).RETURNING(tblPosts.AllColumns)
|
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
|
|
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
m.log().WithField("func", "Update").Errorf("error updating Posts item: %v", err)
|
|
return err
|
|
}
|
|
|
|
m.log().WithField("func", "Update").Infof("Posts item updated successfully")
|
|
return nil
|
|
}
|
|
|
|
// GetByCond
|
|
func (m *Posts) GetByCond(ctx context.Context, conds ...Cond) (*Posts, error) {
|
|
cond := CondTrue(conds...)
|
|
|
|
stmt := tblPosts.SELECT(tblPosts.AllColumns).WHERE(cond)
|
|
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
|
|
|
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
|
m.log().WithField("func", "GetByCond").Errorf("error getting Posts item by ID: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
m.log().WithField("func", "GetByCond").Infof("Posts item retrieved successfully")
|
|
return m, nil
|
|
}
|
|
|
|
// GetByID
|
|
func (m *Posts) GetByID(ctx context.Context, id int64, conds ...Cond) (*Posts, error) {
|
|
return m.GetByCond(ctx, CondJoin(m.CondID(id), conds...)...)
|
|
}
|
|
|
|
// Count
|
|
func (m *Posts) Count(ctx context.Context, conds ...Cond) (int64, error) {
|
|
cond := CondTrue(conds...)
|
|
|
|
stmt := tblPosts.SELECT(COUNT(tblPosts.ID).AS("count")).WHERE(cond)
|
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
|
|
|
var count struct {
|
|
Count int64
|
|
}
|
|
|
|
if err := stmt.QueryContext(ctx, db, &count); err != nil {
|
|
m.log().Errorf("error counting Posts items: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
return count.Count, nil
|
|
}
|