implemented readone with parambinder
This commit is contained in:
parent
d06ed68125
commit
9e75e9b73b
9 changed files with 44 additions and 42 deletions
|
@ -3,7 +3,7 @@ package models
|
||||||
// CRUDable defines the crud methods
|
// CRUDable defines the crud methods
|
||||||
type CRUDable interface {
|
type CRUDable interface {
|
||||||
Create(*User) error
|
Create(*User) error
|
||||||
ReadOne(int64) error
|
ReadOne() error
|
||||||
ReadAll(*User) (interface{}, error)
|
ReadAll(*User) (interface{}, error)
|
||||||
Update(int64) error
|
Update(int64) error
|
||||||
Delete() error
|
Delete() error
|
||||||
|
|
|
@ -2,11 +2,11 @@ package models
|
||||||
|
|
||||||
// List represents a list of items
|
// List represents a list of items
|
||||||
type List struct {
|
type List struct {
|
||||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listid"`
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"`
|
||||||
Title string `xorm:"varchar(250)" json:"title"`
|
Title string `xorm:"varchar(250)" json:"title"`
|
||||||
Description string `xorm:"varchar(1000)" json:"description"`
|
Description string `xorm:"varchar(1000)" json:"description"`
|
||||||
OwnerID int64 `xorm:"int(11)" json:"-"`
|
OwnerID int64 `xorm:"int(11)" json:"-"`
|
||||||
NamespaceID int64 `xorm:"int(11)" json:"-" param:"nid"`
|
NamespaceID int64 `xorm:"int(11)" json:"-" param:"namespace"`
|
||||||
|
|
||||||
Owner User `xorm:"-" json:"owner"`
|
Owner User `xorm:"-" json:"owner"`
|
||||||
Items []*ListItem `xorm:"-" json:"items"`
|
Items []*ListItem `xorm:"-" json:"items"`
|
||||||
|
@ -75,7 +75,7 @@ func (l *List) ReadAll(user *User) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOne gets one list by its ID
|
// ReadOne gets one list by its ID
|
||||||
func (l *List) ReadOne(id int64) (err error) {
|
func (l *List) ReadOne() (err error) {
|
||||||
*l, err = GetListByID(id)
|
*l, err = GetListByID(l.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ type ListItem struct {
|
||||||
DueDateUnix int64 `xorm:"int(11)" json:"dueDate"`
|
DueDateUnix int64 `xorm:"int(11)" json:"dueDate"`
|
||||||
ReminderUnix int64 `xorm:"int(11)" json:"reminderDate"`
|
ReminderUnix int64 `xorm:"int(11)" json:"reminderDate"`
|
||||||
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that item on the list
|
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that item on the list
|
||||||
ListID int64 `xorm:"int(11)" json:"listID" param:"listid"`
|
ListID int64 `xorm:"int(11)" json:"listID" param:"list"`
|
||||||
Created int64 `xorm:"created" json:"created"`
|
Created int64 `xorm:"created" json:"created"`
|
||||||
Updated int64 `xorm:"updated" json:"updated"`
|
Updated int64 `xorm:"updated" json:"updated"`
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
// Namespace holds informations about a namespace
|
// Namespace holds informations about a namespace
|
||||||
type Namespace struct {
|
type Namespace struct {
|
||||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"nid"`
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"namespace"`
|
||||||
Name string `xorm:"varchar(250)" json:"name"`
|
Name string `xorm:"varchar(250)" json:"name"`
|
||||||
Description string `xorm:"varchar(1000)" json:"description"`
|
Description string `xorm:"varchar(1000)" json:"description"`
|
||||||
OwnerID int64 `xorm:"int(11) not null" json:"-"`
|
OwnerID int64 `xorm:"int(11) not null" json:"-"`
|
||||||
|
@ -48,15 +48,15 @@ func GetNamespaceByID(id int64) (namespace Namespace, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOne gets one namespace
|
// ReadOne gets one namespace
|
||||||
func (n *Namespace) ReadOne(id int64) (err error) {
|
func (n *Namespace) ReadOne() (err error) {
|
||||||
getN := Namespace{}
|
getN := Namespace{}
|
||||||
exists, err := x.ID(id).Get(&getN)
|
exists, err := x.ID(n.ID).Get(&getN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return ErrNamespaceDoesNotExist{ID: id}
|
return ErrNamespaceDoesNotExist{ID: n.ID}
|
||||||
}
|
}
|
||||||
|
|
||||||
*n = getN
|
*n = getN
|
||||||
|
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
// Team holds a team object
|
// Team holds a team object
|
||||||
type Team struct {
|
type Team struct {
|
||||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"teamid"`
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"team"`
|
||||||
Name string `xorm:"varchar(250) not null" json:"name"`
|
Name string `xorm:"varchar(250) not null" json:"name"`
|
||||||
Description string `xorm:"varchar(250)" json:"description"`
|
Description string `xorm:"varchar(250)" json:"description"`
|
||||||
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
CreatedByID int64 `xorm:"int(11) not null" json:"-"`
|
||||||
|
@ -100,8 +100,8 @@ func GetTeamByID(id int64) (team Team, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOne implements the CRUD method to get one team
|
// ReadOne implements the CRUD method to get one team
|
||||||
func (t *Team) ReadOne(id int64) (err error) {
|
func (t *Team) ReadOne() (err error) {
|
||||||
*t, err = GetTeamByID(id)
|
*t, err = GetTeamByID(t.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,15 +24,6 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get an ID if we have one
|
|
||||||
/*var id int64
|
|
||||||
if ctx.Param("id") != "" {
|
|
||||||
id, err = models.GetIntURLParam("id", ctx)
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Bad id.")
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Check rights
|
// Check rights
|
||||||
if !c.CObject.CanCreate(¤tUser) {
|
if !c.CObject.CanCreate(¤tUser) {
|
||||||
return echo.NewHTTPError(http.StatusForbidden)
|
return echo.NewHTTPError(http.StatusForbidden)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const paramTagName = "param"
|
const paramTagName = "param"
|
||||||
|
@ -21,7 +22,11 @@ func ParamBinder(i interface{}, c echo.Context) (err error) {
|
||||||
paramValues := c.ParamValues()
|
paramValues := c.ParamValues()
|
||||||
paramVars := make(map[string][]string)
|
paramVars := make(map[string][]string)
|
||||||
for in, name := range paramNames {
|
for in, name := range paramNames {
|
||||||
paramVars[name] = append(paramVars[name], paramValues[in])
|
// Hotfix for an echo bug where a param name would show up which dont exist
|
||||||
|
names := strings.Split(name, ",")
|
||||||
|
for _, n := range names {
|
||||||
|
paramVars[n] = append(paramVars[name], paramValues[in])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b := Binder{}
|
b := Binder{}
|
||||||
|
|
|
@ -4,19 +4,19 @@ 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
|
||||||
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||||
|
|
||||||
// Get the ID
|
// Get the object & bind params to struct
|
||||||
id, err := models.GetIntURLParam("id", ctx)
|
if err := ParamBinder(c.CObject, ctx); err != nil {
|
||||||
if err != nil {
|
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get our object
|
// Get our object
|
||||||
err = c.CObject.ReadOne(id)
|
err := c.CObject.ReadOne()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrListDoesNotExist(err) {
|
if models.IsErrListDoesNotExist(err) {
|
||||||
return echo.NewHTTPError(http.StatusNotFound)
|
return echo.NewHTTPError(http.StatusNotFound)
|
||||||
|
@ -26,6 +26,12 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||||
return echo.NewHTTPError(http.StatusNotFound)
|
return echo.NewHTTPError(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if models.IsErrTeamDoesNotExist(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.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,27 +113,27 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
CObject: &models.List{},
|
CObject: &models.List{},
|
||||||
}
|
}
|
||||||
a.GET("/lists", listHandler.ReadAllWeb)
|
a.GET("/lists", listHandler.ReadAllWeb)
|
||||||
a.GET("/lists/:id", listHandler.ReadOneWeb)
|
a.GET("/lists/:list", listHandler.ReadOneWeb)
|
||||||
a.POST("/lists/:id", listHandler.UpdateWeb)
|
a.POST("/lists/:list", listHandler.UpdateWeb)
|
||||||
a.DELETE("/lists/:listid", listHandler.DeleteWeb)
|
a.DELETE("/lists/:list", listHandler.DeleteWeb)
|
||||||
a.PUT("/namespaces/:nid/lists", listHandler.CreateWeb)
|
a.PUT("/namespaces/:namespace/lists", listHandler.CreateWeb)
|
||||||
|
|
||||||
itemHandler := &crud.WebHandler{
|
itemHandler := &crud.WebHandler{
|
||||||
CObject: &models.ListItem{},
|
CObject: &models.ListItem{},
|
||||||
}
|
}
|
||||||
a.PUT("/lists/:listid", itemHandler.CreateWeb)
|
a.PUT("/lists/:list", itemHandler.CreateWeb)
|
||||||
a.DELETE("/items/:listitemid", itemHandler.DeleteWeb)
|
a.DELETE("/items/:listitem", itemHandler.DeleteWeb)
|
||||||
a.POST("/items/:id", itemHandler.UpdateWeb)
|
a.POST("/items/:listitem", itemHandler.UpdateWeb)
|
||||||
|
|
||||||
namespaceHandler := &crud.WebHandler{
|
namespaceHandler := &crud.WebHandler{
|
||||||
CObject: &models.Namespace{},
|
CObject: &models.Namespace{},
|
||||||
}
|
}
|
||||||
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
||||||
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
||||||
a.GET("/namespaces/:id", namespaceHandler.ReadOneWeb)
|
a.GET("/namespaces/:namespace", namespaceHandler.ReadOneWeb)
|
||||||
a.POST("/namespaces/:id", namespaceHandler.UpdateWeb)
|
a.POST("/namespaces/:namespace", namespaceHandler.UpdateWeb)
|
||||||
a.DELETE("/namespaces/:nid", namespaceHandler.DeleteWeb)
|
a.DELETE("/namespaces/:namespace", namespaceHandler.DeleteWeb)
|
||||||
a.GET("/namespaces/:id/lists", apiv1.GetListsByNamespaceID)
|
a.GET("/namespaces/:namespace/lists", apiv1.GetListsByNamespaceID)
|
||||||
|
|
||||||
namespaceTeamHandler := &crud.WebHandler{
|
namespaceTeamHandler := &crud.WebHandler{
|
||||||
CObject: &models.TeamNamespace{},
|
CObject: &models.TeamNamespace{},
|
||||||
|
@ -146,8 +146,8 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
CObject: &models.Team{},
|
CObject: &models.Team{},
|
||||||
}
|
}
|
||||||
a.GET("/teams", teamHandler.ReadAllWeb)
|
a.GET("/teams", teamHandler.ReadAllWeb)
|
||||||
a.GET("/teams/:id", teamHandler.ReadOneWeb)
|
a.GET("/teams/:team", teamHandler.ReadOneWeb)
|
||||||
a.PUT("/teams", teamHandler.CreateWeb)
|
a.PUT("/teams", teamHandler.CreateWeb)
|
||||||
a.POST("/teams/:id", teamHandler.UpdateWeb)
|
a.POST("/teams/:team", teamHandler.UpdateWeb)
|
||||||
a.DELETE("/teams/:teamid", teamHandler.DeleteWeb)
|
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue