Implemented Delete methods on list
This commit is contained in:
parent
b1de837c4f
commit
c8622b0029
5 changed files with 52 additions and 14 deletions
|
@ -5,5 +5,5 @@ type CRUDable interface {
|
||||||
ReadOne(int64) error
|
ReadOne(int64) error
|
||||||
ReadAll(*User) (interface{}, error)
|
ReadAll(*User) (interface{}, error)
|
||||||
Update(int64, *User) error
|
Update(int64, *User) error
|
||||||
Delete()
|
Delete(int64, *User) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,29 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
func DeleteListByID(listID int64, doer *User) (err error) {
|
func (l *List) Delete(id int64, doer *User) (err error) {
|
||||||
|
|
||||||
// Check if the list exists
|
// Check if the list exists
|
||||||
list, err := GetListByID(listID)
|
list, err := GetListByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if list.Owner.ID != doer.ID {
|
// Check rights
|
||||||
return ErrNeedToBeListAdmin{ListID: listID, UserID: doer.ID}
|
user, _, err := GetUserByID(doer.ID)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !list.IsAdmin(&user) {
|
||||||
|
return ErrNeedToBeListAdmin{ListID:id, UserID:user.ID}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the list
|
// Delete the list
|
||||||
_, err = x.ID(listID).Delete(&List{})
|
_, err = x.ID(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 = ?", listID).Delete(&ListItem{})
|
_, err = x.Where("list_id = ?", id).Delete(&ListItem{})
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,6 @@ func (c *CRUDWebHandler) UpdateWeb(ctx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the ID
|
// Get the ID
|
||||||
var err error
|
|
||||||
id, err := models.GetIntURLParam("id", ctx)
|
id, err := models.GetIntURLParam("id", ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||||
|
@ -110,3 +109,33 @@ func (c *CRUDWebHandler) CreateWeb(ctx echo.Context) error {
|
||||||
|
|
||||||
return ctx.JSON(http.StatusOK, c.CObject)
|
return ctx.JSON(http.StatusOK, c.CObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteWeb is the web handler to delete something
|
||||||
|
func (c *CRUDWebHandler) DeleteWeb(ctx echo.Context) error {
|
||||||
|
// Get the ID
|
||||||
|
id, err := models.GetIntURLParam("id", ctx)
|
||||||
|
if err != nil {
|
||||||
|
return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the user has the right to delete
|
||||||
|
user, err := models.GetCurrentUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.CObject.Delete(id, &user)
|
||||||
|
if err != nil {
|
||||||
|
if models.IsErrNeedToBeListAdmin(err) {
|
||||||
|
return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if models.IsErrListDoesNotExist(err) {
|
||||||
|
return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."})
|
||||||
|
}
|
|
@ -1,10 +1,11 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kolaente.de/konrad/list/models"
|
// "git.kolaente.de/konrad/list/models"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
// "net/http"
|
||||||
|
// "strconv"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeleteListByID(c echo.Context) error {
|
func DeleteListByID(c echo.Context) error {
|
||||||
|
@ -33,6 +34,7 @@ func DeleteListByID(c echo.Context) error {
|
||||||
// "500":
|
// "500":
|
||||||
// "$ref": "#/responses/Message"
|
// "$ref": "#/responses/Message"
|
||||||
|
|
||||||
|
/*
|
||||||
// Check if we have our ID
|
// Check if we have our ID
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
// Make int
|
// Make int
|
||||||
|
@ -47,7 +49,7 @@ func DeleteListByID(c echo.Context) error {
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||||
}
|
}
|
||||||
|
|
||||||
err = models.DeleteListByID(itemID, &user)
|
// err = models.DeleteListByID(itemID, &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrNeedToBeListAdmin(err) {
|
if models.IsErrNeedToBeListAdmin(err) {
|
||||||
return c.JSON(http.StatusForbidden, models.Message{"You need to be the list owner to delete a list."})
|
return c.JSON(http.StatusForbidden, models.Message{"You need to be the list owner to delete a list."})
|
||||||
|
@ -61,4 +63,7 @@ func DeleteListByID(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, models.Message{"The list was deleted with success."})
|
return c.JSON(http.StatusOK, models.Message{"The list was deleted with success."})
|
||||||
|
*/
|
||||||
|
|
||||||
|
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
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.PUT("/lists/:id", apiv1.AddListItem)
|
a.PUT("/lists/:id", apiv1.AddListItem)
|
||||||
a.DELETE("/lists/:id", apiv1.DeleteListByID)
|
a.DELETE("/lists/:id", listHandler.DeleteWeb)
|
||||||
|
|
||||||
a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID)
|
a.DELETE("/item/:id", apiv1.DeleteListItemByIDtemByID)
|
||||||
a.POST("/item/:id", apiv1.UpdateListItem)
|
a.POST("/item/:id", apiv1.UpdateListItem)
|
||||||
|
|
Loading…
Reference in a new issue