mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 23:40:35 +01:00
Update README_CN.md
Former-commit-id: 59c2c0e4afa5a495db7bc1dab6eb52bd8999b97f
This commit is contained in:
parent
330ae5cdae
commit
f971686123
78
README_CN.md
78
README_CN.md
|
@ -652,7 +652,7 @@ func (r *movieMemoryRepository) InsertOrUpdate(movie datamodels.Movie) (datamode
|
||||||
if movie.Poster != "" {
|
if movie.Poster != "" {
|
||||||
current.Poster = movie.Poster
|
current.Poster = movie.Poster
|
||||||
}
|
}
|
||||||
|
|
||||||
if movie.Genre != "" {
|
if movie.Genre != "" {
|
||||||
current.Genre = movie.Genre
|
current.Genre = movie.Genre
|
||||||
}
|
}
|
||||||
|
@ -756,9 +756,10 @@ func (s *movieService) DeleteByID(id int64) bool {
|
||||||
|
|
||||||
#### 视图模型
|
#### 视图模型
|
||||||
|
|
||||||
视图模型处理给客户端看的
|
视图模型将处理结果返回给客户端
|
||||||
There should be the view models, the structure that the client will be able to see.
|
There should be the view models, the structure that the client will be able to see.
|
||||||
|
|
||||||
|
例子:
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -773,28 +774,38 @@ type Movie struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Movie) IsValid() bool {
|
func (m Movie) IsValid() bool {
|
||||||
/* do some checks and return true if it's valid... */
|
/* 做一些检测,如果ID合法就返回true */
|
||||||
return m.ID > 0
|
return m.ID > 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Iris允许在HTTP Response Dispatcher中使用任何自定义数据结构,所以理论上下面的代码不建议使用
|
||||||
|
|
||||||
Iris is able to convert any custom data Structure into an HTTP Response Dispatcher,
|
Iris is able to convert any custom data Structure into an HTTP Response Dispatcher,
|
||||||
so theoretically, something like the following is permitted if it's really necessary;
|
so theoretically, something like the following is permitted if it's really necessary;
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// Dispatch实现了`kataras/iris/mvc#Result`接口。在函数最后发送了一个`Movie`对象作为http response对象。
|
||||||
|
// 如果ID小于等于0就回返回404,或者就返回json数据。
|
||||||
|
//(这样就像控制器的方法默认返回自定义类型一样)
|
||||||
// Dispatch completes the `kataras/iris/mvc#Result` interface.
|
// Dispatch completes the `kataras/iris/mvc#Result` interface.
|
||||||
// Sends a `Movie` as a controlled http response.
|
// Sends a `Movie` as a controlled http response.
|
||||||
// If its ID is zero or less then it returns a 404 not found error
|
// If its ID is zero or less then it returns a 404 not found error
|
||||||
// else it returns its json representation,
|
// else it returns its json representation,
|
||||||
// (just like the controller's functions do for custom types by default).
|
// (just like the controller's functions do for custom types by default).
|
||||||
//
|
//
|
||||||
|
// 不要在这里写过多的代码,应用的主要逻辑不在这里
|
||||||
|
// 在方法返回之前可以做个简单验证处理等等;
|
||||||
// Don't overdo it, the application's logic should not be here.
|
// Don't overdo it, the application's logic should not be here.
|
||||||
// It's just one more step of validation before the response,
|
// It's just one more step of validation before the response,
|
||||||
// simple checks can be added here.
|
// simple checks can be added here.
|
||||||
//
|
//
|
||||||
|
// 这里只是一个小例子,想想这个优势在设计大型应用是很有作用的
|
||||||
// It's just a showcase,
|
// It's just a showcase,
|
||||||
// imagine the potentials this feature gives when designing a bigger application.
|
// imagine the potentials this feature gives when designing a bigger application.
|
||||||
//
|
//
|
||||||
|
// 这个方法是在`Movie`类型的控制器调用的。
|
||||||
|
// 例子在这里:`controllers/movie_controller.go#GetBy`。
|
||||||
// This is called where the return value from a controller's method functions
|
// This is called where the return value from a controller's method functions
|
||||||
// is type of `Movie`.
|
// is type of `Movie`.
|
||||||
// For example the `controllers/movie_controller.go#GetBy`.
|
// For example the `controllers/movie_controller.go#GetBy`.
|
||||||
|
@ -806,13 +817,14 @@ func (m Movie) Dispatch(ctx context.Context) {
|
||||||
ctx.JSON(m, context.JSON{Indent: " "})
|
ctx.JSON(m, context.JSON{Indent: " "})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
然而,我们仅仅用"datamodels"作为一个数据模型包是因为Movie数据结构没有包含敏感数据,客户端可以访问到其所有字段,我们不需要再有额外的功能去做验证处理了
|
||||||
However, we will use the "datamodels" as the only one models package because
|
However, we will use the "datamodels" as the only one models package because
|
||||||
Movie structure doesn't contain any sensitive data, clients are able to see all of its fields
|
Movie structure doesn't contain any sensitive data, clients are able to see all of its fields
|
||||||
and we don't need any extra functionality or validation inside it.
|
and we don't need any extra functionality or validation inside it.
|
||||||
|
|
||||||
#### Controllers
|
#### 控制器
|
||||||
|
|
||||||
|
控制器处理Web请求,它是服务层和客户端之间的桥梁
|
||||||
Handles web requests, bridge between the services and the client.
|
Handles web requests, bridge between the services and the client.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -830,20 +842,21 @@ import (
|
||||||
"github.com/kataras/iris/mvc"
|
"github.com/kataras/iris/mvc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MovieController is our /movies controller.
|
// MovieController是/movies的控制器
|
||||||
type MovieController struct {
|
type MovieController struct {
|
||||||
mvc.C
|
mvc.C
|
||||||
|
|
||||||
// Our MovieService, it's an interface which
|
// MovieService是一个接口,主app对象会持有它
|
||||||
|
// Our MovieService, it's an interface which
|
||||||
// is binded from the main application.
|
// is binded from the main application.
|
||||||
Service services.MovieService
|
Service services.MovieService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns list of the movies.
|
// 获取movies列表
|
||||||
// Demo:
|
// 例子:
|
||||||
// curl -i http://localhost:8080/movies
|
// curl -i http://localhost:8080/movies
|
||||||
//
|
//
|
||||||
// The correct way if you have sensitive data:
|
// 如果你有一些敏感的数据要处理的话,可以按照如下所示的方式:
|
||||||
// func (c *MovieController) Get() (results []viewmodels.Movie) {
|
// func (c *MovieController) Get() (results []viewmodels.Movie) {
|
||||||
// data := c.Service.GetAll()
|
// data := c.Service.GetAll()
|
||||||
//
|
//
|
||||||
|
@ -852,47 +865,49 @@ type MovieController struct {
|
||||||
// }
|
// }
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
// otherwise just return the datamodels.
|
//否则直接返回数据模型
|
||||||
func (c *MovieController) Get() (results []datamodels.Movie) {
|
func (c *MovieController) Get() (results []datamodels.Movie) {
|
||||||
return c.Service.GetAll()
|
return c.Service.GetAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBy returns a movie.
|
// GetBy返回一个movie对象
|
||||||
// Demo:
|
// 例子:
|
||||||
// curl -i http://localhost:8080/movies/1
|
// curl -i http://localhost:8080/movies/1
|
||||||
func (c *MovieController) GetBy(id int64) (movie datamodels.Movie, found bool) {
|
func (c *MovieController) GetBy(id int64) (movie datamodels.Movie, found bool) {
|
||||||
return c.Service.GetByID(id) // it will throw 404 if not found.
|
return c.Service.GetByID(id) // 404 没有找到
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutBy updates a movie.
|
// PutBy更新一个movie.
|
||||||
// Demo:
|
// 例子:
|
||||||
// curl -i -X PUT -F "genre=Thriller" -F "poster=@/Users/kataras/Downloads/out.gif" http://localhost:8080/movies/1
|
// curl -i -X PUT -F "genre=Thriller" -F "poster=@/Users/kataras/Downloads/out.gif" http://localhost:8080/movies/1
|
||||||
func (c *MovieController) PutBy(id int64) (datamodels.Movie, error) {
|
func (c *MovieController) PutBy(id int64) (datamodels.Movie, error) {
|
||||||
// get the request data for poster and genre
|
// 从请求中获取poster和genre
|
||||||
file, info, err := c.Ctx.FormFile("poster")
|
file, info, err := c.Ctx.FormFile("poster")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return datamodels.Movie{}, errors.New("failed due form file 'poster' missing")
|
return datamodels.Movie{}, errors.New("failed due form file 'poster' missing")
|
||||||
}
|
}
|
||||||
// we don't need the file so close it now.
|
// 关闭文件
|
||||||
file.Close()
|
file.Close()
|
||||||
|
|
||||||
// imagine that is the url of the uploaded file...
|
//想象这就是一个上传文件的url
|
||||||
poster := info.Filename
|
poster := info.Filename
|
||||||
genre := c.Ctx.FormValue("genre")
|
genre := c.Ctx.FormValue("genre")
|
||||||
|
|
||||||
return c.Service.UpdatePosterAndGenreByID(id, poster, genre)
|
return c.Service.UpdatePosterAndGenreByID(id, poster, genre)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteBy deletes a movie.
|
// DeleteBy删除一个movie对象
|
||||||
// Demo:
|
// 例子:
|
||||||
// curl -i -X DELETE -u admin:password http://localhost:8080/movies/1
|
// curl -i -X DELETE -u admin:password http://localhost:8080/movies/1
|
||||||
func (c *MovieController) DeleteBy(id int64) interface{} {
|
func (c *MovieController) DeleteBy(id int64) interface{} {
|
||||||
wasDel := c.Service.DeleteByID(id)
|
wasDel := c.Service.DeleteByID(id)
|
||||||
if wasDel {
|
if wasDel {
|
||||||
// return the deleted movie's ID
|
// 返回要删除的ID
|
||||||
return iris.Map{"deleted": 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),
|
//现在我们可以看到这里可以返回一个有2个返回值(map或int)的函数
|
||||||
|
//我们并没有指定一个返回的类型
|
||||||
|
// 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.
|
// we don't have to specify the return type to a specific type.
|
||||||
return iris.StatusBadRequest
|
return iris.StatusBadRequest
|
||||||
}
|
}
|
||||||
|
@ -909,8 +924,8 @@ import (
|
||||||
"github.com/kataras/iris/mvc"
|
"github.com/kataras/iris/mvc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HelloController is our sample controller
|
// HelloController是控制器的例子
|
||||||
// it handles GET: /hello and GET: /hello/{name}
|
// 下面会处理GET: /hello and GET: /hello/{name}
|
||||||
type HelloController struct {
|
type HelloController struct {
|
||||||
mvc.C
|
mvc.C
|
||||||
}
|
}
|
||||||
|
@ -923,17 +938,16 @@ var helloView = mvc.View{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get will return a predefined view with bind data.
|
// Get会返回预定义绑定数据的视图
|
||||||
//
|
//
|
||||||
// `mvc.Result` is just an interface with a `Dispatch` function.
|
// `mvc.Result`是一个含有`Dispatch`方法的接口
|
||||||
// `mvc.Response` and `mvc.View` are the built'n result type dispatchers
|
// `mvc.Response` 和 `mvc.View` dispatchers 内置类型
|
||||||
// you can even create custom response dispatchers by
|
// 你也可以通过实现`github.com/kataras/iris/mvc#Result`接口来自定义dispatchers
|
||||||
// implementing the `github.com/kataras/iris/mvc#Result` interface.
|
|
||||||
func (c *HelloController) Get() mvc.Result {
|
func (c *HelloController) Get() mvc.Result {
|
||||||
return helloView
|
return helloView
|
||||||
}
|
}
|
||||||
|
|
||||||
// you can define a standard error in order to be re-usable anywhere in your app.
|
// 你可以定义一个标准通用的error
|
||||||
var errBadName = errors.New("bad name")
|
var errBadName = errors.New("bad name")
|
||||||
|
|
||||||
// you can just return it as error or even better
|
// you can just return it as error or even better
|
||||||
|
|
Loading…
Reference in New Issue
Block a user