iris/_examples/database/mongodb/api/store/movie.go
Gerasimos (Makis) Maropoulos ed45c77be5 reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
2020-06-07 15:26:06 +03:00

102 lines
2.1 KiB
Go

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
}
}