vikunja-api/models/namespaces_add_update.go

57 lines
1.2 KiB
Go
Raw Normal View History

package models
2018-07-04 08:15:47 +02:00
// Create implements the creation method via the interface
func (n *Namespace) Create(doer *User, _ int64) (err error) {
// Check if we have at least a name
if n.Name == "" {
return ErrNamespaceNameCannotBeEmpty{NamespaceID:0, UserID:doer.ID}
2018-07-04 08:15:47 +02:00
}
// Check if the User exists
n.Owner, _, err = GetUserByID(doer.ID)
2018-07-04 08:15:47 +02:00
if err != nil {
return
}
n.OwnerID = n.Owner.ID
2018-07-04 08:15:47 +02:00
// Insert
_, err = x.Insert(n)
return
}
// Update implements the update method via the interface
func (n *Namespace) Update(id int64, doer *User) (err error) {
// Check if we have at least a name
if n.Name == "" {
return ErrNamespaceNameCannotBeEmpty{NamespaceID:id, UserID:doer.ID}
2018-07-04 08:15:47 +02:00
}
n.ID = id
2018-07-04 08:15:47 +02:00
// Check if the namespace exists
currentNamespace, err := GetNamespaceByID(id)
2018-07-04 08:15:47 +02:00
if err != nil {
return
}
// Check if the (new) owner exists
if currentNamespace.OwnerID != n.OwnerID {
n.Owner, _, err = GetUserByID(doer.ID)
if err != nil {
return
}
}
// Check rights
user, _, err := GetUserByID(doer.ID)
if err != nil {
return
}
if !currentNamespace.IsAdmin(&user) {
return ErrNeedToBeNamespaceAdmin{NamespaceID:id, UserID:doer.ID}
}
// Do the actual update
_, err = x.ID(currentNamespace.ID).Update(n)
return
}