|
| 1 | +// Copyright © 2018 Thomas Winsnes <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package ui |
| 22 | + |
| 23 | +import ( |
| 24 | + "encoding/json" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "net/http" |
| 28 | + "strconv" |
| 29 | + |
| 30 | + "github.com/gorilla/mux" |
| 31 | + "github.com/vibrato/TechTestApp/db" |
| 32 | + "github.com/vibrato/TechTestApp/model" |
| 33 | +) |
| 34 | + |
| 35 | +// TaskID parameter. |
| 36 | +// |
| 37 | +// swagger:parameters deleteTask |
| 38 | +type TaskID struct { |
| 39 | + // The ID of the task |
| 40 | + // |
| 41 | + // in: path |
| 42 | + // min: 0 |
| 43 | + // required: true |
| 44 | + ID int `json:"id"` |
| 45 | +} |
| 46 | + |
| 47 | +// Sucessful Task Array Response |
| 48 | +// |
| 49 | +// swagger:response allTasks |
| 50 | +type allTasks struct { |
| 51 | + // in: body |
| 52 | + // The tasks being returned |
| 53 | + // required: true |
| 54 | + Tasks []model.Task `json:"tasks"` |
| 55 | +} |
| 56 | + |
| 57 | +// swagger:route GET /api/task/ getTasks |
| 58 | +// |
| 59 | +// Fetch all tasks |
| 60 | +// |
| 61 | +// Produces: |
| 62 | +// - application/json |
| 63 | +// |
| 64 | +// Responses: |
| 65 | +// 200: allTasks |
| 66 | +// |
| 67 | +func getTasks(cfg Config) http.Handler { |
| 68 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 69 | + output, _ := db.GetAllTasks(cfg.DB) |
| 70 | + js, _ := json.Marshal(output) |
| 71 | + w.Header().Set("Content-Type", "application/json") |
| 72 | + fmt.Fprintf(w, string(js)) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +// Sucessful Single Task Response |
| 77 | +// |
| 78 | +// swagger:response aTask |
| 79 | +type aTask struct { |
| 80 | + // in: body |
| 81 | + // The tasks being returned |
| 82 | + // required: true |
| 83 | + Task model.Task `json:"task"` |
| 84 | +} |
| 85 | + |
| 86 | +// swagger:parameters addTask |
| 87 | +type taskParameter struct { |
| 88 | + // in:body |
| 89 | + Task model.Task `json:"task"` |
| 90 | +} |
| 91 | + |
| 92 | +// swagger:route POST /api/task/ addTask |
| 93 | +// |
| 94 | +// Add a new task to the list. |
| 95 | +// |
| 96 | +// Produces: |
| 97 | +// - application/json |
| 98 | +// |
| 99 | +// Responses: |
| 100 | +// 200: aTask |
| 101 | +// 400: |
| 102 | +// 500: |
| 103 | +// |
| 104 | +func addTask(cfg Config) http.Handler { |
| 105 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 106 | + decoder := json.NewDecoder(r.Body) |
| 107 | + var task model.Task |
| 108 | + |
| 109 | + err := decoder.Decode(&task) |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + log.Println(err) |
| 113 | + http.Error(w, err.Error(), 400) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + newTask, err := db.AddTask(cfg.DB, task) |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + log.Println(err) |
| 121 | + http.Error(w, err.Error(), 500) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + js, _ := json.Marshal(newTask) |
| 126 | + w.Header().Set("Content-Type", "application/json") |
| 127 | + fmt.Fprintf(w, string(js)) |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +// swagger:route DELETE /api/task/{id}/ deleteTask |
| 132 | +// |
| 133 | +// Delete a Task by ID |
| 134 | +// |
| 135 | +// Responses: |
| 136 | +// 204: |
| 137 | +// 404: |
| 138 | +// 500: |
| 139 | +// |
| 140 | +func deleteTask(cfg Config) http.Handler { |
| 141 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 142 | + vars := mux.Vars(r) |
| 143 | + |
| 144 | + id, err := strconv.Atoi(vars["id"]) |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + fmt.Print(err) |
| 148 | + http.Error(w, err.Error(), 500) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + err = db.DeleteTask(cfg.DB, model.Task{ID: id}) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + fmt.Print(err) |
| 156 | + http.Error(w, err.Error(), 500) |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + w.WriteHeader(http.StatusNoContent) |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +func apiHandler(cfg Config, router *mux.Router) { |
| 165 | + router.Handle("/task/{id:[0-9]+}/", deleteTask(cfg)).Methods("DELETE") |
| 166 | + router.Handle("/task/", getTasks(cfg)).Methods("GET") |
| 167 | + router.Handle("/task/", addTask(cfg)).Methods("POST") |
| 168 | +} |
0 commit comments