1
0

Implemented Create and Update methods on list

This commit is contained in:
kolaente
2018-07-09 23:17:19 +02:00
parent 9e8f13edf6
commit b1de837c4f
12 changed files with 152 additions and 29 deletions

View File

@ -2,18 +2,6 @@ package models
// CreateOrUpdateList updates a list or creates it if it doesn't exist
func CreateOrUpdateList(list *List) (err error) {
// Check if it exists
_, err = GetListByID(list.ID)
if err != nil {
return
}
// Check if the user exists
list.Owner, _, err = GetUserByID(list.Owner.ID)
if err != nil {
return
}
list.OwnerID = list.Owner.ID
if list.ID == 0 {
_, err = x.Insert(list)
@ -30,3 +18,46 @@ func CreateOrUpdateList(list *List) (err error) {
return
}
func (l *List) Update(id int64, doer *User) (err error) {
l.ID = id
// Check if it exists
oldList, err := GetListByID(l.ID)
if err != nil {
return
}
// Check rights
user, _, err := GetUserByID(doer.ID)
if err != nil {
return
}
if !oldList.IsAdmin(&user) {
return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID}
}
return CreateOrUpdateList(l)
}
func (l *List) Create(doer *User) (err error) {
// Check rights
user, _, err := GetUserByID(doer.ID)
if err != nil {
return
}
// Get the namespace of the list to check if the user can write to it
namespace, err := GetNamespaceByID(l.NamespaceID)
if err != nil {
return
}
if !namespace.CanWrite(doer) {
return ErrUserDoesNotHaveWriteAccessToNamespace{UserID:user.ID, NamespaceID:namespace.ID}
}
l.Owner.ID = user.ID
return CreateOrUpdateList(l)
}