-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve logic in the starbook #135
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,18 @@ done! | |
--- | ||
`Logic` 是业务逻辑层,存放在`internal/logic`下,供`Controller`调用从而实现具体的业务逻辑。 | ||
|
||
定义一个`Users`对象: | ||
|
||
*internal/logic/users/users.go* | ||
```go | ||
package users | ||
|
||
type Users struct { | ||
} | ||
``` | ||
|
||
编写注册方法: | ||
|
||
*internal/logic/users/register.go* | ||
```go | ||
package users | ||
|
@@ -58,7 +70,7 @@ import ( | |
"star/internal/model/do" | ||
) | ||
|
||
func Register(ctx context.Context, username, password, email string) error { | ||
func (u *Users) Register(ctx context.Context, username, password, email string) error { | ||
_, err := dao.Users.Ctx(ctx).Data(do.Users{ | ||
Username: username, | ||
Password: password, | ||
|
@@ -77,6 +89,31 @@ func Register(ctx context.Context, username, password, email string) error { | |
--- | ||
`Controller` 层负责接收 `Req` 请求对象后调用一个或多个`Logic`完成业务逻辑,一些简单的逻辑也可以直接放在`Controller`中处理。处理完成后的结果封装在约定的 `Res` 数据结构中返回。这里的`Res`数据结构为空,返回`nil`即可。 | ||
|
||
将`Users`对象封装到控制器中,方便后续调用。 | ||
|
||
*internal/controller/users/users_new.go* | ||
```go | ||
... | ||
|
||
package users | ||
|
||
import ( | ||
"star/api/users" | ||
usersL "star/internal/logic/users" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在API中,接口包名应该是 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) | ||
|
||
type ControllerV1 struct { | ||
users *usersL.Users | ||
} | ||
|
||
func NewV1() users.IUsersV1 { | ||
return &ControllerV1{ | ||
users: &usersL.Users{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 最好通过 |
||
} | ||
} | ||
``` | ||
|
||
|
||
*internal/controller/users/users_v1_register.go* | ||
```go | ||
package users | ||
|
@@ -90,7 +127,7 @@ import ( | |
) | ||
|
||
func (c *ControllerV1) Register(ctx context.Context, req *v1.RegisterReq) (res *v1.RegisterRes, err error) { | ||
err = users.Register(ctx, req.Username, req.Password, req.Email) | ||
err = c.users.Register(ctx, req.Username, req.Password, req.Email) | ||
return nil, err | ||
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个结构体为什么不公开呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个结构体
users
内部用的,想着也没必要公开