diff --git "a/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" "b/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" index ee486448d9c..55937263849 100644 --- "a/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" +++ "b/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" @@ -56,13 +56,13 @@ import ( "star/utility" ) -type UserClaims struct { +type userClaims struct { Id uint Username string jwt.RegisteredClaims } -func Login(ctx context.Context, username, password string) (tokenString string, err error) { +func (u *Users) Login(ctx context.Context, username, password string) (tokenString string, err error) { var user entity.Users err = dao.Users.Ctx(ctx).Where("username", username).Scan(&user) if err != nil { @@ -79,19 +79,19 @@ func Login(ctx context.Context, username, password string) (tokenString string, } // 生成token - userClaims := &UserClaims{ + uc := &userClaims{ Id: user.Id, Username: user.Username, RegisteredClaims: jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(time.Now().Add(6 * time.Hour)), }, } - token := jwt.NewWithClaims(jwt.SigningMethodHS256, userClaims) + token := jwt.NewWithClaims(jwt.SigningMethodHS256, uc) return token.SignedString(utility.JwtKey) } ``` -从上面的代码可以看到,我们需要声明一个结构体`UserClaims`保存签名信息,这里保存了`Id`和`Username`并设置了`Token`有效期为 6 个小时。最后的声明对象还需要调用`SignedString`方法传入`JwtKey`生成签名。 +从上面的代码可以看到,我们需要声明一个结构体`userClaims`保存签名信息,这里保存了`Id`和`Username`并设置了`Token`有效期为 6 个小时。最后的声明对象还需要调用`SignedString`方法传入`JwtKey`生成签名。 ## Controller调用Logic --- @@ -107,7 +107,7 @@ import ( ) func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) { - token, err := users.Login(ctx, req.Username, req.Password) + token, err := c.users.Login(ctx, req.Username, req.Password) if err != nil { return } diff --git "a/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" "b/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" index a52bcedc197..dffbc6c5855 100644 --- "a/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" +++ "b/docs/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" @@ -92,15 +92,15 @@ import ( ... -func Info(ctx context.Context) (user *entity.Users, err error) { +func (u *Users) Info(ctx context.Context) (user *entity.Users, err error) { user = new(entity.Users) tokenString := g.RequestFromCtx(ctx).Request.Header.Get("Authorization") - tokenClaims, _ := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { + tokenClaims, _ := jwt.ParseWithClaims(tokenString, &userClaims{}, func(token *jwt.Token) (interface{}, error) { return utility.JwtKey, nil }) - if claims, ok := tokenClaims.Claims.(*UserClaims); ok && tokenClaims.Valid { + if claims, ok := tokenClaims.Claims.(*userClaims); ok && tokenClaims.Valid { err = dao.Users.Ctx(ctx).Where("id", claims.Id).Scan(&user) } return @@ -113,6 +113,30 @@ func Info(ctx context.Context) (user *entity.Users, err error) { ## Controller调用Logic --- +同样将`logic`注册到控制器中。 + +*internal/controller/users/users_new.go* +```go +... + +package account + +import ( + "star/api/account" + usersL "star/internal/logic/users" +) + +type ControllerV1 struct { + users *usersL.Users +} + +func NewV1() account.IAccountV1 { + return &ControllerV1{ + users: &usersL.Users{}, + } +} +``` + *internal/controller/account/account_v1_info.go* ```go package account @@ -125,7 +149,7 @@ import ( ) func (c *ControllerV1) Info(ctx context.Context, req *v1.InfoReq) (res *v1.InfoRes, err error) { - user, err := users.Info(ctx) + user, err := c.users.Info(ctx) if err != nil { return nil, err } diff --git "a/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" "b/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" index 648971e9094..df2d774673f 100644 --- "a/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" +++ "b/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" @@ -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" +) + +type ControllerV1 struct { + users *usersL.Users +} + +func NewV1() users.IUsersV1 { + return &ControllerV1{ + users: &usersL.Users{}, + } +} +``` + + *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 } ``` diff --git "a/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" "b/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" index 59d5e68518e..2e02b94ead9 100644 --- "a/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" +++ "b/docs/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" @@ -153,8 +153,8 @@ package users ... -func Register(ctx context.Context, username, password, email string) error { - if err := checkUser(ctx, username); err != nil { +func (u *Users) Register(ctx context.Context, username, password, email string) error { + if err := u.checkUser(ctx, username); err != nil { return err } @@ -169,7 +169,7 @@ func Register(ctx context.Context, username, password, email string) error { return nil } -func checkUser(ctx context.Context, username string) error { +func (u *Users) checkUser(ctx context.Context, username string) error { count, err := dao.Users.Ctx(ctx).Where("username", username).Count() if err != nil { return err @@ -222,7 +222,7 @@ package users ... -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{ @@ -279,8 +279,8 @@ import ( ... ) -func Register(ctx context.Context, in *model.UserInput) error { - if err := CheckUser(ctx, in.Username); err != nil { +func (u *Users) Register(ctx context.Context, in *model.UserInput) error { + if err := u.checkUser(ctx, in.Username); err != nil { return err } @@ -310,7 +310,7 @@ import ( ) func (c *ControllerV1) Register(ctx context.Context, req *v1.RegisterReq) (res *v1.RegisterRes, err error) { - err = users.Register(ctx, &model.UserInput{ + err = c.users.Register(ctx, &model.UserInput{ Username: req.Username, Password: req.Password, Email: req.Email, diff --git "a/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" "b/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" index e0d05d284bb..6c33ba515c4 100644 --- "a/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" +++ "b/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" @@ -36,7 +36,7 @@ type RandListRes struct { ## 编写Logic --- -*internal/controller/words/words_v1_rand_list.go* +*internal/logic/words/learn_words.go* ```go package words @@ -50,7 +50,7 @@ import ( ) // Rand 随机若干获取单词 -func Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) { +func (w *Words) Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) { if limit <= 0 { limit = 50 } @@ -72,7 +72,7 @@ func Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) { ## Controller调用Logic --- -*internal/logic/words/learn_words.go* +*internal/controller/words/words_v1_rand_list.go* ```go package words @@ -80,18 +80,16 @@ import ( "context" "star/api/words/v1" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" ) func (c *ControllerV1) RandList(ctx context.Context, req *v1.RandListReq) (res *v1.RandListRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - wordList, err := words.Rand(ctx, uid, req.Limit) + wordList, err := c.words.Rand(ctx, uid, req.Limit) if err != nil { return nil, err } diff --git "a/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" "b/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" index 70d926048a2..5906b4d3394 100644 --- "a/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" +++ "b/docs/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" @@ -26,12 +26,12 @@ type SetLevelRes struct { ## 编写Logic --- -*internal/controller/words/words_v1_set_level.go* +*internal/logic/words/learn_words.go* ```go ... // SetLevel 设置单词熟练度 -func SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error { +func (w *Words) SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error { if level < 0 || level > 5 { return gerror.New("熟练度值不合法") } @@ -50,7 +50,7 @@ func SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) e ## Controller调用Logic --- -*internal/logic/words/learn_words.go* +*internal/controller/words/words_v1_set_level.go* ```go package words @@ -58,17 +58,15 @@ import ( "context" "star/api/words/v1" - "star/internal/logic/users" - "star/internal/logic/words" ) func (c *ControllerV1) SetLevel(ctx context.Context, req *v1.SetLevelReq) (res *v1.SetLevelRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.SetLevel(ctx, uid, req.Id, req.Level) + err = c.words.SetLevel(ctx, uid, req.Id, req.Level) return nil, err } ``` diff --git "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" index 400ad641765..3c9693ce080 100644 --- "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" +++ "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" @@ -72,10 +72,13 @@ import ( "star/internal/dao" "star/internal/model" "star/internal/model/do" -) +) + +type Words struct { +} -func Create(ctx context.Context, in *model.WordInput) error { - if err := checkWord(ctx, in); err != nil { +func (w *Words) Create(ctx context.Context, in *model.WordInput) error { + if err := w.checkWord(ctx, in); err != nil { return err } @@ -94,7 +97,7 @@ func Create(ctx context.Context, in *model.WordInput) error { return nil } -func checkWord(ctx context.Context, in *model.WordInput) error { +func (w *Words) checkWord(ctx context.Context, in *model.WordInput) error { count, err := dao.Words.Ctx(ctx).Where("uid", in.Uid).Where("word", in.Word).Count() if err != nil { return err @@ -108,14 +111,13 @@ func checkWord(ctx context.Context, in *model.WordInput) error { 在`Logic`中我们也需要确保同一用户单词不能重复,和数据库保持一致。 -## Controller调用Logic ---- +### account logic 单词表中保存有`uid`字段,我们需要在`logic/users`包中封装一个`GetUid`函数提供`uid`。 *internal/logic/users/account.go* ```go -func GetUid(ctx context.Context) (uint, error) { - user, err := Info(ctx) +func (u *Users) GetUid(ctx context.Context) (uint, error) { + user, err := u.Info(ctx) if err != nil { return 0, err } @@ -123,6 +125,37 @@ func GetUid(ctx context.Context) (uint, error) { } ``` +## Controller调用Logic +--- +在创建单词的控制器中,我们需要调用`account`和`words`两个`logic`,我们将他封装到控制器中。 + +*internal/controller/words/words_new.go* +```go +... + + +package words + +import ( + "star/api/words" + usersL "star/internal/logic/users" + wordsL "star/internal/logic/words" +) + +type ControllerV1 struct { + users *usersL.Users + words *wordsL.Words +} + +func NewV1() words.IWordsV1 { + return &ControllerV1{ + users: &usersL.Users{}, + words: &wordsL.Words{}, + } +} +``` + + *internal/controller/words/words_v1_create.go* ```go package words @@ -130,18 +163,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" "star/api/words/v1" ) func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.Create(ctx, &model.WordInput{ + err = c.words.Create(ctx, &model.WordInput{ Uid: uid, Word: req.Word, Definition: req.Definition, @@ -154,7 +185,7 @@ func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.C } ``` -在`Controller`中调用两个`Logic`层级的函数:`users.GetUid`和`words.Create`来实现功能。注意,不要在`words.Create`中直接调用`users.GetUid`,删除`Controller`中的`users.GetUid`,这样会加重`words`包的耦合。 +在`Controller`中调用两个`Logic`层级的方法:`users.GetUid`和`words.Create`来实现功能。注意,不要在`words.Create`中直接调用`users.GetUid`,这样会加重`words`包的耦合。 最佳实验是,**尽量保证`Logic`函数的功能单一化,在`Controller`中多次调用`Logic`完成功能。** diff --git "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" index 63b529be737..526bd22b077 100644 --- "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" +++ "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" @@ -37,15 +37,15 @@ package words ... -func Create(ctx context.Context, in *model.WordInput) error { - if err := checkWord(ctx, 0, in); err != nil { +func (w *Words) Create(ctx context.Context, in *model.WordInput) error { + if err := w.checkWord(ctx, 0, in); err != nil { return err } ... } // checkWord 在更新时不检查自身 -func checkWord(ctx context.Context, id uint, in *model.WordInput) error { +func (w *Words) checkWord(ctx context.Context, id uint, in *model.WordInput) error { db := dao.Words.Ctx(ctx).Where("uid", in.Uid).Where("word", in.Word) if id > 0 { db = db.WhereNot("id", id) @@ -69,8 +69,8 @@ package words ... -func Update(ctx context.Context, id uint, in *model.WordInput) error { - if err := checkWord(ctx, id, in); err != nil { +func (w *Words) Update(ctx context.Context, id uint, in *model.WordInput) error { + if err := w.checkWord(ctx, id, in); err != nil { return err } @@ -107,18 +107,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" "star/api/words/v1" ) func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.Update(ctx, req.Id, &model.WordInput{ + err = c.words.Update(ctx, req.Id, &model.WordInput{ Uid: uid, Word: req.Word, Definition: req.Definition, diff --git "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" index 99d550042a9..f8e642e5956 100644 --- "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" +++ "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" @@ -69,7 +69,7 @@ type WordQuery struct { ```go ... -func List(ctx context.Context, query *model.WordQuery) (list []entity.Words, total uint, err error) { +func (w *Words) List(ctx context.Context, query *model.WordQuery) (list []entity.Words, total uint, err error) { if query == nil { query = &model.WordQuery{} } @@ -120,13 +120,11 @@ import ( "context" "star/api/words/v1" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" ) func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } @@ -137,7 +135,7 @@ func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListR Page: req.Page, Size: req.Size, } - wordList, total, err := words.List(ctx, query) + wordList, total, err := c.words.List(ctx, query) if err != nil { return nil, err } diff --git "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" index 20e601c5257..e02a61310fb 100644 --- "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" +++ "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" @@ -38,7 +38,7 @@ type DetailRes struct { ```go ... -func Detail(ctx context.Context, uid, id uint) (word *entity.Words, err error) { +func (w *Words) Detail(ctx context.Context, uid, id uint) (word *entity.Words, err error) { word = &entity.Words{} db := dao.Words.Ctx(ctx).Where("id", id) if uid > 0 { @@ -58,19 +58,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" - "star/api/words/v1" ) func (c *ControllerV1) Detail(ctx context.Context, req *v1.DetailReq) (res *v1.DetailRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - word, err := words.Detail(ctx, uid, req.Id) + word, err := c.words.Detail(ctx, uid, req.Id) if err != nil { return nil, err } diff --git "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" index 583eed9e5cb..ebb37520ad6 100644 --- "a/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" +++ "b/docs/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" @@ -25,7 +25,7 @@ type DeleteRes struct { *internal/logic/words/words.go* ```go ... -func Delete(ctx context.Context, uid, id uint) (err error) { +func (w *Words) Delete(ctx context.Context, uid, id uint) (err error) { db := dao.Words.Ctx(ctx).Where("id", id) if uid > 0 { db = db.Where("uid", uid) @@ -44,18 +44,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" "star/api/words/v1" ) func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.Delete(ctx, uid, req.Id) + err = c.words.Delete(ctx, uid, req.Id) return } ``` diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" index 9081885a4fc..475d4e02b77 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.2.\347\231\273\345\275\225.md" @@ -56,13 +56,13 @@ import ( "star/utility" ) -type UserClaims struct { +type userClaims struct { Id uint Username string jwt.RegisteredClaims } -func Login(ctx context.Context, username, password string) (tokenString string, err error) { +func (u *Users) Login(ctx context.Context, username, password string) (tokenString string, err error) { var user entity.Users err = dao.Users.Ctx(ctx).Where("username", username).Scan(&user) if err != nil { @@ -79,14 +79,14 @@ func Login(ctx context.Context, username, password string) (tokenString string, } // Generate token - userClaims := &UserClaims{ + uc := &userClaims{ Id: user.Id, Username: user.Username, RegisteredClaims: jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(time.Now().Add(6 * time.Hour)), }, } - token := jwt.NewWithClaims(jwt.SigningMethodHS256, userClaims) + token := jwt.NewWithClaims(jwt.SigningMethodHS256, uc) return token.SignedString(utility.JwtKey) } ``` @@ -107,7 +107,7 @@ import ( ) func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) { - token, err := users.Login(ctx, req.Username, req.Password) + token, err := c.users.Login(ctx, req.Username, req.Password) if err != nil { return } diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" index aeb18f7b79a..9f31b0170f8 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\270\211\347\253\240-\344\274\232\350\257\235\347\256\241\347\220\206/3.3.\350\216\267\345\217\226\347\224\250\346\210\267\344\277\241\346\201\257.md" @@ -92,15 +92,15 @@ import ( ... -func Info(ctx context.Context) (user *entity.Users, err error) { +func (u *Users) Info(ctx context.Context) (user *entity.Users, err error) { user = new(entity.Users) tokenString := g.RequestFromCtx(ctx).Request.Header.Get("Authorization") - tokenClaims, _ := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { + tokenClaims, _ := jwt.ParseWithClaims(tokenString, &userClaims{}, func(token *jwt.Token) (interface{}, error) { return utility.JwtKey, nil }) - if claims, ok := tokenClaims.Claims.(*UserClaims); ok && tokenClaims.Valid { + if claims, ok := tokenClaims.Claims.(*userClaims); ok && tokenClaims.Valid { err = dao.Users.Ctx(ctx).Where("id", claims.Id).Scan(&user) } return @@ -113,6 +113,30 @@ The `Scan` method is a powerful one that automatically recognizes and converts b ## Controller Calls Logic --- +Also register 'logic' with the controller. + +*internal/controller/users/users_new.go* +```go +... + +package account + +import ( + "star/api/account" + usersL "star/internal/logic/users" +) + +type ControllerV1 struct { + users *usersL.Users +} + +func NewV1() account.IAccountV1 { + return &ControllerV1{ + users: &usersL.Users{}, + } +} +``` + *internal/controller/account/account_v1_info.go* ```go package account @@ -125,7 +149,7 @@ import ( ) func (c *ControllerV1) Info(ctx context.Context, req *v1.InfoReq) (res *v1.InfoRes, err error) { - user, err := users.Info(ctx) + user, err := c.users.Info(ctx) if err != nil { return nil, err } diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" index 20cc530193d..93e3a74ec42 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.3.\346\263\250\345\206\214\346\216\245\345\217\243.md" @@ -47,6 +47,18 @@ Among the four generated files, we only need to focus on `users_v1_register.go`, --- `Logic` is the business logic layer, stored in `internal/logic`, which is called by the `Controller` to implement specific business logic. +Define a 'Users' object: + +*internal/logic/users/users.go* +```go +package users + +type Users struct { +} +``` + +Write registration methods: + *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,30 @@ func Register(ctx context.Context, username, password, email string) error { --- The `Controller` layer is responsible for receiving `Req` request objects and then calling one or more `Logic` to complete the business logic. Some simple logic can also be directly handled in the `Controller`. The results of the processing are wrapped in the agreed `Res` data structure and returned. Here, the `Res` data structure is empty, so returning `nil` is sufficient. +Encapsulate the 'Users' object into the controller for easy subsequent calls. + +*internal/controller/users/users_new.go* +```go +... + +package users + +import ( + "star/api/users" + usersL "star/internal/logic/users" +) + +type ControllerV1 struct { + users *usersL.Users +} + +func NewV1() users.IUsersV1 { + return &ControllerV1{ + users: &usersL.Users{}, + } +} +``` + *internal/controller/users/users_v1_register.go* ```go package users @@ -90,7 +126,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 } ``` diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" index 8c1a42dd534..18df36b047b 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\214\347\253\240-\347\224\250\346\210\267\346\263\250\345\206\214/2.4.\344\270\232\345\212\241\344\274\230\345\214\226.md" @@ -153,8 +153,8 @@ package users ... -func Register(ctx context.Context, username, password, email string) error { - if err := checkUser(ctx, username); err != nil { +func (u *Users) Register(ctx context.Context, username, password, email string) error { + if err := u.checkUser(ctx, username); err != nil { return err } @@ -169,7 +169,7 @@ func Register(ctx context.Context, username, password, email string) error { return nil } -func checkUser(ctx context.Context, username string) error { +func (u *Users) checkUser(ctx context.Context, username string) error { count, err := dao.Users.Ctx(ctx).Where("username", username).Count() if err != nil { return err @@ -222,7 +222,7 @@ package users ... -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{ @@ -279,8 +279,8 @@ import ( ... ) -func Register(ctx context.Context, in *model.UserInput) error { - if err := CheckUser(ctx, in.Username); err != nil { +func (u *Users) Register(ctx context.Context, in *model.UserInput) error { + if err := u.checkUser(ctx, in.Username); err != nil { return err } @@ -310,7 +310,7 @@ import ( ) func (c *ControllerV1) Register(ctx context.Context, req *v1.RegisterReq) (res *v1.RegisterRes, err error) { - err = users.Register(ctx, &model.UserInput{ + err = c.users.Register(ctx, &model.UserInput{ Username: req.Username, Password: req.Password, Email: req.Email, diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" index a01179e68a4..7843020f1fe 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.2.\351\232\217\346\234\272\350\216\267\345\217\226\350\213\245\345\271\262\345\215\225\350\257\215.md" @@ -36,7 +36,7 @@ type RandListRes struct { ## Write Logic --- -*internal/controller/words/words_v1_rand_list.go* +*internal/logic/words/learn_words.go* ```go package words @@ -50,7 +50,7 @@ import ( ) // Rand Randomly retrieve a few words -func Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) { +func (w *Words) Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) { if limit <= 0 { limit = 50 } @@ -72,7 +72,7 @@ func Rand(ctx context.Context, uid, limit uint) ([]entity.Words, error) { ## Controller Calls Logic --- -*internal/logic/words/learn_words.go* +*internal/controller/words/words_v1_rand_list.go* ```go package words @@ -80,18 +80,16 @@ import ( "context" "star/api/words/v1" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" ) func (c *ControllerV1) RandList(ctx context.Context, req *v1.RandListReq) (res *v1.RandListRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - wordList, err := words.Rand(ctx, uid, req.Limit) + wordList, err := c.words.Rand(ctx, uid, req.Limit) if err != nil { return nil, err } diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" index 22c7422f917..86da10b92cb 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\344\272\224\347\253\240-\345\255\246\344\271\240\345\215\225\350\257\215/5.3.\350\256\276\347\275\256\346\216\214\346\217\241\347\250\213\345\272\246.md" @@ -26,12 +26,12 @@ Both `words/{id}/level` and `words/level/{id}` route styles are acceptable, but ## Write Logic --- -*internal/controller/words/words_v1_set_level.go* +*internal/logic/words/learn_words.go* ```go ... // SetLevel sets the word proficiency level -func SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error { +func (w *Words) SetLevel(ctx context.Context, uid, id uint, level model.ProficiencyLevel) error { if level < 0 || level > 5 { return gerror.New("Invalid proficiency level value") } @@ -50,7 +50,7 @@ To prevent data anomalies, we need to check if the level is between `1-5` before ## Controller calls Logic --- -*internal/logic/words/learn_words.go* +*internal/controller/words/words_v1_set_level.go* ```go package words @@ -58,17 +58,15 @@ import ( "context" "star/api/words/v1" - "star/internal/logic/users" - "star/internal/logic/words" ) func (c *ControllerV1) SetLevel(ctx context.Context, req *v1.SetLevelReq) (res *v1.SetLevelRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.SetLevel(ctx, uid, req.Id, req.Level) + err = c.words.SetLevel(ctx, uid, req.Id, req.Level) return nil, err } ``` diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" index 172e8acd36f..9bef0cf085b 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.3.\346\226\260\345\242\236\345\215\225\350\257\215.md" @@ -73,10 +73,13 @@ import ( "star/internal/dao" "star/internal/model" "star/internal/model/do" -) +) + +type Words struct { +} -func Create(ctx context.Context, in *model.WordInput) error { - if err := checkWord(ctx, in); err != nil { +func (w *Words) Create(ctx context.Context, in *model.WordInput) error { + if err := w.checkWord(ctx, in); err != nil { return err } @@ -95,7 +98,7 @@ func Create(ctx context.Context, in *model.WordInput) error { return nil } -func checkWord(ctx context.Context, in *model.WordInput) error { +func (w *Words) checkWord(ctx context.Context, in *model.WordInput) error { count, err := dao.Words.Ctx(ctx).Where("uid", in.Uid).Where("word", in.Word).Count() if err != nil { return err @@ -109,14 +112,13 @@ func checkWord(ctx context.Context, in *model.WordInput) error { In the `Logic` layer, we also need to ensure that the same user's words cannot be duplicated, keeping consistency with the database. -## Controller Calls Logic ---- -The word table contains a `uid` field, and we need to encapsulate a `GetUid` function in the `logic/users` package to provide `uid`. +### account logic +The 'uid' field is stored in the word list, and we need to provide the 'uid' by wrapping a 'GetUid' function in the 'logic/users' package. *internal/logic/users/account.go* ```go -func GetUid(ctx context.Context) (uint, error) { - user, err := Info(ctx) +func (u *Users) GetUid(ctx context.Context) (uint, error) { + user, err := u.Info(ctx) if err != nil { return 0, err } @@ -124,6 +126,38 @@ func GetUid(ctx context.Context) (uint, error) { } ``` +## Controller Calls Logic +--- +In the controller where the words are created, we need to call both 'account' and 'Words' logic, which we encapsulate in the controller. + + +*internal/controller/words/words_new.go* +```go +... + + +package words + +import ( + "star/api/words" + usersL "star/internal/logic/users" + wordsL "star/internal/logic/words" +) + +type ControllerV1 struct { + users *usersL.Users + words *wordsL.Words +} + +func NewV1() words.IWordsV1 { + return &ControllerV1{ + users: &usersL.Users{}, + words: &wordsL.Words{}, + } +} +``` + + *internal/controller/words/words_v1_create.go* ```go package words @@ -131,18 +165,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" "star/api/words/v1" ) func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.Create(ctx, &model.WordInput{ + err = c.words.Create(ctx, &model.WordInput{ Uid: uid, Word: req.Word, Definition: req.Definition, diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" index 8573fa53e0f..de3547001f0 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.4.\347\274\226\350\276\221\345\215\225\350\257\215.md" @@ -37,15 +37,15 @@ package words ... -func Create(ctx context.Context, in *model.WordInput) error { - if err := checkWord(ctx, 0, in); err != nil { +func (w *Words) Create(ctx context.Context, in *model.WordInput) error { + if err := w.checkWord(ctx, 0, in); err != nil { return err } ... } // checkWord does not check itself during an update -func checkWord(ctx context.Context, id uint, in *model.WordInput) error { +func (w *Words) checkWord(ctx context.Context, id uint, in *model.WordInput) error { db := dao.Words.Ctx(ctx).Where("uid", in.Uid).Where("word", in.Word) if id > 0 { db = db.WhereNot("id", id) @@ -69,8 +69,8 @@ package words ... -func Update(ctx context.Context, id uint, in *model.WordInput) error { - if err := checkWord(ctx, id, in); err != nil { +func (w *Words) Update(ctx context.Context, id uint, in *model.WordInput) error { + if err := w.checkWord(ctx, id, in); err != nil { return err } @@ -107,18 +107,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" "star/api/words/v1" ) func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.Update(ctx, req.Id, &model.WordInput{ + err = c.words.Update(ctx, req.Id, &model.WordInput{ Uid: uid, Word: req.Word, Definition: req.Definition, diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" index 04030686170..6cd96a23eb9 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.5.\345\215\225\350\257\215\345\210\206\351\241\265\345\210\227\350\241\250.md" @@ -70,7 +70,7 @@ type WordQuery struct { ```go ... -func List(ctx context.Context, query *model.WordQuery) (list []entity.Words, total uint, err error) { +func (w *Words) List(ctx context.Context, query *model.WordQuery) (list []entity.Words, total uint, err error) { if query == nil { query = &model.WordQuery{} } @@ -121,13 +121,11 @@ import ( "context" "star/api/words/v1" - "star/internal/logic/users" - "star/internal/logic/words" "star/internal/model" ) func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } @@ -138,7 +136,7 @@ func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListR Page: req.Page, Size: req.Size, } - wordList, total, err := words.List(ctx, query) + wordList, total, err := c.words.List(ctx, query) if err != nil { return nil, err } diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" index 12f9e5c1560..e7aa5a5d920 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.6.\345\215\225\350\257\215\350\257\246\346\203\205.md" @@ -38,7 +38,7 @@ type DetailRes struct { ```go ... -func Detail(ctx context.Context, uid, id uint) (word *entity.Words, err error) { +func (w *Words) Detail(ctx context.Context, uid, id uint) (word *entity.Words, err error) { word = &entity.Words{} db := dao.Words.Ctx(ctx).Where("id", id) if uid > 0 { @@ -58,19 +58,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" - "star/api/words/v1" ) func (c *ControllerV1) Detail(ctx context.Context, req *v1.DetailReq) (res *v1.DetailRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - word, err := words.Detail(ctx, uid, req.Id) + word, err := c.words.Detail(ctx, uid, req.Id) if err != nil { return nil, err } diff --git "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" index f390a634208..2490f84b562 100644 --- "a/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" +++ "b/i18n/en/docusaurus-plugin-content-docs/current/course/starbook/\347\254\254\345\233\233\347\253\240-\345\215\225\350\257\215\347\256\241\347\220\206/4.7.\345\210\240\351\231\244\345\215\225\350\257\215.md" @@ -25,7 +25,7 @@ type DeleteRes struct { *internal/logic/words/words.go* ```go ... -func Delete(ctx context.Context, uid, id uint) (err error) { +func (w *Words) Delete(ctx context.Context, uid, id uint) (err error) { db := dao.Words.Ctx(ctx).Where("id", id) if uid > 0 { db = db.Where("uid", uid) @@ -44,18 +44,16 @@ package words import ( "context" - "star/internal/logic/users" - "star/internal/logic/words" "star/api/words/v1" ) func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) { - uid, err := users.GetUid(ctx) + uid, err := c.users.GetUid(ctx) if err != nil { return nil, err } - err = words.Delete(ctx, uid, req.Id) + err = c.words.Delete(ctx, uid, req.Id) return } ```