implemented namespace update via interface method
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
)
|
||||
@ -36,55 +35,7 @@ func AddList(c echo.Context) error {
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
// Get the list
|
||||
var list *models.List
|
||||
|
||||
if err := c.Bind(&list); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No list model provided."})
|
||||
}
|
||||
|
||||
// Get the namespace ID
|
||||
var err error
|
||||
list.NamespaceID, err = models.GetIntURLParam("nID", c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid namespace ID."})
|
||||
}
|
||||
|
||||
// Get the current user for later checks
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
list.Owner = user
|
||||
|
||||
// Get the namespace
|
||||
namespace, err := models.GetNamespaceByID(list.NamespaceID)
|
||||
if err != nil {
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."})
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
// Check if the user has write acces to that namespace
|
||||
err = user.HasNamespaceWriteAccess(&namespace)
|
||||
if err != nil {
|
||||
if models.IsErrUserDoesNotHaveAccessToNamespace(err) {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You don't have access to this namespace."})
|
||||
}
|
||||
if models.IsErrUserDoesNotHaveWriteAccessToNamespace(err) {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You don't have write access to this namespace."})
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
// Create the new list
|
||||
err = models.CreateOrUpdateList(list)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, list)
|
||||
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// UpdateList ...
|
||||
@ -116,51 +67,5 @@ func UpdateList(c echo.Context) error {
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
// Get the list
|
||||
var list *models.List
|
||||
|
||||
if err := c.Bind(&list); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No list model provided."})
|
||||
}
|
||||
|
||||
// Get the list ID
|
||||
var err error
|
||||
list.ID, err = models.GetIntURLParam("id", c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
|
||||
// Check if the list exists
|
||||
// ID = 0 means new list, no error
|
||||
var oldList models.List
|
||||
if list.ID != 0 {
|
||||
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."})
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the list exists."})
|
||||
}
|
||||
}
|
||||
|
||||
// Get the current user for later checks
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
list.Owner = user
|
||||
|
||||
// Check if the user owns the list
|
||||
// TODO use list function for that
|
||||
if user.ID != oldList.Owner.ID {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a list you don't own."})
|
||||
}
|
||||
|
||||
// Update the list
|
||||
err = models.CreateOrUpdateList(list)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, list)
|
||||
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// AddNamespace ...
|
||||
@ -65,74 +63,3 @@ func UpdateNamespace(c echo.Context) error {
|
||||
|
||||
return echo.NewHTTPError(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// AddOrUpdateNamespace Adds or updates a new namespace
|
||||
func addOrUpdateNamespace(c echo.Context) error {
|
||||
|
||||
// Get the namespace
|
||||
var namespace *models.Namespace
|
||||
|
||||
if err := c.Bind(&namespace); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No namespace model provided."})
|
||||
}
|
||||
|
||||
// Check if we have an ID other than the one in the struct
|
||||
id := c.Param("id")
|
||||
if id != "" {
|
||||
// Make int
|
||||
namespaceID, err := strconv.ParseInt(id, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
namespace.ID = namespaceID
|
||||
}
|
||||
|
||||
// Check if the namespace exists
|
||||
// ID = 0 means new namespace, no error
|
||||
if namespace.ID != 0 {
|
||||
_, err := models.GetNamespaceByID(namespace.ID)
|
||||
if err != nil {
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The namespace does not exist."})
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the namespace exists."})
|
||||
}
|
||||
}
|
||||
|
||||
// Get the current user for later checks
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
namespace.Owner = user
|
||||
|
||||
// update or create...
|
||||
if namespace.ID == 0 {
|
||||
err = models.CreateOrUpdateNamespace(namespace)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
} else {
|
||||
// Check if the user has admin access to the namespace
|
||||
oldNamespace, err := models.GetNamespaceByID(namespace.ID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
err = user.IsNamespaceAdmin(&oldNamespace)
|
||||
if err != nil {
|
||||
if models.IsErrUserNeedsToBeNamespaceAdmin(err) {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You need to be namespace admin to edit a namespace."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
err = models.CreateOrUpdateNamespace(namespace)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, namespace)
|
||||
}
|
||||
|
@ -44,9 +44,6 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to have write access on that list.")
|
||||
}
|
||||
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The namespace does not exist.")
|
||||
}
|
||||
if models.IsErrNamespaceNameCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The namespace name cannot be empty.")
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package crud
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
@ -29,11 +28,21 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
|
||||
// Do the update
|
||||
err = c.CObject.Update(id, ¤tUser)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if models.IsErrNeedToBeListAdmin(err) {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")
|
||||
}
|
||||
|
||||
|
||||
if models.IsErrNamespaceDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "The namespace does not exist.")
|
||||
}
|
||||
if models.IsErrNamespaceNameCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The namespace name cannot be empty.")
|
||||
}
|
||||
if models.IsErrNamespaceOwnerCannotBeEmpty(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The namespace owner cannot be empty.")
|
||||
}
|
||||
|
||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
|
@ -109,7 +109,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
||||
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
||||
a.GET("/namespaces/:id", apiv1.ShowNamespace)
|
||||
a.POST("/namespaces/:id", apiv1.UpdateNamespace)
|
||||
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
|
||||
a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID)
|
||||
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
|
||||
}
|
||||
|
Reference in New Issue
Block a user