-
Notifications
You must be signed in to change notification settings - Fork 2
/
link_addon.go
66 lines (53 loc) · 1.11 KB
/
link_addon.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
package main
import (
"fmt"
"net/url"
"strings"
"github.com/gin-gonic/gin"
)
func (a App) createLink(l *Link) error {
return a.DB.Model(&Link{}).
Create(l).
Error
}
func (a App) updateLink(name string, l *Link) error {
err := a.DB.Model(&Link{}).
Where("name = ?", name).
Update(&l).
Error
if err != nil {
return err
}
return nil
}
func getLinkFromContext(c *gin.Context) (*Link, error) {
link := &Link{}
err := c.ShouldBindJSON(link)
if err != nil {
return nil, err
}
// Lowercase name of the link
link.Name = strings.ToLower(link.Name)
// Check correctness of the destination url
u, err := url.Parse(link.Redirect)
if err != nil {
return nil, err
}
if u.Scheme != "http" && u.Scheme != "https" {
u.Scheme = "https"
link.Redirect = u.String()
}
return link, nil
}
func (a App) getAllLink(search string) ([]*Link, error) {
var links []*Link
searchString := fmt.Sprintf("%%%s%%", search)
q := a.DB.Model(&Link{})
if search != "" {
q = q.Where("name LIKE ? OR redirect LIKE ?", searchString, searchString)
}
err := q.Order("name").
Find(&links).
Error
return links, err
}