2018-07-02 08:54:44 +02:00
|
|
|
package models
|
2018-07-04 08:15:47 +02:00
|
|
|
|
2018-07-12 00:30:31 +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 == "" {
|
2018-07-12 00:37:31 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: 0, UserID: doer.ID}
|
2018-07-04 08:15:47 +02:00
|
|
|
}
|
2018-07-12 23:22:02 +02:00
|
|
|
n.ID = 0 // This would otherwise prevent the creation of new lists after one was created
|
2018-07-04 08:15:47 +02:00
|
|
|
|
|
|
|
// Check if the User exists
|
2018-07-12 00:30:31 +02:00
|
|
|
n.Owner, _, err = GetUserByID(doer.ID)
|
2018-07-04 08:15:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-12 00:30:31 +02:00
|
|
|
n.OwnerID = n.Owner.ID
|
2018-07-04 08:15:47 +02:00
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Insert
|
|
|
|
_, err = x.Insert(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements the update method via the interface
|
2018-07-12 23:07:03 +02:00
|
|
|
func (n *Namespace) Update(id int64) (err error) {
|
2018-07-12 00:30:31 +02:00
|
|
|
// Check if we have at least a name
|
|
|
|
if n.Name == "" {
|
2018-07-12 23:07:03 +02:00
|
|
|
return ErrNamespaceNameCannotBeEmpty{NamespaceID: id}
|
2018-07-04 08:15:47 +02:00
|
|
|
}
|
2018-07-12 00:30:31 +02:00
|
|
|
n.ID = id
|
2018-07-04 08:15:47 +02:00
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Check if the namespace exists
|
|
|
|
currentNamespace, err := GetNamespaceByID(id)
|
2018-07-04 08:15:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Check if the (new) owner exists
|
|
|
|
if currentNamespace.OwnerID != n.OwnerID {
|
2018-07-12 23:07:03 +02:00
|
|
|
n.Owner, _, err = GetUserByID(n.OwnerID)
|
2018-07-12 00:30:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-12 00:09:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 00:30:31 +02:00
|
|
|
// Do the actual update
|
|
|
|
_, err = x.ID(currentNamespace.ID).Update(n)
|
2018-07-12 00:09:16 +02:00
|
|
|
return
|
2018-07-12 00:37:31 +02:00
|
|
|
}
|