Implemented method to add a list to a namespace
This commit is contained in:
@ -4,20 +4,25 @@ import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func AddList(c echo.Context) error {
|
||||
// swagger:operation PUT /lists lists addList
|
||||
// swagger:operation PUT /namespaces/{namespaceID}/lists lists addList
|
||||
// ---
|
||||
// summary: Creates a new list owned by the currently logged in user
|
||||
// summary: Creates a new list owned by the currently logged in user in that namespace
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: namespaceID
|
||||
// in: path
|
||||
// description: ID of the namespace that list should belong to
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: body
|
||||
// in: body
|
||||
// required: true
|
||||
// schema:
|
||||
// "$ref": "#/definitions/List"
|
||||
// responses:
|
||||
@ -30,7 +35,55 @@ func AddList(c echo.Context) error {
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
return addOrUpdateList(c)
|
||||
// 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)
|
||||
}
|
||||
|
||||
func UpdateList(c echo.Context) error {
|
||||
@ -61,12 +114,6 @@ func UpdateList(c echo.Context) error {
|
||||
// "500":
|
||||
// "$ref": "#/responses/Message"
|
||||
|
||||
return addOrUpdateList(c)
|
||||
}
|
||||
|
||||
// AddOrUpdateList Adds or updates a new list
|
||||
func addOrUpdateList(c echo.Context) error {
|
||||
|
||||
// Get the list
|
||||
var list *models.List
|
||||
|
||||
@ -74,22 +121,16 @@ func addOrUpdateList(c echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No list model provided."})
|
||||
}
|
||||
|
||||
// Check if we have an ID other than the one in the struct
|
||||
id := c.Param("id")
|
||||
if id != "" {
|
||||
// Make int
|
||||
listID, err := strconv.ParseInt(id, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
list.ID = listID
|
||||
// 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
|
||||
var err error
|
||||
if list.ID != 0 {
|
||||
oldList, err = models.GetListByID(list.ID)
|
||||
if err != nil {
|
||||
@ -107,22 +148,16 @@ func addOrUpdateList(c echo.Context) error {
|
||||
}
|
||||
list.Owner = user
|
||||
|
||||
// update or create...
|
||||
if list.ID == 0 {
|
||||
err = models.CreateOrUpdateList(list)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
} else {
|
||||
// Check if the user owns the list
|
||||
if user.ID != oldList.Owner.ID {
|
||||
return c.JSON(http.StatusForbidden, models.Message{"You cannot edit a list you don't own."})
|
||||
}
|
||||
// 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."})
|
||||
}
|
||||
|
||||
err = models.CreateOrUpdateList(list)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
// 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)
|
||||
|
Reference in New Issue
Block a user