/
main.go
92 lines (83 loc) · 2.32 KB
/
main.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
package main
import (
"database/sql"
"log"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"github.com/narenaryan/dbutils"
)
// DB Driver visible to whole program
var DB *sql.DB
// StationResource holds information about locations
type StationResource struct {
ID int `json:"id"`
Name string `json:"name"`
OpeningTime string `json:"opening_time"`
ClosingTime string `json:"closing_time"`
}
// GetStation returns the station detail
func GetStation(c *gin.Context) {
var station StationResource
id := c.Param("station_id")
err := DB.QueryRow("select ID, NAME, CAST(OPENING_TIME as CHAR), CAST(CLOSING_TIME as CHAR) from station where id=?", id).Scan(&station.ID, &station.Name, &station.OpeningTime, &station.ClosingTime)
if err != nil {
log.Println(err)
c.JSON(500, gin.H{
"error": err.Error(),
})
} else {
c.JSON(200, gin.H{
"result": station,
})
}
}
// CreateStation handles the POST
func CreateStation(c *gin.Context) {
var station StationResource
// Parse the body into our resrource
if err := c.BindJSON(&station); err == nil {
// Format Time to Go time format
statement, _ := DB.Prepare("insert into station (NAME, OPENING_TIME, CLOSING_TIME) values (?, ?, ?)")
result, _ := statement.Exec(station.Name, station.OpeningTime, station.ClosingTime)
if err == nil {
newID, _ := result.LastInsertId()
station.ID = int(newID)
c.JSON(http.StatusOK, gin.H{
"result": station,
})
} else {
c.String(http.StatusInternalServerError, err.Error())
}
} else {
c.String(http.StatusInternalServerError, err.Error())
}
}
// RemoveStation handles the removing of resource
func RemoveStation(c *gin.Context) {
id := c.Param("station-id")
statement, _ := DB.Prepare("delete from station where id=?")
_, err := statement.Exec(id)
if err != nil {
log.Println(err)
c.JSON(500, gin.H{
"error": err.Error(),
})
} else {
c.String(http.StatusOK, "")
}
}
func main() {
var err error
DB, err = sql.Open("sqlite3", "./railapi.db")
if err != nil {
log.Println("Driver creation failed!")
}
dbutils.Initialize(DB)
r := gin.Default()
// Add routes to REST verbs
r.GET("/v1/stations/:station_id", GetStation)
r.POST("/v1/stations", CreateStation)
r.DELETE("/v1/stations/:station_id", RemoveStation)
r.Run(":8000") // Default listen and serve on 0.0.0.0:8080
}