feat: Update project structure and configuration files
This commit is contained in:
77
backend/app/models/users.go
Normal file
77
backend/app/models/users.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type usersModel struct {
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (m *usersModel) Prepare() error {
|
||||
m.log = logrus.WithField("model", "usersModel")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (m *usersModel) GetByID(ctx context.Context, id int64) (*model.Users, error) {
|
||||
tbl := table.Users
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var user model.Users
|
||||
err := stmt.QueryContext(ctx, db, &user)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying user by ID: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, error) {
|
||||
tblUserPosts := table.UserPosts
|
||||
stmtUserPosts := tblUserPosts.
|
||||
SELECT(tblUserPosts.PostID).
|
||||
WHERE(tblUserPosts.UserID.EQ(Int64(userID)))
|
||||
m.log.Infof("sql: %s", stmtUserPosts.DebugSql())
|
||||
|
||||
var userPosts []model.UserPosts
|
||||
err := stmtUserPosts.QueryContext(ctx, db, &userPosts)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying user posts: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postIds := lo.Map(userPosts, func(up model.UserPosts, _ int) Expression {
|
||||
return Int64(up.PostID)
|
||||
})
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(tbl.ID.IN(postIds...))
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var posts []*model.Posts
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, &posts); err != nil {
|
||||
m.log.Errorf("error querying posts by user ID: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
Reference in New Issue
Block a user