diff --git a/Featurecreep.md b/Featurecreep.md index 37be72acd..6ffbdc001 100644 --- a/Featurecreep.md +++ b/Featurecreep.md @@ -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 diff --git a/models/list_items.go b/models/list_items.go index d1d4b6c11..86b7b8f0c 100644 --- a/models/list_items.go +++ b/models/list_items.go @@ -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 { diff --git a/models/lists.go b/models/lists.go index e803103ee..3145c4b1a 100644 --- a/models/lists.go +++ b/models/lists.go @@ -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 diff --git a/routes/CRUD/CRUD_helper.go b/routes/CRUD/CRUD_helper.go index 10c3990cc..340ae7303 100644 --- a/routes/CRUD/CRUD_helper.go +++ b/routes/CRUD/CRUD_helper.go @@ -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(¤tUser) if err != nil { fmt.Println(err) diff --git a/routes/api/v1/lists_list.go b/routes/api/v1/lists_list.go index 95b117985..dfdb139c9 100644 --- a/routes/api/v1/lists_list.go +++ b/routes/api/v1/lists_list.go @@ -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(¤tUser) 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) } diff --git a/routes/routes.go b/routes/routes.go index befcaa876..118b82d20 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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 {