Files
kontor/go/pkg/tysc/parallelset_dao.go
T
2025-01-14 15:35:53 +01:00

83 lines
2.7 KiB
Go

package tysc
import (
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// ParallelSetDAO extends the type BaseDAO.
type ParallelSetDAO struct {
Db dao.BaseDAO
}
const (
// PARALLELSETCOLLECTION defines the collection name for storing parallel sets.
PARALLELSETCOLLECTION = "parallelSet"
// PARALLELSETMODEL defines the name of the parallel set data model.
PARALLELSETMODEL = "kontor.tysc.parallelSet"
)
// FindAll retrieves the list of parallel sets from the database.
func (m *ParallelSetDAO) FindAll() ([]ParallelSet, error) {
m.Db.Connect()
var parallelSets []ParallelSet
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).Find(bson.M{"model": PARALLELSETMODEL}).All(&parallelSets)
return parallelSets, err
}
// FindByID returns a parallel set with given id or returns the error.
func (m *ParallelSetDAO) FindByID(id string) (ParallelSet, error) {
m.Db.Connect()
var parallelSet ParallelSet
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&parallelSet)
return parallelSet, err
}
// FindByManufacturer returns a paralle set with given manufacturer or returns the error.
func (m *ParallelSetDAO) FindByManufacturer(manufacturer string) ([]ParallelSet, error) {
m.Db.Connect()
var parallelSets []ParallelSet
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).Find(bson.M{"model": PARALLELSETMODEL, "manufacturer": bson.ObjectIdHex(manufacturer)}).All(&parallelSets)
return parallelSets, err
}
// FindByName returns a parallel set with given name or returns the error.
func (m *ParallelSetDAO) FindByName(name string) (ParallelSet, error) {
m.Db.Connect()
var parallelSet ParallelSet
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).Find(bson.M{"name": name, "model": PARALLELSETMODEL}).One(&parallelSet)
return parallelSet, err
}
// Insert a parallel set into database.
func (m *ParallelSetDAO) Insert(parallelSet ParallelSet) error {
m.Db.Connect()
parallelSet.Model = PARALLELSETMODEL
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).Insert(&parallelSet)
return err
}
// Upsert a parallel set into database.
func (m *ParallelSetDAO) Upsert(parallelSet ParallelSet) (*mgo.ChangeInfo, error) {
m.Db.Connect()
parallelSet.Model = PARALLELSETMODEL
info, err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).Upsert(bson.M{"name": parallelSet.Name}, &parallelSet)
return info, err
}
// Update an existing parallel set.
func (m *ParallelSetDAO) Update(parallelSet ParallelSet) error {
m.Db.Connect()
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).UpdateId(parallelSet.ID, &parallelSet)
return err
}
// Delete an existing parallel set.
func (m *ParallelSetDAO) Delete(parallelSet ParallelSet) error {
m.Db.Connect()
err := m.Db.MongoDb.C(PARALLELSETCOLLECTION).Remove(&parallelSet)
return err
}