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