implemented binding url params directly to struct instead of passing them to the method for deleting items
This commit is contained in:
parent
f0c003d069
commit
249128a46e
17 changed files with 48 additions and 40 deletions
|
@ -6,5 +6,5 @@ type CRUDable interface {
|
||||||
ReadOne(int64) error
|
ReadOne(int64) error
|
||||||
ReadAll(*User) (interface{}, error)
|
ReadAll(*User) (interface{}, error)
|
||||||
Update(int64) error
|
Update(int64) error
|
||||||
Delete(int64) error
|
Delete() error
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
// List represents a list of items
|
// List represents a list of items
|
||||||
type List struct {
|
type List 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" param:"listid"`
|
||||||
Title string `xorm:"varchar(250)" json:"title"`
|
Title string `xorm:"varchar(250)" json:"title"`
|
||||||
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:"-"`
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// Delete implements the delete method of CRUDable
|
// Delete implements the delete method of CRUDable
|
||||||
func (l *List) Delete(id int64) (err error) {
|
func (l *List) Delete() (err error) {
|
||||||
// Check if the list exists
|
// Check if the list exists
|
||||||
_, err = GetListByID(id)
|
_, err = GetListByID(l.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the list
|
// Delete the list
|
||||||
_, err = x.ID(id).Delete(&List{})
|
_, err = x.ID(l.ID).Delete(&List{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all todoitems on that list
|
// Delete all todoitems on that list
|
||||||
_, err = x.Where("list_id = ?", id).Delete(&ListItem{})
|
_, err = x.Where("list_id = ?", l.ID).Delete(&ListItem{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
// ListItem represents an item in a todolist
|
// ListItem represents an item in a todolist
|
||||||
type ListItem struct {
|
type ListItem 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" param:"listitemid"`
|
||||||
Text string `xorm:"varchar(250)" json:"text"`
|
Text string `xorm:"varchar(250)" json:"text"`
|
||||||
Description string `xorm:"varchar(250)" json:"description"`
|
Description string `xorm:"varchar(250)" json:"description"`
|
||||||
Done bool `json:"done"`
|
Done bool `json:"done"`
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// Delete implements the delete method for listItem
|
// Delete implements the delete method for listItem
|
||||||
func (i *ListItem) Delete(id int64) (err error) {
|
func (i *ListItem) Delete() (err error) {
|
||||||
|
|
||||||
// Check if it exists
|
// Check if it exists
|
||||||
_, err = GetListItemByID(id)
|
_, err = GetListItemByID(i.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = x.ID(id).Delete(ListItem{})
|
_, err = x.ID(i.ID).Delete(ListItem{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// CanDelete checks if the user can delete an item
|
// CanDelete checks if the user can delete an item
|
||||||
func (i *ListItem) CanDelete(doer *User, id int64) bool {
|
func (i *ListItem) CanDelete(doer *User) bool {
|
||||||
// Get the item
|
// Get the item
|
||||||
lI, _ := GetListItemByID(id)
|
lI, _ := GetListItemByID(i.ID)
|
||||||
|
|
||||||
// A user can delete an item if he has write acces to its list
|
// A user can delete an item if he has write acces to its list
|
||||||
list, _ := GetListByID(lI.ListID)
|
list, _ := GetListByID(lI.ListID)
|
||||||
|
|
|
@ -81,8 +81,8 @@ func (l *List) CanRead(user *User) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanDelete checks if the user can delete a list
|
// CanDelete checks if the user can delete a list
|
||||||
func (l *List) CanDelete(doer *User, id int64) bool {
|
func (l *List) CanDelete(doer *User) bool {
|
||||||
list, _ := GetListByID(id)
|
list, _ := GetListByID(l.ID)
|
||||||
return list.IsAdmin(doer)
|
return list.IsAdmin(doer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// Delete deletes a namespace
|
// Delete deletes a namespace
|
||||||
func (n *Namespace) Delete(id int64) (err error) {
|
func (n *Namespace) Delete() (err error) {
|
||||||
|
|
||||||
// Check if the namespace exists
|
// Check if the namespace exists
|
||||||
_, err = GetNamespaceByID(id)
|
_, err = GetNamespaceByID(n.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the namespace
|
// Delete the namespace
|
||||||
_, err = x.ID(id).Delete(&Namespace{})
|
_, err = x.ID(n.ID).Delete(&Namespace{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete all lists with their items
|
// Delete all lists with their items
|
||||||
lists, err := GetListsByNamespaceID(id)
|
lists, err := GetListsByNamespaceID(n.ID)
|
||||||
var listIDs []int64
|
var listIDs []int64
|
||||||
// We need to do that for here because we need the list ids to delete two times:
|
// We need to do that for here because we need the list ids to delete two times:
|
||||||
// 1) to delete the lists itself
|
// 1) to delete the lists itself
|
||||||
|
|
|
@ -77,8 +77,8 @@ func (n *Namespace) CanUpdate(user *User, id int64) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanDelete checks if the user can delete a namespace
|
// CanDelete checks if the user can delete a namespace
|
||||||
func (n *Namespace) CanDelete(user *User, id int64) bool {
|
func (n *Namespace) CanDelete(user *User) bool {
|
||||||
nn, _ := GetNamespaceByID(id)
|
nn, _ := GetNamespaceByID(n.ID)
|
||||||
return nn.IsAdmin(user)
|
return nn.IsAdmin(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ 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" param:"nid"`
|
||||||
Name string `xorm:"varchar(250)" json:"name"`
|
Name string `xorm:"varchar(250)" json:"name"`
|
||||||
Description string `xorm:"varchar(1000)" json:"description"`
|
Description string `xorm:"varchar(1000)" json:"description"`
|
||||||
OwnerID int64 `xorm:"int(11) not null" json:"-"`
|
OwnerID int64 `xorm:"int(11) not null" json:"-"`
|
||||||
|
|
|
@ -5,7 +5,7 @@ type Rights interface {
|
||||||
IsAdmin(*User) bool
|
IsAdmin(*User) bool
|
||||||
CanWrite(*User) bool
|
CanWrite(*User) bool
|
||||||
CanRead(*User) bool
|
CanRead(*User) bool
|
||||||
CanDelete(*User, int64) bool
|
CanDelete(*User) bool
|
||||||
CanUpdate(*User, int64) bool
|
CanUpdate(*User, int64) bool
|
||||||
CanCreate(*User, int64) bool
|
CanCreate(*User, int64) bool
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,8 @@ package models
|
||||||
// TeamNamespace defines the relationship between a Team and a Namespace
|
// TeamNamespace defines the relationship between a Team and a Namespace
|
||||||
type TeamNamespace struct {
|
type TeamNamespace 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"`
|
||||||
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
|
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"teamid"`
|
||||||
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id"`
|
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"nid"`
|
||||||
Right NamespaceRight `xorm:"int(11)" json:"right"`
|
Right NamespaceRight `xorm:"int(11)" json:"right"`
|
||||||
|
|
||||||
Created int64 `xorm:"created" json:"created"`
|
Created int64 `xorm:"created" json:"created"`
|
||||||
|
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
// Team holds a team object
|
// Team holds a team object
|
||||||
type Team struct {
|
type Team 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" param:"teamid"`
|
||||||
Name string `xorm:"varchar(250) not null" json:"name"`
|
Name string `xorm:"varchar(250) not null" json:"name"`
|
||||||
Description string `xorm:"varchar(250)" json:"description"`
|
Description string `xorm:"varchar(250)" json:"description"`
|
||||||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||||
|
|
|
@ -1,33 +1,33 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
// Delete deletes a team
|
// Delete deletes a team
|
||||||
func (t *Team) Delete(id int64) (err error) {
|
func (t *Team) Delete() (err error) {
|
||||||
|
|
||||||
// Check if the team exists
|
// Check if the team exists
|
||||||
_, err = GetTeamByID(id)
|
_, err = GetTeamByID(t.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the team
|
// Delete the team
|
||||||
_, err = x.ID(id).Delete(&Team{})
|
_, err = x.ID(t.ID).Delete(&Team{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete team members
|
// Delete team members
|
||||||
_, err = x.Where("team_id = ?", id).Delete(&TeamMember{})
|
_, err = x.Where("team_id = ?", t.ID).Delete(&TeamMember{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete team <-> namespace relations
|
// Delete team <-> namespace relations
|
||||||
_, err = x.Where("team_id = ?", id).Delete(&TeamNamespace{})
|
_, err = x.Where("team_id = ?", t.ID).Delete(&TeamNamespace{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete team <-> lists relations
|
// Delete team <-> lists relations
|
||||||
_, err = x.Where("team_id = ?", id).Delete(&TeamList{})
|
_, err = x.Where("team_id = ?", t.ID).Delete(&TeamList{})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,8 @@ func (t *Team) CanUpdate(user *User, id int64) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanDelete checks if a user can delete a team
|
// CanDelete checks if a user can delete a team
|
||||||
func (t *Team) CanDelete(user *User, id int64) bool {
|
func (t *Team) CanDelete(user *User) bool {
|
||||||
t.ID = id
|
//t.ID = id
|
||||||
return t.IsAdmin(user)
|
return t.IsAdmin(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,19 @@ import (
|
||||||
"git.kolaente.de/konrad/list/models"
|
"git.kolaente.de/konrad/list/models"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteWeb is the web handler to delete something
|
// DeleteWeb is the web handler to delete something
|
||||||
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||||
// Get the ID
|
// Get the ID
|
||||||
id, err := models.GetIntURLParam("id", ctx)
|
/*id, err := models.GetIntURLParam("id", ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
||||||
|
}*/
|
||||||
|
// Bind params to struct
|
||||||
|
if err := ParamBinder(c.CObject, ctx); err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL param.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the user has the right to delete
|
// Check if the user has the right to delete
|
||||||
|
@ -19,12 +24,15 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if !c.CObject.CanDelete(&user, id) {
|
if !c.CObject.CanDelete(&user) {
|
||||||
return echo.NewHTTPError(http.StatusForbidden)
|
return echo.NewHTTPError(http.StatusForbidden)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.CObject.Delete(id)
|
err = c.CObject.Delete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
if models.IsErrNeedToBeListAdmin(err) {
|
if models.IsErrNeedToBeListAdmin(err) {
|
||||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.")
|
return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,14 +93,14 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.GET("/lists", listHandler.ReadAllWeb)
|
a.GET("/lists", listHandler.ReadAllWeb)
|
||||||
a.GET("/lists/:id", listHandler.ReadOneWeb)
|
a.GET("/lists/:id", listHandler.ReadOneWeb)
|
||||||
a.POST("/lists/:id", listHandler.UpdateWeb)
|
a.POST("/lists/:id", listHandler.UpdateWeb)
|
||||||
a.DELETE("/lists/:id", listHandler.DeleteWeb)
|
a.DELETE("/lists/:listid", listHandler.DeleteWeb)
|
||||||
a.PUT("/namespaces/:id/lists", listHandler.CreateWeb)
|
a.PUT("/namespaces/:id/lists", listHandler.CreateWeb)
|
||||||
|
|
||||||
itemHandler := &crud.WebHandler{
|
itemHandler := &crud.WebHandler{
|
||||||
CObject: &models.ListItem{},
|
CObject: &models.ListItem{},
|
||||||
}
|
}
|
||||||
a.PUT("/lists/:id", itemHandler.CreateWeb)
|
a.PUT("/lists/:id", itemHandler.CreateWeb)
|
||||||
a.DELETE("/items/:id", itemHandler.DeleteWeb)
|
a.DELETE("/items/:listitemid", itemHandler.DeleteWeb)
|
||||||
a.POST("/items/:id", itemHandler.UpdateWeb)
|
a.POST("/items/:id", itemHandler.UpdateWeb)
|
||||||
|
|
||||||
namespaceHandler := &crud.WebHandler{
|
namespaceHandler := &crud.WebHandler{
|
||||||
|
@ -110,7 +110,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
||||||
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
|
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
|
||||||
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
|
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
|
||||||
a.DELETE("/namespaces/:id", namespaceHandler.DeleteWeb)
|
a.DELETE("/namespaces/:nid", namespaceHandler.DeleteWeb)
|
||||||
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
|
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
|
||||||
|
|
||||||
namespaceTeamHandler := &crud.WebHandler{
|
namespaceTeamHandler := &crud.WebHandler{
|
||||||
|
@ -127,5 +127,5 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.GET("/teams/:id", teamHandler.ReadOneWeb)
|
a.GET("/teams/:id", teamHandler.ReadOneWeb)
|
||||||
a.PUT("/teams", teamHandler.CreateWeb)
|
a.PUT("/teams", teamHandler.CreateWeb)
|
||||||
a.POST("/teams/:id", teamHandler.UpdateWeb)
|
a.POST("/teams/:id", teamHandler.UpdateWeb)
|
||||||
a.DELETE("/teams/:id", teamHandler.DeleteWeb)
|
a.DELETE("/teams/:teamid", teamHandler.DeleteWeb)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue