Fixed namespace updates not working

This commit is contained in:
kolaente 2018-07-04 08:15:47 +02:00
parent ac134fb16b
commit b57ca9375a
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
7 changed files with 83 additions and 72 deletions

View File

@ -8,15 +8,25 @@ func CreateOrUpdateList(list *List) (err error) {
return return
} }
// Check if the user exists
list.Owner, _, err = GetUserByID(list.Owner.ID)
if err != nil {
return
}
list.OwnerID = list.Owner.ID list.OwnerID = list.Owner.ID
if list.ID == 0 { if list.ID == 0 {
_, err = x.Insert(list) _, err = x.Insert(list)
} else { } else {
_, err = x.ID(list.ID).Update(list) _, err = x.ID(list.ID).Update(list)
}
if err != nil {
return return
} }
*list, err = GetListByID(list.ID)
return return
} }

View File

@ -7,29 +7,30 @@ type List struct {
Description string `xorm:"varchar(1000)" json:"description"` Description string `xorm:"varchar(1000)" json:"description"`
OwnerID int64 `xorm:"int(11)" json:"-"` OwnerID int64 `xorm:"int(11)" json:"-"`
NamespaceID 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"` Owner User `xorm:"-" json:"owner"`
Items []*ListItem `xorm:"-" json:"items"` Items []*ListItem `xorm:"-" json:"items"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
} }
// GetListByID returns a list by its ID // 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 list.ID = id
exists, err := x.Get(&list) exists, err := x.Get(&list)
if err != nil { if err != nil {
return &List{}, err return List{}, err
} }
if !exists { if !exists {
return &List{}, ErrListDoesNotExist{ID: id} return List{}, ErrListDoesNotExist{ID: id}
} }
// Get the list owner // Get the list owner
user, _, err := GetUserByID(list.OwnerID) user, _, err := GetUserByID(list.OwnerID)
if err != nil { if err != nil {
return &List{}, err return List{}, err
} }
list.Owner = user list.Owner = user

View File

@ -3,9 +3,9 @@ package models
// Namespace holds informations about a namespace // Namespace holds informations about a namespace
type Namespace struct { type Namespace struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
Name string `xorm:"varchar(250) autoincr not null" json:"name"` Name string `xorm:"varchar(250)" json:"name"`
Description string `xorm:"varchar(700) autoincr not null" json:"description"` Description string `xorm:"varchar(1000)" json:"description"`
OwnerID int64 `xorm:"int(11) autoincr not null" json:"-"` OwnerID int64 `xorm:"int(11) not null" json:"-"`
Owner User `xorm:"-" json:"owner"` Owner User `xorm:"-" json:"owner"`
@ -58,9 +58,9 @@ func (user *User) HasNamespaceAccess(namespace *Namespace) (has bool, err error)
return return
} }
func GetNamespaceByID(id int64) (namespace *Namespace, err error) { func GetNamespaceByID(id int64) (namespace Namespace, err error) {
namespace.ID = id namespace.ID = id
exists, err := x.Get(namespace) exists, err := x.Get(&namespace)
if err != nil { if err != nil {
return namespace, err return namespace, err
} }
@ -70,7 +70,7 @@ func GetNamespaceByID(id int64) (namespace *Namespace, err error) {
} }
// Get the namespace Owner // Get the namespace Owner
namespace.Owner, _, err = GetUserByID(namespace.Owner.ID) namespace.Owner, _, err = GetUserByID(namespace.OwnerID)
if err != nil { if err != nil {
return namespace, err return namespace, err
} }

View File

@ -1 +1,58 @@
package models 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
}

View File

@ -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
}

View File

@ -88,8 +88,10 @@ func addOrUpdateList(c echo.Context) error {
// Check if the list exists // Check if the list exists
// ID = 0 means new list, no error // ID = 0 means new list, no error
var oldList models.List
var err error
if list.ID != 0 { if list.ID != 0 {
_, err := models.GetListByID(list.ID) oldList, err = models.GetListByID(list.ID)
if err != nil { if err != nil {
if models.IsErrListDoesNotExist(err) { if models.IsErrListDoesNotExist(err) {
return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."}) return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."})
@ -113,10 +115,6 @@ func addOrUpdateList(c echo.Context) error {
} }
} else { } else {
// Check if the user owns the list // 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 { if user.ID != oldList.Owner.ID {
return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a list you don't own."}) return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a list you don't own."})
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"strconv" "strconv"
"fmt"
) )
func AddNamespace(c echo.Context) error { 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."}) return c.JSON(http.StatusForbidden, models.Message{"You need to be namespace admin to edit a namespace."})
} }
fmt.Println(namespace)
err = models.CreateOrUpdateNamespace(namespace) err = models.CreateOrUpdateNamespace(namespace)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})