Fixed namespace updates not working

This commit is contained in:
kolaente 2018-07-04 08:15:47 +02:00
parent ac134fb16b
commit b57ca9375a
No known key found for this signature in database
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
}
// 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
}

View file

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

View file

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

View file

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

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
// 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."})
}

View file

@ -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."})