import sources from develop/0.1.0

This commit is contained in:
Thomas Peetz
2025-04-29 12:52:55 +02:00
parent 304005822c
commit 4c96de27db
976 changed files with 58265 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
package comics
import (
"gopkg.in/mgo.v2/bson"
)
// Artist defines the data model for comic artists with id and name.
type Artist struct {
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"`
Name string `json:"name" bson:"name"`
Model string `json:"model" bson:"model"`
}
+78
View File
@@ -0,0 +1,78 @@
package comics
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
"log"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// ArtistDAO extends the type BaseDAO.
type ArtistDAO struct {
Db dao.BaseDAO
}
const (
// ARTISTCOLLECTION defines the collection name for storing comic artists.
ARTISTCOLLECTION = "artist"
// ARTISTMODEL defines the name of the artist data model.
ARTISTMODEL = "kontor.comics.artist"
)
// FindAll retrieves the list of artists from the database.
func (m *ArtistDAO) FindAll() ([]Artist, error) {
m.Db.Connect()
var artists []Artist
err := m.Db.MongoDb.C(ARTISTCOLLECTION).Find(bson.M{"model": ARTISTMODEL}).All(&artists)
return artists, err
}
// FindByID returns an artists with given id or returns the error.
func (m *ArtistDAO) FindByID(id string) (Artist, error) {
m.Db.Connect()
var artist Artist
err := m.Db.MongoDb.C(ARTISTCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&artist)
return artist, err
}
// FindByName returns an artists with given name or returns the error.
func (m *ArtistDAO) FindByName(name string) (Artist, error) {
m.Db.Connect()
var artist Artist
err := m.Db.MongoDb.C(ARTISTCOLLECTION).Find(bson.M{"name": name, "model": ARTISTMODEL}).One(&artist)
return artist, err
}
// Insert an artist into database.
func (m *ArtistDAO) Insert(artist Artist) error {
m.Db.Connect()
artist.Model = ARTISTMODEL
err := m.Db.MongoDb.C(ARTISTCOLLECTION).Insert(&artist)
return err
}
// Upsert an artist into database.
func (m *ArtistDAO) Upsert(artist Artist) (*mgo.ChangeInfo, error) {
m.Db.Connect()
artist.Model = ARTISTMODEL
info, err := m.Db.MongoDb.C(ARTISTCOLLECTION).Upsert(bson.M{"name": artist.Name}, &artist)
return info, err
}
// Delete an existing artist.
func (m *ArtistDAO) Delete(artist Artist) error {
m.Db.Connect()
err := m.Db.MongoDb.C(ARTISTCOLLECTION).Remove(&artist)
if err != nil {
log.Printf("ArtistDao.Delete: %v", err)
}
return err
}
// Update an existing artist.
func (m *ArtistDAO) Update(artist Artist) error {
m.Db.Connect()
err := m.Db.MongoDb.C(ARTISTCOLLECTION).UpdateId(artist.ID, &artist)
return err
}
+96
View File
@@ -0,0 +1,96 @@
package comics
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
"reflect"
"testing"
"gopkg.in/mgo.v2/bson"
)
var artistModelTestTable = []struct {
name string
typeName string
}{
{"Id", "string"},
{"Name", "string"},
{"Model", "string"},
}
func TestArtistModel(t *testing.T) {
m := Artist{}
if reflect.TypeOf(m).NumField() != len(artistModelTestTable) {
t.Fail()
}
for index, testData := range artistModelTestTable {
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
if givenType != testData.typeName {
t.Fail()
}
}
}
func TestListArtists(t *testing.T) {
var (
artistDao = ArtistDAO{Db: dao.TestDb}
)
artists, err := artistDao.FindAll()
if err != nil {
t.Fail()
}
if artists != nil {
t.Fail()
}
}
func TestInsertArtist(t *testing.T) {
var (
artistDao = ArtistDAO{Db: dao.TestDb}
artist = Artist{}
artists []Artist
)
artist.ID = bson.NewObjectId()
artist.Name = "Turner, Michael"
err := artistDao.Insert(artist)
if err != nil {
t.Fail()
}
artists, _ = artistDao.FindAll()
if len(artists) != 1 {
t.Fail()
}
}
func TestUpsertArtist(t *testing.T) {
var (
artistDao = ArtistDAO{Db: dao.TestDb}
artist = Artist{}
)
artist.ID = bson.NewObjectId()
artist.Name = "Marz, Ron"
_, err := artistDao.Upsert(artist)
if err != nil {
t.Fail()
}
artists, _ := artistDao.FindAll()
if len(artists) != 2 {
t.Fail()
}
}
func TestDeleteArtist(t *testing.T) {
var (
artistDao = ArtistDAO{Db: dao.TestDb}
)
artists, err := artistDao.FindAll()
if err != nil {
t.Fail()
}
for _, artist := range artists {
artistDao.Delete(artist)
}
artists, _ = artistDao.FindAll()
if len(artists) != 0 {
t.Fail()
}
}
+23
View File
@@ -0,0 +1,23 @@
package comics
import (
"net/http"
"github.com/gin-gonic/gin"
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
"gitlab.thpeetz.de/kontor/kontor-go/pkg/util"
)
const (
// ArtistPublisherTemplate defines name of template file for comics publishers
ArtistPublisherTemplate = "comics/publishers.html"
)
func showArtistList(c *gin.Context) {
var dao = ArtistDAO{Db: dao.KontorDb}
if artists, err := dao.FindAll(); err == nil {
util.Render(c, gin.H{"title": "Comic Artists", "payload": artists}, "artists.html")
} else {
c.AbortWithError(http.StatusNotFound, err)
}
}
+15
View File
@@ -0,0 +1,15 @@
package comics
import (
"gopkg.in/mgo.v2/bson"
)
// Comic defines the data model for comic issues with id, title, publisher, order and completion status.
type Comic struct {
ID bson.ObjectId `json:"_id" bson:"_id,omitempty"`
Title string `json:"title" bson:"title"`
Publisher bson.ObjectId `json:"publisher" bson:"publisher,omitempty"`
CurrentOrder bool `json:"current_order" bson:"current_order"`
Completed bool `json:"completed" bson:"completed"`
Model string `json:"model" bson:"model"`
}
+75
View File
@@ -0,0 +1,75 @@
package comics
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// ComicDAO extends the type BaseDAO.
type ComicDAO struct {
Db dao.BaseDAO
}
const (
// COMICCOLLECTION defines the collection name for storing comics.
COMICCOLLECTION = "comic"
// COMICMODEL defines the name of the comic data model.
COMICMODEL = "kontor.comics.comic"
)
// FindAll retrieves the list of comisc from the database.
func (m *ComicDAO) FindAll() ([]Comic, error) {
m.Db.Connect()
var comics []Comic
err := m.Db.MongoDb.C(COMICCOLLECTION).Find(bson.M{"model": COMICMODEL}).All(&comics)
return comics, err
}
// FindByID returns an comic with given id or returns the error.
func (m *ComicDAO) FindByID(id string) (Comic, error) {
m.Db.Connect()
var comic Comic
err := m.Db.MongoDb.C(COMICCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&comic)
return comic, err
}
// FindByName returns an comic with given name or returns the error.
func (m *ComicDAO) FindByName(name string) (Comic, error) {
m.Db.Connect()
var comic Comic
err := m.Db.MongoDb.C(COMICCOLLECTION).Find(bson.M{"name": name, "model": COMICMODEL}).One(&comic)
return comic, err
}
// Insert a comic into database.
func (m *ComicDAO) Insert(comic Comic) error {
m.Db.Connect()
comic.Model = COMICMODEL
err := m.Db.MongoDb.C(COMICCOLLECTION).Insert(&comic)
//util.PrintDebug("ComicDAO.Insert: %v", comic)
return err
}
// Upsert a comic into database.
func (m *ComicDAO) Upsert(comic Comic) (*mgo.ChangeInfo, error) {
m.Db.Connect()
comic.Model = COMICMODEL
info, err := m.Db.MongoDb.C(COMICCOLLECTION).Upsert(bson.M{"title": comic.Title}, &comic)
return info, err
}
// Delete an existing comic.
func (m *ComicDAO) Delete(comic Comic) error {
m.Db.Connect()
err := m.Db.MongoDb.C(COMICCOLLECTION).Remove(&comic)
return err
}
// Update an existing movie
func (m *ComicDAO) Update(comic Comic) error {
m.Db.Connect()
err := m.Db.MongoDb.C(COMICCOLLECTION).UpdateId(comic.ID, &comic)
return err
}
+88
View File
@@ -0,0 +1,88 @@
package comics
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
"reflect"
"testing"
"gopkg.in/mgo.v2/bson"
)
var comicModelTestTable = []struct {
name string
typeName string
}{
{"Id", "string"},
{"Title", "string"},
{"Publisher", "string"},
{"CurrentOrder", "bool"},
{"Completed", "bool"},
{"Model", "string"},
}
func TestComicModel(t *testing.T) {
m := Comic{}
if reflect.TypeOf(m).NumField() != len(comicModelTestTable) {
t.Fail()
}
for index, testData := range comicModelTestTable {
givenType := reflect.TypeOf(m).Field(index).Type.Kind().String()
if givenType != testData.typeName {
t.Fail()
}
}
}
func TestListComics(t *testing.T) {
var (
comicDao = ComicDAO{Db: dao.TestDb}
)
comics, err := comicDao.FindAll()
if err != nil {
t.Fail()
}
if len(comics) != 0 {
t.Fail()
}
}
func TestInsertComic(t *testing.T) {
var (
comicDao = ComicDAO{Db: dao.TestDb}
comic = Comic{}
comics []Comic
)
comic.ID = bson.NewObjectId()
comic.Title = "Simpsons"
comicDao.Insert(comic)
comics, _ = comicDao.FindAll()
if len(comics) != 1 {
t.Fail()
}
}
func TestUpsertComic(t *testing.T) {
var (
comicDao = ComicDAO{Db: dao.TestDb}
comic = Comic{}
)
comic.ID = bson.NewObjectId()
comic.Title = "1602"
comicDao.Upsert(comic)
comics, _ := comicDao.FindAll()
if len(comics) != 2 {
t.Fail()
}
}
func TestDeleteComic(t *testing.T) {
var (
comicDao = ComicDAO{Db: dao.TestDb}
)
comics, _ := comicDao.FindAll()
for _, comic := range comics {
comicDao.Delete(comic)
}
comics, _ = comicDao.FindAll()
if len(comics) != 0 {
t.Fail()
}
}
+12
View File
@@ -0,0 +1,12 @@
package comics
import (
"gopkg.in/mgo.v2/bson"
)
// Publisher defines the data model for comic 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"`
}
+75
View File
@@ -0,0 +1,75 @@
package comics
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.comics.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)
//util.PrintDebug("PublisherDAO.Insert: %v", 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
}
+87
View File
@@ -0,0 +1,87 @@
package comics
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 = "CrossGen"
publisherDao.Insert(publisher)
publishers, _ = publisherDao.FindAll()
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 = "Marvel"
publisherDao.Upsert(publisher)
publishers, _ := publisherDao.FindAll()
if len(publishers) != 2 {
t.Fail()
}
}
func TestDeletePublisher(t *testing.T) {
var (
publisherDao = PublisherDAO{Db: dao.TestDb}
)
publishers, _ := publisherDao.FindAll()
for _, publisher := range publishers {
publisherDao.Delete(publisher)
}
publishers, _ = publisherDao.FindAll()
if len(publishers) != 0 {
t.Fail()
}
}
+70
View File
@@ -0,0 +1,70 @@
package comics
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 (
// ComicsPublisherTemplate defines name of template file for comics publishers
ComicsPublisherTemplate = "comics/publishers.html"
// ComicsPublisherDetailsTemplate defines name of template file for comics publishers
ComicsPublisherDetailsTemplate = "comics/publisher.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": "Comics Publisher List", "payload": publishers}, ComicsPublisherTemplate)
} 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": "Comics Publisher", "payload": publisher, "action": util.SaveAction}, ComicsPublisherDetailsTemplate)
} else {
c.AbortWithError(http.StatusNotFound, err)
}
}
func showPublisherCreation(c *gin.Context) {
var publisher = Publisher{}
util.Render(c, gin.H{"title": "Comics Publisher Creation", "payload": publisher, "action": util.AddAction}, ComicsPublisherTemplate)
}
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, "/comics/publisher")
} else {
c.HTML(http.StatusBadRequest, "comics/publisher.html", gin.H{
"ErrorTitle": "Publisher Creation Failed",
"ErrorMessage": err.Error()})
}
}
+28
View File
@@ -0,0 +1,28 @@
package comics
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/auth"
"github.com/gin-gonic/gin"
)
// GetRoutes returns all routes for comic related data.
func GetRoutes(router *gin.Engine) {
comicRoutes := router.Group("/comics")
{
comicRoutes.GET("/", auth.EnsureLoggedIn(), showComicList)
comicRoutes.GET("/artist", auth.EnsureLoggedIn(), showArtistList)
comicRoutes.GET("/artist/view/:artist_id", auth.EnsureLoggedIn(), showArtistList)
comicRoutes.GET("/artist/create", auth.EnsureLoggedIn(), showArtistList)
comicRoutes.POST("/artist/create", auth.EnsureLoggedIn(), showArtistList)
comicRoutes.GET("/comic", auth.EnsureLoggedIn(), showComicList)
comicRoutes.GET("/comic/view/:comic_id", auth.EnsureLoggedIn(), showComic)
comicRoutes.GET("/comic/create", auth.EnsureLoggedIn(), showComicList)
comicRoutes.POST("/comic/create", auth.EnsureLoggedIn(), showComicList)
comicRoutes.GET("/publisher", auth.EnsureLoggedIn(), showPublisherList)
comicRoutes.POST("/publisher", auth.EnsureLoggedIn(), showPublisherList)
comicRoutes.GET("/publisher/view/:publisher_id", auth.EnsureLoggedIn(), showPublisherDetails)
comicRoutes.POST("/publisher/validate", auth.EnsureLoggedIn(), validatePublisherDetails)
comicRoutes.GET("/publisher/create", auth.EnsureLoggedIn(), showPublisherCreation)
}
}
+28
View File
@@ -0,0 +1,28 @@
package comics
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
"gitlab.thpeetz.de/kontor/kontor-go/pkg/util"
"net/http"
"github.com/gin-gonic/gin"
)
func showComicList(c *gin.Context) {
var dao = ComicDAO{Db: dao.KontorDb}
if comics, err := dao.FindAll(); err == nil {
util.Render(c, gin.H{"title": "Comics", "payload": comics}, "comics.html")
} else {
c.AbortWithError(http.StatusNotFound, err)
}
}
func showComic(c *gin.Context) {
var dao = ComicDAO{Db: dao.KontorDb}
comicID := c.Param("comic_id")
if comic, err := dao.FindByID(comicID); err == nil {
util.Render(c, gin.H{"title": "Comic Details", "payload": comic, "action": util.SaveAction}, "comic.html")
} else {
c.AbortWithError(http.StatusNotFound, err)
}
}