From e9e3910fe501246b98576e778652ac39b4855b95 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 19 Aug 2023 23:10:12 +0800 Subject: [PATCH] sqlx: fixed create tweet error --- internal/dao/sakila/auto/pg/yesql.go | 7 ++++++- internal/dao/sakila/tweets_pg.go | 13 ++++++++++--- internal/dao/sakila/yesql/yesql_pg.sql | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/internal/dao/sakila/auto/pg/yesql.go b/internal/dao/sakila/auto/pg/yesql.go index 42942c70..df9a886e 100644 --- a/internal/dao/sakila/auto/pg/yesql.go +++ b/internal/dao/sakila/auto/pg/yesql.go @@ -17,7 +17,8 @@ var ( ) const ( - _TweetManage_AddPost = `INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replied_on, created_on) VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on)` + _TweetManage_AddPost = `INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, latest_replied_on, created_on) VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on) RETURNING id` + _TweetManage_AddPostContent = `INSERT INTO @post_content (post_id, user_id, content, type, sort, created_on) VALUES (:post_id, :user_id, :content, :type, :sort, :created_on) RETURNING id` ) // PreparexContext enhances the Conn interface with context. @@ -43,6 +44,7 @@ type PreparexBuilder interface { type TweetManage struct { yesql.Namespace `yesql:"tweet_manage"` AddPost *sqlx.NamedStmt `yesql:"add_post"` + AddPostContent *sqlx.NamedStmt `yesql:"add_post_content"` } func BuildTweetManage(p PreparexBuilder, ctx ...context.Context) (obj *TweetManage, err error) { @@ -56,5 +58,8 @@ func BuildTweetManage(p PreparexBuilder, ctx ...context.Context) (obj *TweetMana if obj.AddPost, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPost))); err != nil { return nil, fmt.Errorf("prepare _TweetManage_AddPost error: %w", err) } + if obj.AddPostContent, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_TweetManage_AddPostContent))); err != nil { + return nil, fmt.Errorf("prepare _TweetManage_AddPostContent error: %w", err) + } return } diff --git a/internal/dao/sakila/tweets_pg.go b/internal/dao/sakila/tweets_pg.go index 4066f19e..633f4c20 100644 --- a/internal/dao/sakila/tweets_pg.go +++ b/internal/dao/sakila/tweets_pg.go @@ -26,12 +26,19 @@ func (s *pgTweetManageSrv) CreatePost(r *ms.Post) (*ms.Post, error) { now := time.Now().Unix() r.Model = &dbr.Model{CreatedOn: now} r.LatestRepliedOn = now - var postId int64 - err := s.p.AddPost.Get(&postId, r) + err := s.p.AddPost.Get(&r.ID, r) if err != nil { return nil, err } - r.ID = postId s.cis.SendAction(core.IdxActCreatePost, r) return r, nil } + +func (s *pgTweetManageSrv) CreatePostContent(r *ms.PostContent) (*ms.PostContent, error) { + r.Model = &ms.Model{CreatedOn: time.Now().Unix()} + err := s.p.AddPostContent.Get(&r.ID, r) + if err != nil { + return nil, err + } + return r, nil +} diff --git a/internal/dao/sakila/yesql/yesql_pg.sql b/internal/dao/sakila/yesql/yesql_pg.sql index b7c84681..3406d098 100644 --- a/internal/dao/sakila/yesql/yesql_pg.sql +++ b/internal/dao/sakila/yesql/yesql_pg.sql @@ -9,3 +9,8 @@ INSERT INTO @post (user_id, tags, ip, ip_loc, attachment_price, visibility, late VALUES (:user_id, :tags, :ip, :ip_loc, :attachment_price, :visibility, :latest_replied_on, :created_on) RETURNING id; +-- name: add_post_content@tweet_manage +-- prepare: named_stmt +INSERT INTO @post_content (post_id, user_id, content, type, sort, created_on) +VALUES (:post_id, :user_id, :content, :type, :sort, :created_on) +RETURNING id;