Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion repository/db_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var db *gorm.DB

func Init() error {
var err error
dsn := "root:00000000@tcp(127.0.0.1:3306)/community?charset=utf8mb4&parseTime=True&loc=Local"
// user:password@tcp(127.0.0.1:3306)
dsn := "root:root@tcp(127.0.0.1:3306)/community?charset=utf8mb4&parseTime=True&loc=Local"
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
return err
}
2 changes: 2 additions & 0 deletions repository/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ type TopicDao struct {
var topicDao *TopicDao
var topicOnce sync.Once

// topicDao初始化
func NewTopicDaoInstance() *TopicDao {
// 只执行一次
topicOnce.Do(
func() {
topicDao = &TopicDao{}
Expand Down
1 change: 1 addition & 0 deletions service/publish_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (f *PublishPostFlow) Do() (int64, error) {
return f.postId, nil
}

// 参数验证,需增加业务逻辑验证(判断用户是否存在,对应topic是否存在)
func (f *PublishPostFlow) checkParam() error {
if f.userId <= 0 {
return errors.New("userId id must be larger than 0")
Expand Down
17 changes: 11 additions & 6 deletions service/query_page_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func QueryPageInfo(topicId int64) (*PageInfo, error) {
return NewQueryPageInfoFlow(topicId).Do()
}

// 新建QueryPageInfoFlow结构体
func NewQueryPageInfoFlow(topId int64) *QueryPageInfoFlow {
return &QueryPageInfoFlow{
topicId: topId,
Expand All @@ -35,19 +36,22 @@ func NewQueryPageInfoFlow(topId int64) *QueryPageInfoFlow {
type QueryPageInfoFlow struct {
topicId int64
pageInfo *PageInfo

// 数据库对于DO
topic *repository.Topic
posts []*repository.Post
userMap map[int64]*repository.User
}

func (f *QueryPageInfoFlow) Do() (*PageInfo, error) {
// 校验参数
if err := f.checkParam(); err != nil {
return nil, err
}
// 查询初始信息
if err := f.prepareInfo(); err != nil {
return nil, err
}
// 组合页面信息
if err := f.packPageInfo(); err != nil {
return nil, err
}
Expand All @@ -62,7 +66,7 @@ func (f *QueryPageInfoFlow) checkParam() error {
}

func (f *QueryPageInfoFlow) prepareInfo() error {
//获取topic信息
//根据topicId获取topic信息
var wg sync.WaitGroup
wg.Add(2)
var topicErr, postErr error
Expand All @@ -75,7 +79,7 @@ func (f *QueryPageInfoFlow) prepareInfo() error {
}
f.topic = topic
}()
//获取post列表
//根据topicId获取post列表
go func() {
defer wg.Done()
posts, err := repository.NewPostDaoInstance().QueryPostByParentId(f.topicId)
Expand All @@ -93,10 +97,11 @@ func (f *QueryPageInfoFlow) prepareInfo() error {
return postErr
}
//获取用户信息
uids := []int64{f.topic.Id}
uids := []int64{f.topic.UserId}
for _, post := range f.posts {
uids = append(uids, post.Id)
uids = append(uids, post.UserId)
}
// 查询topic所有相关用户(topic用户和post用户)
userMap, err := repository.NewUserDaoInstance().MQueryUserById(uids)
if err != nil {
return err
Expand All @@ -112,7 +117,7 @@ func (f *QueryPageInfoFlow) packPageInfo() error {
if !ok {
return errors.New("has no topic user info")
}
//post list
//post list (组建post信息和发post用户信息)
postList := make([]*PostInfo, 0)
for _, post := range f.posts {
postUser, ok := userMap[post.UserId]
Expand Down
13 changes: 7 additions & 6 deletions sever.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,35 @@ import (
)

func main() {
if err := Init(); err != nil {
if Init() != nil {
os.Exit(-1)
}
r := gin.Default()

r.Use(gin.Logger())

// gin.Default()默认添加了日志中间件
//r.Use(gin.Logger())
// 测试接口
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})

// page信息获取接口
r.GET("/community/page/get/:id", func(c *gin.Context) {
topicId := c.Param("id")
data := handler.QueryPageInfo(topicId)
c.JSON(200, data)
})

// 新增post接口
r.POST("/community/post/do", func(c *gin.Context) {
uid, _ := c.GetPostForm("uid")
topicId, _ := c.GetPostForm("topic_id")
content, _ := c.GetPostForm("content")
data := handler.PublishPost(uid, topicId, content)
c.JSON(200, data)
})
err := r.Run()
if err != nil {
if r.Run() != nil {
return
}
}
Expand Down