Skip to content

Commit 718d52d

Browse files
committed
Implement get shortcut endpoint
Add HTTP test Signed-off-by: David Kröll <[email protected]>
1 parent 489a1ad commit 718d52d

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# Output of the go coverage tool, specifically when used with LiteIDE
1414
*.out
1515

16-
.idea
16+
.idea/
1717
.env

api.http

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ Accept: application/json
99
"validThru": "2019-04-01T08:00:00.000+01:00"
1010
}
1111

12-
###
12+
###
13+
14+
GET http://localhost:9999/api/shortcuts/011c7aa1-89e5-4a1a-97af-b73ed18c5a29
15+
Accept: application/json
16+
17+
###

routes/shortcuts.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func createShortcut(w http.ResponseWriter, r *http.Request) {
1313
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
1414
Response{
1515
Success: false,
16-
Code: 1002,
16+
Code: 1003,
1717
Message: "Malformed JSON body",
1818
}.JSON(w, 400)
1919
return
@@ -22,7 +22,7 @@ func createShortcut(w http.ResponseWriter, r *http.Request) {
2222
if err := s.Save(); err != nil {
2323
Response{
2424
Success: false,
25-
Code: 1003,
25+
Code: 1004,
2626
Message: "Internal server error. " + err.Error(),
2727
}.JSON(w, 500)
2828
return
@@ -39,7 +39,26 @@ func listShortcuts(w http.ResponseWriter, r *http.Request) {
3939
}
4040

4141
func getShortcut(w http.ResponseWriter, r *http.Request) {
42+
vars := mux.Vars(r)
43+
id := vars["id"]
4244

45+
shortcut, err := models.ShortcutBy(models.ID, id)
46+
if err != nil && err != models.ErrNotFound {
47+
Response{
48+
Success: false,
49+
Code: 1005,
50+
Message: "Bad Request. " + err.Error(),
51+
}.JSON(w, 400)
52+
return
53+
} else if err == models.ErrNotFound {
54+
Response{
55+
Success: false,
56+
Code: 1002,
57+
Message: id + " not found",
58+
}.JSON(w, 404)
59+
return
60+
}
61+
_ = json.NewEncoder(w).Encode(shortcut)
4362
}
4463

4564
func updateShortcut(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)