Skip to content

Commit

Permalink
feat: get-metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Apr 17, 2024
1 parent de971b3 commit 17616fe
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
4 changes: 3 additions & 1 deletion backend/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type APIController struct {
func NewApiController(
as service.AccountService,
ps service.PostService,
ms service.MiscService,
) *APIController {
r := gin.Default()

Expand Down Expand Up @@ -45,7 +46,7 @@ func NewApiController(
return
}
// 没有文件都返回/
if util.IsFileExist("./frontend/dist"+c.Request.URL.Path) == false {
if !util.IsFileExist("./frontend/dist" + c.Request.URL.Path) {
http.ServeFile(c.Writer, c.Request, "./frontend/dist/index.html")
} else {
http.ServeFile(c.Writer, c.Request, "./frontend/dist"+c.Request.URL.Path)
Expand All @@ -57,6 +58,7 @@ func NewApiController(
// bind routes
NewAccountRouter(rg, as)
NewPostRouter(rg, ps)
NewMiscRouter(rg, ms)

return &APIController{
R: r,
Expand Down
45 changes: 45 additions & 0 deletions backend/controller/miscapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package controller

import (
"github.com/RockChinQ/Campux/backend/service"
"github.com/gin-gonic/gin"
)

type MiscRouter struct {
APIRouter
MiscService service.MiscService
}

func NewMiscRouter(rg *gin.RouterGroup, ms service.MiscService) *MiscRouter {
mr := &MiscRouter{
MiscService: ms,
}

group := rg.Group("/misc")

// bind routes
group.GET("/get-metadata", mr.GetMetadata)

return mr
}

// 获取元数据
func (mr *MiscRouter) GetMetadata(c *gin.Context) {
key := c.Query("key")

if key == "" {
mr.Fail(c, 1, "key is required")
return
}

value, err := mr.MiscService.GetMetadata(key)

if err != nil {
mr.Fail(c, 1, err.Error())
return
}

mr.Success(c, gin.H{
"value": value,
})
}
4 changes: 3 additions & 1 deletion backend/core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ func NewApplication() *Application {
fs := oss.NewMinioClient()
ps := service.NewPostService(*db, *fs)

ms := service.NewMiscService(*db)

return &Application{
API: controller.NewApiController(*as, *ps),
API: controller.NewApiController(*as, *ps, *ms),
}
}

Expand Down
15 changes: 15 additions & 0 deletions backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
ACCOUNT_COLLECTION = "account"
POST_COLLECTION = "post"
POST_LOG_COLLECTION = "post_log"
METADATA_COLLECTION = "metadata"
)

type MongoDBManager struct {
Expand Down Expand Up @@ -172,3 +173,17 @@ func (m *MongoDBManager) UpdatePostStatus(id int, status PostStatus) error {
)
return err
}

func (m *MongoDBManager) GetMetadata(key string) (string, error) {
var meta struct {
Value string `bson:"value"`
}
err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(METADATA_COLLECTION).FindOne(
context.TODO(),
bson.M{"key": key},
).Decode(&meta)
if err != nil {
return "", err
}
return meta.Value, nil
}
17 changes: 17 additions & 0 deletions backend/service/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package service

import "github.com/RockChinQ/Campux/backend/database"

type MiscService struct {
DB database.MongoDBManager
}

func NewMiscService(db database.MongoDBManager) *MiscService {
return &MiscService{
DB: db,
}
}

func (ms *MiscService) GetMetadata(key string) (string, error) {
return ms.DB.GetMetadata(key)
}

0 comments on commit 17616fe

Please sign in to comment.