-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
119 lines (101 loc) · 2.71 KB
/
profile.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package api
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/lsnow99/conductorr/internal/conductorr/dbstore"
)
type NewProfile struct {
Name string `json:"name"`
}
type Profile struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Filter string `json:"filter,omitempty"`
Sorter string `json:"sorter,omitempty"`
}
func NewProfileFromDB(dbProfile *dbstore.Profile) Profile {
profile := Profile{}
profile.ID = dbProfile.ID
profile.Name = dbProfile.Name.String
profile.Filter = dbProfile.Filter.String
profile.Sorter = dbProfile.Sorter.String
return profile
}
func GetProfiles(w http.ResponseWriter, r *http.Request) {
dbProfiles, err := dbstore.GetProfiles()
if err != nil {
Respond(w, r, err, nil, true)
return
}
profiles := make([]Profile, len(dbProfiles))
for i, dbProfile := range dbProfiles {
profiles[i] = NewProfileFromDB(dbProfile)
}
Respond(w, r, nil, profiles, true)
}
func GetProfile(w http.ResponseWriter, r *http.Request) {
profileIDStr := mux.Vars(r)["id"]
profileID, err := strconv.Atoi(profileIDStr)
if err != nil {
Respond(w, r, err, nil, true)
return
}
profile, err := dbstore.GetProfileByID(profileID)
Respond(w, r, err, NewProfileFromDB(&profile), true)
}
func CreateProfile(w http.ResponseWriter, r *http.Request) {
profile := NewProfile{}
if err := json.NewDecoder(r.Body).Decode(&profile); err != nil {
Respond(w, r, err, nil, true)
return
}
err := dbstore.CreateProfile(profile.Name)
Respond(w, r, err, nil, true)
}
func UpdateProfile(w http.ResponseWriter, r *http.Request) {
profile := Profile{}
if err := json.NewDecoder(r.Body).Decode(&profile); err != nil {
Respond(w, r, err, nil, true)
return
}
idStr := mux.Vars(r)["id"]
idInt, err := strconv.Atoi(idStr)
if err != nil {
Respond(w, r, err, nil, true)
return
}
err = dbstore.UpdateProfile(idInt, profile.Name, profile.Filter, profile.Sorter)
Respond(w, r, err, nil, true)
}
func DeleteProfile(w http.ResponseWriter, r *http.Request) {
idStr := mux.Vars(r)["id"]
idInt, err := strconv.Atoi(idStr)
if err != nil {
Respond(w, r, err, nil, true)
return
}
err = dbstore.DeleteProfile(idInt)
Respond(w, r, err, nil, true)
}
func GetRawProfileScript(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
scriptType := mux.Vars(r)["script_type"]
profile, err := dbstore.GetProfileByName(name)
if err != nil {
Respond(w, r, err, nil, true)
return
}
if scriptType == "filter" {
w.Write([]byte(profile.Filter.String))
return
} else if scriptType == "sorter" {
w.Write([]byte(profile.Sorter.String))
return
} else {
Respond(w, r, fmt.Errorf("unrecognized script type %s", scriptType), nil, true)
return
}
}