mirror of
https://github.com/kataras/iris.git
synced 2025-12-23 12:57:05 +00:00
create a new package, name it as hero, I was thinking super or superb but hero is better name for what it does - the goal is to split the new 'mvc handlers' from the mvc system because they are not the same, users should know that they can use these type of rich binded handlers without controllers as well, like a normal handler and that I implemented here, the old files exist on the mvc package but will be removed at the next commit, I have to decide if we want type aliases for Result or no
Former-commit-id: cb775edc72bedc88aeab4c5a6de6bfc6bd56fae2
This commit is contained in:
50
_examples/hero/overview/web/routes/hello.go
Normal file
50
_examples/hero/overview/web/routes/hello.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// file: web/routes/hello.go
|
||||
|
||||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/kataras/iris/hero"
|
||||
)
|
||||
|
||||
var helloView = hero.View{
|
||||
Name: "hello/index.html",
|
||||
Data: map[string]interface{}{
|
||||
"Title": "Hello Page",
|
||||
"MyMessage": "Welcome to my awesome website",
|
||||
},
|
||||
}
|
||||
|
||||
// Hello will return a predefined view with bind data.
|
||||
//
|
||||
// `hero.Result` is just an interface with a `Dispatch` function.
|
||||
// `hero.Response` and `hero.View` are the built'n result type dispatchers
|
||||
// you can even create custom response dispatchers by
|
||||
// implementing the `github.com/kataras/iris/hero#Result` interface.
|
||||
func Hello() hero.Result {
|
||||
return helloView
|
||||
}
|
||||
|
||||
// you can define a standard error in order to re-use anywhere in your app.
|
||||
var errBadName = errors.New("bad name")
|
||||
|
||||
// you can just return it as error or even better
|
||||
// wrap this error with an hero.Response to make it an hero.Result compatible type.
|
||||
var badName = hero.Response{Err: errBadName, Code: 400}
|
||||
|
||||
// HelloName returns a "Hello {name}" response.
|
||||
// Demos:
|
||||
// curl -i http://localhost:8080/hello/iris
|
||||
// curl -i http://localhost:8080/hello/anything
|
||||
func HelloName(name string) hero.Result {
|
||||
if name != "iris" {
|
||||
return badName
|
||||
}
|
||||
|
||||
// return hero.Response{Text: "Hello " + name} OR:
|
||||
return hero.View{
|
||||
Name: "hello/name.html",
|
||||
Data: name,
|
||||
}
|
||||
}
|
||||
59
_examples/hero/overview/web/routes/movies.go
Normal file
59
_examples/hero/overview/web/routes/movies.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// file: web/routes/movie.go
|
||||
|
||||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/kataras/iris/_examples/hero/overview/datamodels"
|
||||
"github.com/kataras/iris/_examples/hero/overview/services"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// Movies returns list of the movies.
|
||||
// Demo:
|
||||
// curl -i http://localhost:8080/movies
|
||||
func Movies(service services.MovieService) (results []datamodels.Movie) {
|
||||
return service.GetAll()
|
||||
}
|
||||
|
||||
// MovieByID returns a movie.
|
||||
// Demo:
|
||||
// curl -i http://localhost:8080/movies/1
|
||||
func MovieByID(service services.MovieService, id int64) (movie datamodels.Movie, found bool) {
|
||||
return service.GetByID(id) // it will throw 404 if not found.
|
||||
}
|
||||
|
||||
// UpdateMovieByID updates a movie.
|
||||
// Demo:
|
||||
// curl -i -X PUT -F "genre=Thriller" -F "poster=@/Users/kataras/Downloads/out.gif" http://localhost:8080/movies/1
|
||||
func UpdateMovieByID(ctx iris.Context, service services.MovieService, 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 service.UpdatePosterAndGenreByID(id, poster, genre)
|
||||
}
|
||||
|
||||
// DeleteMovieByID deletes a movie.
|
||||
// Demo:
|
||||
// curl -i -X DELETE -u admin:password http://localhost:8080/movies/1
|
||||
func DeleteMovieByID(service services.MovieService, id int64) interface{} {
|
||||
wasDel := 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
|
||||
}
|
||||
Reference in New Issue
Block a user