Added functions for adding items to a todolist
This commit is contained in:
50
routes/api/v1/list_item_add_update.go
Normal file
50
routes/api/v1/list_item_add_update.go
Normal file
@ -0,0 +1,50 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func AddOrUpdateListItem(c echo.Context) error {
|
||||
// Get the list item
|
||||
var listItem *models.ListItem
|
||||
|
||||
if err := c.Bind(&listItem); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No list model provided."})
|
||||
}
|
||||
|
||||
// Get the list ID
|
||||
id := c.Param("id")
|
||||
// Make int
|
||||
listID, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
listItem.ListID = listID
|
||||
|
||||
// Set the user
|
||||
user, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
listItem.CreatedBy = user
|
||||
|
||||
err = models.CreateOrUpdateListItem(listItem)
|
||||
if err != nil {
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The list does not exist."})
|
||||
}
|
||||
if models.IsErrListItemCannotBeEmpty(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"You must provide at least a list item text."})
|
||||
}
|
||||
if models.IsErrUserDoesNotExist(err) {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"The user does not exist."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, listItem)
|
||||
}
|
32
routes/api/v1/list_show.go
Normal file
32
routes/api/v1/list_show.go
Normal file
@ -0,0 +1,32 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// AddOrUpdateList Adds or updates a new list
|
||||
func GetListByID(c echo.Context) error {
|
||||
// Check if we have our ID
|
||||
id := c.Param("id")
|
||||
// Make int
|
||||
listID, err := strconv.ParseInt(id, 10, 64)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
|
||||
}
|
||||
|
||||
// Get the list
|
||||
list, err := models.GetListByID(listID)
|
||||
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{"An error occured."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, list)
|
||||
}
|
@ -16,6 +16,11 @@ func GetListsByUser(c echo.Context) error {
|
||||
|
||||
allLists, err := models.GetListsByUser(¤tUser)
|
||||
if err != nil {
|
||||
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user