-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from vibrato/swagger
Add swagger json and UI to application
- Loading branch information
Showing
9 changed files
with
320 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,14 @@ | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
// Vibrato Tech Test Application | ||
// | ||
// This application is used as part of the Vibrato Technical Assessment. | ||
// | ||
// Version: 1.0 | ||
// Contact: Thomas Winsnes<[email protected]> https://www.vibrato.com.au | ||
// | ||
// swagger:meta | ||
package main | ||
|
||
import "github.com/vibrato/TechTestApp/cmd" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Copyright © 2018 Thomas Winsnes <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package ui | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/vibrato/TechTestApp/db" | ||
"github.com/vibrato/TechTestApp/model" | ||
) | ||
|
||
// TaskID parameter. | ||
// | ||
// swagger:parameters deleteTask | ||
type TaskID struct { | ||
// The ID of the task | ||
// | ||
// in: path | ||
// min: 0 | ||
// required: true | ||
ID int `json:"id"` | ||
} | ||
|
||
// Sucessful Task Array Response | ||
// | ||
// swagger:response allTasks | ||
type allTasks struct { | ||
// in: body | ||
// The tasks being returned | ||
// required: true | ||
Tasks []model.Task `json:"tasks"` | ||
} | ||
|
||
// swagger:route GET /api/task/ getTasks | ||
// | ||
// Fetch all tasks | ||
// | ||
// Produces: | ||
// - application/json | ||
// | ||
// Responses: | ||
// 200: allTasks | ||
// | ||
func getTasks(cfg Config) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
output, _ := db.GetAllTasks(cfg.DB) | ||
js, _ := json.Marshal(output) | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, string(js)) | ||
}) | ||
} | ||
|
||
// Sucessful Single Task Response | ||
// | ||
// swagger:response aTask | ||
type aTask struct { | ||
// in: body | ||
// The tasks being returned | ||
// required: true | ||
Task model.Task `json:"task"` | ||
} | ||
|
||
// swagger:parameters addTask | ||
type taskParameter struct { | ||
// in:body | ||
Task model.Task `json:"task"` | ||
} | ||
|
||
// swagger:route POST /api/task/ addTask | ||
// | ||
// Add a new task to the list. | ||
// | ||
// Produces: | ||
// - application/json | ||
// | ||
// Responses: | ||
// 200: aTask | ||
// 400: | ||
// 500: | ||
// | ||
func addTask(cfg Config) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
decoder := json.NewDecoder(r.Body) | ||
var task model.Task | ||
|
||
err := decoder.Decode(&task) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
http.Error(w, err.Error(), 400) | ||
return | ||
} | ||
|
||
newTask, err := db.AddTask(cfg.DB, task) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
http.Error(w, err.Error(), 500) | ||
return | ||
} | ||
|
||
js, _ := json.Marshal(newTask) | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, string(js)) | ||
}) | ||
} | ||
|
||
// swagger:route DELETE /api/task/{id}/ deleteTask | ||
// | ||
// Delete a Task by ID | ||
// | ||
// Responses: | ||
// 204: | ||
// 404: | ||
// 500: | ||
// | ||
func deleteTask(cfg Config) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
|
||
id, err := strconv.Atoi(vars["id"]) | ||
|
||
if err != nil { | ||
fmt.Print(err) | ||
http.Error(w, err.Error(), 500) | ||
return | ||
} | ||
|
||
err = db.DeleteTask(cfg.DB, model.Task{ID: id}) | ||
|
||
if err != nil { | ||
fmt.Print(err) | ||
http.Error(w, err.Error(), 500) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
} | ||
|
||
func apiHandler(cfg Config, router *mux.Router) { | ||
router.Handle("/task/{id:[0-9]+}/", deleteTask(cfg)).Methods("DELETE") | ||
router.Handle("/task/", getTasks(cfg)).Methods("GET") | ||
router.Handle("/task/", addTask(cfg)).Methods("POST") | ||
} |
Oops, something went wrong.