Added lists view
This commit is contained in:
parent
b841ec121b
commit
125f0ba230
3 changed files with 44 additions and 0 deletions
|
@ -34,6 +34,25 @@ func GetListByID(id int64) (list List, err error) {
|
|||
return list, nil
|
||||
}
|
||||
|
||||
//
|
||||
func GetListsByUser(user *User) (lists []*List, err error) {
|
||||
fullUser, _, err := GetUserByID(user.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = x.Where("owner_id = ?", user.ID).Find(&lists)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for in := range lists {
|
||||
lists[in].Owner = fullUser
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CreateOrUpdateList updates a list or creates it if it doesn't exist
|
||||
func CreateOrUpdateList(list *List) (err error) {
|
||||
// Check if it exists
|
||||
|
|
22
routes/api/v1/lists_list.go
Normal file
22
routes/api/v1/lists_list.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo"
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetListsByUser(c echo.Context) error {
|
||||
|
||||
currentUser, err := models.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
|
||||
}
|
||||
|
||||
allLists, err := models.GetListsByUser(¤tUser)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, allLists)
|
||||
}
|
|
@ -42,6 +42,8 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.OPTIONS("/register", SetCORSHeader)
|
||||
a.OPTIONS("/users", SetCORSHeader)
|
||||
a.OPTIONS("/users/:id", SetCORSHeader)
|
||||
a.OPTIONS("/lists", SetCORSHeader)
|
||||
a.OPTIONS("/lists/:id", SetCORSHeader)
|
||||
|
||||
a.POST("/login", apiv1.Login)
|
||||
a.POST("/register", apiv1.UserAddOrUpdate)
|
||||
|
@ -52,5 +54,6 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.POST("/tokenTest", apiv1.CheckToken)
|
||||
|
||||
a.PUT("/lists", apiv1.AddOrUpdateList)
|
||||
a.GET("/lists", apiv1.GetListsByUser)
|
||||
a.POST("/lists/:id", apiv1.AddOrUpdateList)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue