diff --git a/models/crudable.go b/models/crudable.go index 6d2a77f2015..4defc7f59a9 100644 --- a/models/crudable.go +++ b/models/crudable.go @@ -7,8 +7,4 @@ type CRUDable interface { ReadAll(*User) (interface{}, error) Update(int64) error Delete(int64) error - - // This method is needed, because old values would otherwise remain in the struct. - // TODO find a way of not needing an extra function - Empty() } diff --git a/models/teams.go b/models/teams.go index 1a705eefe0f..444b018d385 100644 --- a/models/teams.go +++ b/models/teams.go @@ -84,15 +84,3 @@ func GetAllTeamsByNamespaceID(id int64) (teams []*Team, err error) { return } - -// Empty empties a struct. Because we heavily use pointers, the old values remain in the struct. -// If you then update by not providing evrything, you have i.e. the old description still in the -// newly created team, but you didn't provided one. -func (t *Team) Empty() { - t.ID = 0 - t.CreatedByID = 0 - t.CreatedBy = &User{} - t.Name = "" - t.Description = "" - t.Members = []*User{} -} diff --git a/routes/crud/create.go b/routes/crud/create.go index a1acfdffa0d..d693d7d52a4 100644 --- a/routes/crud/create.go +++ b/routes/crud/create.go @@ -1,16 +1,17 @@ package crud import ( - "fmt" "git.kolaente.de/konrad/list/models" "github.com/labstack/echo" "net/http" + "reflect" ) // CreateWeb is the handler to create an object func (c *WebHandler) CreateWeb(ctx echo.Context) error { // Re-initialize our model - c.CObject.Empty() + p := reflect.ValueOf(c.CObject).Elem() + p.Set(reflect.Zero(p.Type())) // Get the object if err := ctx.Bind(&c.CObject); err != nil { @@ -57,8 +58,6 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error { return echo.NewHTTPError(http.StatusNotFound, "The namespace name cannot be empty.") } - fmt.Println(err) - return echo.NewHTTPError(http.StatusInternalServerError) } diff --git a/routes/crud/update.go b/routes/crud/update.go index c4bca31de79..59124fbb879 100644 --- a/routes/crud/update.go +++ b/routes/crud/update.go @@ -4,10 +4,15 @@ import ( "git.kolaente.de/konrad/list/models" "github.com/labstack/echo" "net/http" + "reflect" ) // UpdateWeb is the webhandler to update an object func (c *WebHandler) UpdateWeb(ctx echo.Context) error { + // Re-initialize our model + p := reflect.ValueOf(c.CObject).Elem() + p.Set(reflect.Zero(p.Type())) + // Get the object if err := ctx.Bind(&c.CObject); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")