This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
links.go
103 lines (87 loc) · 3.06 KB
/
links.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package controller
import (
"github.com/harderthanitneedstobe/rest-api/v0/pkg/models"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"net/http"
"strings"
"time"
)
func (c *Controller) linksController(p iris.Party) {
// GET will list all the links in the current account.
p.Get("/", c.getLinks)
p.Post("/", c.postLinks)
p.Put("/{linkId:uint64}", func(ctx *context.Context) {
linkId := ctx.Params().GetUint64Default("linkId", 0)
if linkId == 0 {
c.returnError(ctx, http.StatusBadRequest, "must specify a link Id to update")
return
}
var link models.Link
if err := ctx.ReadJSON(&link); err != nil {
// TODO (elliotcourant) Add tests for malformed json.
c.wrapAndReturnError(ctx, err, http.StatusBadRequest, "malformed JSON")
return
}
link.LinkId = linkId
// We are not going to update default value or null fields. So we can simply clear these fields out to make sure
// the user does not overwrite them somehow.
link.CreatedByUserId = 0 // Make sure they don't change the created by userId.
link.CreatedAt = time.Time{}
link.InstitutionName = "" // This cannot be changed. If the user wants to set a name then they need to change the custom one.
link.LinkType = 0 // Make sure they don't change the link type. This can be changed, but not by the user.
link.PlaidLinkId = nil // Make sure they don't change the plaidLink.
repo := c.mustGetAuthenticatedRepository(ctx)
if err := repo.UpdateLink(&link); err != nil {
c.wrapPgError(ctx, err, "could not update link")
return
}
ctx.JSON(link)
})
}
// List all links
// @Summary List All Links
// @id list-all-links
// @tags Links
// @description Lists all of the links for the currently authenticated user.
// @Produce json
// @Security ApiKeyAuth
// @Router /links [get]
// @Success 200 {array} models.Link
// @Failure 500 {object} ApiError Something went wrong on our end.
func (c *Controller) getLinks(ctx *context.Context) {
repo := c.mustGetAuthenticatedRepository(ctx)
links, err := repo.GetLinks()
if err != nil {
c.wrapAndReturnError(ctx, err, http.StatusInternalServerError, "failed to retrieve links")
return
}
ctx.JSON(links)
}
// Create A Link
// @Summary Create A Link
// @id create-link
// @tags Links
// @description Create a manual link.
// @Produce json
// @Security ApiKeyAuth
// @Router /links [post]
// @Success 200 {object} models.Link
// @Failure 500 {object} ApiError Something went wrong on our end.
func (c *Controller) postLinks(ctx *context.Context) {
var link models.Link
if err := ctx.ReadJSON(&link); err != nil {
// TODO (elliotcourant) Add tests for malformed json.
c.wrapAndReturnError(ctx, err, http.StatusBadRequest, "malformed JSON")
return
}
link.LinkId = 0 // Make sure the link Id is unset.
link.InstitutionName = strings.TrimSpace(link.InstitutionName)
link.LinkType = models.ManualLinkType
repo := c.mustGetAuthenticatedRepository(ctx)
if err := repo.CreateLink(&link); err != nil {
c.wrapAndReturnError(ctx, err, http.StatusInternalServerError, "could not create manual link")
return
}
ctx.JSON(link)
}