75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package tysc
|
|
|
|
import (
|
|
"gitlab.thpeetz.de/kontor/kontor-go/pkg/dao"
|
|
|
|
mgo "gopkg.in/mgo.v2"
|
|
"gopkg.in/mgo.v2/bson"
|
|
)
|
|
|
|
// PositionDAO extends the type BaseDAO.
|
|
type PositionDAO struct {
|
|
Db dao.BaseDAO
|
|
}
|
|
|
|
const (
|
|
// POSITIONCOLLECTION defines the collection name for storing positions.
|
|
POSITIONCOLLECTION = "position"
|
|
// POSITIONMODEL defines the name of the position data model.
|
|
POSITIONMODEL = "kontor.tysc.position"
|
|
)
|
|
|
|
// FindAll retrieves the list of positions from the database.
|
|
func (m *PositionDAO) FindAll() ([]Position, error) {
|
|
m.Db.Connect()
|
|
var positions []Position
|
|
err := m.Db.MongoDb.C(POSITIONCOLLECTION).Find(bson.M{"model": POSITIONMODEL}).All(&positions)
|
|
return positions, err
|
|
}
|
|
|
|
// FindByID returns a position with given id or returns the error.
|
|
func (m *PositionDAO) FindByID(id string) (Position, error) {
|
|
m.Db.Connect()
|
|
var position Position
|
|
err := m.Db.MongoDb.C(POSITIONCOLLECTION).FindId(bson.ObjectIdHex(id)).One(&position)
|
|
return position, err
|
|
}
|
|
|
|
// FindByName returns a position with given name or returns the error.
|
|
func (m *PositionDAO) FindByName(name string) (Position, error) {
|
|
m.Db.Connect()
|
|
var position Position
|
|
err := m.Db.MongoDb.C(POSITIONCOLLECTION).Find(bson.M{"name": name, "model": POSITIONMODEL}).One(&position)
|
|
return position, err
|
|
}
|
|
|
|
// Insert a position into database.
|
|
func (m *PositionDAO) Insert(position Position) error {
|
|
m.Db.Connect()
|
|
position.Model = POSITIONMODEL
|
|
err := m.Db.MongoDb.C(POSITIONCOLLECTION).Insert(&position)
|
|
return err
|
|
}
|
|
|
|
// Upsert a position into database.
|
|
func (m *PositionDAO) Upsert(position Position) (*mgo.ChangeInfo, error) {
|
|
m.Db.Connect()
|
|
position.Model = POSITIONMODEL
|
|
info, err := m.Db.MongoDb.C(POSITIONCOLLECTION).Upsert(bson.M{"name": position.Name}, &position)
|
|
return info, err
|
|
}
|
|
|
|
// Update an existing position.
|
|
func (m *PositionDAO) Update(position Position) error {
|
|
m.Db.Connect()
|
|
err := m.Db.MongoDb.C(POSITIONCOLLECTION).UpdateId(position.ID, &position)
|
|
return err
|
|
}
|
|
|
|
// Delete an existing position.
|
|
func (m *PositionDAO) Delete(position Position) error {
|
|
m.Db.Connect()
|
|
err := m.Db.MongoDb.C(POSITIONCOLLECTION).Remove(&position)
|
|
return err
|
|
}
|