Added method to show all lists in a namespace

This commit is contained in:
kolaente 2018-07-03 08:26:28 +02:00
parent 06cae09f77
commit e0244e28d7
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 98 additions and 7 deletions

View File

@ -6,29 +6,30 @@ type List struct {
Title string `xorm:"varchar(250)" json:"title"`
Description string `xorm:"varchar(1000)" json:"description"`
OwnerID int64 `xorm:"int(11)" json:"-"`
Owner User `xorm:"-" json:"owner"`
NamespaceID int64 `xorm:"int(11)" json:"-"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
Owner User `xorm:"-" json:"owner"`
Items []*ListItem `xorm:"-" json:"items"`
}
// GetListByID returns a list by its ID
func GetListByID(id int64) (list List, err error) {
func GetListByID(id int64) (list *List, err error) {
list.ID = id
exists, err := x.Get(&list)
if err != nil {
return List{}, err
return &List{}, err
}
if !exists {
return List{}, ErrListDoesNotExist{ID: id}
return &List{}, ErrListDoesNotExist{ID: id}
}
// Get the list owner
user, _, err := GetUserByID(list.OwnerID)
if err != nil {
return List{}, err
return &List{}, err
}
list.Owner = user
@ -61,3 +62,12 @@ func GetListsByUser(user *User) (lists []*List, err error) {
return
}
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
exists, err := x.Where("namespace_id = ?", nID).Get(lists)
if !exists {
return lists, ErrNamespaceDoesNotExist{}
}
return
}

View File

@ -0,0 +1,68 @@
package v1
import (
"github.com/labstack/echo"
"strconv"
"net/http"
"git.kolaente.de/konrad/list/models"
)
func GetListsByNamespaceID(c echo.Context) error {
// swagger:operation GET /namespaces/{namespaceID}/lists namespaces getNamespaces
// ---
// summary: gets all lists belonging to that namespace
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: namespaceID
// in: path
// description: ID of the namespace
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Namespace"
// "400":
// "$ref": "#/responses/Message"
// "500":
// "$ref": "#/responses/Message"
// Check if we have our ID
id := c.Param("id")
// Make int
namespaceID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
}
// Check if the user has acces to that namespace
user, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
has, err := user.HasNamespaceAccess(&models.Namespace{ID:namespaceID})
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
if !has {
return c.JSON(http.StatusForbidden, models.Message{"You don't have access to this namespace."})
}
// Get the lists
lists, err := models.GetListsByNamespaceID(namespaceID)
if err != nil {
if models.IsErrNamespaceDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"Namespace not found."})
}
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
return c.JSON(http.StatusOK, lists)
}

View File

@ -38,6 +38,19 @@ func ShowNamespace(c echo.Context) error {
return c.JSON(http.StatusBadRequest, models.Message{"Invalid ID."})
}
// Check if the user has acces to that namespace
user, err := models.GetCurrentUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
has, err := user.HasNamespaceAccess(&models.Namespace{ID:namespaceID})
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
if !has {
return c.JSON(http.StatusForbidden, models.Message{"You don't have access to this namespace."})
}
// Get the namespace
namespace, err := models.GetNamespaceByID(namespaceID)
if err != nil {

View File

@ -96,8 +96,8 @@ func RegisterRoutes(e *echo.Echo) {
a.GET("/namespaces", apiv1.GetAllNamespacesByCurrentUser)
a.PUT("/namespaces", apiv1.AddNamespace)
a.GET("/namespaces/:id", apiv1.ShowNamespace)
//a.GET("/namespaces/:id/lists") // Gets all lists for that namespace
a.POST("/namespaces/:id", apiv1.UpdateNamespace)
//a.PUT("/namespaces/:id") // Creates a new list in that namespace
// a.DELETE("/namespaces/:id") // Deletes a namespace with all lists
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
//a.PUT("/namespaces/:id/lists") // Creates a new list in that namespace
}