补充controllers
This commit is contained in:
parent
048ac6cf39
commit
ba14513baa
53
book/v3/controllers/arch.drawio
Normal file
53
book/v3/controllers/arch.drawio
Normal file
@ -0,0 +1,53 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="BHjv1J8RDqaP8pKBLOqJ" name="第 1 页">
|
||||
<mxGraphModel dx="777" dy="395" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="book handlers" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="140" y="130" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="7" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="3" target="2">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="接口" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="7">
|
||||
<mxGeometry x="-0.0238" y="-2" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="15" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=0.75;exitY=1;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="3" target="12">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="16" value="依赖这个业务逻辑" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="15">
|
||||
<mxGeometry x="0.401" y="2" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="comment handlers" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="330" y="130" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="5" style="edgeStyle=none;html=1;exitX=0;exitY=0.3333333333333333;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="4" target="3">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="4" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="550" y="20" width="40" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="6" value="..." style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="500" y="130" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="对外提供的功能" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="260" y="80" width="60" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" value="<h1 style="margin-top: 0px;">controllers</h1><p>controllers.Book.GetBook(id) -&gt; book</p>" style="text;html=1;whiteSpace=wrap;overflow=hidden;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="270" y="230" width="180" height="120" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="12" value="Book Controller" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="150" y="340" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="14" style="edgeStyle=none;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0.565;entryY=0.997;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="12" target="2">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
42
book/v3/controllers/book.go
Normal file
42
book/v3/controllers/book.go
Normal file
@ -0,0 +1,42 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"122.51.31.227/go-course/go18/book/v3/config"
|
||||
"122.51.31.227/go-course/go18/book/v3/models"
|
||||
)
|
||||
|
||||
var Book = &BookController{}
|
||||
|
||||
type BookController struct {
|
||||
}
|
||||
|
||||
func NewGetBookRequest(bookNumber string) *GetBookRequest {
|
||||
return &GetBookRequest{
|
||||
BookNumber: bookNumber,
|
||||
}
|
||||
}
|
||||
|
||||
type GetBookRequest struct {
|
||||
BookNumber string
|
||||
// RequestId string
|
||||
// ...
|
||||
}
|
||||
|
||||
// 核心功能
|
||||
// ctx: Trace, 支持请求的取消, request_id
|
||||
// GetBookRequest 为什么要把他封装为1个对象, GetBook(ctx context.Context, BookNumber string), 保证你的接口的签名的兼容性
|
||||
// BookController.GetBook(, "")
|
||||
func (c *BookController) GetBook(ctx context.Context, in *GetBookRequest) (*models.Book, error) {
|
||||
// context.WithValue(ctx, "request_id", 111)
|
||||
// ctx.Value("request_id")
|
||||
|
||||
bookInstance := &models.Book{}
|
||||
// 需要从数据库中获取一个对象
|
||||
if err := config.DB().Where("id = ?", in.BookNumber).Take(bookInstance).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bookInstance, nil
|
||||
}
|
30
book/v3/controllers/comment.go
Normal file
30
book/v3/controllers/comment.go
Normal file
@ -0,0 +1,30 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"122.51.31.227/go-course/go18/book/v3/models"
|
||||
)
|
||||
|
||||
var Comment = &CommentController{}
|
||||
|
||||
type CommentController struct {
|
||||
}
|
||||
|
||||
type AddCommentRequest struct {
|
||||
BookNumber string
|
||||
}
|
||||
|
||||
func (c *CommentController) AddComment(ctx context.Context, in *AddCommentRequest) (*models.Comment, error) {
|
||||
// 业务处理的细节
|
||||
// 多个业务模块 进行交互
|
||||
book, err := Book.GetBook(ctx, NewGetBookRequest(in.BookNumber))
|
||||
if err != nil {
|
||||
// 获取查询不到报错
|
||||
return nil, err
|
||||
}
|
||||
// 判断book的状态
|
||||
fmt.Println(book)
|
||||
return nil, nil
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"122.51.31.227/go-course/go18/book/v3/config"
|
||||
"122.51.31.227/go-course/go18/book/v3/controllers"
|
||||
"122.51.31.227/go-course/go18/book/v3/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -135,14 +136,13 @@ func (h *BookApiHandler) createBook(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *BookApiHandler) getBook(ctx *gin.Context) {
|
||||
bookInstance := &models.Book{}
|
||||
// 需要从数据库中获取一个对象
|
||||
if err := config.DB().Where("id = ?", ctx.Param("bn")).Take(bookInstance).Error; err != nil {
|
||||
ctx.JSON(400, gin.H{"code": 500, "message": err.Error()})
|
||||
book, err := controllers.Book.GetBook(ctx, controllers.NewGetBookRequest(ctx.Param("bn")))
|
||||
if err != nil {
|
||||
ctx.JSON(400, gin.H{"code": 400, "message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(200, bookInstance)
|
||||
ctx.JSON(200, book)
|
||||
}
|
||||
|
||||
func (h *BookApiHandler) updateBook(ctx *gin.Context) {
|
||||
|
16
book/v3/handlers/comment.go
Normal file
16
book/v3/handlers/comment.go
Normal file
@ -0,0 +1,16 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var Comment = &CommentApiHandler{}
|
||||
|
||||
type CommentApiHandler struct {
|
||||
}
|
||||
|
||||
func (h *CommentApiHandler) AddComment(ctx *gin.Context) {
|
||||
// Book.getBook()
|
||||
// Book.GetBook(id) -> BookInstance
|
||||
// controllers.Book.GetBook(ctx, controllers.NewGetBookRequest(ctx.Param("bn")))
|
||||
}
|
1
book/v3/handlers/user.go
Normal file
1
book/v3/handlers/user.go
Normal file
@ -0,0 +1 @@
|
||||
package handlers
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"122.51.31.227/go-course/go18/book/v3/config"
|
||||
"122.51.31.227/go-course/go18/book/v3/handlers"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -13,7 +14,9 @@ func main() {
|
||||
|
||||
handlers.Book.Registry(server)
|
||||
|
||||
if err := server.Run(":8080"); err != nil {
|
||||
ac := config.C().Application
|
||||
// 启动服务
|
||||
if err := server.Run(fmt.Sprintf("%s:%d", ac.Host, ac.Port)); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
# 数据模型
|
||||
# 数据模型
|
||||
|
||||
|
4
book/v3/models/comment.go
Normal file
4
book/v3/models/comment.go
Normal file
@ -0,0 +1,4 @@
|
||||
package models
|
||||
|
||||
type Comment struct {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user