Skip to content

Commit dfac584

Browse files
committed
Udated with the API
1 parent 7ea1f7d commit dfac584

File tree

6 files changed

+268
-2
lines changed

6 files changed

+268
-2
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ If you already have GO setup, then install the following packages:
1212

1313
`$ go get -u github.com/jinzhu/gorm`
1414

15-
`$ go get -u github.com/kataras/go-template`
16-
1715
`$ go get github.com/pusher/pusher-http-go`
1816

1917

api-goggles/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# api-goggles
2+
An simple CRUD API for storing and retrieving movies, written with Go
3+
---------------------------------------
4+
5+
# Installation
6+
7+
If you already have GO setup, install the following packages:
8+
9+
`$ go get -u github.com/kataras/iris`
10+
11+
`$ go get -u github.com/mattn/go-sqlite3`
12+
13+
`$ go get -u github.com/jinzhu/gorm`
14+
15+
Then run:
16+
17+
`$ go run index.go`
18+
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"goggles/models"
6+
7+
"github.com/kataras/iris"
8+
"github.com/kataras/iris/mvc"
9+
"github.com/pusher/pusher-http-go"
10+
)
11+
12+
// MoviesController - controller object to serve movie data
13+
type MoviesController struct {
14+
mvc.BaseController
15+
Cntx iris.Context
16+
}
17+
18+
var client = pusher.Client{
19+
AppId: "316051",
20+
Key: "72c71489a8698580e447",
21+
Secret: "7ed785e9087b1d944177",
22+
Cluster: "mt1",
23+
}
24+
25+
//Get - get a list of all available movies
26+
func (m MoviesController) Get() {
27+
movie := models.Movies{}
28+
movies := movie.Get()
29+
30+
go m.saveEndpointCall("Movies List")
31+
32+
m.Cntx.JSON(iris.Map{"status": "success", "data": movies})
33+
}
34+
35+
//GetByID - Get movie by ID
36+
func (m MoviesController) GetByID(ID int64) {
37+
movie := models.Movies{}
38+
movie = movie.GetByID(ID)
39+
40+
if !movie.Validate() {
41+
msg := fmt.Sprintf("Movie with ID: %v not found", ID)
42+
m.Cntx.StatusCode(iris.StatusNotFound)
43+
m.Cntx.JSON(iris.Map{"status": "error", "message": msg})
44+
} else {
45+
m.Cntx.JSON(iris.Map{"status": "success", "data": movie})
46+
}
47+
48+
name := fmt.Sprintf("Single Movie with ID: %v Retrieval", ID)
49+
go m.saveEndpointCall(name)
50+
}
51+
52+
//Add - Add new movie
53+
func (m MoviesController) Add() {
54+
movie := models.Movies{}
55+
m.Cntx.ReadForm(&movie)
56+
57+
if !movie.Validate() {
58+
m.Cntx.StatusCode(iris.StatusBadRequest)
59+
m.Cntx.JSON(iris.Map{"status": "error", "message": "Movie not added"})
60+
} else {
61+
movie.Create()
62+
m.Cntx.JSON(iris.Map{"status": "success", "data": movie})
63+
}
64+
65+
go m.saveEndpointCall("Add Movie")
66+
}
67+
68+
//Edit - Edit a movie
69+
func (m MoviesController) Edit(ID int64) {
70+
movie := models.Movies{}
71+
movie = movie.GetByID(ID)
72+
m.Cntx.ReadForm(&movie)
73+
74+
if !movie.Validate() {
75+
msg := fmt.Sprintf("Movie with ID: %v not found", ID)
76+
m.Cntx.StatusCode(iris.StatusNotFound)
77+
m.Cntx.JSON(iris.Map{"status": "error", "message": msg})
78+
} else {
79+
movie.Edit()
80+
m.Cntx.JSON(iris.Map{"status": "success", "data": movie})
81+
}
82+
83+
name := fmt.Sprintf("Single Movie with ID: %v Edit", ID)
84+
go m.saveEndpointCall(name)
85+
86+
}
87+
88+
//Delete - delete a specific movie
89+
func (m MoviesController) Delete(ID int64) {
90+
movie := models.Movies{}
91+
movie = movie.GetByID(ID)
92+
93+
if !movie.Validate() {
94+
msg := fmt.Sprintf("Movie with ID: %v not found", ID)
95+
m.Cntx.StatusCode(iris.StatusNotFound)
96+
m.Cntx.JSON(iris.Map{"status": "error", "message": msg})
97+
} else {
98+
movie.Delete()
99+
m.Cntx.JSON(iris.Map{"status": "success", "message": "Movie with ID: "})
100+
}
101+
102+
name := fmt.Sprintf("Single Movie with ID: %v Delete", ID)
103+
go m.saveEndpointCall(name)
104+
}
105+
106+
func (m MoviesController) saveEndpointCall(name string) {
107+
108+
endpoint := models.EndPoints{
109+
Name: name,
110+
URL: m.Cntx.Path(),
111+
Type: m.Cntx.Request().Method,
112+
}
113+
114+
endpoint = endpoint.SaveOrCreate()
115+
endpointCall := endpoint.SaveCall(m.Cntx)
116+
117+
endpointWithCallSummary := models.EndPointWithCallSummary{
118+
ID: endpoint.ID,
119+
Name: endpoint.Name,
120+
URL: endpoint.URL,
121+
Type: endpoint.Type,
122+
LastStatus: endpointCall.ResponseCode,
123+
NumRequests: 1,
124+
LastRequester: endpointCall.RequestIP,
125+
}
126+
127+
client.Trigger("goggles_channel", "new_endpoint_request", endpointWithCallSummary)
128+
}

api-goggles/db/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

api-goggles/index.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"goggles/controllers"
5+
"goggles/models"
6+
"strconv"
7+
8+
"github.com/jinzhu/gorm"
9+
_ "github.com/jinzhu/gorm/dialects/sqlite"
10+
"github.com/kataras/iris"
11+
)
12+
13+
func main() {
14+
app := iris.New()
15+
16+
//initialise ORM
17+
db, _ := gorm.Open("sqlite3", "./db/gorm.db")
18+
19+
//AutoMigrate models
20+
db.AutoMigrate(&models.Movies{})
21+
22+
//api endpoints
23+
app.Get("/api/movies", func(ctx iris.Context) {
24+
mv := controllers.MoviesController{Cntx: ctx}
25+
mv.Get()
26+
})
27+
28+
app.Post("/api/movies", func(ctx iris.Context) {
29+
mv := controllers.MoviesController{Cntx: ctx}
30+
mv.Add()
31+
})
32+
33+
app.Get("/api/movies/{id}", func(ctx iris.Context) {
34+
ID, _ := strconv.ParseInt(ctx.Params().Get("id"), 10, 64)
35+
mv := controllers.MoviesController{Cntx: ctx}
36+
mv.GetByID(ID)
37+
})
38+
39+
app.Put("/api/movies/{id}", func(ctx iris.Context) {
40+
ID, _ := strconv.ParseInt(ctx.Params().Get("id"), 10, 64)
41+
42+
mv := controllers.MoviesController{Cntx: ctx}
43+
mv.Edit(ID)
44+
})
45+
46+
app.Delete("/api/movies/{id}", func(ctx iris.Context) {
47+
ID, _ := strconv.ParseInt(ctx.Params().Get("id"), 10, 64)
48+
49+
mv := controllers.MoviesController{Cntx: ctx}
50+
mv.Delete(ID)
51+
})
52+
53+
app.Get("/", func(ctx iris.Context) {
54+
55+
})
56+
57+
app.Run(iris.Addr("127.0.0.1:1234"))
58+
}

api-goggles/models/Movies.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package models
2+
3+
import (
4+
"github.com/jinzhu/gorm"
5+
_ "github.com/jinzhu/gorm/dialects/sqlite"
6+
)
7+
8+
//Movies - movies model object
9+
type Movies struct {
10+
gorm.Model
11+
Name string `sql:"not null;"`
12+
Genre, Description string
13+
Year int64
14+
}
15+
16+
var db, _ = gorm.Open("sqlite3", "./db/gorm.db")
17+
18+
//Get - get all movies
19+
func (m Movies) Get() []Movies {
20+
var movies []Movies
21+
db.Find(&movies)
22+
23+
return movies
24+
}
25+
26+
//GetByID - get movie by id
27+
func (m Movies) GetByID(ID int64) Movies {
28+
db.First(&m, ID)
29+
30+
return m
31+
}
32+
33+
//Create - create new movie
34+
func (m Movies) Create() Movies {
35+
db.Create(&m)
36+
37+
return m
38+
}
39+
40+
//Delete - delete movie
41+
func (m Movies) Delete() Movies {
42+
db.Delete(&m)
43+
44+
return m
45+
}
46+
47+
//Edit - edit movie
48+
func (m Movies) Edit() Movies {
49+
db.Save(&m)
50+
51+
return m
52+
}
53+
54+
//Validate - validate movies
55+
func (m Movies) Validate() bool {
56+
57+
if m == (Movies{}) || m.Name == "" {
58+
return false
59+
}
60+
61+
return true
62+
}

0 commit comments

Comments
 (0)