@@ -2,7 +2,6 @@ package controller
2
2
3
3
import (
4
4
"github.com/kataras/iris/v12"
5
- "github.com/kataras/iris/v12/context"
6
5
"github.com/monetr/rest-api/pkg/models"
7
6
"net/http"
8
7
"strings"
@@ -13,40 +12,7 @@ func (c *Controller) linksController(p iris.Party) {
13
12
// GET will list all the links in the current account.
14
13
p .Get ("/" , c .getLinks )
15
14
p .Post ("/" , c .postLinks )
16
-
17
- p .Put ("/{linkId:uint64}" , func (ctx * context.Context ) {
18
- linkId := ctx .Params ().GetUint64Default ("linkId" , 0 )
19
- if linkId == 0 {
20
- c .returnError (ctx , http .StatusBadRequest , "must specify a link Id to update" )
21
- return
22
- }
23
-
24
- var link models.Link
25
- if err := ctx .ReadJSON (& link ); err != nil {
26
- // TODO (elliotcourant) Add tests for malformed json.
27
- c .wrapAndReturnError (ctx , err , http .StatusBadRequest , "malformed JSON" )
28
- return
29
- }
30
-
31
- link .LinkId = linkId
32
-
33
- // We are not going to update default value or null fields. So we can simply clear these fields out to make sure
34
- // the user does not overwrite them somehow.
35
- link .CreatedByUserId = 0 // Make sure they don't change the created by userId.
36
- link .CreatedAt = time.Time {}
37
- link .InstitutionName = "" // This cannot be changed. If the user wants to set a name then they need to change the custom one.
38
- link .LinkType = 0 // Make sure they don't change the link type. This can be changed, but not by the user.
39
- link .PlaidLinkId = nil // Make sure they don't change the plaidLink.
40
-
41
- repo := c .mustGetAuthenticatedRepository (ctx )
42
-
43
- if err := repo .UpdateLink (& link ); err != nil {
44
- c .wrapPgError (ctx , err , "could not update link" )
45
- return
46
- }
47
-
48
- ctx .JSON (link )
49
- })
15
+ p .Put ("/{linkId:uint64}" , c .putLinks )
50
16
}
51
17
52
18
// List all links
@@ -58,8 +24,9 @@ func (c *Controller) linksController(p iris.Party) {
58
24
// @Security ApiKeyAuth
59
25
// @Router /links [get]
60
26
// @Success 200 {array} swag.LinkResponse
27
+ // @Failure 402 {object} SubscriptionNotActiveError The user's subscription is not active.
61
28
// @Failure 500 {object} ApiError Something went wrong on our end.
62
- func (c * Controller ) getLinks (ctx * context .Context ) {
29
+ func (c * Controller ) getLinks (ctx iris .Context ) {
63
30
repo := c .mustGetAuthenticatedRepository (ctx )
64
31
65
32
links , err := repo .GetLinks ()
@@ -82,9 +49,10 @@ func (c *Controller) getLinks(ctx *context.Context) {
82
49
// @Router /links [post]
83
50
// @Param newLink body swag.CreateLinkRequest true "New Manual Link"
84
51
// @Success 200 {object} swag.LinkResponse "Newly created manual link"
85
- // @Failure 400 {object} ApiError "Malformed JSON."
52
+ // @Failure 400 {object} MalformedJSONError "Malformed JSON."
53
+ // @Failure 402 {object} SubscriptionNotActiveError The user's subscription is not active.
86
54
// @Failure 500 {object} ApiError "Something went wrong on our end."
87
- func (c * Controller ) postLinks (ctx * context .Context ) {
55
+ func (c * Controller ) postLinks (ctx iris .Context ) {
88
56
var link models.Link
89
57
if err := ctx .ReadJSON (& link ); err != nil {
90
58
c .wrapAndReturnError (ctx , err , http .StatusBadRequest , "malformed JSON" )
@@ -104,3 +72,36 @@ func (c *Controller) postLinks(ctx *context.Context) {
104
72
105
73
ctx .JSON (link )
106
74
}
75
+
76
+ func (c * Controller ) putLinks (ctx iris.Context ) {
77
+ linkId := ctx .Params ().GetUint64Default ("linkId" , 0 )
78
+ if linkId == 0 {
79
+ c .returnError (ctx , http .StatusBadRequest , "must specify a link Id to update" )
80
+ return
81
+ }
82
+
83
+ var link models.Link
84
+ if err := ctx .ReadJSON (& link ); err != nil {
85
+ c .wrapAndReturnError (ctx , err , http .StatusBadRequest , "malformed JSON" )
86
+ return
87
+ }
88
+
89
+ link .LinkId = linkId
90
+
91
+ // We are not going to update default value or null fields. So we can simply clear these fields out to make sure
92
+ // the user does not overwrite them somehow.
93
+ link .CreatedByUserId = 0 // Make sure they don't change the created by userId.
94
+ link .CreatedAt = time.Time {}
95
+ link .InstitutionName = "" // This cannot be changed. If the user wants to set a name then they need to change the custom one.
96
+ link .LinkType = 0 // Make sure they don't change the link type. This can be changed, but not by the user.
97
+ link .PlaidLinkId = nil // Make sure they don't change the plaidLink.
98
+
99
+ repo := c .mustGetAuthenticatedRepository (ctx )
100
+
101
+ if err := repo .UpdateLink (& link ); err != nil {
102
+ c .wrapPgError (ctx , err , "could not update link" )
103
+ return
104
+ }
105
+
106
+ ctx .JSON (link )
107
+ }
0 commit comments