diff --git a/models/author.go b/models/author.go index 20eb023..7b49417 100644 --- a/models/author.go +++ b/models/author.go @@ -1,5 +1,6 @@ package models +// Author holds infos about an author type Author struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Forename string `xorm:"varchar(250)"` @@ -8,10 +9,12 @@ type Author struct { Updated int64 `xorm:"updated"` } +// TableName returns the table name for struct author func (Author) TableName() string { return "authors" } +// AuthorBook holds the relation between an author and a books type AuthorBook struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` AuthorID int64 `xorm:"int(11)"` @@ -21,10 +24,12 @@ type AuthorBook struct { Updated int64 `xorm:"updated"` } +// TableName returns the name for the relation author <-> book func (AuthorBook) TableName() string { return "authors_books" } +// GetAuthorByID gets information about an author by its ID func GetAuthorByID(id int64) (author Author, exists bool, err error) { has, err := x.Id(id).Get(&author) diff --git a/models/authors_add.go b/models/authors_add.go index 821c4ea..588b11b 100644 --- a/models/authors_add.go +++ b/models/authors_add.go @@ -1,5 +1,6 @@ package models +// AddAuthor adds a new author based on an author struct func AddAuthor(author Author) (newAuthor Author, err error) { _, err = x.Insert(&author) diff --git a/models/authors_delete.go b/models/authors_delete.go index f111632..5c7c1f1 100644 --- a/models/authors_delete.go +++ b/models/authors_delete.go @@ -1,5 +1,6 @@ package models +// DeleteAuthorByID deletes an author by its ID func DeleteAuthorByID(id int64) error { // Delete the author _, err := x.Id(id).Delete(&Author{}) diff --git a/models/authors_list.go b/models/authors_list.go index 135a409..95ab469 100644 --- a/models/authors_list.go +++ b/models/authors_list.go @@ -1,5 +1,6 @@ package models +// ListAuthors returns a list with all authors, filtered by an optional searchstring func ListAuthors(searchterm string) (authors []Author, err error) { if searchterm == "" { diff --git a/models/authors_update.go b/models/authors_update.go index bb88e95..fe5c3e4 100644 --- a/models/authors_update.go +++ b/models/authors_update.go @@ -1,5 +1,6 @@ package models +// UpdateAuthor updates an author by its ID and a new author struct func UpdateAuthor(author Author, id int64) (newAuthor Author, err error) { _, err = x.Where("id = ?", id).Update(&author) diff --git a/models/book.go b/models/book.go index f4e7402..b6ccfc2 100644 --- a/models/book.go +++ b/models/book.go @@ -2,6 +2,7 @@ package models import "fmt" +// Book holds a book type Book struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Title string `xorm:"varchar(250) not null"` @@ -17,11 +18,13 @@ type Book struct { Authors []Author `xorm:"-"` } +// TableName returns the name for the books table func (Book) TableName() string { return "books" } -func GetBookById(ID int64) (book Book, exists bool, err error) { +// GetBookByID gets a Book by its ID +func GetBookByID(ID int64) (book Book, exists bool, err error) { // Get the Book has, err := x.ID(ID).Get(&book) diff --git a/models/books_add.go b/models/books_add.go index 6841760..8ec9f77 100644 --- a/models/books_add.go +++ b/models/books_add.go @@ -10,11 +10,12 @@ ID verwendet. Wenn die ID nicht existiert oder 0 ist, wird geguckt, ob der Publi Book.PublisherFull bereits existtiert (über die ID), ist das nicht der Fall, wird er in 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 +Bei den Autoren wird ebenfalls überprüft, ob sie bereits existieren, wenn dem nicht so ist werden sie in die Datenbank eingetragen und mit dem Buch verknüpft. */ +// AddBook adds a new book, it takes a book struct with author and publisher. Inserts them if they don't already exist func AddBook(book Book) (newBook Book, err error) { // Take Publisher, check if it exists. If not, insert it diff --git a/models/books_delete.go b/models/books_delete.go index bcd2636..f48facf 100644 --- a/models/books_delete.go +++ b/models/books_delete.go @@ -1,5 +1,6 @@ package models +// DeleteBookByID deletes a book by its ID func DeleteBookByID(id int64) error { // Delete the book _, err := x.Id(id).Delete(&Book{}) diff --git a/models/books_list.go b/models/books_list.go index 93c8ac4..27ac2be 100644 --- a/models/books_list.go +++ b/models/books_list.go @@ -2,11 +2,13 @@ package models import "fmt" +// BookPublisher struct to join books with publishers type BookPublisher struct { Book `xorm:"extends"` Publisher `xorm:"extends"` } +// ListBooks returns a list with all books, filtered by an optional searchstring func ListBooks(searchterm string) (books []*Book, err error) { if searchterm == "" { diff --git a/models/config.go b/models/config.go index cb04ee7..7c67a22 100644 --- a/models/config.go +++ b/models/config.go @@ -5,6 +5,7 @@ import ( "os" ) +// Config holds the config struct var Config struct { Database struct { Host string @@ -16,6 +17,7 @@ var Config struct { JWTLoginSecret []byte } +// SetConfig initianlises the config and publishes it for other functions to use func SetConfig() error { // File Checks diff --git a/models/message.go b/models/message.go index 19d028e..9107dec 100644 --- a/models/message.go +++ b/models/message.go @@ -1,5 +1,6 @@ package models +// Message is a standard message type Message struct { Message string } diff --git a/models/models.go b/models/models.go index 6e74a0f..03467f7 100644 --- a/models/models.go +++ b/models/models.go @@ -2,7 +2,7 @@ package models import ( "fmt" - _ "github.com/go-sql-driver/mysql" + _ "github.com/go-sql-driver/mysql" // Because. "github.com/go-xorm/core" "github.com/go-xorm/xorm" ) diff --git a/models/publisher.go b/models/publisher.go index d294b39..baacfea 100644 --- a/models/publisher.go +++ b/models/publisher.go @@ -1,5 +1,6 @@ package models +// Publisher holds publisher informations type Publisher struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Name string `xorm:"varchar(250) not null"` @@ -7,10 +8,12 @@ type Publisher struct { Updated int64 `xorm:"updated"` } +// TableName returns the table name for publishers struct func (Publisher) TableName() string { return "publishers" } +// GetPublisherByID returns a publisher by its ID func GetPublisherByID(id int64) (publisher Publisher, exists bool, err error) { has, err := x.Id(id).Get(&publisher) diff --git a/models/publishers_add.go b/models/publishers_add.go index 1a5f263..94d4711 100644 --- a/models/publishers_add.go +++ b/models/publishers_add.go @@ -1,5 +1,6 @@ package models +// AddPublisher adds a publisher from a publisher struct func AddPublisher(publisher Publisher) (newPublisher Publisher, err error) { _, err = x.Insert(&publisher) diff --git a/models/publishers_delete.go b/models/publishers_delete.go index e0859ea..db25ceb 100644 --- a/models/publishers_delete.go +++ b/models/publishers_delete.go @@ -1,5 +1,6 @@ package models +// DeletePublisherByID deletes a publisher by its ID func DeletePublisherByID(id int64) error { // Delete the publisher _, err := x.Id(id).Delete(&Publisher{}) diff --git a/models/publishers_list.go b/models/publishers_list.go index f67ce41..514b060 100644 --- a/models/publishers_list.go +++ b/models/publishers_list.go @@ -1,5 +1,6 @@ package models +// ListPublishers returns a list with all publishers, filtered by an optional searchstring func ListPublishers(searchterm string) (publishers []Publisher, err error) { if searchterm == "" { diff --git a/models/publishers_update.go b/models/publishers_update.go index d3e94cc..854eb61 100644 --- a/models/publishers_update.go +++ b/models/publishers_update.go @@ -1,5 +1,6 @@ package models +// UpdatePublisher updates a publisher, takes an ID and a publisher struct with the new publisher infos func UpdatePublisher(publisher Publisher, id int64) (newPublisher Publisher, err error) { _, err = x.Where("id = ?", id).Update(&publisher) diff --git a/models/user.go b/models/user.go index fe93c0a..0f8cba4 100644 --- a/models/user.go +++ b/models/user.go @@ -5,6 +5,7 @@ import ( "golang.org/x/crypto/bcrypt" ) +// User holds information about an user type User struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Name string `xorm:"varchar(250)"` @@ -15,17 +16,18 @@ type User struct { Updated int64 `xorm:"updated"` } +// TableName returns the table name for users func (User) TableName() string { return "users" } -// Hash a password +// HashPassword hashes a password func HashPassword(password string) (string, error) { bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) return string(bytes), err } -// Check user credentials +// CheckUserCredentials checks user credentials func CheckUserCredentials(username, password string) (User, error) { // Check if the user exists @@ -36,7 +38,7 @@ func CheckUserCredentials(username, password string) (User, error) { } if !exists { - return User{}, fmt.Errorf("User does not exist!") + return User{}, fmt.Errorf("user does not exist") } // Check the users password diff --git a/routes/api/v1/author_delete.go b/routes/api/v1/author_delete.go index cc19c1b..57175ca 100644 --- a/routes/api/v1/author_delete.go +++ b/routes/api/v1/author_delete.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// AuthorDelete is the handler for deleting an author func AuthorDelete(c echo.Context) error { id := c.Param("id") diff --git a/routes/api/v1/author_show.go b/routes/api/v1/author_show.go index 52ab13e..e747ae9 100644 --- a/routes/api/v1/author_show.go +++ b/routes/api/v1/author_show.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// AuthorShow is the handler to show an author func AuthorShow(c echo.Context) error { author := c.Param("id") diff --git a/routes/api/v1/authors_add.go b/routes/api/v1/authors_add.go index dc60a80..2c48015 100644 --- a/routes/api/v1/authors_add.go +++ b/routes/api/v1/authors_add.go @@ -8,6 +8,7 @@ import ( "strings" ) +// AuthorAdd is the handler to add an author func AuthorAdd(c echo.Context) error { // Check for Request Content author := c.FormValue("author") diff --git a/routes/api/v1/authors_list.go b/routes/api/v1/authors_list.go index e3b7248..3b4863b 100644 --- a/routes/api/v1/authors_list.go +++ b/routes/api/v1/authors_list.go @@ -6,6 +6,7 @@ import ( "net/http" ) +// AuthorsList is the handler to list authors func AuthorsList(c echo.Context) error { list, err := models.ListAuthors("") diff --git a/routes/api/v1/authors_search.go b/routes/api/v1/authors_search.go index a558ecb..38bd147 100644 --- a/routes/api/v1/authors_search.go +++ b/routes/api/v1/authors_search.go @@ -7,6 +7,7 @@ import ( "git.mowie.cc/konrad/Library/models" ) +// AuthorSearch is the handler to search for authors func AuthorSearch(c echo.Context) error { // Prepare the searchterm diff --git a/routes/api/v1/authors_update.go b/routes/api/v1/authors_update.go index d00c4d4..144d39a 100644 --- a/routes/api/v1/authors_update.go +++ b/routes/api/v1/authors_update.go @@ -9,6 +9,7 @@ import ( "strings" ) +// AuthorUpdate is the handler to update an author func AuthorUpdate(c echo.Context) error { // Check for Request Content author := c.FormValue("author") diff --git a/routes/api/v1/book_delete.go b/routes/api/v1/book_delete.go index c3239e7..c1dde04 100644 --- a/routes/api/v1/book_delete.go +++ b/routes/api/v1/book_delete.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// BookDelete is the handler to delete a book func BookDelete(c echo.Context) error { id := c.Param("id") diff --git a/routes/api/v1/book_show.go b/routes/api/v1/book_show.go index 8efaced..99b5c0a 100644 --- a/routes/api/v1/book_show.go +++ b/routes/api/v1/book_show.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// BookShow is the handler to show informations about a book func BookShow(c echo.Context) error { book := c.Param("id") diff --git a/routes/api/v1/books_add.go b/routes/api/v1/books_add.go index 5f55e44..1ba8032 100644 --- a/routes/api/v1/books_add.go +++ b/routes/api/v1/books_add.go @@ -8,6 +8,7 @@ import ( "strings" ) +// BookAdd is the handler to add a book func BookAdd(c echo.Context) error { // Check for Request Content book := c.FormValue("book") diff --git a/routes/api/v1/books_list.go b/routes/api/v1/books_list.go index 48a2d7c..b2c0cae 100644 --- a/routes/api/v1/books_list.go +++ b/routes/api/v1/books_list.go @@ -7,6 +7,7 @@ import ( "git.mowie.cc/konrad/Library/models" ) +// BookList is the handler to list books func BookList(c echo.Context) error { list, err := models.ListBooks("") diff --git a/routes/api/v1/books_search.go b/routes/api/v1/books_search.go index 8309265..caa6f8d 100644 --- a/routes/api/v1/books_search.go +++ b/routes/api/v1/books_search.go @@ -7,6 +7,7 @@ import ( "git.mowie.cc/konrad/Library/models" ) +// BookSearch is the handler to search for books func BookSearch(c echo.Context) error { // Prepare the searchterm diff --git a/routes/api/v1/publisher_show.go b/routes/api/v1/publisher_show.go index 896fe56..9e7b3d2 100644 --- a/routes/api/v1/publisher_show.go +++ b/routes/api/v1/publisher_show.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// PublisherShow is the handler to show informations about a publisher func PublisherShow(c echo.Context) error { publisher := c.Param("id") diff --git a/routes/api/v1/publishers_add.go b/routes/api/v1/publishers_add.go index b18eb04..c926281 100644 --- a/routes/api/v1/publishers_add.go +++ b/routes/api/v1/publishers_add.go @@ -8,6 +8,7 @@ import ( "strings" ) +// PublisherAdd is the handler to add a publisher func PublisherAdd(c echo.Context) error { // Check for Request Content publisher := c.FormValue("publisher") diff --git a/routes/api/v1/publishers_delete.go b/routes/api/v1/publishers_delete.go index ee846a8..15b3306 100644 --- a/routes/api/v1/publishers_delete.go +++ b/routes/api/v1/publishers_delete.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// PublisherDelete is the handler to delete a publisher func PublisherDelete(c echo.Context) error { id := c.Param("id") diff --git a/routes/api/v1/publishers_list.go b/routes/api/v1/publishers_list.go index 382885c..3acb8db 100644 --- a/routes/api/v1/publishers_list.go +++ b/routes/api/v1/publishers_list.go @@ -6,6 +6,7 @@ import ( "net/http" ) +// PublishersList is the handler to list publishers func PublishersList(c echo.Context) error { list, err := models.ListPublishers("") diff --git a/routes/api/v1/publishers_search.go b/routes/api/v1/publishers_search.go index cbffe36..bba32d2 100644 --- a/routes/api/v1/publishers_search.go +++ b/routes/api/v1/publishers_search.go @@ -7,6 +7,7 @@ import ( "git.mowie.cc/konrad/Library/models" ) +// PublisherSearch is the handler to search for a publisher func PublisherSearch(c echo.Context) error { // Prepare the searchterm diff --git a/routes/api/v1/publishers_update.go b/routes/api/v1/publishers_update.go index 12d69f2..96ff3cc 100644 --- a/routes/api/v1/publishers_update.go +++ b/routes/api/v1/publishers_update.go @@ -9,6 +9,7 @@ import ( "strings" ) +// PublisherUpdate is the handler to update a publishers information func PublisherUpdate(c echo.Context) error { // Check for Request Content publisher := c.FormValue("publisher") diff --git a/routes/api/v1/token_check.go b/routes/api/v1/token_check.go index 909f2b5..09aaae0 100644 --- a/routes/api/v1/token_check.go +++ b/routes/api/v1/token_check.go @@ -6,6 +6,7 @@ import ( "github.com/labstack/echo" ) +// CheckToken checks prints a message if the token is valid or not. Currently only used for testing pourposes. func CheckToken(c echo.Context) error { user := c.Get("user").(*jwt.Token) diff --git a/routes/login.go b/routes/login.go index a88e98b..2c05b82 100644 --- a/routes/login.go +++ b/routes/login.go @@ -8,6 +8,7 @@ import ( "time" ) +// Login is the login handler func Login(c echo.Context) error { username := c.FormValue("username") password := c.FormValue("password") diff --git a/routes/routes.go b/routes/routes.go index 2d2e099..313bf34 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -8,6 +8,7 @@ import ( "git.mowie.cc/konrad/Library/models" ) +// NewEcho registers a new Echo instance func NewEcho() *echo.Echo { e := echo.New() @@ -22,6 +23,7 @@ func NewEcho() *echo.Echo { return e } +// RegisterRoutes registers all routes for the application func RegisterRoutes(e *echo.Echo) { // API Routes