Skip to content

Commit

Permalink
feat: add storage of user basic info
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasyRL committed Dec 30, 2024
1 parent 526db69 commit fe925b9
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 39 deletions.
11 changes: 9 additions & 2 deletions cmd/user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@ import (
"github.com/west2-online/fzuhelper-server/config"
"github.com/west2-online/fzuhelper-server/internal/user"
"github.com/west2-online/fzuhelper-server/kitex_gen/user/userservice"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)

var serviceName = constants.UserServiceName
var (
serviceName = constants.UserServiceName
clientSet *base.ClientSet
)

func init() {
config.Init(serviceName)
logger.Init(serviceName, config.GetLoggerLevel())
// eshook.InitLoggerWithHook(serviceName)
clientSet = base.NewClientSet(
base.WithDBClient(constants.UserTableName),
)

Check warning on line 47 in cmd/user/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/user/main.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}

func main() {
Expand All @@ -55,7 +62,7 @@ func main() {
}

svr := userservice.NewServer(
new(user.UserServiceImpl),
user.NewUserService(clientSet),

Check warning on line 65 in cmd/user/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/user/main.go#L65

Added line #L65 was not covered by tests
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
ServiceName: serviceName,
}),
Expand Down
21 changes: 10 additions & 11 deletions config/sql/init.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
create table `fzu-helper`.`student`
(
`id` bigint not null comment 'ID',
`number` varchar(16) not null comment '学号',
`sex` varchar(8) not null comment '性别',
`college` varchar(255) not null comment '学院',
`grade` bigint not null comment '年级',
`major` varchar(255) not null comment '专业',
`created_at` timestamp default current_timestamp not null,
`updated_at` timestamp default current_timestamp not null on update current_timestamp comment 'update profile time',
`stu_id` varchar(16) not null comment '学号',
`sex` varchar(8) not null comment '性别',
`college` varchar(255) not null comment '学院',
`grade` bigint not null comment '年级',
`major` varchar(255) not null comment '专业',
`created_at` timestamp not null default current_timestamp,
`updated_at` timestamp not null default current_timestamp on update current_timestamp comment 'update profile time',
`deleted_at` timestamp default null null,
constraint `id`
primary key (`id`)
primary key (`stu_id`)
)engine=InnoDB default charset=utf8mb4;

create table `fzu-helper`.`term`
Expand All @@ -25,7 +24,7 @@ create table `fzu-helper`.`term`
primary key (`id`),
constraint `term_student`
foreign key (`stu_id`)
references `fzu-helper`.`student` (`id`)
references `fzu-helper`.`student` (`stu_id`)
on delete cascade
)engine=InnoDB default charset=utf8mb4;

Expand Down Expand Up @@ -54,7 +53,7 @@ create table `fzu-helper`.`mark`
primary key (`id`),
constraint `mark_student`
foreign key (`stu_id`)
references `fzu-helper`.`student` (`id`)
references `fzu-helper`.`student` (`stu_id`)
on delete cascade
)engine=InnoDB default charset=utf8mb4;

Expand Down
12 changes: 10 additions & 2 deletions internal/user/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ import (
)

// UserServiceImpl implements the last service interface defined in the IDL.
type UserServiceImpl struct{}
type UserServiceImpl struct {
ClientSet *base.ClientSet
}

func NewUserService(clientSet *base.ClientSet) *UserServiceImpl {
return &UserServiceImpl{
ClientSet: clientSet,
}

Check warning on line 35 in internal/user/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/user/handler.go#L32-L35

Added lines #L32 - L35 were not covered by tests
}

// GetLoginData implements the UserServiceImpl interface.
func (s *UserServiceImpl) GetLoginData(ctx context.Context, req *user.GetLoginDataRequest) (resp *user.GetLoginDataResponse, err error) {
resp = new(user.GetLoginDataResponse)
l := service.NewUserService(ctx, "", nil)
l := service.NewUserService(ctx, "", nil, s.ClientSet)

Check warning on line 41 in internal/user/handler.go

View check run for this annotation

Codecov / codecov/patch

internal/user/handler.go#L41

Added line #L41 was not covered by tests
id, cookies, err := l.GetLoginData(req)
if err != nil {
resp.Base = base.BuildBaseResp(err)
Expand Down
44 changes: 43 additions & 1 deletion internal/user/service/get_logindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,57 @@ limitations under the License.
package service

import (
"strconv"
"time"

"gorm.io/gorm"

"github.com/west2-online/fzuhelper-server/kitex_gen/user"
"github.com/west2-online/fzuhelper-server/pkg/db/model"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

func (s *UserService) GetLoginData(req *user.GetLoginDataRequest) (string, []string, error) {
id, rawCookies, err := jwch.NewStudent().WithUser(req.Id, req.Password).GetIdentifierAndCookies()
stu := jwch.NewStudent().WithUser(req.Id, req.Password)
id, rawCookies, err := stu.GetIdentifierAndCookies()

Check warning on line 34 in internal/user/service/get_logindata.go

View check run for this annotation

Codecov / codecov/patch

internal/user/service/get_logindata.go#L33-L34

Added lines #L33 - L34 were not covered by tests
if err != nil {
return "", nil, err
}
// 进行学生信息的存储
go s.insertStudentInfo(req, stu)

Check warning on line 40 in internal/user/service/get_logindata.go

View check run for this annotation

Codecov / codecov/patch

internal/user/service/get_logindata.go#L39-L40

Added lines #L39 - L40 were not covered by tests
return id, utils.ParseCookiesToString(rawCookies), nil
}

func (s *UserService) insertStudentInfo(req *user.GetLoginDataRequest, stu *jwch.Student) {
// 查询数据库是否存入此学生信息
exist, _, err := s.db.User.GetStudentById(s.ctx, req.Id)
if err != nil {
logger.Errorf("service.insertStudentInfo: %v", err)
}
if exist {
return
}

Check warning on line 52 in internal/user/service/get_logindata.go

View check run for this annotation

Codecov / codecov/patch

internal/user/service/get_logindata.go#L44-L52

Added lines #L44 - L52 were not covered by tests
// 若未查询则将学生信息插入
resp, err := stu.GetInfo()
if err != nil {
logger.Errorf("service.insertStudentInfo: jwch failed: %v", err)
}
grade, _ := strconv.Atoi(resp.Grade)
userModel := &model.Student{
StuId: req.Id,
Sex: resp.Sex,
College: resp.College,
Grade: int64(grade),
Major: resp.Major,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: gorm.DeletedAt{},
}
err = s.db.User.CreateStudent(s.ctx, userModel)
if err != nil {
logger.Errorf("service.insertStudentInfo: %v", err)
}

Check warning on line 72 in internal/user/service/get_logindata.go

View check run for this annotation

Codecov / codecov/patch

internal/user/service/get_logindata.go#L54-L72

Added lines #L54 - L72 were not covered by tests
}
7 changes: 6 additions & 1 deletion internal/user/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ package service
import (
"context"
"net/http"

"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/db"
)

type UserService struct {
ctx context.Context
Identifier string
cookies []*http.Cookie
db *db.Database
}

func NewUserService(ctx context.Context, identifier string, cookies []*http.Cookie) *UserService {
func NewUserService(ctx context.Context, identifier string, cookies []*http.Cookie, clientset *base.ClientSet) *UserService {

Check warning on line 34 in internal/user/service/service.go

View check run for this annotation

Codecov / codecov/patch

internal/user/service/service.go#L34

Added line #L34 was not covered by tests
return &UserService{
ctx: ctx,
Identifier: identifier,
cookies: cookies,
db: clientset.DBClient,

Check warning on line 39 in internal/user/service/service.go

View check run for this annotation

Codecov / codecov/patch

internal/user/service/service.go#L39

Added line #L39 was not covered by tests
}
}
5 changes: 3 additions & 2 deletions pkg/constants/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
)

const (
UserTableName = "user"
CourseTableName = "course"
UserTableName = "student"
CourseTableName = "course"
LaunchScreenTableName = "launch_screen"
)
1 change: 0 additions & 1 deletion pkg/constants/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ const (
PaperServiceName = "paper"
VersionServiceName = "version"
AcademicServiceName = "academic"
LaunchScreenTableName = "launch_screen"
CommonServiceName = "common"
)
3 changes: 3 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/west2-online/fzuhelper-server/pkg/db/course"
"github.com/west2-online/fzuhelper-server/pkg/db/launch_screen"
"github.com/west2-online/fzuhelper-server/pkg/db/user"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)

Expand All @@ -29,6 +30,7 @@ type Database struct {
sf *utils.Snowflake
Course *course.DBCourse
LaunchScreen *launch_screen.DBLaunchScreen
User *user.DBUser
}

func NewDatabase(client *gorm.DB, sf *utils.Snowflake) *Database {
Expand All @@ -37,5 +39,6 @@ func NewDatabase(client *gorm.DB, sf *utils.Snowflake) *Database {
sf: sf,
Course: course.NewDBCourse(client, sf),
LaunchScreen: launch_screen.NewDBLaunchScreen(client, sf),
User: user.NewDBUser(client, sf),

Check warning on line 42 in pkg/db/db.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/db.go#L42

Added line #L42 was not covered by tests
}
}
17 changes: 13 additions & 4 deletions cmd/user/dal/init.go → pkg/db/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package dal
package model

import (
"github.com/west2-online/fzuhelper-server/cmd/user/dal/db"
"time"

"gorm.io/gorm"
)

func Init() {
db.InitMySQL()
type Student struct {
StuId string
Sex string
College string
Grade int64
Major string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `sql:"index"`
}
31 changes: 31 additions & 0 deletions pkg/db/user/create_student.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package user

import (
"context"
"fmt"

"github.com/west2-online/fzuhelper-server/pkg/db/model"
)

func (c *DBUser) CreateStudent(ctx context.Context, userModel *model.Student) error {
if err := c.client.WithContext(ctx).Create(&userModel).Error; err != nil {
return fmt.Errorf("dal.CreateStudent error: %w", err)
}
return nil

Check warning on line 30 in pkg/db/user/create_student.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/user/create_student.go#L26-L30

Added lines #L26 - L30 were not covered by tests
}
38 changes: 38 additions & 0 deletions pkg/db/user/get_student.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package user

import (
"context"
"errors"
"fmt"

"gorm.io/gorm"

"github.com/west2-online/fzuhelper-server/pkg/db/model"
)

func (c *DBUser) GetStudentById(ctx context.Context, stuId string) (bool, *model.Student, error) {
stuModel := new(model.Student)
if err := c.client.WithContext(ctx).Where("stu_id = ?", stuId).First(stuModel).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil, nil
}
return false, nil, fmt.Errorf("dal.GetStudentById error:%w", err)

Check warning on line 35 in pkg/db/user/get_student.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/user/get_student.go#L29-L35

Added lines #L29 - L35 were not covered by tests
}
return true, stuModel, nil

Check warning on line 37 in pkg/db/user/get_student.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/user/get_student.go#L37

Added line #L37 was not covered by tests
}
25 changes: 10 additions & 15 deletions cmd/user/dal/db/init.go → pkg/db/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package db
package user

import (
"github.com/west2-online/fzuhelper-server/pkg/client"
"github.com/west2-online/fzuhelper-server/pkg/logger"
"github.com/west2-online/fzuhelper-server/pkg/utils"

"gorm.io/gorm"

"github.com/west2-online/fzuhelper-server/pkg/constants"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)

var (
DB *gorm.DB
SF *utils.Snowflake
err error
)
type DBUser struct {
client *gorm.DB
sf *utils.Snowflake
}

func InitMySQL() {
DB, SF, err = client.InitMySQL(constants.UserTableName)
if err != nil {
logger.Fatal(err)
func NewDBUser(client *gorm.DB, sf *utils.Snowflake) *DBUser {
return &DBUser{
client: client,
sf: sf,
}

Check warning on line 34 in pkg/db/user/user.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/user/user.go#L30-L34

Added lines #L30 - L34 were not covered by tests
}

0 comments on commit fe925b9

Please sign in to comment.