This commit is contained in:
konrad 2017-11-07 16:35:10 +01:00 committed by kolaente
parent 110361361b
commit b4b0b737a3
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
39 changed files with 158 additions and 163 deletions

View File

@ -1,13 +1,13 @@
package main
import (
"git.mowie.cc/konrad/Library/routes"
"git.mowie.cc/konrad/Library/models"
"git.mowie.cc/konrad/Library/routes"
"fmt"
)
func main(){
func main() {
// Set Engine
err := models.SetEngine()

View File

@ -1,11 +1,11 @@
package models
type Author struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"`
ID int64 `xorm:"int(11) autoincr not null unique pk"`
Forename string `xorm:"varchar(250)"`
Lastname string `xorm:"varchar(250) not null"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
}
func (Author) TableName() string {
@ -13,9 +13,9 @@ func (Author) TableName() string {
}
type AuthorBook struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"`
ID int64 `xorm:"int(11) autoincr not null unique pk"`
AuthorID int64 `xorm:"int(11)"`
BookID int64 `xorm:"int(11)"`
BookID int64 `xorm:"int(11)"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
@ -29,4 +29,4 @@ func GetAuthorByID(id int64) (author Author, exists bool, err error) {
has, err := x.Id(id).Get(&author)
return author, has, err
}
}

View File

@ -1,6 +1,6 @@
package models
func AddAuthor(author Author) (newAuthor Author, err error){
func AddAuthor(author Author) (newAuthor Author, err error) {
_, err = x.Insert(&author)
if err != nil {
@ -11,4 +11,4 @@ func AddAuthor(author Author) (newAuthor Author, err error){
newAuthor, _, err = GetAuthorByID(author.ID)
return newAuthor, err
}
}

View File

@ -1,6 +1,6 @@
package models
func DeleteAuthorByID(id int64) error {
func DeleteAuthorByID(id int64) error {
// Delete the author
_, err := x.Id(id).Delete(&Author{})
@ -9,7 +9,7 @@ func DeleteAuthorByID(id int64) error {
}
// Delete all book relations associated with that author
_, err = x.Delete(&AuthorBook{AuthorID:id})
_, err = x.Delete(&AuthorBook{AuthorID: id})
return err
}
}

View File

@ -1,13 +1,12 @@
package models
func ListAuthors(searchterm string) (authors []Author, err error) {
if searchterm == "" {
err = x.Find(&authors)
} else {
err = x.
Where("forename LIKE ?", "%"+searchterm+"%").
Where("forename LIKE ?", "%"+searchterm+"%").
Or("lastname LIKE ?", "%"+searchterm+"%").
Find(&authors)
}
@ -16,4 +15,4 @@ func ListAuthors(searchterm string) (authors []Author, err error) {
}
return authors, nil
}
}

View File

@ -1,6 +1,6 @@
package models
func UpdateAuthor(author Author, id int64) (newAuthor Author, err error){
func UpdateAuthor(author Author, id int64) (newAuthor Author, err error) {
_, err = x.Where("id = ?", id).Update(&author)
if err != nil {
@ -11,4 +11,4 @@ func UpdateAuthor(author Author, id int64) (newAuthor Author, err error){
newAuthor, _, err = GetAuthorByID(id)
return newAuthor, err
}
}

View File

@ -3,25 +3,25 @@ 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)"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
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)"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
PublisherFull Publisher `xorm:"-"`
Authors []Author `xorm:"-"`
Authors []Author `xorm:"-"`
}
func (Book) TableName() string{
func (Book) TableName() string {
return "books"
}
func GetBookById(ID int64) (book Book, exists bool, err error) {
func GetBookById(ID int64) (book Book, exists bool, err error) {
// Get the Book
has, err := x.ID(ID).Get(&book)
@ -36,7 +36,7 @@ func GetBookById(ID int64) (book Book, exists bool, err error) {
// Get all authors
var authors []Author
err = x.
Table("authors_books").
Table("authors_books").
Select("authors.*").
Join("INNER", "authors", "authors_books.author_id = authors.id").
Where("book_id = ?", book.ID).
@ -49,4 +49,4 @@ func GetBookById(ID int64) (book Book, exists bool, err error) {
book.Authors = authors
return book, has, err
}
}

View File

@ -13,7 +13,7 @@ die Datenbank eingetragen und mit dem Buch verknüpft.
Bei den Autoren wirdebenfalls überprüft, ob sie bereits existieren, wenn dem nicht so ist werden
sie in die Datenbank eingetragen und mit dem Buch verknüpft.
*/
*/
func AddBook(book Book) (newBook Book, err error) {

View File

@ -1,6 +1,6 @@
package models
func DeleteBookByID(id int64) error {
func DeleteBookByID(id int64) error {
// Delete the book
_, err := x.Id(id).Delete(&Book{})
@ -9,7 +9,7 @@ func DeleteBookByID(id int64) error {
}
// Delete all authors associated with that book
_, err = x.Delete(&AuthorBook{BookID:id})
_, err = x.Delete(&AuthorBook{BookID: id})
return err
}
}

View File

@ -3,7 +3,7 @@ package models
import "fmt"
type BookPublisher struct {
Book `xorm:"extends"`
Book `xorm:"extends"`
Publisher `xorm:"extends"`
}
@ -11,19 +11,18 @@ func ListBooks(searchterm string) (books []*Book, err error) {
if searchterm == "" {
err = x.Table("books").
//Join("INNER", "publishers", "books.publisher = publishers.id").
//Join("INNER", "publishers", "books.publisher = publishers.id").
Find(&books)
if err != nil {
fmt.Println("Error getting Books", err)
}
} else {
err = x.Where("title LIKE ?", "%" + searchterm + "%").Find(&books)
err = x.Where("title LIKE ?", "%"+searchterm+"%").Find(&books)
if err != nil {
fmt.Println("Error getting Books", err)
}
}
// Get all authors and publishers
for i, book := range books {
@ -38,7 +37,7 @@ func ListBooks(searchterm string) (books []*Book, err error) {
// Get all authors
var authors []Author
err = x.
Table("authors_books").
Table("authors_books").
Join("INNER", "authors", "authors_books.author_id = authors.id").
Where("book_id = ?", book.ID).
Find(&authors)

View File

@ -5,10 +5,10 @@ import (
"os"
)
var Config struct{
Database struct{
Host string
User string
var Config struct {
Database struct {
Host string
User string
Password string
Database string
}
@ -16,7 +16,7 @@ var Config struct{
JWTLoginSecret []byte
}
func SetConfig() error{
func SetConfig() error {
// File Checks
if _, err := os.Stat("config.ini"); os.IsNotExist(err) {
@ -35,4 +35,4 @@ func SetConfig() error{
Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String())
return nil
}
}

View File

@ -1,5 +1,5 @@
package models
type Message struct{
type Message struct {
Message string
}
}

View File

@ -1,21 +1,20 @@
package models
import (
"github.com/go-xorm/core"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
)
var x *xorm.Engine
func getEngine() (*xorm.Engine, error){
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()

View File

@ -1,10 +1,10 @@
package models
type Publisher struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"`
Name string `xorm:"varchar(250) not null"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
ID int64 `xorm:"int(11) autoincr not null unique pk"`
Name string `xorm:"varchar(250) not null"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
}
func (Publisher) TableName() string {
@ -15,4 +15,4 @@ func GetPublisherByID(id int64) (publisher Publisher, exists bool, err error) {
has, err := x.Id(id).Get(&publisher)
return publisher, has, err
}
}

View File

@ -1,6 +1,6 @@
package models
func AddPublisher(publisher Publisher) (newPublisher Publisher, err error){
func AddPublisher(publisher Publisher) (newPublisher Publisher, err error) {
_, err = x.Insert(&publisher)
if err != nil {
@ -10,4 +10,4 @@ func AddPublisher(publisher Publisher) (newPublisher Publisher, err error){
newPublisher, _, err = GetPublisherByID(publisher.ID)
return newPublisher, err
}
}

View File

@ -1,6 +1,6 @@
package models
func DeletePublisherByID(id int64) error {
func DeletePublisherByID(id int64) error {
// Delete the publisher
_, err := x.Id(id).Delete(&Publisher{})
@ -12,8 +12,7 @@ func DeletePublisherByID(id int64) error {
//book := Book{Publisher:0}
//book.Publisher = 0
//_, err = x.Where("publisher = ?", id).Update(book)
_, err = x.Table("books").Where("publisher = ?", id).Update(map[string]interface{}{"publisher":0})
_, err = x.Table("books").Where("publisher = ?", id).Update(map[string]interface{}{"publisher": 0})
return err
}
}

View File

@ -6,7 +6,7 @@ func ListPublishers(searchterm string) (publishers []Publisher, err error) {
err = x.Find(&publishers)
} else {
err = x.
Where("name LIKE ?", "%" + searchterm + "%").
Where("name LIKE ?", "%"+searchterm+"%").
Find(&publishers)
}
@ -15,4 +15,4 @@ func ListPublishers(searchterm string) (publishers []Publisher, err error) {
}
return publishers, nil
}
}

View File

@ -1,6 +1,6 @@
package models
func UpdatePublisher(publisher Publisher, id int64) (newPublisher Publisher, err error){
func UpdatePublisher(publisher Publisher, id int64) (newPublisher Publisher, err error) {
_, err = x.Where("id = ?", id).Update(&publisher)
if err != nil {
@ -11,4 +11,4 @@ func UpdatePublisher(publisher Publisher, id int64) (newPublisher Publisher, err
newPublisher, _, err = GetPublisherByID(id)
return newPublisher, err
}
}

View File

@ -1,18 +1,18 @@
package models
import (
"golang.org/x/crypto/bcrypt"
"fmt"
"golang.org/x/crypto/bcrypt"
)
type User struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"`
Name string `xorm:"varchar(250)"`
ID int64 `xorm:"int(11) autoincr not null unique pk"`
Name string `xorm:"varchar(250)"`
Username string `xorm:"varchar(250) not null"`
Password string `xorm:"varchar(250) not null"`
Email string `xorm:"varchar(250) not null"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
Email string `xorm:"varchar(250) not null"`
Created int64 `xorm:"created"`
Updated int64 `xorm:"updated"`
}
func (User) TableName() string {
@ -26,10 +26,10 @@ func HashPassword(password string) (string, error) {
}
// Check user credentials
func CheckUserCredentials (username, password string) (User, error) {
func CheckUserCredentials(username, password string) (User, error) {
// Check if the user exists
var user = User{Username:username}
var user = User{Username: username}
exists, err := x.Get(&user)
if err != nil {
return User{}, err
@ -47,4 +47,4 @@ func CheckUserCredentials (username, password string) (User, error) {
}
return user, nil
}
}

View File

@ -1,10 +1,10 @@
package v1
import (
"github.com/labstack/echo"
"strconv"
"net/http"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"strconv"
)
func AuthorDelete(c echo.Context) error {
@ -37,4 +37,4 @@ func AuthorDelete(c echo.Context) error {
}
return c.JSON(http.StatusOK, models.Message{"success"})
}
}

View File

@ -1,9 +1,9 @@
package v1
import (
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"strconv"
)

View File

@ -1,10 +1,10 @@
package v1
import (
"encoding/json"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"encoding/json"
"strings"
)

View File

@ -1,18 +1,18 @@
package v1
import (
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
func AuthorsList(c echo.Context) error {
list, err := models.ListAuthors("")
if err != nil{
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"})
}
return c.JSON(http.StatusOK, list)
}
}

View File

@ -19,14 +19,14 @@ func AuthorSearch(c echo.Context) error {
// Get the Authors
list, err := models.ListAuthors(search)
if err != nil{
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"})
}
// Check if we have any results
if len(list) == 0{
if len(list) == 0 {
return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any authors matching your search term"})
}
return c.JSON(http.StatusOK, list)
}
}

View File

@ -1,12 +1,12 @@
package v1
import (
"encoding/json"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"encoding/json"
"strings"
"strconv"
"strings"
)
func AuthorUpdate(c echo.Context) error {

View File

@ -1,10 +1,10 @@
package v1
import (
"github.com/labstack/echo"
"strconv"
"net/http"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"strconv"
)
func BookDelete(c echo.Context) error {
@ -37,4 +37,4 @@ func BookDelete(c echo.Context) error {
}
return c.JSON(http.StatusOK, models.Message{"success"})
}
}

View File

@ -1,9 +1,9 @@
package v1
import (
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"strconv"
)
@ -30,4 +30,4 @@ func BookShow(c echo.Context) error {
}
return c.JSON(http.StatusOK, bookInfo)
}
}

View File

@ -1,10 +1,10 @@
package v1
import (
"encoding/json"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"encoding/json"
"strings"
)

View File

@ -11,9 +11,9 @@ func BookList(c echo.Context) error {
list, err := models.ListBooks("")
if err != nil{
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"})
}
return c.JSON(http.StatusOK, list)
}
}

View File

@ -19,14 +19,14 @@ func BookSearch(c echo.Context) error {
// Get the Books
list, err := models.ListBooks(search)
if err != nil{
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"})
}
// Check if we have any results
if len(list) == 0{
if len(list) == 0 {
return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any books matching your search term"})
}
return c.JSON(http.StatusOK, list)
}
}

View File

@ -1,9 +1,9 @@
package v1
import (
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"strconv"
)

View File

@ -1,10 +1,10 @@
package v1
import (
"encoding/json"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"encoding/json"
"strings"
)

View File

@ -1,10 +1,10 @@
package v1
import (
"github.com/labstack/echo"
"strconv"
"net/http"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"strconv"
)
func PublisherDelete(c echo.Context) error {
@ -37,4 +37,4 @@ func PublisherDelete(c echo.Context) error {
}
return c.JSON(http.StatusOK, models.Message{"success"})
}
}

View File

@ -1,18 +1,18 @@
package v1
import (
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
func PublishersList(c echo.Context) error {
list, err := models.ListPublishers("")
if err != nil{
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publishers"})
}
return c.JSON(http.StatusOK, list)
}
}

View File

@ -19,14 +19,14 @@ func PublisherSearch(c echo.Context) error {
// Get the Publishers
list, err := models.ListPublishers(search)
if err != nil{
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publisher"})
}
// Check if we have any results
if len(list) == 0{
if len(list) == 0 {
return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any publisher matching your search term"})
}
return c.JSON(http.StatusOK, list)
}
}

View File

@ -1,12 +1,12 @@
package v1
import (
"encoding/json"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"encoding/json"
"strings"
"strconv"
"strings"
)
func PublisherUpdate(c echo.Context) error {

View File

@ -1,16 +1,16 @@
package v1
import (
"github.com/labstack/echo"
"github.com/dgrijalva/jwt-go"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
)
func CheckToken(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
user := c.Get("user").(*jwt.Token)
fmt.Println(user.Valid)
return nil
}
}

View File

@ -1,11 +1,11 @@
package routes
import (
"github.com/labstack/echo"
"github.com/dgrijalva/jwt-go"
"time"
"net/http"
"git.mowie.cc/konrad/Library/models"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"net/http"
"time"
)
func Login(c echo.Context) error {

View File

@ -1,9 +1,9 @@
package routes
import (
apiv1 "git.mowie.cc/konrad/Library/routes/api/v1"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
apiv1 "git.mowie.cc/konrad/Library/routes/api/v1"
"git.mowie.cc/konrad/Library/models"
)
@ -44,7 +44,6 @@ func RegisterRoutes(e *echo.Echo) {
// Login Route
e.POST("/login", Login)
// ===== Routes with Authetification =====
// Authetification
a.Use(middleware.JWT(models.Config.JWTLoginSecret))
@ -67,41 +66,41 @@ func RegisterRoutes(e *echo.Echo) {
// Manage Users
/*
Alles nur mit Api machen, davor dann einen onepager mit vue.js.
Alles nur mit Api machen, davor dann einen onepager mit vue.js.
(Alles mit | benötigt Authentifizierung)
(Alles mit | benötigt Authentifizierung)
Routes:
GET / - entweder übersicht anzeigen (wenn der nutzer eingeloggt ist) oder auf /login weiterleiten
POST /login - Einloggen
POST /logout - ausloggen
Routes:
GET / - entweder übersicht anzeigen (wenn der nutzer eingeloggt ist) oder auf /login weiterleiten
POST /login - Einloggen
POST /logout - ausloggen
GET /books/:id - Buch anzeigen
POST /books/:id - |Buch bearbeiten (inkl mengen)
DELETE /books/:id - |Buch löschen (+alle einträge in authors_books löschen dessen Bush dazu gehört)
GET /books/search?s=se - Suchen
GET /books/list - Auflisten
PUT /books/add - |Hinzufügen
GET /books/:id - Buch anzeigen
POST /books/:id - |Buch bearbeiten (inkl mengen)
DELETE /books/:id - |Buch löschen (+alle einträge in authors_books löschen dessen Bush dazu gehört)
GET /books/search?s=se - Suchen
GET /books/list - Auflisten
PUT /books/add - |Hinzufügen
GET /authors/:id - Autor anzeigen
POST /authors/:id - |Autor bearbeiten
DELETE /authors/:id - |Autor löschen (auch mit allem in books_author)
GET /authors/list - Autoren auflisten
GET /authors/search?s=d - Autoren suchen
PUT /authors/add - |Hinzufügen
GET /authors/:id - Autor anzeigen
POST /authors/:id - |Autor bearbeiten
DELETE /authors/:id - |Autor löschen (auch mit allem in books_author)
GET /authors/list - Autoren auflisten
GET /authors/search?s=d - Autoren suchen
PUT /authors/add - |Hinzufügen
GET /publishers/:id - Verlag anzeigen
POST /publishers/:id - |Verlag bearbeiten
DELETE /publishers/:id - |Verlag löschen (bei büchern Verlag auf 0 setzen)
GET /publishers/list - Verlage auflisten
GET /publishers/search?s= - Verlage suchen
PUT /publishers/add - |Hinzufügen
GET /publishers/:id - Verlag anzeigen
POST /publishers/:id - |Verlag bearbeiten
DELETE /publishers/:id - |Verlag löschen (bei büchern Verlag auf 0 setzen)
GET /publishers/list - Verlage auflisten
GET /publishers/search?s= - Verlage suchen
PUT /publishers/add - |Hinzufügen
GET /settings - |Nutzereinstellungen (Passwort, name etc)
POST /settings - |Nutzereinstellungen (Passwort, name etc)
GET /user - Nutzer anzeigen
PUT /user - |neue Nutzer anlegen
DELETE /user/:id - |nutzer löschen
POST /user/:id - |nutzer bearbeiten
GET /settings - |Nutzereinstellungen (Passwort, name etc)
POST /settings - |Nutzereinstellungen (Passwort, name etc)
GET /user - Nutzer anzeigen
PUT /user - |neue Nutzer anlegen
DELETE /user/:id - |nutzer löschen
POST /user/:id - |nutzer bearbeiten
*/
}