implemented get one namespace via interface mthod

This commit is contained in:
konrad 2018-07-12 11:54:55 +02:00 committed by kolaente
parent 6f5cf55e42
commit 868b93dbbc
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 28 additions and 1 deletions

View File

@ -21,6 +21,11 @@ func (Namespace) TableName() string {
return "namespaces" return "namespaces"
} }
// AfterLoad gets the owner
func (n *Namespace) AfterLoad() {
n.Owner, _, _ = GetUserByID(n.OwnerID)
}
// NamespaceRight defines the rights teams can have for namespaces // NamespaceRight defines the rights teams can have for namespaces
type NamespaceRight int type NamespaceRight int
@ -108,6 +113,20 @@ func GetNamespaceByID(id int64) (namespace Namespace, err error) {
return namespace, err return namespace, err
} }
// ReadOne gets one namespace
func (n *Namespace) ReadOne(id int64) (err error) {
exists, err := x.ID(id).Get(n)
if err != nil {
return
}
if !exists {
return ErrNamespaceDoesNotExist{ID: id}
}
return
}
// ReadAll gets all namespaces a user has access to // ReadAll gets all namespaces a user has access to
func (n *Namespace) ReadAll(doer *User) (interface{}, error) { func (n *Namespace) ReadAll(doer *User) (interface{}, error) {

View File

@ -4,6 +4,7 @@ import (
"git.kolaente.de/konrad/list/models" "git.kolaente.de/konrad/list/models"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"fmt"
) )
// ReadOneWeb is the webhandler to get one object // ReadOneWeb is the webhandler to get one object
@ -16,6 +17,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
} }
// TODO check rights // TODO check rights
//c.CObject.CanRead(doer)
// Get our object // Get our object
err = c.CObject.ReadOne(id) err = c.CObject.ReadOne(id)
@ -24,6 +26,12 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusNotFound) return echo.NewHTTPError(http.StatusNotFound)
} }
if models.IsErrNamespaceDoesNotExist(err) {
return echo.NewHTTPError(http.StatusNotFound)
}
fmt.Println(err)
return echo.NewHTTPError(http.StatusInternalServerError, "An error occured.") return echo.NewHTTPError(http.StatusInternalServerError, "An error occured.")
} }

View File

@ -108,7 +108,7 @@ func RegisterRoutes(e *echo.Echo) {
} }
a.GET("/namespaces", namespaceHandler.ReadAllWeb) a.GET("/namespaces", namespaceHandler.ReadAllWeb)
a.PUT("/namespaces", namespaceHandler.CreateWeb) a.PUT("/namespaces", namespaceHandler.CreateWeb)
a.GET("/namespaces/:id", apiv1.ShowNamespace) a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb) a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID) a.DELETE("/namespaces/:id", apiv1.DeleteNamespaceByID)
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID) a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)