|
18 | 18 | package openapi
|
19 | 19 |
|
20 | 20 | import (
|
| 21 | + "context" |
| 22 | + "errors" |
21 | 23 | "fmt"
|
22 | 24 | "time"
|
23 | 25 |
|
| 26 | + "github.com/acmestack/envcd/internal/core/storage" |
24 | 27 | "github.com/acmestack/envcd/internal/core/storage/dao"
|
| 28 | + "github.com/acmestack/envcd/internal/pkg/constant" |
25 | 29 | "github.com/acmestack/envcd/internal/pkg/entity"
|
26 | 30 | "github.com/acmestack/envcd/pkg/entity/result"
|
| 31 | + "github.com/acmestack/godkits/array" |
27 | 32 | "github.com/acmestack/godkits/gox/stringsx"
|
| 33 | + "github.com/acmestack/pagehelper" |
28 | 34 | "github.com/gin-gonic/gin"
|
29 | 35 | )
|
30 | 36 |
|
31 |
| -type dictParams struct { |
32 |
| - DictKey string `json:"dictKey"` |
| 37 | +type DictionaryDTO struct { |
| 38 | + UserId int `json:"userId" binding:"required"` |
| 39 | + ScopeSpaceId int `json:"scopeSpaceId" binding:"required"` |
| 40 | + DictKey string `json:"dictKey" binding:"required"` |
| 41 | + DictValue string `json:"dictValue" binding:"required"` |
| 42 | + Version string `json:"version" binding:"required"` |
| 43 | + State string `json:"state" binding:"required"` |
| 44 | +} |
| 45 | + |
| 46 | +type dictionUpdateDTO struct { |
| 47 | + DictId int `json:"dictId" binding:"required"` |
33 | 48 | DictValue string `json:"dictValue"`
|
34 |
| - Version string `json:"version"` |
35 | 49 | State string `json:"state"`
|
36 | 50 | }
|
37 | 51 |
|
| 52 | +func dictionary(storage *storage.Storage, dictionaryId *int, ginCtx *gin.Context) (*entity.Dictionary, error) { |
| 53 | + // get user id from gin context |
| 54 | + dictId := stringsx.ToInt(ginCtx.Param("dictionaryId")) |
| 55 | + if dictionaryId != nil { |
| 56 | + dictId = *dictionaryId |
| 57 | + } |
| 58 | + dict := entity.Dictionary{Id: dictId} |
| 59 | + dictionaries, err := dao.New(storage).SelectDictionary(dict, nil) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + if array.Empty(dictionaries) { |
| 64 | + return nil, nil |
| 65 | + } |
| 66 | + return &dictionaries[0], nil |
| 67 | +} |
| 68 | + |
| 69 | +// dictionary query single dictionary mapping |
| 70 | +// @receiver openapi common openapi |
| 71 | +// @param ginCtx gin context |
38 | 72 | func (openapi *Openapi) dictionary(ginCtx *gin.Context) {
|
39 | 73 | openapi.response(ginCtx, nil, func() *result.EnvcdResult {
|
40 |
| - // get user id from gin context |
41 |
| - userId := stringsx.ToInt(ginCtx.Param("userId")) |
42 |
| - scopeSpaceId := stringsx.ToInt(ginCtx.Param("scopeSpaceId")) |
43 |
| - dictId := stringsx.ToInt(ginCtx.Param("dictId")) |
44 |
| - dict := entity.Dictionary{Id: dictId, UserId: userId, ScopeSpaceId: scopeSpaceId} |
45 |
| - dictionary, err := dao.New(openapi.storage).SelectDictionary(dict) |
| 74 | + dict, err := dictionary(openapi.storage, nil, ginCtx) |
46 | 75 | if err != nil {
|
47 | 76 | return result.InternalFailure(err)
|
48 | 77 | }
|
49 |
| - return result.Success(dictionary) |
| 78 | + return result.Success(dict) |
50 | 79 | })
|
51 | 80 | }
|
52 | 81 |
|
| 82 | +// createDictionary create dictionary |
| 83 | +// @receiver openapi openapi |
| 84 | +// @param ginCtx gin context |
53 | 85 | func (openapi *Openapi) createDictionary(ginCtx *gin.Context) {
|
54 | 86 | openapi.response(ginCtx, nil, func() *result.EnvcdResult {
|
55 |
| - param := dictParams{} |
56 |
| - if err := ginCtx.ShouldBindJSON(¶m); err != nil { |
| 87 | + dictParams := &DictionaryDTO{} |
| 88 | + if err := ginCtx.ShouldBindJSON(dictParams); err != nil { |
57 | 89 | fmt.Printf("Bind error, %v\n", err)
|
58 | 90 | return result.InternalFailure(err)
|
59 | 91 | }
|
60 |
| - // get userId and appId from gin context |
61 |
| - userId := stringsx.ToInt(ginCtx.Param("userId")) |
62 |
| - scopeSpaceId := stringsx.ToInt(ginCtx.Param("scopeSpaceId")) |
63 |
| - |
| 92 | + daoAction := dao.New(openapi.storage) |
| 93 | + // build dictionary with parameters |
64 | 94 | dictionary := entity.Dictionary{
|
65 |
| - UserId: userId, |
66 |
| - ScopeSpaceId: scopeSpaceId, |
67 |
| - DictKey: param.DictKey, |
68 |
| - DictValue: param.DictValue, |
69 |
| - State: param.State, |
| 95 | + UserId: dictParams.UserId, |
| 96 | + ScopeSpaceId: dictParams.ScopeSpaceId, |
| 97 | + DictKey: dictParams.DictKey + "@" + dictParams.Version, |
| 98 | + DictValue: dictParams.DictValue, |
| 99 | + State: dictParams.State, |
70 | 100 | CreatedAt: time.Now(),
|
71 | 101 | UpdatedAt: time.Now(),
|
72 | 102 | }
|
73 |
| - // Strategy |
74 |
| - // scopespace + username + dictKey + version |
75 |
| - // insertDictionary, i, err := dao.New(openapi.storage).InsertDictionary(dictionary) |
76 |
| - // openapi.exchange.Put(dictionary.DictKey, dictionary.DictValue) |
77 |
| - // if err != nil { |
78 |
| - // return nil |
79 |
| - //} |
80 |
| - fmt.Println(dictionary) |
81 |
| - // create config |
82 |
| - // ConfigDao.save(); |
83 |
| - // go LogDao.save() |
84 |
| - // openapi.exchange.Put("key", "value") |
85 |
| - return nil |
| 103 | + _, id, err := daoAction.InsertDictionary(dictionary) |
| 104 | + if err != nil { |
| 105 | + return result.InternalFailure(err) |
| 106 | + } |
| 107 | + path, PathErr := buildEtcdPath(daoAction, dictionary) |
| 108 | + if PathErr != nil { |
| 109 | + return result.Failure0(result.ErrorEtcdPath) |
| 110 | + } |
| 111 | + if stringsx.Empty(path) { |
| 112 | + return result.Failure0(result.NilExchangePath) |
| 113 | + } |
| 114 | + exchangeErr := openapi.exchange.Put(path, dictParams.DictValue) |
| 115 | + if exchangeErr != nil { |
| 116 | + return result.InternalFailure(exchangeErr) |
| 117 | + } |
| 118 | + openapi.doOperationLogging(dictParams.UserId, "create dictionary and insert into mysql and etcd") |
| 119 | + return result.Success(id) |
86 | 120 | })
|
87 | 121 | }
|
88 | 122 |
|
| 123 | +// updateDictionary update dictionary |
| 124 | +// @receiver openapi openapi |
| 125 | +// @param ginCtx gin context |
89 | 126 | func (openapi *Openapi) updateDictionary(ginCtx *gin.Context) {
|
90 | 127 | openapi.response(ginCtx, nil, func() *result.EnvcdResult {
|
91 |
| - fmt.Println("hello world") |
92 |
| - // create config |
93 |
| - // ConfigDao.save(); |
94 |
| - // go LogDao.save() |
95 |
| - // openapi.exchange.Put("key", "value") |
96 |
| - return nil |
| 128 | + dictParams := &dictionUpdateDTO{} |
| 129 | + if err := ginCtx.ShouldBindJSON(dictParams); err != nil { |
| 130 | + fmt.Printf("Bind error, %v\n", err) |
| 131 | + return result.InternalFailure(err) |
| 132 | + } |
| 133 | + daoAction := dao.New(openapi.storage) |
| 134 | + |
| 135 | + dictionary := entity.Dictionary{ |
| 136 | + Id: dictParams.DictId, |
| 137 | + DictValue: dictParams.DictValue, |
| 138 | + UpdatedAt: time.Now(), |
| 139 | + } |
| 140 | + // update dictionary |
| 141 | + _, updateDictErr := daoAction.UpdateDictionary(dictionary) |
| 142 | + if updateDictErr != nil { |
| 143 | + return result.InternalFailure(updateDictErr) |
| 144 | + } |
| 145 | + // update state |
| 146 | + ret := openapi.updateDictionaryState(dictParams.DictId, dictParams.State) |
| 147 | + if ret != nil { |
| 148 | + return ret |
| 149 | + } |
| 150 | + return result.Success(nil) |
97 | 151 | })
|
98 | 152 | }
|
99 | 153 |
|
| 154 | +// removeDictionary remove dictionary |
| 155 | +// @receiver openapi |
| 156 | +// @param ginCtx gin context |
100 | 157 | func (openapi *Openapi) removeDictionary(ginCtx *gin.Context) {
|
101 | 158 | openapi.response(ginCtx, nil, func() *result.EnvcdResult {
|
102 |
| - userId := stringsx.ToInt(ginCtx.Param("userId")) |
103 |
| - appId := stringsx.ToInt(ginCtx.Param("appId")) |
104 |
| - dictId := stringsx.ToInt(ginCtx.Param("dictId")) |
105 |
| - dict := entity.Dictionary{Id: dictId, UserId: userId, ScopeSpaceId: appId} |
106 |
| - // query dictionary exist |
107 |
| - daoAction := dao.New(openapi.storage) |
108 |
| - dictionary, err := daoAction.SelectDictionary(dict) |
| 159 | + dict, err := dictionary(openapi.storage, nil, ginCtx) |
109 | 160 | if err != nil {
|
110 | 161 | return result.InternalFailure(err)
|
111 | 162 | }
|
112 |
| - if len(dictionary) == 0 { |
| 163 | + if dict == nil { |
113 | 164 | return result.Failure0(result.ErrorDictionaryNotExist)
|
114 | 165 | }
|
115 |
| - exchangeErr := openapi.exchange.Remove(getFirstDictionary(dictionary).DictKey) |
116 |
| - if exchangeErr != nil { |
117 |
| - return result.InternalFailure(exchangeErr) |
118 |
| - } |
119 |
| - retId, delErr := daoAction.DeleteDictionary(getFirstDictionary(dictionary)) |
| 166 | + daoAction := dao.New(openapi.storage) |
| 167 | + // set dictionaries state: deleted |
| 168 | + retId, delErr := daoAction.DeleteDictionary(*dict) |
120 | 169 | if delErr != nil {
|
121 | 170 | return result.InternalFailure(delErr)
|
122 | 171 | }
|
| 172 | + // delete etcd path |
| 173 | + path, etcdPathError := buildEtcdPath(daoAction, *dict) |
| 174 | + if etcdPathError != nil { |
| 175 | + return result.Failure0(result.ErrorEtcdPath) |
| 176 | + } |
| 177 | + if stringsx.Empty(path) { |
| 178 | + return result.Failure0(result.NilExchangePath) |
| 179 | + } |
| 180 | + if stringsx.NotEmpty(path) { |
| 181 | + exchangeErr := openapi.exchange.Remove(path) |
| 182 | + if exchangeErr != nil { |
| 183 | + return result.InternalFailure(exchangeErr) |
| 184 | + } |
| 185 | + } |
| 186 | + openapi.doOperationLogging(dict.UserId, "remove dictionaries from mysql and etcd") |
123 | 187 | return result.Success(retId)
|
124 | 188 | })
|
125 | 189 | }
|
126 | 190 |
|
127 |
| -func getFirstDictionary(dictionaryList []entity.Dictionary) entity.Dictionary { |
128 |
| - return dictionaryList[0] |
129 |
| -} |
130 |
| - |
131 | 191 | func (openapi *Openapi) dictionaries(ginCtx *gin.Context) {
|
132 | 192 | openapi.response(ginCtx, nil, func() *result.EnvcdResult {
|
133 |
| - fmt.Println("hello world") |
134 |
| - // create config |
135 |
| - // ConfigDao.save(); |
136 |
| - // go LogDao.save() |
137 |
| - // openapi.exchange.Put("key", "value") |
138 |
| - return nil |
| 193 | + pageNum := stringsx.ToInt(ginCtx.DefaultQuery("page", "1")) |
| 194 | + pageSize := stringsx.ToInt(ginCtx.DefaultQuery("pageSize", "20")) |
| 195 | + daoAction := dao.New(openapi.storage) |
| 196 | + ctx := pagehelper.C(context.Background()).PageWithCount(int64(pageNum-1), int64(pageSize), "").Build() |
| 197 | + dictionary, err := daoAction.SelectDictionary(entity.Dictionary{}, ctx) |
| 198 | + if err != nil { |
| 199 | + return result.InternalFailure(err) |
| 200 | + } |
| 201 | + pageInfo := pagehelper.GetPageInfo(ctx) |
| 202 | + return result.Success(PageListVO{ |
| 203 | + Page: pageInfo.Page + 1, |
| 204 | + PageSize: pageInfo.PageSize, |
| 205 | + Total: pageInfo.GetTotal(), |
| 206 | + TotalPage: pageInfo.GetTotalPage(), |
| 207 | + List: dictionary, |
| 208 | + }) |
139 | 209 | })
|
140 | 210 | }
|
| 211 | + |
| 212 | +// buildEtcdPath build etcd path |
| 213 | +// @param daoAction dao |
| 214 | +// @param dictionary |
| 215 | +// @return string path |
| 216 | +// @return error message |
| 217 | +func buildEtcdPath(daoAction *dao.Dao, dictionary entity.Dictionary) (string, error) { |
| 218 | + // todo user name from jwt |
| 219 | + user, userErr := daoAction.SelectUser(entity.User{Id: dictionary.UserId}) |
| 220 | + if userErr != nil { |
| 221 | + return "", userErr |
| 222 | + } |
| 223 | + scopeSpace, scopeSpaceErr := daoAction.SelectScopeSpace(entity.ScopeSpace{Id: dictionary.ScopeSpaceId}) |
| 224 | + if scopeSpaceErr != nil { |
| 225 | + return "", scopeSpaceErr |
| 226 | + } |
| 227 | + // user and scopeSpace not exist |
| 228 | + if len(user) == 0 || len(scopeSpace) == 0 { |
| 229 | + return "", errors.New("user or spaceSpace not exist") |
| 230 | + } |
| 231 | + // build path |
| 232 | + build := stringsx.Builder{} |
| 233 | + // /scopeSpaceName/userName/dictKey, etc. /spring/moremind/userKey@version |
| 234 | + _, err := build.JoinString("/", scopeSpace[0].Name, "/", user[0].Name, "/", dictionary.DictKey) |
| 235 | + if err != nil { |
| 236 | + return "", err |
| 237 | + } |
| 238 | + return build.String(), nil |
| 239 | +} |
| 240 | + |
| 241 | +// updateDictionaryState update dictionary state |
| 242 | +// @receiver openapi openapi |
| 243 | +// @param dictId dict id |
| 244 | +// @param state updated state |
| 245 | +// @return *result.EnvcdResult |
| 246 | +func (openapi *Openapi) updateDictionaryState(dictId int, state string) *result.EnvcdResult { |
| 247 | + daoAction := dao.New(openapi.storage) |
| 248 | + dictionaries, dictErr := daoAction.SelectDictionary(entity.Dictionary{Id: dictId}, nil) |
| 249 | + if dictErr != nil { |
| 250 | + return result.InternalFailure(dictErr) |
| 251 | + } |
| 252 | + if array.Empty(dictionaries) { |
| 253 | + return result.Failure0(result.ErrorDictionaryNotExist) |
| 254 | + } |
| 255 | + defaultDictionary := dictionaries[0] |
| 256 | + path, err := buildEtcdPath(daoAction, defaultDictionary) |
| 257 | + if stringsx.Empty(path) { |
| 258 | + return result.Failure0(result.NilExchangePath) |
| 259 | + } |
| 260 | + if err != nil { |
| 261 | + return result.Failure0(result.ErrorEtcdPath) |
| 262 | + } |
| 263 | + switch state { |
| 264 | + case constant.EnabledState: |
| 265 | + // case enabled, should generate path and put key and value |
| 266 | + if defaultDictionary.State != constant.EnabledState { |
| 267 | + exchangeErr := openapi.exchange.Put(path, defaultDictionary.DictValue) |
| 268 | + if exchangeErr != nil { |
| 269 | + return result.InternalFailure(exchangeErr) |
| 270 | + } |
| 271 | + } |
| 272 | + break |
| 273 | + case constant.DisabledState: |
| 274 | + // case disabled, should set state in mysql and delete dictionaries in etcd |
| 275 | + case constant.DeletedState: |
| 276 | + // case deleted, should set state in mysql and delete dictionaries in etcd |
| 277 | + if defaultDictionary.State == constant.DisabledState || defaultDictionary.State == constant.DeletedState { |
| 278 | + _, updateErr := daoAction.UpdateDictionary(entity.Dictionary{State: state}) |
| 279 | + if updateErr != nil { |
| 280 | + return result.InternalFailure(updateErr) |
| 281 | + } |
| 282 | + exchangeErr := openapi.exchange.Remove(path) |
| 283 | + if exchangeErr != nil { |
| 284 | + return result.InternalFailure(exchangeErr) |
| 285 | + } |
| 286 | + } |
| 287 | + break |
| 288 | + default: |
| 289 | + return result.Failure0(result.ErrorNotExistState) |
| 290 | + } |
| 291 | + return nil |
| 292 | +} |
0 commit comments