diff --git a/models/list_create_update.go b/models/list_create_update.go index ac8ad90c..9f3138e3 100644 --- a/models/list_create_update.go +++ b/models/list_create_update.go @@ -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 } diff --git a/models/lists.go b/models/lists.go index bdf1297c..ca6b6330 100644 --- a/models/lists.go +++ b/models/lists.go @@ -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 diff --git a/models/namespaces.go b/models/namespaces.go index f1141354..662f39b4 100644 --- a/models/namespaces.go +++ b/models/namespaces.go @@ -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 } diff --git a/models/namespaces_add_update.go b/models/namespaces_add_update.go index 2640e7f9..67e7ba99 100644 --- a/models/namespaces_add_update.go +++ b/models/namespaces_add_update.go @@ -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 +} diff --git a/models/namespaces_list.go b/models/namespaces_list.go deleted file mode 100644 index 990791ad..00000000 --- a/models/namespaces_list.go +++ /dev/null @@ -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 -} diff --git a/routes/api/v1/lists_add_update.go b/routes/api/v1/lists_add_update.go index 2148ad73..1e755444 100644 --- a/routes/api/v1/lists_add_update.go +++ b/routes/api/v1/lists_add_update.go @@ -88,8 +88,10 @@ func addOrUpdateList(c echo.Context) error { // Check if the list exists // ID = 0 means new list, no error + var oldList models.List + var err error if list.ID != 0 { - _, err := models.GetListByID(list.ID) + oldList, err = models.GetListByID(list.ID) if err != nil { if models.IsErrListDoesNotExist(err) { return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."}) @@ -113,10 +115,6 @@ func addOrUpdateList(c echo.Context) error { } } else { // Check if the user owns the list - oldList, err := models.GetListByID(list.ID) - if err != nil { - return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) - } if user.ID != oldList.Owner.ID { return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a list you don't own."}) } diff --git a/routes/api/v1/namespace_add_update.go b/routes/api/v1/namespace_add_update.go index 511c075c..7d7771d0 100644 --- a/routes/api/v1/namespace_add_update.go +++ b/routes/api/v1/namespace_add_update.go @@ -5,7 +5,6 @@ import ( "github.com/labstack/echo" "net/http" "strconv" - "fmt" ) func AddNamespace(c echo.Context) error { @@ -126,8 +125,6 @@ func addOrUpdateNamespace(c echo.Context) error { return c.JSON(http.StatusForbidden, models.Message{"You need to be namespace admin to edit a namespace."}) } - fmt.Println(namespace) - err = models.CreateOrUpdateNamespace(namespace) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})