Getting all lists or one works now

This commit is contained in:
kolaente 2018-07-09 19:49:27 +02:00
parent d5eb2f08e3
commit 9e8f13edf6
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 25 additions and 40 deletions

View file

@ -150,6 +150,8 @@ Teams sind global, d.h. Ein Team kann mehrere Namespaces verwalten.
* [ ] Namen finden
* [ ] Alle Packages umziehen
* [x] Swagger UI aufsetzen
* [ ] Cacher konfigurierbar
* [ ] Überall echo.NewHTTPError statt c.JSON(Message{}) benutzen
* [ ] Bessere Fehlermeldungen wenn das Model was ankommt falsch ist und nicht geparst werden kann
* [ ] Endpoints neu organisieren? Also zb `namespaces/:nID/lists/:lID/items/:iID` statt einzelnen Endpoints für alles

View file

@ -28,6 +28,11 @@ func GetItemsByListID(listID int64) (items []*ListItem, err error) {
return
}
// No need to iterate over users if the list doesn't has items
if len(items) == 0 {
return
}
// Get all users and put them into the array
var userIDs []int64
for _, i := range items {

View file

@ -17,6 +17,10 @@ type List struct {
CRUDable `xorm:"-" json:"-"`
}
// Lists is a multiple of list
type Lists []List
// AfterLoad loads the owner and list items
func (l *List) AfterLoad() {
// Get the owner
@ -28,7 +32,7 @@ func (l *List) AfterLoad() {
// GetListByID returns a list by its ID
func GetListByID(id int64) (list List, err error) {
exists, err := x.ID(id).Get(&list) // tName ist hässlich, geht das nicht auch anders?
exists, err := x.ID(id).Get(&list)
if err != nil {
return list, err
}
@ -40,57 +44,31 @@ func GetListByID(id int64) (list List, err error) {
return list, nil
}
// GetListsByUser gets all lists a user owns
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
}
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
err = x.Where("namespace_id = ?", nID).Find(&lists)
return lists, err
}
// ReadAll gets all List a user has access to
func (list *List) ReadAll(user *User) (interface{}, error) {
lists := Lists{}
err := lists.ReadAll(user)
return lists, err
}
type Lists []List
func (lists *Lists) ReadAll(user *User) (err error) {
fullUser, _, err := GetUserByID(user.ID)
if err != nil {
return
return lists, err
}
// TODO: namespaces...
err = x.Select("list.*").
Join("LEFT", "team_list", "list.id = team_list.list_id").
Join("LEFT", "team_members", "team_members.team_id = team_list.team_id").
Where("team_members.user_id = ?", fullUser.ID).
Or("list.owner_id = ?", fullUser.ID).
Find(lists)
if err != nil {
return
}
Find(&lists)
return
return lists, err
}
// ReadOne gets one list by its ID
func (l *List) ReadOne(id int64) (err error) {
*l, err = GetListByID(id)
return

View file

@ -44,8 +44,6 @@ func (c *CRUDWebHandler) ReadAllWeb(ctx echo.Context) error {
return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
}
//c.CObject.IsAdmin()
lists, err := c.CObject.ReadAll(&currentUser)
if err != nil {
fmt.Println(err)

View file

@ -1,7 +1,6 @@
package v1
import (
"git.kolaente.de/konrad/list/models"
"github.com/labstack/echo"
"net/http"
)
@ -21,7 +20,7 @@ func GetListsByUser(c echo.Context) error {
// "500":
// "$ref": "#/responses/Message"
currentUser, err := models.GetCurrentUser(c)
/*currentUser, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
}
@ -29,7 +28,7 @@ func GetListsByUser(c echo.Context) error {
allLists, err := models.GetListsByUser(&currentUser)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."})
}
}*/
return c.JSON(http.StatusOK, allLists)
return c.JSON(http.StatusOK, nil)
}

View file

@ -41,7 +41,7 @@ func NewEcho() *echo.Echo {
// Logger
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "${time_rfc3339}: ${remote_ip} ${method} ${status} ${uri} ${latency_human} - ${user_agent}\n",
Format: "${time_rfc3339_nano}: ${remote_ip} ${method} ${status} ${uri} ${latency_human} - ${user_agent}\n",
}))
return e
@ -50,6 +50,9 @@ func NewEcho() *echo.Echo {
// RegisterRoutes registers all routes for the application
func RegisterRoutes(e *echo.Echo) {
// TODO: Use proper cors middleware by echo
// Middleware for cors
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {