// file: web/controllers/movie_controller.go package controllers import ( "errors" "github.com/kataras/iris/v12/_examples/mvc/repository/datamodels" "github.com/kataras/iris/v12/_examples/mvc/repository/services" "github.com/kataras/iris/v12" ) // 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 // // 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) { return c.Service.GetAll() } // GetBy returns a movie. // Demo: // curl -i http://localhost:8080/movies/1 func (c *MovieController) GetBy(id int64) (movie datamodels.Movie, found bool) { return c.Service.GetByID(id) // it will throw 404 if not found. } // PutBy updates a movie. // Demo: // curl -i -X PUT -F "genre=Thriller" -F "poster=@/Users/kataras/Downloads/out.gif" http://localhost:8080/movies/1 func (c *MovieController) PutBy(ctx iris.Context, id int64) (datamodels.Movie, error) { // get the request data for poster and genre file, info, err := ctx.FormFile("poster") if err != nil { return datamodels.Movie{}, errors.New("failed due form file 'poster' missing") } // we don't need the file so close it now. file.Close() // imagine that is the url of the uploaded file... poster := info.Filename genre := ctx.FormValue("genre") return c.Service.UpdatePosterAndGenreByID(id, poster, genre) } // 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 { // return the deleted movie's ID return iris.Map{"deleted": id} } // right here we can see that a method function can return any of those two types(map or int), // we don't have to specify the return type to a specific type. return iris.StatusBadRequest }