From 05230212f24ff8f8d5095d14562b9458f54b9820 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sun, 1 Oct 2023 18:12:21 +0800 Subject: [PATCH] sqlx: add get user profile data logic implement --- internal/dao/sakila/auto/cc/cc.go | 5 +++++ internal/dao/sakila/user.go | 5 +++++ internal/dao/sakila/yesql/yesql_cc.sql | 17 +++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/internal/dao/sakila/auto/cc/cc.go b/internal/dao/sakila/auto/cc/cc.go index 52bffb39..288b299e 100644 --- a/internal/dao/sakila/auto/cc/cc.go +++ b/internal/dao/sakila/auto/cc/cc.go @@ -187,6 +187,7 @@ const ( _UserManage_GetUsersByIds = `SELECT * FROM @user WHERE id IN (?) AND is_del=0` _UserManage_GetUsersByKeyword = `SELECT * FROM @user WHERE username LIKE ? AND is_del=0 limit 6` _UserManage_UpdateUser = `UPDATE @user SET username=:username, nickname=:nickname, phone=:phone, password=:password, salt=:salt, status=:status, avatar=:avatar, balance=:balance, is_admin=:is_admin, modified_on=:modified_on WHERE id=? AND is_del=0` + _UserManage_UserProfileByName = `SELECT u.id, u.username, u.nickname, u.phone, u.status, u.avatar, u.balance, u.is_admin, u.created_on, m.tweets_count FROM @user u LEFT JOIN @user_metric m ON u.id=m.user_id WHERE u.username=? AND u.is_del=0` _UserMetrics_AddUserMetric = `INSERT INTO @user_metric (user_id, created_on) VALUES (?, ?)` _UserMetrics_DeleteUserMetric = `UPDATE @user_metric SET is_del=1, deleted_on=? WHERE user_id=? AND is_del=0` _UserMetrics_GetTweetsCount = `SELECT tweets_count FROM @user_metric WHERE user_id=? AND is_del=0` @@ -457,6 +458,7 @@ type UserManage struct { GetUserByPhone *sqlx.Stmt `yesql:"get_user_by_phone"` GetUserByUsername *sqlx.Stmt `yesql:"get_user_by_username"` GetUsersByKeyword *sqlx.Stmt `yesql:"get_users_by_keyword"` + UserProfileByName *sqlx.Stmt `yesql:"user_profile_by_name"` CreateUser *sqlx.NamedStmt `yesql:"create_user"` UpdateUser *sqlx.NamedStmt `yesql:"update_user"` } @@ -1124,6 +1126,9 @@ func BuildUserManage(p PreparexBuilder, ctx ...context.Context) (obj *UserManage if obj.GetUsersByKeyword, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_UserManage_GetUsersByKeyword))); err != nil { return nil, fmt.Errorf("prepare _UserManage_GetUsersByKeyword error: %w", err) } + if obj.UserProfileByName, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_UserManage_UserProfileByName))); err != nil { + return nil, fmt.Errorf("prepare _UserManage_UserProfileByName error: %w", err) + } if obj.CreateUser, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_UserManage_CreateUser))); err != nil { return nil, fmt.Errorf("prepare _UserManage_CreateUser error: %w", err) } diff --git a/internal/dao/sakila/user.go b/internal/dao/sakila/user.go index c57a8226..c1f0db38 100644 --- a/internal/dao/sakila/user.go +++ b/internal/dao/sakila/user.go @@ -12,6 +12,7 @@ import ( "github.com/bitbus/sqlx" "github.com/bitbus/sqlx/db" "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" "github.com/rocboss/paopao-ce/internal/core/ms" "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/cc" "github.com/rocboss/paopao-ce/internal/dao/sakila/auto/pgc" @@ -40,6 +41,10 @@ func (s *userManageSrv) GetUserByUsername(username string) (*ms.User, error) { return db.Get[ms.User](s.q.GetUserByUsername, username) } +func (s *userManageSrv) UserProfileByName(username string) (*cs.UserProfile, error) { + return db.Get[cs.UserProfile](s.q.UserProfileByName, username) +} + func (s *userManageSrv) GetUserByPhone(phone string) (*ms.User, error) { return db.Get[ms.User](s.q.GetUserByPhone, phone) } diff --git a/internal/dao/sakila/yesql/yesql_cc.sql b/internal/dao/sakila/yesql/yesql_cc.sql index e23ff5e8..d5ae9903 100644 --- a/internal/dao/sakila/yesql/yesql_cc.sql +++ b/internal/dao/sakila/yesql/yesql_cc.sql @@ -1403,6 +1403,23 @@ SELECT * FROM @user WHERE id=? AND is_del=0; -- prepare: stmt SELECT * FROM @user WHERE username=? AND is_del=0; +-- name: user_profile_by_name@user_manage +-- prepare: stmt +SELECT u.id, + u.username, + u.nickname, + u.phone, + u.status, + u.avatar, + u.balance, + u.is_admin, + u.created_on, + m.tweets_count +FROM @user u +LEFT JOIN @user_metric m +ON u.id=m.user_id +WHERE u.username=? AND u.is_del=0; + -- name: get_user_by_phone@user_manage -- prepare: stmt SELECT * FROM @user WHERE phone=? AND is_del=0;