Added functions for adding items to a todolist

This commit is contained in:
konrad 2018-06-10 19:49:40 +02:00 committed by kolaente
parent 7bac9f490e
commit 91f67dc364
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
9 changed files with 190 additions and 2 deletions

View file

@ -29,7 +29,16 @@ Ab v0.3 können wir mit clients anfangen.
#### v0.1 #### v0.1
* [ ] Listen erstellen/bearbeiten/löschen * [ ] Listen erstellen/bearbeiten/löschen
* [x] Ansehen
* [x] Übersicht
* [x] Einzelne liste mit allen todopunkten
* [x] Erstellen
* [x] Bearbeiten
* [ ] Löschen
* [ ] Todopunkte hinzufügen/abhaken/löschen * [ ] Todopunkte hinzufügen/abhaken/löschen
* [x] Erstellen
* [ ] Bearbeiten (abhaken)
* [ ] Löschen
#### v0.2 #### v0.2

View file

@ -142,3 +142,16 @@ func IsErrListDoesNotExist(err error) bool {
func (err ErrListDoesNotExist) Error() string { func (err ErrListDoesNotExist) Error() string {
return fmt.Sprintf("List does not exist [ID: %d]", err.ID) return fmt.Sprintf("List does not exist [ID: %d]", err.ID)
} }
// ErrListItemCannotBeEmpty represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
type ErrListItemCannotBeEmpty struct{}
// IsErrListItemCannotBeEmpty checks if an error is a ErrListDoesNotExist.
func IsErrListItemCannotBeEmpty(err error) bool {
_, ok := err.(ErrListItemCannotBeEmpty)
return ok
}
func (err ErrListItemCannotBeEmpty) Error() string {
return fmt.Sprintf("List item text cannot be empty.")
}

View file

@ -13,7 +13,7 @@ type ListItem struct {
Created int64 `xorm:"created" json:"created"` Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
CreatedBy User `xorm:"-"` CreatedBy User `xorm:"-" json:"createdBy"`
} }
// TableName returns the table name for listitems // TableName returns the table name for listitems
@ -21,7 +21,46 @@ func (ListItem) TableName() string {
return "items" return "items"
} }
// GetItemsByListID gets all todoitems for a list
func GetItemsByListID(listID int64) (items []*ListItem, err error) { func GetItemsByListID(listID int64) (items []*ListItem, err error) {
err = x.Where("list_id = ?", listID).Find(&items) err = x.Where("list_id = ?", listID).Find(&items)
if err != nil {
return
}
// Get all users and put them into the array
var userIDs []int64
for _, i := range items {
found := false
for _, u := range userIDs {
if i.CreatedByID == u {
found = true
break
}
}
if !found {
userIDs = append(userIDs, i.CreatedByID)
}
}
var users []User
err = x.In("id", userIDs).Find(&users)
if err != nil {
return
}
for in, item := range items {
for _, user := range users {
if item.CreatedByID == user.ID {
items[in].CreatedBy = user
break
}
}
// obsfucate the user password
items[in].CreatedBy.Password = ""
}
return return
} }

View file

@ -0,0 +1,37 @@
package models
// CreateOrUpdateListItem adds or updates a todo item to a list
func CreateOrUpdateListItem(item *ListItem) (err error) {
// Check if we have at least a text
if item.Text == "" {
return ErrListItemCannotBeEmpty{}
}
// Check if the list exists
_, err = GetListByID(item.ListID)
if err != nil {
return
}
// Check if the user exists
_, _, err = GetUserByID(item.CreatedBy.ID)
if err != nil {
return
}
item.CreatedByID = item.CreatedBy.ID
if item.ID != 0 {
_, err = x.ID(item.ID).Update(item)
if err != nil {
return
}
} else {
_, err = x.Insert(item)
if err != nil {
return
}
}
return
}

View file

@ -10,7 +10,7 @@ type List struct {
Created int64 `xorm:"created" json:"created"` Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
Items []*ListItem `xorm:"-"` Items []*ListItem `xorm:"-" json:"items"`
} }
// GetListByID returns a list by its ID // GetListByID returns a list by its ID
@ -32,6 +32,7 @@ func GetListByID(id int64) (list List, err error) {
} }
list.Owner = user list.Owner = user
list.Owner.Password = ""
items, err := GetItemsByListID(list.ID) items, err := GetItemsByListID(list.ID)
if err != nil { if err != nil {

View 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)
}

View 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)
}

View file

@ -16,6 +16,11 @@ func GetListsByUser(c echo.Context) error {
allLists, err := models.GetListsByUser(&currentUser) allLists, err := models.GetListsByUser(&currentUser)
if err != nil { if err != nil {
if models.IsErrListDoesNotExist(err) {
}
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."}) return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."})
} }

View file

@ -55,5 +55,7 @@ func RegisterRoutes(e *echo.Echo) {
a.PUT("/lists", apiv1.AddOrUpdateList) a.PUT("/lists", apiv1.AddOrUpdateList)
a.GET("/lists", apiv1.GetListsByUser) a.GET("/lists", apiv1.GetListsByUser)
a.GET("/lists/:id", apiv1.GetListByID)
a.POST("/lists/:id", apiv1.AddOrUpdateList) a.POST("/lists/:id", apiv1.AddOrUpdateList)
a.PUT("/lists/:id", apiv1.AddOrUpdateListItem)
} }