Getting all lists or one works now
This commit is contained in:
parent
d5eb2f08e3
commit
9e8f13edf6
6 changed files with 25 additions and 40 deletions
|
@ -150,6 +150,8 @@ Teams sind global, d.h. Ein Team kann mehrere Namespaces verwalten.
|
||||||
* [ ] Namen finden
|
* [ ] Namen finden
|
||||||
* [ ] Alle Packages umziehen
|
* [ ] Alle Packages umziehen
|
||||||
* [x] Swagger UI aufsetzen
|
* [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
|
* [ ] 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
|
* [ ] Endpoints neu organisieren? Also zb `namespaces/:nID/lists/:lID/items/:iID` statt einzelnen Endpoints für alles
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,11 @@ func GetItemsByListID(listID int64) (items []*ListItem, err error) {
|
||||||
return
|
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
|
// Get all users and put them into the array
|
||||||
var userIDs []int64
|
var userIDs []int64
|
||||||
for _, i := range items {
|
for _, i := range items {
|
||||||
|
|
|
@ -17,6 +17,10 @@ type List struct {
|
||||||
CRUDable `xorm:"-" json:"-"`
|
CRUDable `xorm:"-" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lists is a multiple of list
|
||||||
|
type Lists []List
|
||||||
|
|
||||||
|
// AfterLoad loads the owner and list items
|
||||||
func (l *List) AfterLoad() {
|
func (l *List) AfterLoad() {
|
||||||
|
|
||||||
// Get the owner
|
// Get the owner
|
||||||
|
@ -28,7 +32,7 @@ func (l *List) AfterLoad() {
|
||||||
|
|
||||||
// GetListByID returns a list by its ID
|
// GetListByID returns a list by its ID
|
||||||
func GetListByID(id int64) (list List, err error) {
|
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 {
|
if err != nil {
|
||||||
return list, err
|
return list, err
|
||||||
}
|
}
|
||||||
|
@ -40,57 +44,31 @@ func GetListByID(id int64) (list List, err error) {
|
||||||
return list, nil
|
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) {
|
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
|
||||||
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
||||||
return lists, err
|
return lists, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll gets all List a user has access to
|
||||||
func (list *List) ReadAll(user *User) (interface{}, error) {
|
func (list *List) ReadAll(user *User) (interface{}, error) {
|
||||||
lists := Lists{}
|
lists := Lists{}
|
||||||
err := lists.ReadAll(user)
|
fullUser, _, err := GetUserByID(user.ID)
|
||||||
|
if err != nil {
|
||||||
return lists, err
|
return lists, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Lists []List
|
// TODO: namespaces...
|
||||||
|
|
||||||
func (lists *Lists) ReadAll(user *User) (err error) {
|
|
||||||
fullUser, _, err := GetUserByID(user.ID)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = x.Select("list.*").
|
err = x.Select("list.*").
|
||||||
Join("LEFT", "team_list", "list.id = team_list.list_id").
|
Join("LEFT", "team_list", "list.id = team_list.list_id").
|
||||||
Join("LEFT", "team_members", "team_members.team_id = team_list.team_id").
|
Join("LEFT", "team_members", "team_members.team_id = team_list.team_id").
|
||||||
Where("team_members.user_id = ?", fullUser.ID).
|
Where("team_members.user_id = ?", fullUser.ID).
|
||||||
Or("list.owner_id = ?", fullUser.ID).
|
Or("list.owner_id = ?", fullUser.ID).
|
||||||
Find(lists)
|
Find(&lists)
|
||||||
if err != nil {
|
|
||||||
return
|
return lists, err
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadOne gets one list by its ID
|
||||||
func (l *List) ReadOne(id int64) (err error) {
|
func (l *List) ReadOne(id int64) (err error) {
|
||||||
*l, err = GetListByID(id)
|
*l, err = GetListByID(id)
|
||||||
return
|
return
|
||||||
|
|
|
@ -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."})
|
return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
|
||||||
}
|
}
|
||||||
|
|
||||||
//c.CObject.IsAdmin()
|
|
||||||
|
|
||||||
lists, err := c.CObject.ReadAll(¤tUser)
|
lists, err := c.CObject.ReadAll(¤tUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kolaente.de/konrad/list/models"
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -21,7 +20,7 @@ func GetListsByUser(c echo.Context) error {
|
||||||
// "500":
|
// "500":
|
||||||
// "$ref": "#/responses/Message"
|
// "$ref": "#/responses/Message"
|
||||||
|
|
||||||
currentUser, err := models.GetCurrentUser(c)
|
/*currentUser, err := models.GetCurrentUser(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."})
|
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)
|
allLists, err := models.GetListsByUser(¤tUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."})
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get lists."})
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, allLists)
|
return c.JSON(http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func NewEcho() *echo.Echo {
|
||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
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
|
return e
|
||||||
|
@ -50,6 +50,9 @@ func NewEcho() *echo.Echo {
|
||||||
// RegisterRoutes registers all routes for the application
|
// RegisterRoutes registers all routes for the application
|
||||||
func RegisterRoutes(e *echo.Echo) {
|
func RegisterRoutes(e *echo.Echo) {
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: Use proper cors middleware by echo
|
||||||
|
|
||||||
// Middleware for cors
|
// Middleware for cors
|
||||||
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
|
Loading…
Reference in a new issue