basic list
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
48002e124f
commit
9e6bcb12be
13
main.go
13
main.go
|
@ -1,8 +1,19 @@
|
|||
package main
|
||||
|
||||
import "git.mowie.cc/konrad/Library/routes"
|
||||
import (
|
||||
"git.mowie.cc/konrad/Library/routes"
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main(){
|
||||
|
||||
err := models.SetEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
e := routes.NewEcho()
|
||||
routes.RegisterRoutes(e)
|
||||
e.Start(":8082")
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func List(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "list")
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package models
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Book struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||
Title string `xorm:"varchar(250) not null"`
|
||||
Isbn string `xorm:"varchar(30)"`
|
||||
Year int64 `xorm:"int(11)"`
|
||||
Price float64 `xorm:"double"`
|
||||
Status int64 `xorm:"int(11)"`
|
||||
Publisher int64 `xorm:"int(11)"`
|
||||
}
|
||||
|
||||
func (Book) TableName() string{
|
||||
return "books"
|
||||
}
|
||||
|
||||
func ListBooks() (books []*Book) {
|
||||
|
||||
err := x.Find(&books)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting Books", err)
|
||||
}
|
||||
|
||||
return books
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"github.com/go-xorm/core"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/go-xorm/xorm"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var x *xorm.Engine
|
||||
|
||||
func getEngine() (*xorm.Engine, error){
|
||||
connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
|
||||
"root", "jup2000", "127.0.0.1", "library2")
|
||||
return xorm.NewEngine("mysql", connStr)
|
||||
}
|
||||
|
||||
|
||||
// SetEngine sets the xorm.Engine
|
||||
func SetEngine() (err error) {
|
||||
x, err = getEngine()
|
||||
if err != nil {
|
||||
|
||||
return fmt.Errorf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
x.SetMapper(core.GonicMapper{})
|
||||
|
||||
// Sync
|
||||
x.Sync(&Book{})
|
||||
|
||||
x.ShowSQL(true)
|
||||
return nil
|
||||
}
|
|
@ -3,8 +3,13 @@ package v1
|
|||
import (
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
)
|
||||
|
||||
func List(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "list")
|
||||
|
||||
list := models.ListBooks()
|
||||
|
||||
return c.JSON(http.StatusOK, list)
|
||||
}
|
Loading…
Reference in New Issue