minor: update xorm example based on chat comments (they moved repository)

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-30 17:59:14 +03:00
parent f93c8b13bc
commit 77ce225c2f
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
2 changed files with 18 additions and 12 deletions

View File

@ -7,13 +7,13 @@ import (
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"xorm.io/xorm"
) )
/* /*
go get -u github.com/mattn/go-sqlite3 go get -u github.com/mattn/go-sqlite3
go get -u github.com/go-xorm/xorm go get -u xorm.io/xorm
If you're on win64 and you can't install go-sqlite3: If you're on win64 and you can't install go-sqlite3:
1. Download: https://sourceforge.net/projects/mingw-w64/files/latest/download 1. Download: https://sourceforge.net/projects/mingw-w64/files/latest/download
@ -21,7 +21,7 @@ import (
3. Add C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin 3. Add C:\Program Files\mingw-w64\x86_64-7.1.0-posix-seh-rt_v5-rev1\mingw64\bin
to your PATH env variable. to your PATH env variable.
Docs: http://xorm.io/docs/ Docs: https://gitea.com/xorm/xorm
*/ */
// User is our user table structure. // User is our user table structure.
@ -43,14 +43,11 @@ func main() {
if err != nil { if err != nil {
app.Logger().Fatalf("orm failed to initialized: %v", err) app.Logger().Fatalf("orm failed to initialized: %v", err)
} }
iris.RegisterOnInterrupt(func() { iris.RegisterOnInterrupt(func() {
orm.Close() orm.Close()
}) })
err = orm.Sync2(new(User)) if err = orm.Sync2(new(User)); err != nil {
if err != nil {
app.Logger().Fatalf("orm failed to initialized User table: %v", err) app.Logger().Fatalf("orm failed to initialized User table: %v", err)
} }
@ -63,9 +60,18 @@ func main() {
app.Get("/get", func(ctx iris.Context) { app.Get("/get", func(ctx iris.Context) {
user := User{ID: 1} user := User{ID: 1}
if ok, _ := orm.Get(&user); ok { found, err := orm.Get(&user) // fetch user with ID.
ctx.Writef("user found: %#v", user) if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
} }
if !found {
ctx.StopWithText(iris.StatusNotFound, "User with ID: %d not found", user.ID)
return
}
ctx.Writef("User Found: %#v", user)
}) })
// http://localhost:8080/insert // http://localhost:8080/insert

View File

@ -683,13 +683,13 @@ func (ctx *Context) StopWithStatus(statusCode int) {
} }
// StopWithText stops the handlers chain and writes the "statusCode" // StopWithText stops the handlers chain and writes the "statusCode"
// among with a message "plainText". // among with a fmt-style text of "format" and optional arguments.
// //
// If the status code is a failure one then // If the status code is a failure one then
// it will also fire the specified error code handler. // it will also fire the specified error code handler.
func (ctx *Context) StopWithText(statusCode int, plainText string) { func (ctx *Context) StopWithText(statusCode int, format string, args ...interface{}) {
ctx.StopWithStatus(statusCode) ctx.StopWithStatus(statusCode)
ctx.WriteString(plainText) ctx.WriteString(fmt.Sprintf(format, args...))
} }
// StopWithError stops the handlers chain and writes the "statusCode" // StopWithError stops the handlers chain and writes the "statusCode"