diff --git a/models/book.go b/models/book.go index b6ccfc2..114b62d 100644 --- a/models/book.go +++ b/models/book.go @@ -23,6 +23,20 @@ func (Book) TableName() string { return "books" } +// Quantity is the quantity for a book +type Quantity struct { + ID int64 `xorm:"int(11) autoincr not null unique pk"` + BookID int64 `xorm:"int(11) not null"` + Quantity int64 `xorm:"int(11) not null"` + Created int64 `xorm:"created"` + Updated int64 `xorm:"updated"` +} + +// TableName returns the name for the quantites table +func (Quantity) TableName() string { + return "books_quantities" +} + // GetBookByID gets a Book by its ID func GetBookByID(ID int64) (book Book, exists bool, err error) { // Get the Book diff --git a/models/config.go b/models/config.go index da213f0..4f0b5c4 100644 --- a/models/config.go +++ b/models/config.go @@ -1,12 +1,12 @@ package models import ( + "fmt" "github.com/go-ini/ini" "os" - "fmt" ) -// Config holds the config struct +// ConfigStruct holds the config struct type ConfigStruct struct { Database struct { Host string @@ -20,6 +20,7 @@ type ConfigStruct struct { FirstUser User `ini:"User"` } +// Config holds the configuration for the program var Config = new(ConfigStruct) // SetConfig initianlises the config and publishes it for other functions to use diff --git a/models/models.go b/models/models.go index a890eeb..d6b9b2c 100644 --- a/models/models.go +++ b/models/models.go @@ -19,7 +19,6 @@ func getEngine() (*xorm.Engine, error) { func SetEngine() (err error) { x, err = getEngine() if err != nil { - return fmt.Errorf("Failed to connect to database: %v", err) } @@ -32,6 +31,7 @@ func SetEngine() (err error) { x.Sync(&Author{}) x.Sync(&AuthorBook{}) x.Sync(&Status{}) + x.Sync(&Quantity{}) x.ShowSQL(true) return nil diff --git a/models/user.go b/models/user.go index 97900e6..8334b50 100644 --- a/models/user.go +++ b/models/user.go @@ -33,6 +33,7 @@ func GetUserByID(id int64) (user User, exists bool, err error) { return user, exists, err } +// GetUserByUsername returns a User struct based on its name func GetUserByUsername(username string) (user User, exists bool, err error) { user.Username = username exists, err = x.Get(&user) @@ -46,7 +47,7 @@ func CreateUser(user User) (newUser User, err error) { // Check if we have all needed informations if newUser.Password == "" || newUser.Username == "" { - return User{}, fmt.Errorf("You need to specify at least a username and a password!") + return User{}, fmt.Errorf("you need to specify at least a username and a password") } // Check if the user already exists @@ -55,7 +56,7 @@ func CreateUser(user User) (newUser User, err error) { return User{}, err } if exists { - return User{}, fmt.Errorf("This username is already taken. Please use another.") + return User{}, fmt.Errorf("this username is already taken. Please use another") } // Hash the password