Skip to content

feat: V2 API for get hole #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions apis/hole/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,18 @@ func CreateHole(c *fiber.Ctx) error {
}

hole := Hole{
Floors: Floors{{
UserID: user.ID,
Content: body.Content,
SpecialTag: body.SpecialTag,
IsMe: true,
IsSensitive: !sensitiveResp.Pass,
SensitiveDetail: sensitiveResp.Detail,
}},
UserID: user.ID,
DivisionID: divisionID,
BaseHole: BaseHole{
Floors: Floors{{
UserID: user.ID,
Content: body.Content,
SpecialTag: body.SpecialTag,
IsMe: true,
IsSensitive: !sensitiveResp.Pass,
SensitiveDetail: sensitiveResp.Detail,
}},
UserID: user.ID,
DivisionID: divisionID,
},
}
err = hole.Create(DB, user, body.ToName(), c)
if err != nil {
Expand Down Expand Up @@ -410,16 +412,19 @@ func CreateHoleOld(c *fiber.Ctx) error {

// create hole
hole := Hole{
Floors: Floors{{
UserID: user.ID,
Content: body.Content,
SpecialTag: body.SpecialTag,
IsMe: true,
IsSensitive: !sensitiveResp.Pass,
SensitiveDetail: sensitiveResp.Detail,
}},
UserID: user.ID,
DivisionID: body.DivisionID,
BaseHole: BaseHole{
Floors: Floors{{
UserID: user.ID,
Content: body.Content,
SpecialTag: body.SpecialTag,
IsMe: true,
IsSensitive: !sensitiveResp.Pass,
SensitiveDetail: sensitiveResp.Detail,
}},
UserID: user.ID,
DivisionID: body.DivisionID,
},

}
err = hole.Create(DB, user, body.ToName(), c)
if err != nil {
Expand Down Expand Up @@ -677,7 +682,7 @@ func HideHole(c *fiber.Ctx) error {

var hole Hole
hole.ID = holeID
result := DB.Model(&hole).Select("Hidden").Omit("UpdatedAt").Updates(Hole{Hidden: true})
result := DB.Model(&hole).Select("Hidden").Omit("UpdatedAt").Updates(Hole{BaseHole:BaseHole{Hidden: true}})
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
Expand Down
3 changes: 3 additions & 0 deletions apis/hole/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ func RegisterRoutes(app fiber.Router) {
app.Put("/holes/:id<int>", ModifyHole)
app.Delete("/holes/:id<int>", HideHole)
app.Delete("/holes/:id<int>/_force", DeleteHole)

// V2
app.Get("/v2/holes/:id<int>", GetHole)
}
12 changes: 7 additions & 5 deletions benchmarks/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ func init() {
}
return nowTags
}
holes = append(holes, &Hole{
ID: i + 1,
UserID: 1,
DivisionID: rand.Intn(DIVISION_MAX) + 1,
Tags: generateTag(),
holes = append(holes, &Hole{
BaseHole: BaseHole{
ID: i + 1,
UserID: 1,
DivisionID: rand.Intn(DIVISION_MAX) + 1,
Tags: generateTag(),
},
})
}

Expand Down
38 changes: 23 additions & 15 deletions models/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"treehole_next/utils"
)

type Hole struct {
type Hole = HoleV1

type BaseHole struct {
/// saved fields
ID int `json:"id" gorm:"primaryKey"`
ID int `json:"id" gorm:"primaryKey;"`
CreatedAt time.Time `json:"time_created" gorm:"not null;index:idx_hole_div_cre,priority:2,sort:desc"`
UpdatedAt time.Time `json:"time_updated" gorm:"not null;index:idx_hole_div_upd,priority:2,sort:desc"`
DeletedAt gorm.DeletedAt `json:"time_deleted,omitempty" gorm:"index"`
Expand Down Expand Up @@ -54,7 +56,7 @@ type Hole struct {
Tags Tags `json:"tags" gorm:"many2many:hole_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`

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

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

// 兼容旧版 id
HoleID int `json:"hole_id" gorm:"-:all"`
}


type HoleV1 struct {
BaseHole

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

}

func (hole *Hole) GetID() int {
func (hole *BaseHole) GetID() int {
return hole.ID
}

func (hole *Hole) CacheName() string {
func (hole *BaseHole) CacheName() string {
return fmt.Sprintf("hole_%d", hole.ID)
}

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

const HoleCacheExpire = time.Minute * 10

func loadTags(holes Holes) (err error) {
func (holes Holes)loadTags() error {
if len(holes) == 0 {
return nil
}
Expand All @@ -107,7 +115,7 @@ func loadTags(holes Holes) (err error) {
}

var holeTags HoleTags
err = DB.Where("hole_id in ?", holeIDs).Find(&holeTags).Error
err := DB.Where("hole_id in ?", holeIDs).Find(&holeTags).Error
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +152,7 @@ func loadTags(holes Holes) (err error) {
return nil
}

func loadFloors(holes Holes) error {
func (holes Holes) loadFloors() error {
if len(holes) == 0 {
return nil
}
Expand Down Expand Up @@ -288,12 +296,12 @@ func (holes Holes) Preprocess(c *fiber.Ctx) error {
}

func UpdateHoleCache(holes Holes) (err error) {
err = loadFloors(holes)
err = holes.loadFloors()
if err != nil {
return
}

err = loadTags(holes)
err = holes.loadTags()
if err != nil {
return
}
Expand Down Expand Up @@ -351,7 +359,7 @@ func (holes Holes) MakeQuerySet(offset common.CustomTime, size int, order string
// set hole.HoleFloor from hole.Floors or hole.HoleFloor.Floors
// if Floors is not empty, set HoleFloor.Floors from Floors, in case loading from database
// if Floors is empty, set HoleFloor.Floors from HoleFloor.Floors, in case loading from cache
func (hole *Hole) SetHoleFloor() {
func (hole *HoleV1) SetHoleFloor() {
if len(hole.Floors) != 0 {
holeFloorSize := len(hole.Floors)

Expand Down Expand Up @@ -380,7 +388,7 @@ func (hole *Hole) SetHoleFloor() {
//hole.HoleFloor.LastFloor.SetDefaults(c)
}

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

func (hole *Hole) AfterCreate(_ *gorm.DB) (err error) {
func (hole *BaseHole) AfterCreate(_ *gorm.DB) (err error) {
hole.HoleID = hole.ID
return nil
}

func (hole *Hole) AfterFind(_ *gorm.DB) (err error) {
func (hole *BaseHole) AfterFind(_ *gorm.DB) (err error) {
hole.HoleID = hole.ID
return nil
}
Expand All @@ -473,7 +481,7 @@ func (holes Holes) RemoveIf(delCondition func(*Hole) bool) Holes {
return result
}

func (hole *Hole) HoleHook() {
func (hole *HoleV1) HoleHook() {
if hole == nil {
return
}
Expand Down
Loading
Loading