mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 10:57:05 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
101
_examples/database/mongodb/api/store/movie.go
Normal file
101
_examples/database/mongodb/api/store/movie.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package storeapi
|
||||
|
||||
import (
|
||||
"myapp/httputil"
|
||||
"myapp/store"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
type MovieHandler struct {
|
||||
service store.MovieService
|
||||
}
|
||||
|
||||
func NewMovieHandler(service store.MovieService) *MovieHandler {
|
||||
return &MovieHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *MovieHandler) GetAll(ctx iris.Context) {
|
||||
movies, err := h.service.GetAll(nil)
|
||||
if err != nil {
|
||||
httputil.InternalServerErrorJSON(ctx, err, "Server was unable to retrieve all movies")
|
||||
return
|
||||
}
|
||||
|
||||
if movies == nil {
|
||||
// will return "null" if empty, with this "trick" we return "[]" json.
|
||||
movies = make([]store.Movie, 0)
|
||||
}
|
||||
|
||||
ctx.JSON(movies)
|
||||
}
|
||||
|
||||
func (h *MovieHandler) Get(ctx iris.Context) {
|
||||
id := ctx.Params().Get("id")
|
||||
|
||||
m, err := h.service.GetByID(nil, id)
|
||||
if err != nil {
|
||||
if err == store.ErrNotFound {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
httputil.InternalServerErrorJSON(ctx, err, "Server was unable to retrieve movie [%s]", id)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(m)
|
||||
}
|
||||
|
||||
func (h *MovieHandler) Add(ctx iris.Context) {
|
||||
m := new(store.Movie)
|
||||
|
||||
err := ctx.ReadJSON(m)
|
||||
if err != nil {
|
||||
httputil.FailJSON(ctx, iris.StatusBadRequest, err, "Malformed request payload")
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.Create(nil, m)
|
||||
if err != nil {
|
||||
httputil.InternalServerErrorJSON(ctx, err, "Server was unable to create a movie")
|
||||
return
|
||||
}
|
||||
|
||||
ctx.StatusCode(iris.StatusCreated)
|
||||
ctx.JSON(m)
|
||||
}
|
||||
|
||||
func (h *MovieHandler) Update(ctx iris.Context) {
|
||||
id := ctx.Params().Get("id")
|
||||
|
||||
var m store.Movie
|
||||
err := ctx.ReadJSON(&m)
|
||||
if err != nil {
|
||||
httputil.FailJSON(ctx, iris.StatusBadRequest, err, "Malformed request payload")
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.Update(nil, id, m)
|
||||
if err != nil {
|
||||
if err == store.ErrNotFound {
|
||||
ctx.NotFound()
|
||||
return
|
||||
}
|
||||
httputil.InternalServerErrorJSON(ctx, err, "Server was unable to update movie [%s]", id)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MovieHandler) Delete(ctx iris.Context) {
|
||||
id := ctx.Params().Get("id")
|
||||
|
||||
err := h.service.Delete(nil, id)
|
||||
if err != nil {
|
||||
if err == store.ErrNotFound {
|
||||
ctx.NotFound()
|
||||
return
|
||||
}
|
||||
httputil.InternalServerErrorJSON(ctx, err, "Server was unable to delete movie [%s]", id)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user