From 821dda53951a17dd745982eeb7149f46a154aa27 Mon Sep 17 00:00:00 2001 From: shaoyongjun Date: Mon, 30 Sep 2024 15:41:00 +0800 Subject: [PATCH] to:sync --- infrastructure/repository/as_db_pg.go | 2 +- infrastructure/repository/as_db_pg_test.go | 2 +- .../repository/{g_account.go => user_base.go} | 0 ..._repository.go => user_base_repository.go} | 20 +++--- .../repository/user_base_repository_test.go | 71 +++++++++++++++++++ service/{third_login.go => login.go} | 8 +-- .../{third_login_test.go => login_test.go} | 0 7 files changed, 87 insertions(+), 16 deletions(-) rename infrastructure/repository/{g_account.go => user_base.go} (100%) rename infrastructure/repository/{g_account_repository.go => user_base_repository.go} (90%) create mode 100644 infrastructure/repository/user_base_repository_test.go rename service/{third_login.go => login.go} (95%) rename service/{third_login_test.go => login_test.go} (100%) diff --git a/infrastructure/repository/as_db_pg.go b/infrastructure/repository/as_db_pg.go index 77feb5b..41bbf4e 100644 --- a/infrastructure/repository/as_db_pg.go +++ b/infrastructure/repository/as_db_pg.go @@ -18,7 +18,7 @@ func getInstance() *gorm.DB { // unmarshal config ll := logs.NewZapLogger() if config.Instance.PgSql.LogSql { - ll = ll.LogMode(loglevel.Info) + ll = ll.LogMode(loglevel.Warn) } else { ll = ll.LogMode(loglevel.Warn) } diff --git a/infrastructure/repository/as_db_pg_test.go b/infrastructure/repository/as_db_pg_test.go index 74a957e..8a9363f 100644 --- a/infrastructure/repository/as_db_pg_test.go +++ b/infrastructure/repository/as_db_pg_test.go @@ -33,7 +33,7 @@ func TestPg(t *testing.T) { } var total int - db.Raw("select max(id) from user").Scan(&total) + db.Raw("select max(id) from user_base").Scan(&total) // 打印成功信息 fmt.Println("连接数据库成功", total) diff --git a/infrastructure/repository/g_account.go b/infrastructure/repository/user_base.go similarity index 100% rename from infrastructure/repository/g_account.go rename to infrastructure/repository/user_base.go diff --git a/infrastructure/repository/g_account_repository.go b/infrastructure/repository/user_base_repository.go similarity index 90% rename from infrastructure/repository/g_account_repository.go rename to infrastructure/repository/user_base_repository.go index afaa825..34e3e98 100644 --- a/infrastructure/repository/g_account_repository.go +++ b/infrastructure/repository/user_base_repository.go @@ -19,8 +19,8 @@ type UserDO struct { Avatar *string `gorm:"column:avatar;type:varchar(128);comment:头像" json:"avatar"` Gender uint `gorm:"column:gender;type:smallint(4) unsigned;default:0;comment:性别。0:未知 ;1:男;2:女; 3:其他" json:"gender"` Phone *string `gorm:"column:phone;type:varchar(16);comment:手机号码" json:"phone"` - WxId *string `gorm:"column:wxId;type:varchar(16);comment:微信unionId" json:"wxId"` - QqId *string `gorm:"column:qqId;type:varchar(16);comment:qqId" json:"qqId"` + WxId *string `gorm:"column:wx_id;type:varchar(16);comment:微信unionId" json:"wx_id"` + QqId *string `gorm:"column:qq_id;type:varchar(16);comment:qqId" json:"qq_id"` Email *string `gorm:"column:email;type:varchar(64);comment:email" json:"email"` GoogleId *string `gorm:"column:google_id;type:varchar(32);comment:GoogleId" json:"google_id"` FacebookId *string `gorm:"column:facebook_id;type:varchar(32);comment:FacebookId" json:"facebook_id"` @@ -30,17 +30,17 @@ type UserDO struct { RegisterSource string `gorm:"column:register_source;type:varchar(16);comment:注册来源" json:"register_source"` LatestIp string `gorm:"column:latest_ip;type:varchar(32);comment:最近登录的ip" json:"latest_ip"` - LatestLoginTime string `gorm:"column:latest_login_time;type:varchar(32);comment:最近登录的时间戳" json:"latest_login_time"` + LatestLoginTime uint64 `gorm:"column:latest_login_time;type:varchar(32);comment:最近登录的时间戳" json:"latest_login_time"` Status uint `gorm:"column:status;type:smallint(4) unsigned;default:1;comment:是否有效。0:否;1:是;NOT NULL" json:"status"` - Deleted uint `gorm:"column:deleted;type:smallint(4) unsigned;default:0;comment:逻辑删除。 0:未删除;1:已删除;NOT NULL" json:"deleted"` + Deleted bool `gorm:"column:deleted;type:smallint(4) unsigned;default:0;comment:逻辑删除。 0:未删除;1:已删除;NOT NULL" json:"deleted"` Remark string `gorm:"column:remark;type:varchar(64);comment:备注" json:"remark"` CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:创建时间;NOT NULL" json:"create_time"` UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP;comment:更新时间;NOT NULL" json:"update_time"` } func (m *UserDO) TableName() string { - return "g_account" + return "user_base" } type gUserRepositoryImpl struct { @@ -151,7 +151,7 @@ func (rp *gUserRepositoryImpl) FindByWxId(ctx context.Context, wxId string) *Use db = Db } var model UserDO - sqlErr := db.Model(&model).Where("wxId=?", wxId).First(&model).Error + sqlErr := db.Model(&model).Where("wx_id=?", wxId).First(&model).Error if sqlErr != nil { return nil } @@ -168,7 +168,7 @@ func (rp *gUserRepositoryImpl) FindByQqId(ctx context.Context, qqId string) *Use db = Db } var model UserDO - sqlErr := db.Model(&model).Where("qqId=?", qqId).First(&model).Error + sqlErr := db.Model(&model).Where("qq_id=?", qqId).First(&model).Error if sqlErr != nil { return nil } @@ -185,7 +185,7 @@ func (rp *gUserRepositoryImpl) FindByGoogleId(ctx context.Context, googleId stri db = Db } var model UserDO - sqlErr := db.Model(&model).Where("googleId=?", googleId).First(&model).Error + sqlErr := db.Model(&model).Where("google_id=?", googleId).First(&model).Error if sqlErr != nil { return nil } @@ -202,7 +202,7 @@ func (rp *gUserRepositoryImpl) FindByFacebookId(ctx context.Context, facebookId db = Db } var model UserDO - sqlErr := db.Model(&model).Where("facebookId=?", facebookId).First(&model).Error + sqlErr := db.Model(&model).Where("facebook_id=?", facebookId).First(&model).Error if sqlErr != nil { return nil } @@ -216,7 +216,7 @@ func (rp *gUserRepositoryImpl) Create(ctx context.Context, data *UserDO) error { db = Db } - data.Deleted = 0 + data.Deleted = false return db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "email"}}, diff --git a/infrastructure/repository/user_base_repository_test.go b/infrastructure/repository/user_base_repository_test.go new file mode 100644 index 0000000..fb7b2bc --- /dev/null +++ b/infrastructure/repository/user_base_repository_test.go @@ -0,0 +1,71 @@ +package repository + +import ( + "context" + "testing" +) + +func TestFindById(t *testing.T) { + + data := GUser.FindById(context.Background(), 2) + if data.Id != uint64(2) { + t.Error("err") + } + +} + +func TestFindByAccount(t *testing.T) { + query := "syj" + data := GUser.FindByAccount(context.Background(), query) + if *data.Account != query { + t.Error("err") + } +} + +func TestFindByPhone(t *testing.T) { + query := "19901639350" + data := GUser.FindByPhone(context.Background(), query) + if *data.Phone != query { + t.Error("err") + } +} + +func TestFindByEmail(t *testing.T) { + query := "1290251929@qq.com" + data := GUser.FindByEmail(context.Background(), query) + if *data.Email != query { + t.Error("err") + } +} + +func TestFindByWxId(t *testing.T) { + query := "unionsyj" + data := GUser.FindByWxId(context.Background(), query) + if *data.WxId != query { + t.Error("err") + } +} + +func TestFindByQqId(t *testing.T) { + query := "1290251929" + data := GUser.FindByQqId(context.Background(), query) + if *data.QqId != query { + t.Error("err") + } +} + +func TestFindByGoogleId(t *testing.T) { + query := "googleIdsyj" + data := GUser.FindByGoogleId(context.Background(), query) + if *data.GoogleId != query { + t.Error("err") + } +} + +func TestFindByFacebookId(t *testing.T) { + query := "facebookIdSyj" + data := GUser.FindByFacebookId(context.Background(), query) + if *data.FacebookId != query { + t.Error("err") + } +} diff --git a/service/third_login.go b/service/login.go similarity index 95% rename from service/third_login.go rename to service/login.go index 32968e5..391aae1 100644 --- a/service/third_login.go +++ b/service/login.go @@ -32,7 +32,7 @@ var limiter = rate.NewLimiter(10, 10) // Register 注册 func (l login) Register(ctx context.Context, req dto.RegisterReq) error { acUser := repository.GUser.FindByReq(ctx, req.LoginReq) - if acUser != nil && acUser.Deleted == 0 { + if acUser != nil && acUser.Deleted == false { return errors.New("user exist") } @@ -51,7 +51,7 @@ func (l login) Login(ctx context.Context, req dto.LoginReq) (*dto.UserVO, error) //1. 查询用户 acUser := repository.GUser.FindByReq(ctx, req) - if acUser == nil || acUser.Deleted == 1 { + if acUser == nil || acUser.Deleted == true { return nil, errors.New("user not exist") } @@ -84,7 +84,7 @@ func (l login) Login(ctx context.Context, req dto.LoginReq) (*dto.UserVO, error) func (l login) SendResetPwdCode(ctx context.Context, account string) error { //1. 验证账号 acUser := repository.GUser.FindByAccount(ctx, account) - if acUser == nil || acUser.Deleted == 1 { + if acUser == nil || acUser.Deleted == true { return errors.New("user not exist") } @@ -119,7 +119,7 @@ func (l login) ResetPwd(ctx context.Context, req dto.ResetPwdReq) error { //2. 查询用户 acUser := repository.GUser.FindByAccount(ctx, req.Account) - if acUser == nil || acUser.Deleted == 1 { + if acUser == nil || acUser.Deleted == true { return errors.New("user not exist") } diff --git a/service/third_login_test.go b/service/login_test.go similarity index 100% rename from service/third_login_test.go rename to service/login_test.go