Skip to content

Commit cbe1473

Browse files
committed
feat: V2 api for get hole
1 parent 3566423 commit cbe1473

File tree

8 files changed

+280
-48
lines changed

8 files changed

+280
-48
lines changed

apis/hole/apis.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,18 @@ func CreateHole(c *fiber.Ctx) error {
341341
}
342342

343343
hole := Hole{
344-
Floors: Floors{{
345-
UserID: user.ID,
346-
Content: body.Content,
347-
SpecialTag: body.SpecialTag,
348-
IsMe: true,
349-
IsSensitive: !sensitiveResp.Pass,
350-
SensitiveDetail: sensitiveResp.Detail,
351-
}},
352-
UserID: user.ID,
353-
DivisionID: divisionID,
344+
BaseHole: BaseHole{
345+
Floors: Floors{{
346+
UserID: user.ID,
347+
Content: body.Content,
348+
SpecialTag: body.SpecialTag,
349+
IsMe: true,
350+
IsSensitive: !sensitiveResp.Pass,
351+
SensitiveDetail: sensitiveResp.Detail,
352+
}},
353+
UserID: user.ID,
354+
DivisionID: divisionID,
355+
},
354356
}
355357
err = hole.Create(DB, user, body.ToName(), c)
356358
if err != nil {
@@ -410,16 +412,19 @@ func CreateHoleOld(c *fiber.Ctx) error {
410412

411413
// create hole
412414
hole := Hole{
413-
Floors: Floors{{
414-
UserID: user.ID,
415-
Content: body.Content,
416-
SpecialTag: body.SpecialTag,
417-
IsMe: true,
418-
IsSensitive: !sensitiveResp.Pass,
419-
SensitiveDetail: sensitiveResp.Detail,
420-
}},
421-
UserID: user.ID,
422-
DivisionID: body.DivisionID,
415+
BaseHole: BaseHole{
416+
Floors: Floors{{
417+
UserID: user.ID,
418+
Content: body.Content,
419+
SpecialTag: body.SpecialTag,
420+
IsMe: true,
421+
IsSensitive: !sensitiveResp.Pass,
422+
SensitiveDetail: sensitiveResp.Detail,
423+
}},
424+
UserID: user.ID,
425+
DivisionID: body.DivisionID,
426+
},
427+
423428
}
424429
err = hole.Create(DB, user, body.ToName(), c)
425430
if err != nil {
@@ -677,7 +682,7 @@ func HideHole(c *fiber.Ctx) error {
677682

678683
var hole Hole
679684
hole.ID = holeID
680-
result := DB.Model(&hole).Select("Hidden").Omit("UpdatedAt").Updates(Hole{Hidden: true})
685+
result := DB.Model(&hole).Select("Hidden").Omit("UpdatedAt").Updates(Hole{BaseHole:BaseHole{Hidden: true}})
681686
if result.RowsAffected == 0 {
682687
return gorm.ErrRecordNotFound
683688
}

apis/hole/routes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ func RegisterRoutes(app fiber.Router) {
2020
app.Put("/holes/:id<int>", ModifyHole)
2121
app.Delete("/holes/:id<int>", HideHole)
2222
app.Delete("/holes/:id<int>/_force", DeleteHole)
23+
24+
// V2
25+
app.Get("/v2/holes/:id<int>", GetHole)
2326
}

benchmarks/init.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ func init() {
5151
}
5252
return nowTags
5353
}
54-
holes = append(holes, &Hole{
55-
ID: i + 1,
56-
UserID: 1,
57-
DivisionID: rand.Intn(DIVISION_MAX) + 1,
58-
Tags: generateTag(),
54+
holes = append(holes, &Hole{
55+
BaseHole: BaseHole{
56+
ID: i + 1,
57+
UserID: 1,
58+
DivisionID: rand.Intn(DIVISION_MAX) + 1,
59+
Tags: generateTag(),
60+
},
5961
})
6062
}
6163

models/hole.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
"treehole_next/utils"
1818
)
1919

20-
type Hole struct {
20+
type Hole = HoleV1
21+
22+
type BaseHole struct {
2123
/// saved fields
22-
ID int `json:"id" gorm:"primaryKey"`
24+
ID int `json:"id" gorm:"primaryKey;"`
2325
CreatedAt time.Time `json:"time_created" gorm:"not null;index:idx_hole_div_cre,priority:2,sort:desc"`
2426
UpdatedAt time.Time `json:"time_updated" gorm:"not null;index:idx_hole_div_upd,priority:2,sort:desc"`
2527
DeletedAt gorm.DeletedAt `json:"time_deleted,omitempty" gorm:"index"`
@@ -54,7 +56,7 @@ type Hole struct {
5456
Tags Tags `json:"tags" gorm:"many2many:hole_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
5557

5658
// 楼层列表
57-
Floors Floors `json:"-" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
59+
Floors Floors `json:"-" gorm:"foreignKey:HoleID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
5860

5961
// 匿名映射表
6062
Mapping Users `json:"-" gorm:"many2many:anonyname_mapping;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
@@ -66,20 +68,26 @@ type Hole struct {
6668

6769
// 兼容旧版 id
6870
HoleID int `json:"hole_id" gorm:"-:all"`
71+
}
72+
73+
74+
type HoleV1 struct {
75+
BaseHole
6976

7077
// 返回给前端的楼层列表,包括首楼、尾楼和预加载的前 n 个楼层
7178
HoleFloor struct {
7279
FirstFloor *Floor `json:"first_floor"` // 首楼
7380
LastFloor *Floor `json:"last_floor"` // 尾楼
7481
Floors Floors `json:"prefetch"` // 预加载的楼层
7582
} `json:"floors" gorm:"-:all"`
83+
7684
}
7785

78-
func (hole *Hole) GetID() int {
86+
func (hole *BaseHole) GetID() int {
7987
return hole.ID
8088
}
8189

82-
func (hole *Hole) CacheName() string {
90+
func (hole *BaseHole) CacheName() string {
8391
return fmt.Sprintf("hole_%d", hole.ID)
8492
}
8593

@@ -97,7 +105,7 @@ func IsHolesExist(tx *gorm.DB, holeID []int) bool {
97105

98106
const HoleCacheExpire = time.Minute * 10
99107

100-
func loadTags(holes Holes) (err error) {
108+
func (holes Holes)loadTags() error {
101109
if len(holes) == 0 {
102110
return nil
103111
}
@@ -107,7 +115,7 @@ func loadTags(holes Holes) (err error) {
107115
}
108116

109117
var holeTags HoleTags
110-
err = DB.Where("hole_id in ?", holeIDs).Find(&holeTags).Error
118+
err := DB.Where("hole_id in ?", holeIDs).Find(&holeTags).Error
111119
if err != nil {
112120
return err
113121
}
@@ -144,7 +152,7 @@ func loadTags(holes Holes) (err error) {
144152
return nil
145153
}
146154

147-
func loadFloors(holes Holes) error {
155+
func (holes Holes) loadFloors() error {
148156
if len(holes) == 0 {
149157
return nil
150158
}
@@ -288,12 +296,12 @@ func (holes Holes) Preprocess(c *fiber.Ctx) error {
288296
}
289297

290298
func UpdateHoleCache(holes Holes) (err error) {
291-
err = loadFloors(holes)
299+
err = holes.loadFloors()
292300
if err != nil {
293301
return
294302
}
295303

296-
err = loadTags(holes)
304+
err = holes.loadTags()
297305
if err != nil {
298306
return
299307
}
@@ -351,7 +359,7 @@ func (holes Holes) MakeQuerySet(offset common.CustomTime, size int, order string
351359
// set hole.HoleFloor from hole.Floors or hole.HoleFloor.Floors
352360
// if Floors is not empty, set HoleFloor.Floors from Floors, in case loading from database
353361
// if Floors is empty, set HoleFloor.Floors from HoleFloor.Floors, in case loading from cache
354-
func (hole *Hole) SetHoleFloor() {
362+
func (hole *HoleV1) SetHoleFloor() {
355363
if len(hole.Floors) != 0 {
356364
holeFloorSize := len(hole.Floors)
357365

@@ -380,7 +388,7 @@ func (hole *Hole) SetHoleFloor() {
380388
//hole.HoleFloor.LastFloor.SetDefaults(c)
381389
}
382390

383-
func (hole *Hole) Create(tx *gorm.DB, user *User, tagNames []string, c *fiber.Ctx) (err error) {
391+
func (hole *HoleV1) Create(tx *gorm.DB, user *User, tagNames []string, c *fiber.Ctx) (err error) {
384392
// Create hole.Tags, in different sql session
385393
hole.Tags, err = FindOrCreateTags(tx, user, tagNames)
386394
if err != nil {
@@ -453,12 +461,12 @@ func (hole *Hole) Create(tx *gorm.DB, user *User, tagNames []string, c *fiber.Ct
453461
return utils.SetCache(hole.CacheName(), hole, HoleCacheExpire)
454462
}
455463

456-
func (hole *Hole) AfterCreate(_ *gorm.DB) (err error) {
464+
func (hole *BaseHole) AfterCreate(_ *gorm.DB) (err error) {
457465
hole.HoleID = hole.ID
458466
return nil
459467
}
460468

461-
func (hole *Hole) AfterFind(_ *gorm.DB) (err error) {
469+
func (hole *BaseHole) AfterFind(_ *gorm.DB) (err error) {
462470
hole.HoleID = hole.ID
463471
return nil
464472
}
@@ -473,7 +481,7 @@ func (holes Holes) RemoveIf(delCondition func(*Hole) bool) Holes {
473481
return result
474482
}
475483

476-
func (hole *Hole) HoleHook() {
484+
func (hole *HoleV1) HoleHook() {
477485
if hole == nil {
478486
return
479487
}

0 commit comments

Comments
 (0)