Skip to content

Commit bbacc8f

Browse files
authored
[type:refactor] refactor openapi and dictionary (#89)
* [type:refactor] refactor openapi and dictionary * [type:refactor] refactor openapi and dictionary
1 parent 0507e60 commit bbacc8f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

internal/core/openapi/dictionary.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ package openapi
1919

2020
import (
2121
"fmt"
22+
"net/http"
2223

2324
"github.com/acmestack/envcd/internal/core/plugin"
25+
"github.com/acmestack/envcd/internal/core/storage/dao"
2426
"github.com/acmestack/envcd/internal/pkg/context"
27+
"github.com/acmestack/envcd/internal/pkg/entity"
2528
"github.com/acmestack/envcd/pkg/entity/data"
2629
"github.com/acmestack/godkits/gox/errorsx"
30+
"github.com/acmestack/godkits/gox/stringsx"
2731
"github.com/gin-gonic/gin"
2832
)
2933

@@ -37,7 +41,7 @@ func (openapi *Openapi) getApp(ctx *gin.Context) {
3741
}
3842
}
3943

40-
func (openapi *Openapi) createApp(ctx *gin.Context) {
44+
func (openapi *Openapi) putApp(ctx *gin.Context) {
4145
c := &context.Context{Action: func() (*data.EnvcdResult, error) {
4246
fmt.Println("hello world")
4347
// create App
@@ -63,18 +67,30 @@ func (openapi *Openapi) DeleteApp(ctx *gin.Context) {
6367
}
6468
}
6569

66-
func (openapi *Openapi) getConfig(ctx *gin.Context) {
67-
c := &context.Context{Action: func() (*data.EnvcdResult, error) {
68-
fmt.Println("hello world")
69-
openapi.exchange.Put("key", "value")
70-
return nil, errorsx.Err("test error")
71-
}}
72-
if ret, err := plugin.NewChain(openapi.executors).Execute(c); err != nil {
70+
func (openapi *Openapi) getDict(ctx *gin.Context) {
71+
c, _ := buildContext(ctx)
72+
c.Action = func() (*data.EnvcdResult, error) {
73+
// get user id from gin context
74+
userId := stringsx.ToInt(ctx.Param("userId"))
75+
appId := stringsx.ToInt(ctx.Param("appId"))
76+
configId := stringsx.ToInt(ctx.Param("configId"))
77+
dict := entity.Dictionary{Id: configId, UserId: userId, ApplicationId: appId}
78+
dictionary, err := dao.New(openapi.storage).SelectDictionary(dict)
79+
if err != nil {
80+
return data.Failure(err.Error()), err
81+
}
82+
return data.Success(dictionary), nil
83+
}
84+
ret, err := plugin.NewChain(openapi.executors).Execute(c)
85+
if err != nil {
86+
// FIXME log.error("ret = %v, error = %v", ret, err)
7387
fmt.Printf("ret = %v, error = %v", ret, err)
88+
ctx.JSON(http.StatusBadRequest, ret.Data)
7489
}
90+
ctx.JSON(http.StatusOK, ret.Data)
7591
}
7692

77-
func (openapi *Openapi) createConfig(ctx *gin.Context) {
93+
func (openapi *Openapi) putDict(ctx *gin.Context) {
7894
c := &context.Context{Action: func() (*data.EnvcdResult, error) {
7995
fmt.Println("hello world")
8096
// create config
@@ -88,7 +104,7 @@ func (openapi *Openapi) createConfig(ctx *gin.Context) {
88104
}
89105
}
90106

91-
func (openapi *Openapi) deleteConfig(ctx *gin.Context) {
107+
func (openapi *Openapi) deleteDict(ctx *gin.Context) {
92108
c := &context.Context{Action: func() (*data.EnvcdResult, error) {
93109
fmt.Println("hello world")
94110
// delete config

internal/core/openapi/openapi.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ func (openapi *Openapi) buildRouter() *gin.Engine {
8787
{
8888
// TODO evncd application
8989
envcdApplication.GET("/user/:userId/application/:appId", openapi.getApp)
90-
envcdApplication.PUT("/user/:userId/application/:appId", openapi.createApp)
90+
envcdApplication.PUT("/user/:userId/application/:appId", openapi.putApp)
9191
envcdApplication.DELETE("/user/:userId/application/:appId", openapi.DeleteApp)
9292

9393
// TODO envcd config
94-
envcdApplication.GET("/user/:userId/application/:appId/config/:configId", openapi.getConfig)
95-
envcdApplication.PUT("/user/:userId/application/:appId/config/:configId", openapi.createConfig)
96-
envcdApplication.DELETE("/user/:userId/application/:appId/config/:configId", openapi.deleteConfig)
94+
envcdApplication.GET("/user/:userId/application/:appId/dict/:dictId", openapi.getDict)
95+
envcdApplication.PUT("/user/:userId/application/:appId/dict/:dictId", openapi.putDict)
96+
envcdApplication.DELETE("/user/:userId/application/:appId/dict/:dictId", openapi.deleteDict)
9797
}
9898
return router
9999
}

0 commit comments

Comments
 (0)