update
This commit is contained in:
parent
43db1bdd7f
commit
627d4af967
@ -57,7 +57,10 @@ func (h *BookApiHandler) GetHook(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 传递HTTP请求的上下文
|
// 传递HTTP请求的上下文
|
||||||
ins, err := ioc.Controller().Get("books").(*BookController).GetBook(ctx.Request.Context(), &controller.GetBookRequest{
|
ins, err := ioc.Controller().
|
||||||
|
Get("books").(*BookController).
|
||||||
|
GetBook(ctx.Request.Context(),
|
||||||
|
&controller.GetBookRequest{
|
||||||
Isbn: id,
|
Isbn: id,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -82,15 +85,20 @@ func (c *BookController) Name() string {
|
|||||||
return "books"
|
return "books"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BookController) GetBook(ctx context.Context, req *controller.GetBookRequest) (*model.Book, error) {
|
func (c *BookController) GetBook(
|
||||||
|
ctx context.Context,
|
||||||
|
req *controller.GetBookRequest,
|
||||||
|
) (*model.Book, error) {
|
||||||
ins := &model.Book{}
|
ins := &model.Book{}
|
||||||
|
|
||||||
if err := datasource.DBFromCtx(ctx).Where("isbn = ?", req.Isbn).Take(ins).Error; err != nil {
|
if err := datasource.
|
||||||
|
DBFromCtx(ctx).
|
||||||
|
Where("isbn = ?", req.Isbn).
|
||||||
|
Take(ins).Error; err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
return nil, exception.NewNotFound("%d not found", req.Isbn)
|
return nil, exception.NewNotFound("%d not found", req.Isbn)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("get book error, %s", err)
|
return nil, fmt.Errorf("get book error, %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ins, nil
|
return ins, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user