Skip to content

Commit 9806fa8

Browse files
author
Jeffrey Buchbinder
committed
API: + GetPluginsForCategory, cleanup, linting, log fixes
1 parent ab36ecd commit 9806fa8

8 files changed

Lines changed: 40 additions & 25 deletions

File tree

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This "TODO" list covers migration from the 0.5.x J2EE backend for implementation
1717
- [X] getOutputMonths
1818
- [X] getOutputYears
1919
- [x] getPlugins
20-
- [ ] getPluginOptions
20+
- [X] getPluginOptions
2121
- [X] getProtocolVersion
2222
- [x] getStatus
2323
- [x] insertPayload

api/api.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package api
33
import (
44
"net/http"
55

6+
"slices"
7+
68
"github.com/freemed/remitt-server/common"
79
"github.com/gin-gonic/gin"
810
)
@@ -37,10 +39,8 @@ func (a Api) aclRequireRole(c *gin.Context, role string) {
3739
c.AbortWithStatus(http.StatusNetworkAuthenticationRequired)
3840
}
3941

40-
for _, x := range r.([]string) {
41-
if x == role {
42-
return
43-
}
42+
if slices.Contains(r.([]string), role) {
43+
return
4444
}
4545
c.AbortWithStatus(http.StatusNetworkAuthenticationRequired)
4646
}
@@ -51,10 +51,5 @@ func (a Api) isAdmin(c *gin.Context) bool {
5151
return false
5252
}
5353

54-
for _, role := range r.([]string) {
55-
if role == "admin" {
56-
return true
57-
}
58-
}
59-
return false
54+
return slices.Contains(r.([]string), "admin")
6055
}

api/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func init() {
1919

2020
func (a Api) ConfigGetAll(c *gin.Context) {
2121
user := c.MustGet(gin.AuthUserKey).(string)
22-
tag := fmt.Sprintf("ConfigGetAll(%s): ", user)
22+
tag := fmt.Sprintf("api.ConfigGetAll(%s): ", user)
2323
o, err := model.GetConfigValues(user)
2424
if err != nil {
2525
log.Print(tag + err.Error())
@@ -36,7 +36,7 @@ func (a Api) ConfigSetValue(c *gin.Context) {
3636
option := c.Param("option")
3737
value := c.Param("value")
3838

39-
tag := fmt.Sprintf("ConfigSetValue(%s,%s,%s) [%s]: ", namespace, option, value, user)
39+
tag := fmt.Sprintf("api.ConfigSetValue(%s,%s,%s) [%s]: ", namespace, option, value, user)
4040

4141
err := model.SetConfigValue(user, namespace, option, []byte(value))
4242
if err != nil {

api/file.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (a Api) GetFile(c *gin.Context) {
2626
category := c.Param("category")
2727
filename := c.Param("filename")
2828

29-
tag := fmt.Sprintf("apiGetFile(%s,%s) [%s]: ", category, filename, user)
29+
tag := fmt.Sprintf("api.GetFile(%s,%s) [%s]: ", category, filename, user)
3030

3131
if category == "" || filename == "" {
3232
log.Print(tag + "Missing category or filename")
@@ -71,7 +71,7 @@ func (a Api) GetFileList(c *gin.Context) {
7171
criteria := c.Param("criteria")
7272
value := c.Param("value")
7373

74-
tag := fmt.Sprintf("apiGetFileList(%s,%s,%s) [%s]: ", category, criteria, value, user)
74+
tag := fmt.Sprintf("api.GetFileList(%s,%s,%s) [%s]: ", category, criteria, value, user)
7575

7676
if category == "" || criteria == "" || value == "" {
7777
log.Print(tag + "Missing category or criteria or value")
@@ -116,7 +116,7 @@ func (a Api) GetOutputMonths(c *gin.Context) {
116116

117117
year := c.Param("year")
118118

119-
tag := fmt.Sprintf("apiGetOutputMonths(%s) [%s]: ", year, user)
119+
tag := fmt.Sprintf("api.GetOutputMonths(%s) [%s]: ", year, user)
120120

121121
if year == "" {
122122
log.Print(tag + "Missing year")
@@ -143,7 +143,7 @@ func (a Api) GetOutputMonths(c *gin.Context) {
143143
func (a Api) GetOutputYears(c *gin.Context) {
144144
user := c.MustGet(gin.AuthUserKey).(string)
145145

146-
tag := fmt.Sprintf("apiGetOutputYears() [%s]: ", user)
146+
tag := fmt.Sprintf("api.GetOutputYears() [%s]: ", user)
147147

148148
query := "SELECT " +
149149
" DISTINCT(YEAR(stamp)) AS year " +

api/payload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (a Api) PayloadInsert(c *gin.Context) {
2929
TransportOption string `json:"transport_option"`
3030
}
3131

32-
tag := fmt.Sprintf("apiPayloadInsert() [%s]: ", user)
32+
tag := fmt.Sprintf("api.PayloadInsert() [%s]: ", user)
3333

3434
var raw inputPayload
3535
if c.BindJSON(&raw) != nil {
@@ -59,7 +59,7 @@ func (a Api) PayloadInsert(c *gin.Context) {
5959
func (a Api) PayloadResubmit(c *gin.Context) {
6060
user := c.MustGet(gin.AuthUserKey).(string)
6161

62-
tag := fmt.Sprintf("apiPayloadResubmit() [%s]: ", user)
62+
tag := fmt.Sprintf("api.PayloadResubmit() [%s]: ", user)
6363

6464
id, err := common.ParamInt(c, "id")
6565
if err != nil {

api/plugins.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import (
1212

1313
func init() {
1414
common.ApiMap["plugins"] = func(r *gin.RouterGroup) {
15-
r.GET("/:category", a.PluginsGetAll)
15+
r.GET("/get/:category", a.PluginsGetAll)
16+
r.GET("/options/:plugin", a.PluginGetOptions)
1617
}
1718
}
1819

1920
func (a Api) PluginsGetAll(c *gin.Context) {
2021
user := c.MustGet(gin.AuthUserKey).(string)
2122
cat := c.Param("category")
2223

23-
tag := fmt.Sprintf("apiPluginsGetAll(%s) [%s]: ", cat, user)
24+
tag := fmt.Sprintf("api.PluginsGetAll(%s) [%s]: ", cat, user)
2425

2526
switch cat {
2627
case "validation":
@@ -44,3 +45,18 @@ func (a Api) PluginsGetAll(c *gin.Context) {
4445
}
4546
c.JSON(http.StatusOK, o)
4647
}
48+
49+
func (a Api) PluginGetOptions(c *gin.Context) {
50+
user := c.MustGet(gin.AuthUserKey).(string)
51+
p := c.Param("plugin")
52+
53+
tag := fmt.Sprintf("api.PluginGetOptions(%s) [%s]: ", p, user)
54+
55+
o, err := model.GetPluginOptions(p)
56+
if err != nil {
57+
log.Print(tag + err.Error())
58+
c.AbortWithError(http.StatusInternalServerError, err)
59+
return
60+
}
61+
c.JSON(http.StatusOK, o)
62+
}

api/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (a Api) GetStatus(c *gin.Context) {
2727

2828
payloadID, err := common.ParamInt(c, "id")
2929

30-
tag := fmt.Sprintf("apiGetStatus(%d) [%s]: ", payloadID, user)
30+
tag := fmt.Sprintf("api.GetStatus(%d) [%s]: ", payloadID, user)
3131

3232
if err != nil {
3333
log.Print(tag + err.Error())
@@ -48,7 +48,7 @@ func (a Api) GetStatus(c *gin.Context) {
4848
func (a Api) GetBulkStatus(c *gin.Context) {
4949
user := c.MustGet(gin.AuthUserKey).(string)
5050

51-
tag := fmt.Sprintf("apiGetBulkStatus() [%s]: ", user)
51+
tag := fmt.Sprintf("api.GetBulkStatus() [%s]: ", user)
5252

5353
var ids []int64
5454
err := c.BindJSON(&ids)

model/pluginoptions.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package model
22

3-
import ()
4-
53
const (
64
TABLE_PLUGIN_OPTIONS = "tPluginOptions"
75
)
@@ -20,3 +18,9 @@ type PluginOptionsModel struct {
2018
func init() {
2119
DbTables = append(DbTables, DbTable{TableName: TABLE_PLUGIN_OPTIONS, Obj: PluginOptionsModel{}, Key: ""})
2220
}
21+
22+
func GetPluginOptions(plugin string) ([]PluginOptionsModel, error) {
23+
var o []PluginOptionsModel
24+
_, err := DbMap.Select(&o, "SELECT * FROM "+TABLE_PLUGIN_OPTIONS+" WHERE plugin = ?", plugin)
25+
return o, err
26+
}

0 commit comments

Comments
 (0)