import other kontor repos into directories
This commit is contained in:
committed by
Thomas Peetz
parent
28746adfbb
commit
b424e95e05
@@ -0,0 +1,12 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// Author defines the data model for library authors with id and name.
|
||||
type Author struct {
|
||||
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Model string `json:"model" bson:"model,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// AuthorDAO extends the type BaseDAO.
|
||||
type AuthorDAO struct {
|
||||
Db dao.BaseDAO
|
||||
}
|
||||
|
||||
const (
|
||||
// AUTHORCOLLECTION defines the collection name for storing authors.
|
||||
AUTHORCOLLECTION = "author"
|
||||
// AUTHORMODEL defines the name of the author data model.
|
||||
AUTHORMODEL = "kontor.library.author"
|
||||
)
|
||||
|
||||
// FindAll retrieves the list of authors from the database.
|
||||
func (m *AuthorDAO) FindAll() ([]Author, error) {
|
||||
m.Db.Connect()
|
||||
var authors []Author
|
||||
err := m.Db.MongoDb.C(AUTHORCOLLECTION).Find(bson.M{"model": AUTHORMODEL}).All(&authors)
|
||||
return authors, err
|
||||
}
|
||||
|
||||
// FindByID returns an author with given id or returns the error.
|
||||
func (m *AuthorDAO) FindByID(id string) (Author, error) {
|
||||
m.Db.Connect()
|
||||
var author Author
|
||||
err := m.Db.MongoDb.C(AUTHORCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&author)
|
||||
return author, err
|
||||
}
|
||||
|
||||
// FindByName returns an author with given name or returns the error.
|
||||
func (m *AuthorDAO) FindByName(name string) (Author, error) {
|
||||
m.Db.Connect()
|
||||
var author Author
|
||||
err := m.Db.MongoDb.C(AUTHORCOLLECTION).Find(bson.M{"name": name, "model": AUTHORMODEL}).One(&author)
|
||||
return author, err
|
||||
}
|
||||
|
||||
// Insert a author into database.
|
||||
func (m *AuthorDAO) Insert(author Author) error {
|
||||
m.Db.Connect()
|
||||
author.Model = AUTHORMODEL
|
||||
err := m.Db.MongoDb.C(AUTHORCOLLECTION).Insert(&author)
|
||||
return err
|
||||
}
|
||||
|
||||
// Upsert a author into database.
|
||||
func (m *AuthorDAO) Upsert(author Author) (*mgo.ChangeInfo, error) {
|
||||
m.Db.Connect()
|
||||
author.Model = AUTHORMODEL
|
||||
info, err := m.Db.MongoDb.C(AUTHORCOLLECTION).Upsert(bson.M{"name": author.Name}, &author)
|
||||
return info, err
|
||||
}
|
||||
|
||||
// Delete an existing author.
|
||||
func (m *AuthorDAO) Delete(author Author) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(AUTHORCOLLECTION).Remove(&author)
|
||||
return err
|
||||
}
|
||||
|
||||
// Update an existing author.
|
||||
func (m *AuthorDAO) Update(author Author) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(AUTHORCOLLECTION).UpdateId(author.ID, &author)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var authorModelTestTable = []struct {
|
||||
name string
|
||||
typeName string
|
||||
}{
|
||||
{"Id", "string"},
|
||||
{"Name", "string"},
|
||||
{"Model", "string"},
|
||||
}
|
||||
|
||||
func TestAuthorModel(t *testing.T) {
|
||||
m := Author{}
|
||||
if reflect.TypeOf(m).NumField() != len(authorModelTestTable) {
|
||||
t.Fail()
|
||||
}
|
||||
for index, testData := range authorModelTestTable {
|
||||
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
|
||||
if givenType != testData.typeName {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAuthors(t *testing.T) {
|
||||
var (
|
||||
authorDao = AuthorDAO{Db: dao.TestDb}
|
||||
)
|
||||
authors, err := authorDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(authors) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAuthor(t *testing.T) {
|
||||
var (
|
||||
authorDao = AuthorDAO{Db: dao.TestDb}
|
||||
author = Author{}
|
||||
authors []Author
|
||||
)
|
||||
author.ID = bson.NewObjectId()
|
||||
author.Name = "Packt Publishing"
|
||||
err := authorDao.Insert(author)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
authors, err = authorDao.FindAll()
|
||||
if len(authors) != 1 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpsertAuthor(t *testing.T) {
|
||||
var (
|
||||
authorDao = AuthorDAO{Db: dao.TestDb}
|
||||
)
|
||||
var author = Author{}
|
||||
author.ID = bson.NewObjectId()
|
||||
author.Name = "Hansa Verlag"
|
||||
authorDao.Upsert(author)
|
||||
authors, err := authorDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(authors) != 2 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAuthor(t *testing.T) {
|
||||
var (
|
||||
authorDao = AuthorDAO{Db: dao.TestDb}
|
||||
)
|
||||
authors, err := authorDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
for _, author := range authors {
|
||||
authorDao.Delete(author)
|
||||
}
|
||||
authors, err = authorDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(authors) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// Book defines the data model for library books with id, title, author, publisher,
|
||||
// isbn, year and edition.
|
||||
type Book struct {
|
||||
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"`
|
||||
Title string `json:"title" bson:"title"`
|
||||
Author string `json:"author" bson:"author"`
|
||||
Publisher bson.ObjectId `json:"publisher" bson:"publisher,omitempty"`
|
||||
Isbn string `json:"isbn" bson:"isbn,omitempty"`
|
||||
Year int `json:"year" bson:"year,omitempty"`
|
||||
Edition string `json:"edition" bson:"edition,omitempty"`
|
||||
Model string `json:"model" bson:"model,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// BookDAO extends the type BaseDAO.
|
||||
type BookDAO struct {
|
||||
Db dao.BaseDAO
|
||||
}
|
||||
|
||||
const (
|
||||
// BOOKCOLLECTION defines the collection name for storing books.
|
||||
BOOKCOLLECTION = "book"
|
||||
// BOOKMODEL defines the name of the book data model.
|
||||
BOOKMODEL = "kontor.library.book"
|
||||
)
|
||||
|
||||
// FindAll retrieves the list of books from the database.
|
||||
func (m *BookDAO) FindAll() ([]Book, error) {
|
||||
m.Db.Connect()
|
||||
var books []Book
|
||||
err := m.Db.MongoDb.C(BOOKCOLLECTION).Find(bson.M{"model": BOOKMODEL}).All(&books)
|
||||
return books, err
|
||||
}
|
||||
|
||||
// FindByID returns an book with given id or returns the error.
|
||||
func (m *BookDAO) FindByID(id string) (Book, error) {
|
||||
m.Db.Connect()
|
||||
var book Book
|
||||
err := m.Db.MongoDb.C(BOOKCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&book)
|
||||
return book, err
|
||||
}
|
||||
|
||||
// FindByTitle returns a book with given title or returns the error.
|
||||
func (m *BookDAO) FindByTitle(title string) (Book, error) {
|
||||
m.Db.Connect()
|
||||
var book Book
|
||||
err := m.Db.MongoDb.C(BOOKCOLLECTION).Find(bson.M{"title": title, "model": BOOKMODEL}).One(&book)
|
||||
return book, err
|
||||
}
|
||||
|
||||
// Insert a book into database.
|
||||
func (m *BookDAO) Insert(book Book) error {
|
||||
m.Db.Connect()
|
||||
book.Model = BOOKMODEL
|
||||
err := m.Db.MongoDb.C(BOOKCOLLECTION).Insert(&book)
|
||||
return err
|
||||
}
|
||||
|
||||
// Upsert a book into database.
|
||||
func (m *BookDAO) Upsert(book Book) (*mgo.ChangeInfo, error) {
|
||||
m.Db.Connect()
|
||||
book.Model = BOOKMODEL
|
||||
info, err := m.Db.MongoDb.C(BOOKCOLLECTION).Upsert(bson.M{"title": book.Title}, &book)
|
||||
return info, err
|
||||
}
|
||||
|
||||
// Delete an existing book.
|
||||
func (m *BookDAO) Delete(book Book) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(BOOKCOLLECTION).Remove(&book)
|
||||
return err
|
||||
}
|
||||
|
||||
// Update an existing book.
|
||||
func (m *BookDAO) Update(book Book) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(BOOKCOLLECTION).UpdateId(book.ID, &book)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var bookModelTestTable = []struct {
|
||||
name string
|
||||
typeName string
|
||||
}{
|
||||
{"Id", "string"},
|
||||
{"Title", "string"},
|
||||
{"Author", "string"},
|
||||
{"Publisher", "string"},
|
||||
{"Isbn", "string"},
|
||||
{"Year", "int"},
|
||||
{"Edition", "string"},
|
||||
{"Model", "string"},
|
||||
}
|
||||
|
||||
func TestBookModel(t *testing.T) {
|
||||
m := Book{}
|
||||
if reflect.TypeOf(m).NumField() != len(bookModelTestTable) {
|
||||
t.Fail()
|
||||
}
|
||||
for index, testData := range bookModelTestTable {
|
||||
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
|
||||
if givenType != testData.typeName {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBooks(t *testing.T) {
|
||||
var (
|
||||
bookDao = BookDAO{Db: dao.TestDb}
|
||||
)
|
||||
books, err := bookDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(books) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertBook(t *testing.T) {
|
||||
var (
|
||||
bookDao = BookDAO{Db: dao.TestDb}
|
||||
book = Book{}
|
||||
books []Book
|
||||
)
|
||||
book.ID = bson.NewObjectId()
|
||||
book.Title = "Packt Publishing"
|
||||
err := bookDao.Insert(book)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
books, err = bookDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(books) != 1 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpsertBook(t *testing.T) {
|
||||
var (
|
||||
bookDao = BookDAO{Db: dao.TestDb}
|
||||
book = Book{}
|
||||
)
|
||||
book.ID = bson.NewObjectId()
|
||||
book.Title = "Hansa Verlag"
|
||||
bookDao.Upsert(book)
|
||||
books, err := bookDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(books) != 2 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteBook(t *testing.T) {
|
||||
var (
|
||||
bookDao = BookDAO{Db: dao.TestDb}
|
||||
)
|
||||
books, err := bookDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
for _, book := range books {
|
||||
bookDao.Delete(book)
|
||||
}
|
||||
books, err = bookDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(books) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// Publisher defines the data model for library publishers with id and name.
|
||||
type Publisher struct {
|
||||
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Model string `json:"model" bson:"model"`
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// PublisherDAO extends the type BaseDAO.
|
||||
type PublisherDAO struct {
|
||||
Db dao.BaseDAO
|
||||
}
|
||||
|
||||
const (
|
||||
// PUBLISHERCOLLECTION defines the collection name for storing publishers.
|
||||
PUBLISHERCOLLECTION = "publisher"
|
||||
// PUBLISHERMODEL defines the name of the publisher data model.
|
||||
PUBLISHERMODEL = "kontor.library.publisher"
|
||||
)
|
||||
|
||||
// FindAll retrieves the list of publishers from the database.
|
||||
func (m *PublisherDAO) FindAll() ([]Publisher, error) {
|
||||
m.Db.Connect()
|
||||
var publishers []Publisher
|
||||
err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).Find(bson.M{"model": PUBLISHERMODEL}).All(&publishers)
|
||||
return publishers, err
|
||||
}
|
||||
|
||||
// FindByID returns an publisher with given id or returns the error.
|
||||
func (m *PublisherDAO) FindByID(id string) (Publisher, error) {
|
||||
m.Db.Connect()
|
||||
var publisher Publisher
|
||||
err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&publisher)
|
||||
return publisher, err
|
||||
}
|
||||
|
||||
// FindByName returns an comic with given name or returns the error.
|
||||
func (m *PublisherDAO) FindByName(name string) (Publisher, error) {
|
||||
m.Db.Connect()
|
||||
var publisher Publisher
|
||||
err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).Find(bson.M{"name": name, "model": PUBLISHERMODEL}).One(&publisher)
|
||||
return publisher, err
|
||||
}
|
||||
|
||||
// Insert a publisher into database.
|
||||
func (m *PublisherDAO) Insert(publisher Publisher) error {
|
||||
m.Db.Connect()
|
||||
publisher.Model = PUBLISHERMODEL
|
||||
err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).Insert(&publisher)
|
||||
return err
|
||||
}
|
||||
|
||||
// Upsert a publisher into database.
|
||||
func (m *PublisherDAO) Upsert(publisher Publisher) (*mgo.ChangeInfo, error) {
|
||||
m.Db.Connect()
|
||||
publisher.Model = PUBLISHERMODEL
|
||||
info, err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).Upsert(bson.M{"name": publisher.Name}, &publisher)
|
||||
return info, err
|
||||
}
|
||||
|
||||
// Delete an existing publisher.
|
||||
func (m *PublisherDAO) Delete(publisher Publisher) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).Remove(&publisher)
|
||||
return err
|
||||
}
|
||||
|
||||
// Update an existing publisher.
|
||||
func (m *PublisherDAO) Update(publisher Publisher) error {
|
||||
m.Db.Connect()
|
||||
err := m.Db.MongoDb.C(PUBLISHERCOLLECTION).UpdateId(publisher.ID, &publisher)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var publisherModelTestTable = []struct {
|
||||
name string
|
||||
typeName string
|
||||
}{
|
||||
{"Id", "string"},
|
||||
{"Name", "string"},
|
||||
{"Model", "string"},
|
||||
}
|
||||
|
||||
func TestPublisherModel(t *testing.T) {
|
||||
m := Publisher{}
|
||||
if reflect.TypeOf(m).NumField() != len(publisherModelTestTable) {
|
||||
t.Fail()
|
||||
}
|
||||
for index, testData := range publisherModelTestTable {
|
||||
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
|
||||
if givenType != testData.typeName {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPublishers(t *testing.T) {
|
||||
var (
|
||||
publisherDao = PublisherDAO{Db: dao.TestDb}
|
||||
)
|
||||
publishers, err := publisherDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(publishers) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertPublisher(t *testing.T) {
|
||||
var (
|
||||
publisherDao = PublisherDAO{Db: dao.TestDb}
|
||||
publisher = Publisher{}
|
||||
publishers []Publisher
|
||||
)
|
||||
publisher.ID = bson.NewObjectId()
|
||||
publisher.Name = "Packt Publishing"
|
||||
err := publisherDao.Insert(publisher)
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
publishers, err = publisherDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(publishers) != 1 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpsertPublisher(t *testing.T) {
|
||||
var (
|
||||
publisherDao = PublisherDAO{Db: dao.TestDb}
|
||||
publisher = Publisher{}
|
||||
)
|
||||
publisher.ID = bson.NewObjectId()
|
||||
publisher.Name = "Hansa Verlag"
|
||||
publisherDao.Upsert(publisher)
|
||||
publishers, err := publisherDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(publishers) != 2 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletePublisher(t *testing.T) {
|
||||
var (
|
||||
publisherDao = PublisherDAO{Db: dao.TestDb}
|
||||
)
|
||||
publishers, err := publisherDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
for _, publisher := range publishers {
|
||||
publisherDao.Delete(publisher)
|
||||
}
|
||||
publishers, err = publisherDao.FindAll()
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if len(publishers) != 0 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/util"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
// LibraryPublisherTemplate defines name of template file for comics publishers
|
||||
LibraryPublisherTemplate = "library/publishers.html"
|
||||
)
|
||||
|
||||
func showPublisherList(c *gin.Context) {
|
||||
var dao = PublisherDAO{Db: dao.KontorDb}
|
||||
if publishers, err := dao.FindAll(); err == nil {
|
||||
util.Render(c, gin.H{"title": "Library Publisher List", "payload": publishers}, LibraryPublisherTemplate)
|
||||
} else {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
func showPublisherDetails(c *gin.Context) {
|
||||
var dao = PublisherDAO{Db: dao.KontorDb}
|
||||
publisherid := c.Param("publisher_id")
|
||||
if publisher, err := dao.FindByID(publisherid); err == nil {
|
||||
util.Render(c, gin.H{"title": "Library Publisher", "payload": publisher, "action": util.SaveAction}, LibraryPublisherTemplate)
|
||||
} else {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
func showPublisherCreation(c *gin.Context) {
|
||||
var publisher = Publisher{}
|
||||
util.Render(c, gin.H{"title": "Library Publisher Creation", "payload": publisher, "action": util.AddAction}, LibraryPublisherTemplate)
|
||||
}
|
||||
|
||||
func validatePublisherDetails(c *gin.Context) {
|
||||
name := c.PostForm("name")
|
||||
action := c.PostForm("action")
|
||||
publisherid := c.PostForm("publisherid")
|
||||
|
||||
var err error
|
||||
var dao = PublisherDAO{Db: dao.KontorDb}
|
||||
var publisher = Publisher{}
|
||||
|
||||
switch action {
|
||||
case util.AddAction:
|
||||
publisher.Name = name
|
||||
_, err = dao.Upsert(publisher)
|
||||
case util.SaveAction:
|
||||
publisher, _ = dao.FindByID(publisherid)
|
||||
publisher.Name = name
|
||||
err = dao.Update(publisher)
|
||||
case util.DeleteAction:
|
||||
publisher, _ = dao.FindByID(publisherid)
|
||||
err = dao.Delete(publisher)
|
||||
}
|
||||
if err == nil {
|
||||
c.Redirect(http.StatusTemporaryRedirect, "/library/publisher")
|
||||
} else {
|
||||
c.HTML(http.StatusBadRequest, "library/publisher.html", gin.H{
|
||||
"ErrorTitle": "Publisher Creation Failed",
|
||||
"ErrorMessage": err.Error()})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetRoutes returns all routes for library related data.
|
||||
func GetRoutes(router *gin.Engine) {
|
||||
libraryRoutes := router.Group("/library")
|
||||
{
|
||||
libraryRoutes.GET("/", auth.EnsureLoggedIn(), showBookList)
|
||||
libraryRoutes.GET("/book", auth.EnsureLoggedIn(), showBookList)
|
||||
libraryRoutes.GET("/author", auth.EnsureLoggedIn(), showAuthorList)
|
||||
libraryRoutes.GET("/publisher", auth.EnsureLoggedIn(), showPublisherList)
|
||||
libraryRoutes.POST("/publisher", auth.EnsureLoggedIn(), showPublisherList)
|
||||
libraryRoutes.GET("/publisher/view/:publisher_id", auth.EnsureLoggedIn(), showPublisherDetails)
|
||||
libraryRoutes.POST("/publisher/validate", auth.EnsureLoggedIn(), validatePublisherDetails)
|
||||
libraryRoutes.GET("/publisher/create", auth.EnsureLoggedIn(), showPublisherCreation)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
||||
"gitlab.thpeetz.de/kontor/kontor-go/pkg/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func showAuthorList(c *gin.Context) {
|
||||
var authorDao = AuthorDAO{Db: dao.KontorDb}
|
||||
if authors, err := authorDao.FindAll(); err == nil {
|
||||
util.Render(c, gin.H{"title": "Author List", "payload": authors}, "library/authors.html")
|
||||
}
|
||||
}
|
||||
|
||||
func showBookList(c *gin.Context) {
|
||||
var bookDao = BookDAO{Db: dao.KontorDb}
|
||||
if books, err := bookDao.FindAll(); err == nil {
|
||||
util.Render(c, gin.H{"title": "Book List", "payload": books}, "library/books.html")
|
||||
} else {
|
||||
util.Render(c, gin.H{"title": "Kontor", "payload": nil}, "kontor/index.html")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user