implemented update method via param binder

This commit is contained in:
konrad 2018-07-21 15:17:02 +02:00 committed by kolaente
parent 9e75e9b73b
commit 9979b7e321
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
12 changed files with 25 additions and 36 deletions

View file

@ -5,6 +5,6 @@ type CRUDable interface {
Create(*User) error
ReadOne() error
ReadAll(*User) (interface{}, error)
Update(int64) error
Update() error
Delete() error
}

View file

@ -20,9 +20,7 @@ func CreateOrUpdateList(list *List) (err error) {
}
// Update implements the update method of CRUDable
func (l *List) Update(id int64) (err error) {
l.ID = id
func (l *List) Update() (err error) {
// Check if it exists
_, err = GetListByID(l.ID)
if err != nil {

View file

@ -2,7 +2,7 @@ package models
// ListItem represents an item in a todolist
type ListItem struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listitemid"`
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"listitem"`
Text string `xorm:"varchar(250)" json:"text"`
Description string `xorm:"varchar(250)" json:"description"`
Done bool `json:"done"`

View file

@ -9,11 +9,9 @@ func (i *ListItem) Create(doer *User) (err error) {
}
// Update updates a list item
func (i *ListItem) Update(ID int64) (err error) {
i.ID = ID
func (i *ListItem) Update() (err error) {
// Check if the item exists
_, err = GetListItemByID(ID)
_, err = GetListItemByID(i.ID)
if err != nil {
return
}

View file

@ -11,9 +11,9 @@ func (i *ListItem) CanDelete(doer *User) bool {
}
// CanUpdate determines if a user has the right to update a list item
func (i *ListItem) CanUpdate(doer *User, id int64) bool {
func (i *ListItem) CanUpdate(doer *User) bool {
// Get the item
lI, _ := GetListItemByID(id)
lI, _ := GetListItemByID(i.ID)
// A user can update an item if he has write acces to its list
list, _ := GetListByID(lI.ListID)

View file

@ -87,8 +87,8 @@ func (l *List) CanDelete(doer *User) bool {
}
// CanUpdate checks if the user can update a list
func (l *List) CanUpdate(doer *User, id int64) bool {
list, _ := GetListByID(id)
func (l *List) CanUpdate(doer *User) bool {
list, _ := GetListByID(l.ID)
return list.CanWrite(doer)
}

View file

@ -71,8 +71,8 @@ func (n *Namespace) CanRead(user *User) bool {
}
// CanUpdate checks if the user can update the namespace
func (n *Namespace) CanUpdate(user *User, id int64) bool {
nn, _ := GetNamespaceByID(id)
func (n *Namespace) CanUpdate(user *User) bool {
nn, _ := GetNamespaceByID(n.ID)
return nn.IsAdmin(user)
}

View file

@ -1,15 +1,14 @@
package models
// Update implements the update method via the interface
func (n *Namespace) Update(id int64) (err error) {
func (n *Namespace) Update() (err error) {
// Check if we have at least a name
if n.Name == "" {
return ErrNamespaceNameCannotBeEmpty{NamespaceID: id}
return ErrNamespaceNameCannotBeEmpty{NamespaceID: n.ID}
}
n.ID = id
// Check if the namespace exists
currentNamespace, err := GetNamespaceByID(id)
currentNamespace, err := GetNamespaceByID(n.ID)
if err != nil {
return
}

View file

@ -6,6 +6,6 @@ type Rights interface {
CanWrite(*User) bool
CanRead(*User) bool
CanDelete(*User) bool
CanUpdate(*User, int64) bool
CanUpdate(*User) bool
CanCreate(*User) bool
}

View file

@ -7,10 +7,10 @@ func (t *Team) CanCreate(user *User) bool {
}
// CanUpdate checks if the user can update a team
func (t *Team) CanUpdate(user *User, id int64) bool {
func (t *Team) CanUpdate(user *User) bool {
// Check if the current user is in the team and has admin rights in it
exists, _ := x.Where("team_id = ?", id).
exists, _ := x.Where("team_id = ?", t.ID).
And("user_id = ?", user.ID).
And("is_admin = ?", true).
Get(&TeamMember{})

View file

@ -1,25 +1,25 @@
package models
// Update is the handler to create a team
func (t *Team) Update(id int64) (err error) {
func (t *Team) Update() (err error) {
// Check if we have a name
if t.Name == "" {
return ErrTeamNameCannotBeEmpty{}
}
// Check if the team exists
_, err = GetTeamByID(id)
_, err = GetTeamByID(t.ID)
if err != nil {
return
}
_, err = x.ID(id).Update(t)
_, err = x.ID(t.ID).Update(t)
if err != nil {
return
}
// Get the newly updated team
*t, err = GetTeamByID(id)
*t, err = GetTeamByID(t.ID)
return
}

View file

@ -13,28 +13,22 @@ func (c *WebHandler) UpdateWeb(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 {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
// Get the ID
id, err := models.GetIntURLParam("id", ctx)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}
// Check if the user has the right to do that
currentUser, err := models.GetCurrentUser(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !c.CObject.CanUpdate(&currentUser, id) {
if !c.CObject.CanUpdate(&currentUser) {
return echo.NewHTTPError(http.StatusForbidden)
}
// Do the update
err = c.CObject.Update(id)
err = c.CObject.Update()
if err != nil {
if models.IsErrNeedToBeListAdmin(err) {
return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.")