Fixed namespace updates not working
This commit is contained in:
@ -8,15 +8,25 @@ func CreateOrUpdateList(list *List) (err error) {
|
||||
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)
|
||||
} else {
|
||||
_, err = x.ID(list.ID).Update(list)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
*list, err = GetListByID(list.ID)
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
@ -7,29 +7,30 @@ type List struct {
|
||||
Description string `xorm:"varchar(1000)" json:"description"`
|
||||
OwnerID int64 `xorm:"int(11)" json:"-"`
|
||||
NamespaceID int64 `xorm:"int(11)" json:"-"`
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
||||
Owner User `xorm:"-" json:"owner"`
|
||||
Items []*ListItem `xorm:"-" json:"items"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
}
|
||||
|
||||
// GetListByID returns a list by its ID
|
||||
func GetListByID(id int64) (list *List, err error) {
|
||||
func GetListByID(id int64) (list List, err error) {
|
||||
list.ID = id
|
||||
exists, err := x.Get(&list)
|
||||
if err != nil {
|
||||
return &List{}, err
|
||||
return List{}, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return &List{}, ErrListDoesNotExist{ID: id}
|
||||
return List{}, ErrListDoesNotExist{ID: id}
|
||||
}
|
||||
|
||||
// Get the list owner
|
||||
user, _, err := GetUserByID(list.OwnerID)
|
||||
if err != nil {
|
||||
return &List{}, err
|
||||
return List{}, err
|
||||
}
|
||||
|
||||
list.Owner = user
|
||||
|
@ -3,9 +3,9 @@ package models
|
||||
// Namespace holds informations about a namespace
|
||||
type Namespace struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
Name string `xorm:"varchar(250) autoincr not null" json:"name"`
|
||||
Description string `xorm:"varchar(700) autoincr not null" json:"description"`
|
||||
OwnerID int64 `xorm:"int(11) autoincr not null" json:"-"`
|
||||
Name string `xorm:"varchar(250)" json:"name"`
|
||||
Description string `xorm:"varchar(1000)" json:"description"`
|
||||
OwnerID int64 `xorm:"int(11) not null" json:"-"`
|
||||
|
||||
Owner User `xorm:"-" json:"owner"`
|
||||
|
||||
@ -58,9 +58,9 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (has bool, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func GetNamespaceByID(id int64) (namespace *Namespace, err error) {
|
||||
func GetNamespaceByID(id int64) (namespace Namespace, err error) {
|
||||
namespace.ID = id
|
||||
exists, err := x.Get(namespace)
|
||||
exists, err := x.Get(&namespace)
|
||||
if err != nil {
|
||||
return namespace, err
|
||||
}
|
||||
@ -70,7 +70,7 @@ func GetNamespaceByID(id int64) (namespace *Namespace, err error) {
|
||||
}
|
||||
|
||||
// Get the namespace Owner
|
||||
namespace.Owner, _, err = GetUserByID(namespace.Owner.ID)
|
||||
namespace.Owner, _, err = GetUserByID(namespace.OwnerID)
|
||||
if err != nil {
|
||||
return namespace, err
|
||||
}
|
||||
|
@ -1 +1,58 @@
|
||||
package models
|
||||
|
||||
// CreateOrUpdateNamespace does what it says
|
||||
func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
|
||||
// Check if the namespace exists
|
||||
_, err = GetNamespaceByID(namespace.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the User exists
|
||||
namespace.Owner, _, err = GetUserByID(namespace.Owner.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
namespace.OwnerID = namespace.Owner.ID
|
||||
|
||||
if namespace.ID == 0 {
|
||||
_, err = x.Insert(namespace)
|
||||
} else {
|
||||
_, err = x.ID(namespace.ID).Update(namespace)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Get the new one
|
||||
*namespace, err = GetNamespaceByID(namespace.ID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllNamespacesByUserID does what it says
|
||||
func GetAllNamespacesByUserID(userID int64) (namespaces []Namespace, err error) {
|
||||
|
||||
// First, get all namespaces which that user owns
|
||||
err = x.Where("owner_id = ?", userID).Find(&namespaces)
|
||||
if err != nil {
|
||||
return namespaces, err
|
||||
}
|
||||
|
||||
// Get all namespaces of teams that user is part of
|
||||
/*err = x.Table("namespaces").
|
||||
Join("INNER", ).
|
||||
Find(namespaces)*/
|
||||
|
||||
// Get user objects
|
||||
// I couldn't come up with a more performant way to do this...
|
||||
for in, n := range namespaces {
|
||||
namespaces[in].Owner, _, err = GetUserByID(n.OwnerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -1,52 +0,0 @@
|
||||
package models
|
||||
|
||||
// CreateOrUpdateNamespace does what it says
|
||||
func CreateOrUpdateNamespace(namespace *Namespace) (err error) {
|
||||
// Check if the User exists
|
||||
_, _, err = GetUserByID(namespace.Owner.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
namespace.OwnerID = namespace.Owner.ID
|
||||
|
||||
if namespace.ID == 0 {
|
||||
_, err = x.Insert(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
_, err = x.ID(namespace.ID).Update(namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllNamespacesByUserID does what it says
|
||||
func GetAllNamespacesByUserID(userID int64) (namespaces []Namespace, err error) {
|
||||
|
||||
// First, get all namespaces which that user owns
|
||||
err = x.Where("owner_id = ?", userID).Find(&namespaces)
|
||||
if err != nil {
|
||||
return namespaces, err
|
||||
}
|
||||
|
||||
// Get all namespaces of teams that user is part of
|
||||
/*err = x.Table("namespaces").
|
||||
Join("INNER", ).
|
||||
Find(namespaces)*/
|
||||
|
||||
// Get user objects
|
||||
// I couldn't come up with a more performant way to do this...
|
||||
for in, n := range namespaces {
|
||||
namespaces[in].Owner, _, err = GetUserByID(n.OwnerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user