Skip to content

Commit be7a91d

Browse files
committed
<feat>(practice):总人数答题情况;<fix>(practice,test):点赞,最热最新,top重复问题
1 parent 09163cb commit be7a91d

11 files changed

Lines changed: 124 additions & 26 deletions

File tree

circle/api.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,9 @@ POST
10121012

10131013
Body:JSON
10141014

1015-
| circle | 圈子(有两个推荐,一个要circle,如果不用则不需要这个数据) |
1016-
| ------ | --------------------------------------------------------- |
1015+
| circle | 圈子(有两个推荐,一个要circle,如果不用则不需要这个数据) |
1016+
| --------- | --------------------------------------------------------- |
1017+
| page(int) | 请求次数 |
10171018

10181019
Response:JSON
10191020

@@ -1043,8 +1044,9 @@ POST
10431044

10441045
Body:JSON
10451046

1046-
| circle | 圈子(有两个推荐,一个要circle,如果不用则不需要这个数据) |
1047-
| ------ | --------------------------------------------------------- |
1047+
| circle | 圈子(有两个推荐,一个要circle,如果不用则不需要这个数据) |
1048+
| --------- | --------------------------------------------------------- |
1049+
| page(int) | 请求次数 |
10481050

10491051
Response:JSON
10501052

@@ -1370,4 +1372,28 @@ Response:JSON
13701372
}
13711373
```
13721374

1373-
# 七牛云token:0bNiwJGpdwmvvuVAzLDjM6gnxj9MiwmSagVpIW81:85DTubmQkSKtCyWaL5KoaucrQKU=:eyJkZWFkbGluZSI6MTczODU3NjI0MCwic2NvcGUiOiJtdXhpLW1pbmlwcm9qZWN0In0=
1375+
1376+
1377+
## 新增
1378+
1379+
### /practice/getpracticesituation
1380+
1381+
POST
1382+
1383+
Body:JSON
1384+
1385+
| practiceid | 练习id |
1386+
| ---------- | ------ |
1387+
1388+
Response:JSON
1389+
1390+
```json
1391+
{
1392+
"message": {
1393+
"Practiceid": 1,
1394+
"Peoplenum": 4,
1395+
"Correctnum": 2
1396+
}
1397+
}
1398+
```
1399+

circle/controllers/practice_contorllers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,13 @@ func (uc *PracticeControllers) Showlovepractice(c *gin.Context) {
143143
n,_:=name.(string)
144144
message:=uc.us.Showlovepractice(n,get)
145145
c.JSON(200, gin.H{"message":message})
146+
}
147+
func (uc *PracticeControllers) GetPracticeSituation(c *gin.Context) {
148+
var get request.GetPractice
149+
if err := c.ShouldBindJSON(&get); err != nil {
150+
c.JSON(400, gin.H{"error": "无效的参数"})
151+
return
152+
}
153+
message:=uc.us.GetPracticeSituation(get)
154+
c.JSON(200, gin.H{"message":message})
146155
}

circle/dao/practice_dao.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,27 @@ func (ud *PracticeDao) GetIdByUser(name string) (int, error) {
139139
err := database.DB.Model(&models.User{}).Where("name = ?", name).Select("id").First(&id).Error
140140
return id, err
141141
}
142+
func (ud *PracticeDao) CreatePracticeSituation(practiceid int) error {
143+
practicesituation:= models.PracticeSituation{
144+
Practiceid: practiceid,
145+
Peoplenum: 0,
146+
Correctnum: 0,
147+
}
148+
err := database.DB.Create(&practicesituation).Error
149+
if err != nil {
150+
return fmt.Errorf("创建失败: %w", err)
151+
}
152+
return nil
153+
}
154+
func (ud *PracticeDao) GetPracticeSituation(practiceid int) (models.PracticeSituation) {
155+
var practicesituation models.PracticeSituation
156+
_ = database.DB.Where("practiceid = ?", practiceid).First(&practicesituation)
157+
return practicesituation
158+
}
159+
func (ud *PracticeDao) UpdatePracticeSituation(practicesituation *models.PracticeSituation,practiceid int) error {
160+
err := database.DB.Where("practiceid = ?" ,practiceid).Save(practicesituation).Error
161+
if err != nil {
162+
return fmt.Errorf("更新练习记录失败: %w", err)
163+
}
164+
return nil
165+
}

circle/dao/search_dao.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ func (ud *SearchDao) SearchTest(testkey string) ([]models.Test) {
2121
return test
2222
}
2323
func (ud *SearchDao) SearchHistory(searchkey string,userid int) {
24-
searchhistory:=models.SearchHistory{
25-
SearchKey:searchkey,
26-
Userid:userid,
24+
var originalhistory models.SearchHistory
25+
err:=database.DB.Where("search_key = ?", searchkey).Where("userid = ?", userid).First(&originalhistory).Error
26+
if err == gorm.ErrRecordNotFound {
27+
searchhistory:=models.SearchHistory{
28+
SearchKey:searchkey,
29+
Userid:userid,
30+
}
31+
database.DB.Create(&searchhistory)
2732
}
28-
database.DB.Create(&searchhistory)
2933
}
3034
func (ud *SearchDao) ShowSearchHistory(userid int) ([]models.SearchHistory){
3135
var searchhistory []models.SearchHistory

circle/dao/test_dao.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,22 @@ func (ud *TestDao) GetTestOptionsByPracticeID(practiceid int) ([]models.TestOpti
7070
return options, nil
7171
}
7272
func (ud *TestDao) SaveTopRecord(top models.Top) error {
73-
err := database.DB.Create(&top).Error
74-
if err != nil {
75-
return fmt.Errorf("保存成绩记录失败: %w", err)
73+
var origintop models.Top
74+
err:= database.DB.Where("testid = ? and userid = ?", top.Testid, top.Userid).First(&origintop).Error
75+
if err == gorm.ErrRecordNotFound {
76+
err := database.DB.Create(&top).Error
77+
if err != nil {
78+
return fmt.Errorf("保存成绩记录失败: %w", err)
79+
}
80+
return nil
81+
}else if err != nil {
82+
return fmt.Errorf("获取成绩记录失败: %w", err)
83+
}
84+
if err==nil&&origintop.Correctnum < top.Correctnum {
85+
err := database.DB.Model(&origintop).Updates(models.Top{Correctnum: top.Correctnum, Time: top.Time}).Error
86+
if err != nil {
87+
return fmt.Errorf("更新成绩记录失败: %w", err)
88+
}
7689
}
7790
return nil
7891
}
@@ -114,21 +127,23 @@ func (ud *TestDao) RecommentTest(circle string) ([]models.Test){
114127
}
115128
return test
116129
}
117-
func (ud *TestDao) HotTest(circle string) ([]models.Test){
130+
func (ud *TestDao) HotTest(circle string,page int) ([]models.Test){
118131
var test []models.Test
132+
offset := (page-1)*6
119133
if circle=="" {
120-
_= database.DB.Order("good desc").Limit(6).Find(&test).Error
134+
_= database.DB.Order("good desc").Offset(offset).Limit(6).Find(&test).Error
121135
}else{
122-
_= database.DB.Where("circle = ?", circle).Order("good desc").Limit(6).Find(&test).Error
136+
_= database.DB.Where("circle = ?", circle).Order("good desc").Offset(offset).Limit(6).Find(&test).Error
123137
}
124138
return test
125139
}
126-
func (ud *TestDao) NewTest(circle string) ([]models.Test){
140+
func (ud *TestDao) NewTest(circle string,page int) ([]models.Test){
127141
var test []models.Test
142+
offset := (page-1)*6
128143
if circle=="" {
129-
_= database.DB.Order("createtime desc").Limit(6).Find(&test).Error
144+
_= database.DB.Order("createtime desc").Offset(offset).Limit(6).Find(&test).Error
130145
}else{
131-
_= database.DB.Where("circle = ?", circle).Order("createtime desc").Limit(6).Find(&test).Error
146+
_= database.DB.Where("circle = ?", circle).Order("createtime desc").Offset(offset).Limit(6).Find(&test).Error
132147
}
133148
return test
134149
}
@@ -138,7 +153,7 @@ func (ud *TestDao) FollowCircleTest(userid int) ([]models.Test){
138153
var circlename []string
139154
_= database.DB.Model(&models.FollowCircle{}).Where("userid = ?", userid).Pluck("circleid", &circleid).Error //pluck表示查询单个数据
140155
_= database.DB.Model(&models.Circle{}).Where("id in (?)", circleid).Pluck("name", &circlename).Error
141-
_= database.DB.Where("circle in (?)", circlename).Order("RAND()").Limit(10).Find(&test).Error //in表示查询多个数据
156+
_= database.DB.Where("circle in (?)", circlename).Order("RAND()").Limit(6).Find(&test).Error //in表示查询多个数据
142157
return test
143158
}
144159
func (ud *TestDao) GetIdByUser(name string) (int, error) {

circle/database/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func InitDB() {
8080
&models.Circle{},
8181
&models.FollowCircle{},
8282
&models.SearchHistory{},
83+
&models.PracticeSituation{},
8384
); err != nil {
8485
log.Fatalf("自动迁移失败: %v", err)
8586
}

circle/models/practice_models.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ type UserPractice struct {
3939
Alltime int
4040
Circle string
4141
}
42+
type PracticeSituation struct {
43+
Practiceid int `gorm:"index:idx_practiceid"`
44+
Peoplenum int
45+
Correctnum int
46+
}

circle/request/test_request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ type Commenttest struct{
3030
}
3131
type GetCircle struct{
3232
Circle string `json:"circle"`
33+
Page int `json:"page"`
3334
}

circle/routes/practice_routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ func RunPractice(db *gorm.DB,r *gin.Engine){
2424
practice.POST("/lovepractice", uc.Lovepractice)
2525
practice.POST("/unlovepractice", uc.Unlovepractice)
2626
practice.POST("/showlovepractice", uc.Showlovepractice)
27+
practice.POST("/getpracticesituation", uc.GetPracticeSituation)
2728
}
2829
}

circle/service/practice_service.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (us *PracticeServices) Createpractice(name string,practice request.Practice
3333
if err != nil {
3434
return -1
3535
}
36+
_=us.ud.CreatePracticeSituation(practices.Practiceid)
3637
return practices.Practiceid
3738
}
3839
func (us *PracticeServices) Createoption(option request.Option) string {
@@ -82,15 +83,22 @@ func (us *PracticeServices) CheckAnswer(name string,get request.CheckAnswer) str
8283
if err != nil {
8384
return "用户不存在"
8485
}
86+
practicesituation:=us.ud.GetPracticeSituation(get.Practiceid)
87+
practicesituation.Peoplenum++
8588
userpractice, err := us.ud.GetUserPracticeByUserID(user.Id, get.Circle)
8689
if err != nil {
8790
return "用户练习记录不存在"
8891
}
8992
userpractice.Alltime += get.Time
9093
userpractice.Practicenum++
9194
if get.Answer == "true" {
95+
practicesituation.Correctnum++
9296
userpractice.Correctnum++
9397
}
98+
err = us.ud.UpdatePracticeSituation(&practicesituation, get.Practiceid)
99+
if err != nil {
100+
return err.Error()
101+
}
94102
err = us.ud.UpdateUserPractice(userpractice)
95103
if err != nil {
96104
return "更新练习记录失败"
@@ -143,12 +151,16 @@ func (us *PracticeServices) Unlovepractice(name string,get request.GetPractice)
143151
func (us *PracticeServices) Showlovepractice(name string,get request.GetPractice) string {
144152
userID, _:=us.ud.GetIdByUser(name)
145153
practiceid:=fmt.Sprintf("%d", get.Practiceid)
146-
count,err := database.Rdb.SAdd("practicelikes:"+practiceid, userID).Result()
154+
count,err := database.Rdb.SIsMember("practicelikes:"+practiceid, userID).Result()
147155
if err != nil {
148156
return "查看是否点赞失败"
149157
}
150-
if count == 0 {
158+
if count {
151159
return "已经点过赞"
152160
}
153161
return "没有点过赞"
154-
}
162+
}
163+
func (us *PracticeServices) GetPracticeSituation(get request.GetPractice) models.PracticeSituation {
164+
practicesituation:= us.ud.GetPracticeSituation(get.Practiceid)
165+
return practicesituation
166+
}

0 commit comments

Comments
 (0)