Created
September 1, 2017 18:49
-
-
Save deleted/3cc981bfea9021df391d8ead419517a8 to your computer and use it in GitHub Desktop.
go-swagger & gorp models
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
package db | |
import ( | |
"database/sql" | |
"log" | |
_ "github.com/mattn/go-sqlite3" // registers sqlite driver | |
gorp "gopkg.in/gorp.v1" | |
) | |
const dbDriver = "sqlite3" | |
const dbSource = "/tmp/wtf.sqlite" | |
type Term struct { | |
Id int64 | |
Name string | |
} | |
type Definition struct { | |
Id int64 | |
TermId int64 `db:"term_id"` | |
Text string | |
} | |
func listDrivers() { | |
drivers := sql.Drivers() | |
log.Printf("%d sql drivers\n", len(drivers)) | |
for _, v := range drivers { | |
log.Println(v) | |
} | |
} | |
/*GetDbMap setup the database*/ | |
func GetDbMap() *gorp.DbMap { | |
listDrivers() | |
db, err := sql.Open(dbDriver, dbSource) | |
checkErr(err, "Open database failed.") | |
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} | |
dbmap.AddTableWithName(Term{}, "terms").SetKeys(true, "Id") | |
dbmap.AddTableWithName(Definition{}, "definitions").SetKeys(true, "Id") | |
return dbmap | |
} | |
/*CreateDB : Create database if it does not already exist*/ | |
func CreateDB() *gorp.DbMap { | |
dbmap := GetDbMap() | |
err := dbmap.CreateTablesIfNotExists() | |
checkErr(err, "Unable to create database") | |
return dbmap | |
} | |
func checkErr(err error, msg string) { | |
if err != nil { | |
log.Fatalln(msg, err) | |
} | |
} |
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
package db | |
import ( | |
"log" | |
"wtf-swagger/server/models" | |
) | |
func GetTerm(termName string) (models.Term, error) { | |
dbmap := GetDbMap() | |
var term models.Term // model struct generated by go-swagger | |
var definitions []models.Definition // model struct generated by go-swagger | |
err := dbmap.SelectOne(&term, ` | |
SELECT * | |
FROM terms | |
WHERE Name = ? | |
COLLATE NOCASE | |
`, termName) | |
fatalError(err, "Something went wrong with the Term SELECT") | |
_, err = dbmap.Select(&definitions, ` | |
SELECT id, text | |
FROM definitions | |
WHERE term_id = ? | |
`, term.ID) | |
fatalError(err, "Select failed") | |
modelTerm := models.Term{ | |
ID: int32(term.ID), | |
Name: term.Name, | |
} | |
for _, definition := range definitions { | |
def := models.Definition{ | |
ID: int32(definition.ID), | |
Text: definition.Text, | |
} | |
modelTerm.Definitions = append(modelTerm.Definitions, &def) | |
} | |
return modelTerm, nil | |
} | |
func fatalError(err error, msg string) { | |
if err != nil { | |
log.Fatal(msg) | |
} | |
} |
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
// Code generated by go-swagger; DO NOT EDIT. | |
package models | |
// This file was generated by the swagger tool. | |
// Editing this file might prove futile when you re-run the swagger generate command | |
import ( | |
strfmt "github.com/go-openapi/strfmt" | |
"github.com/go-openapi/errors" | |
"github.com/go-openapi/swag" | |
) | |
// Definition definition | |
// swagger:model Definition | |
type Definition struct { | |
// primary key | |
// Read Only: true | |
ID int32 `json:"id,omitempty"` | |
// text | |
Text string `json:"text,omitempty"` | |
} |
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
// Code generated by go-swagger; DO NOT EDIT. | |
package models | |
// This file was generated by the swagger tool. | |
// Editing this file might prove futile when you re-run the swagger generate command | |
import ( | |
"strconv" | |
strfmt "github.com/go-openapi/strfmt" | |
"github.com/go-openapi/errors" | |
"github.com/go-openapi/swag" | |
) | |
// Term term | |
// swagger:model Term | |
type Term struct { | |
// definitions | |
Definitions []*Definition `json:"definitions"` | |
// primary key | |
// Read Only: true | |
ID int32 `json:"id,omitempty"` | |
// name | |
Name string `json:"name,omitempty"` | |
} | |
// VALIDATION & MARSHALLING CODE REDACTED |
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
swagger: '2.0' | |
info: | |
title: wtfbot | |
version: '0.1' | |
host: wtfbot.earth.planet.com | |
schemes: | |
- http | |
basePath: /v0 | |
produces: | |
- application/json | |
paths: | |
/terms: | |
get: | |
summary: Term List | |
description: | | |
List all the available terms in the glossary. | |
responses: | |
200: | |
description: An array of terms. | |
schema: | |
type: array | |
items: | |
$ref: '#/definitions/Term' | |
post: | |
summary: Add a definition | |
parameters: | |
- name: definitions | |
in: body | |
schema: | |
$ref: "#/definitions/Definition" | |
responses: | |
200: | |
description: The entire term object (with all definitions posted to date) | |
schema: | |
$ref: "#/definitions/Term" | |
/terms/{termName}: | |
get: | |
summary: Term Query | |
parameters: | |
- name: termName | |
in: path | |
required: true | |
type: string | |
responses: | |
200: | |
description: A single term and its definitions | |
schema: | |
$ref: "#/definitions/Term" | |
definitions: | |
Definition: | |
properties: | |
id: | |
type: integer | |
format: int32 | |
readOnly: true | |
description: primary key | |
text: | |
type: string | |
Term: | |
properties: | |
id: | |
type: integer | |
format: int32 | |
readOnly: true | |
description: primary key | |
name: | |
type: string | |
definitions: | |
type: array | |
items: | |
$ref: '#/definitions/Definition' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment