2017-10-12 02:51:06 +02:00
|
|
|
// file: web/controllers/movie_controller.go
|
2017-10-10 03:46:32 +02:00
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-06-20 23:12:07 +02:00
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/repository/datamodels"
|
|
|
|
"github.com/kataras/iris/v12/_examples/mvc/repository/services"
|
2017-10-10 03:46:32 +02:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-10-10 03:46:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MovieController is our /movies controller.
|
|
|
|
type MovieController struct {
|
|
|
|
// Our MovieService, it's an interface which
|
|
|
|
// is binded from the main application.
|
|
|
|
Service services.MovieService
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns list of the movies.
|
|
|
|
// Demo:
|
|
|
|
// curl -i http://localhost:8080/movies
|
2017-10-12 02:51:06 +02:00
|
|
|
//
|
|
|
|
// The correct way if you have sensitive data:
|
|
|
|
// func (c *MovieController) Get() (results []viewmodels.Movie) {
|
|
|
|
// data := c.Service.GetAll()
|
|
|
|
//
|
|
|
|
// for _, movie := range data {
|
|
|
|
// results = append(results, viewmodels.Movie{movie})
|
|
|
|
// }
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// otherwise just return the datamodels.
|
|
|
|
func (c *MovieController) Get() (results []datamodels.Movie) {
|
2017-10-10 03:46:32 +02:00
|
|
|
return c.Service.GetAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBy returns a movie.
|
|
|
|
// Demo:
|
|
|
|
// curl -i http://localhost:8080/movies/1
|
2017-10-12 02:51:06 +02:00
|
|
|
func (c *MovieController) GetBy(id int64) (movie datamodels.Movie, found bool) {
|
|
|
|
return c.Service.GetByID(id) // it will throw 404 if not found.
|
2017-10-10 03:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBy updates a movie.
|
|
|
|
// Demo:
|
|
|
|
// curl -i -X PUT -F "genre=Thriller" -F "poster=@/Users/kataras/Downloads/out.gif" http://localhost:8080/movies/1
|
2017-12-16 05:38:28 +01:00
|
|
|
func (c *MovieController) PutBy(ctx iris.Context, id int64) (datamodels.Movie, error) {
|
2017-10-10 03:46:32 +02:00
|
|
|
// get the request data for poster and genre
|
2017-12-16 05:38:28 +01:00
|
|
|
file, info, err := ctx.FormFile("poster")
|
2017-10-10 03:46:32 +02:00
|
|
|
if err != nil {
|
2017-10-12 02:51:06 +02:00
|
|
|
return datamodels.Movie{}, errors.New("failed due form file 'poster' missing")
|
2017-10-10 03:46:32 +02:00
|
|
|
}
|
|
|
|
// we don't need the file so close it now.
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
// imagine that is the url of the uploaded file...
|
|
|
|
poster := info.Filename
|
2017-12-16 05:38:28 +01:00
|
|
|
genre := ctx.FormValue("genre")
|
2017-10-10 03:46:32 +02:00
|
|
|
|
2017-10-12 02:51:06 +02:00
|
|
|
return c.Service.UpdatePosterAndGenreByID(id, poster, genre)
|
2017-10-10 03:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBy deletes a movie.
|
|
|
|
// Demo:
|
|
|
|
// curl -i -X DELETE -u admin:password http://localhost:8080/movies/1
|
|
|
|
func (c *MovieController) DeleteBy(id int64) interface{} {
|
|
|
|
wasDel := c.Service.DeleteByID(id)
|
|
|
|
if wasDel {
|
2017-10-10 04:17:53 +02:00
|
|
|
// return the deleted movie's ID
|
2017-10-10 03:46:32 +02:00
|
|
|
return iris.Map{"deleted": id}
|
|
|
|
}
|
2017-10-10 04:17:53 +02:00
|
|
|
// right here we can see that a method function can return any of those two types(map or int),
|
2017-10-10 03:46:32 +02:00
|
|
|
// we don't have to specify the return type to a specific type.
|
|
|
|
return iris.StatusBadRequest
|
|
|
|
}
|