Started implementing book quantities
the build was successful Details

This commit is contained in:
konrad 2017-11-16 12:51:23 +01:00 committed by kolaente
parent 4715459d95
commit 9630e8fda2
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 21 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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