implemented creation of thing via parambinder
This commit is contained in:
parent
ebb5b332b6
commit
de95ff40bf
21 changed files with 39 additions and 40 deletions
|
@ -2,7 +2,7 @@ package models
|
|||
|
||||
// CRUDable defines the crud methods
|
||||
type CRUDable interface {
|
||||
Create(*User, int64) error
|
||||
Create(*User) error
|
||||
ReadOne(int64) error
|
||||
ReadAll(*User) (interface{}, error)
|
||||
Update(int64) error
|
||||
|
|
|
@ -6,7 +6,7 @@ type List struct {
|
|||
Title string `xorm:"varchar(250)" json:"title"`
|
||||
Description string `xorm:"varchar(1000)" json:"description"`
|
||||
OwnerID int64 `xorm:"int(11)" json:"-"`
|
||||
NamespaceID int64 `xorm:"int(11)" json:"-"`
|
||||
NamespaceID int64 `xorm:"int(11)" json:"-" param:"nid"`
|
||||
|
||||
Owner User `xorm:"-" json:"owner"`
|
||||
Items []*ListItem `xorm:"-" json:"items"`
|
||||
|
|
|
@ -33,7 +33,7 @@ func (l *List) Update(id int64) (err error) {
|
|||
}
|
||||
|
||||
// Create implements the create method of CRUDable
|
||||
func (l *List) Create(doer *User, id int64) (err error) {
|
||||
func (l *List) Create(doer *User) (err error) {
|
||||
// Check rights
|
||||
user, _, err := GetUserByID(doer.ID)
|
||||
if err != nil {
|
||||
|
|
|
@ -9,7 +9,7 @@ type ListItem struct {
|
|||
DueDateUnix int64 `xorm:"int(11)" json:"dueDate"`
|
||||
ReminderUnix int64 `xorm:"int(11)" json:"reminderDate"`
|
||||
CreatedByID int64 `xorm:"int(11)" json:"-"` // ID of the user who put that item on the list
|
||||
ListID int64 `xorm:"int(11)" json:"listID"`
|
||||
ListID int64 `xorm:"int(11)" json:"listID" param:"listid"`
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package models
|
||||
|
||||
// Create is the implementation to create a list item
|
||||
func (i *ListItem) Create(doer *User, lID int64) (err error) {
|
||||
i.ListID = lID
|
||||
func (i *ListItem) Create(doer *User) (err error) {
|
||||
//i.ListID = lID
|
||||
i.ID = 0
|
||||
|
||||
return createOrUpdateListItem(i, doer)
|
||||
|
|
|
@ -21,8 +21,8 @@ func (i *ListItem) CanUpdate(doer *User, id int64) bool {
|
|||
}
|
||||
|
||||
// CanCreate determines if a user has the right to create a list item
|
||||
func (i *ListItem) CanCreate(doer *User, lID int64) bool {
|
||||
func (i *ListItem) CanCreate(doer *User) bool {
|
||||
// A user can create an item if he has write acces to its list
|
||||
list, _ := GetListByID(lID)
|
||||
list, _ := GetListByID(i.ListID)
|
||||
return list.CanWrite(doer)
|
||||
}
|
||||
|
|
|
@ -93,8 +93,8 @@ func (l *List) CanUpdate(doer *User, id int64) bool {
|
|||
}
|
||||
|
||||
// CanCreate checks if the user can update a list
|
||||
func (l *List) CanCreate(doer *User, nID int64) bool {
|
||||
func (l *List) CanCreate(doer *User) bool {
|
||||
// A user can create a list if he has write access to the namespace
|
||||
n, _ := GetNamespaceByID(nID)
|
||||
n, _ := GetNamespaceByID(l.NamespaceID)
|
||||
return n.CanWrite(doer)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package models
|
||||
|
||||
// Create implements the creation method via the interface
|
||||
func (n *Namespace) Create(doer *User, _ int64) (err error) {
|
||||
func (n *Namespace) Create(doer *User) (err error) {
|
||||
// Check if we have at least a name
|
||||
if n.Name == "" {
|
||||
return ErrNamespaceNameCannotBeEmpty{NamespaceID: 0, UserID: doer.ID}
|
||||
|
|
|
@ -83,7 +83,7 @@ func (n *Namespace) CanDelete(user *User) bool {
|
|||
}
|
||||
|
||||
// CanCreate checks if the user can create a new namespace
|
||||
func (n *Namespace) CanCreate(user *User, id int64) bool {
|
||||
func (n *Namespace) CanCreate(user *User) bool {
|
||||
// This is currently a dummy function, later on we could imagine global limits etc.
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -7,5 +7,5 @@ type Rights interface {
|
|||
CanRead(*User) bool
|
||||
CanDelete(*User) bool
|
||||
CanUpdate(*User, int64) bool
|
||||
CanCreate(*User, int64) bool
|
||||
CanCreate(*User) bool
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package models
|
||||
|
||||
// Create implements the create method to assign a user to a team
|
||||
func (tm *TeamMember) Create(doer *User, id int64) (err error) {
|
||||
tm.TeamID = id
|
||||
func (tm *TeamMember) Create(doer *User) (err error) {
|
||||
//tm.TeamID = id
|
||||
_, err = x.Insert(tm)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ package models
|
|||
type TeamNamespace struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
TeamID int64 `xorm:"int(11) not null" json:"team_id" param:"teamid"`
|
||||
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"nid"`
|
||||
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id" param:"namespaceid"`
|
||||
Right NamespaceRight `xorm:"int(11)" json:"right"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
package models
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Create creates a new team <-> namespace relation
|
||||
func (tn *TeamNamespace) Create(doer *User, nID int64) (err error) {
|
||||
func (tn *TeamNamespace) Create(doer *User) (err error) {
|
||||
|
||||
// Check if the rights are valid
|
||||
if tn.Right != NamespaceRightAdmin && tn.Right != NamespaceRightRead && tn.Right != NamespaceRightWrite {
|
||||
return ErrInvalidTeamRight{tn.Right}
|
||||
}
|
||||
|
||||
fmt.Println(tn.NamespaceID)
|
||||
|
||||
// Check if the team exists
|
||||
_, err = GetTeamByID(tn.TeamID)
|
||||
if err != nil {
|
||||
|
@ -15,11 +19,10 @@ func (tn *TeamNamespace) Create(doer *User, nID int64) (err error) {
|
|||
}
|
||||
|
||||
// Check if the namespace exists
|
||||
_, err = GetNamespaceByID(nID)
|
||||
_, err = GetNamespaceByID(tn.NamespaceID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tn.NamespaceID = nID
|
||||
|
||||
// Insert the new team
|
||||
_, err = x.Insert(tn)
|
||||
|
|
|
@ -18,7 +18,7 @@ func (tn *TeamNamespace) Delete() (err error) {
|
|||
// Delete the relation
|
||||
_, err = x.Where("team_id = ?", tn.TeamID).
|
||||
And("namespace_id = ?", tn.NamespaceID).
|
||||
Delete(tn)
|
||||
Delete(TeamNamespace{})
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package models
|
||||
|
||||
// CanCreate checks if one can create a new team <-> namespace relation
|
||||
func (tn *TeamNamespace) CanCreate(user *User, _ int64) bool {
|
||||
func (tn *TeamNamespace) CanCreate(user *User) bool {
|
||||
n, _ := GetNamespaceByID(tn.NamespaceID)
|
||||
return n.IsAdmin(user)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package models
|
||||
|
||||
// Create is the handler to create a team
|
||||
func (t *Team) Create(doer *User, _ int64) (err error) {
|
||||
func (t *Team) Create(doer *User) (err error) {
|
||||
// Check if we have a name
|
||||
if t.Name == "" {
|
||||
return ErrTeamNameCannotBeEmpty{}
|
||||
|
@ -17,6 +17,6 @@ func (t *Team) Create(doer *User, _ int64) (err error) {
|
|||
|
||||
// Insert the current user as member and admin
|
||||
tm := TeamMember{TeamID: t.ID, UserID: doer.ID, IsAdmin: true}
|
||||
err = tm.Create(doer, t.ID)
|
||||
err = tm.Create(doer)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package models
|
||||
|
||||
// CanCreate checks if the user can create a new team
|
||||
func (t *Team) CanCreate(user *User, id int64) bool {
|
||||
func (t *Team) CanCreate(user *User) bool {
|
||||
// This is currently a dummy function, later on we could imagine global limits etc.
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ func CreateUser(user User) (newUser User, err error) {
|
|||
|
||||
// Create the user's namespace
|
||||
newN := &Namespace{Name: newUserOut.Username, Description: newUserOut.Username + "'s namespace.", Owner: newUserOut}
|
||||
err = newN.Create(&newUserOut, 0)
|
||||
err = newN.Create(&newUserOut)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
|
|
@ -14,8 +14,9 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
|||
p := reflect.ValueOf(c.CObject).Elem()
|
||||
p.Set(reflect.Zero(p.Type()))
|
||||
|
||||
// Get the object
|
||||
if err := ctx.Bind(&c.CObject); err != nil {
|
||||
// Get the object & bind params to struct
|
||||
if err := ParamBinder(c.CObject, ctx); err != nil {
|
||||
fmt.Println(err)
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
||||
}
|
||||
|
||||
|
@ -26,21 +27,21 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
|||
}
|
||||
|
||||
// Get an ID if we have one
|
||||
var id int64
|
||||
/*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
|
||||
if !c.CObject.CanCreate(¤tUser, id) {
|
||||
if !c.CObject.CanCreate(¤tUser) {
|
||||
return echo.NewHTTPError(http.StatusForbidden)
|
||||
}
|
||||
|
||||
// Create
|
||||
err = c.CObject.Create(¤tUser, id)
|
||||
err = c.CObject.Create(¤tUser)
|
||||
if err != nil {
|
||||
if models.IsErrListDoesNotExist(err) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "The list does not exist.")
|
||||
|
|
|
@ -9,11 +9,6 @@ import (
|
|||
|
||||
// DeleteWeb is the web handler to delete something
|
||||
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||
// Get the ID
|
||||
/*id, err := models.GetIntURLParam("id", ctx)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
||||
}*/
|
||||
// Bind params to struct
|
||||
if err := ParamBinder(c.CObject, ctx); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL param.")
|
||||
|
|
|
@ -94,12 +94,12 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.GET("/lists/:id", listHandler.ReadOneWeb)
|
||||
a.POST("/lists/:id", listHandler.UpdateWeb)
|
||||
a.DELETE("/lists/:listid", listHandler.DeleteWeb)
|
||||
a.PUT("/namespaces/:id/lists", listHandler.CreateWeb)
|
||||
a.PUT("/namespaces/:nid/lists", listHandler.CreateWeb)
|
||||
|
||||
itemHandler := &crud.WebHandler{
|
||||
CObject: &models.ListItem{},
|
||||
}
|
||||
a.PUT("/lists/:id", itemHandler.CreateWeb)
|
||||
a.PUT("/lists/:listid", itemHandler.CreateWeb)
|
||||
a.DELETE("/items/:listitemid", itemHandler.DeleteWeb)
|
||||
a.POST("/items/:id", itemHandler.UpdateWeb)
|
||||
|
||||
|
@ -117,8 +117,8 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
CObject: &models.TeamNamespace{},
|
||||
}
|
||||
a.GET("/namespaces/:id/teams", namespaceTeamHandler.ReadAllWeb)
|
||||
a.PUT("/namespaces/:id/teams", namespaceTeamHandler.CreateWeb)
|
||||
a.DELETE("/namespaces/:nid/teams/:teamid", namespaceTeamHandler.DeleteWeb)
|
||||
a.PUT("/namespaces/:namespaceid/teams", namespaceTeamHandler.CreateWeb)
|
||||
a.DELETE("/namespaces/:namespaceid/teams/:teamid", namespaceTeamHandler.DeleteWeb)
|
||||
|
||||
teamHandler := &crud.WebHandler{
|
||||
CObject: &models.Team{},
|
||||
|
|
Loading…
Reference in a new issue