diff --git a/Gopkg.lock b/Gopkg.lock index 79f68be225..11692cbcfd 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -72,6 +72,14 @@ revision = "8cb6e5b959231cc1119e43259c4a608f9c51a241" version = "v1.0.0" +[[projects]] + digest = "1:8eb1de8112c9924d59bf1d3e5c26f5eaa2bfc2a5fcbb92dc1c2e4546d695f277" + name = "github.com/imdario/mergo" + packages = ["."] + pruneopts = "UT" + revision = "9f23e2d6bd2a77f959b2bf6acdbefd708a83a4a4" + version = "v0.3.6" + [[projects]] digest = "1:82c5850e702ccef3976cdfdf737725013587c64e02e5ecae57e86431163b27aa" name = "github.com/labstack/echo" @@ -274,6 +282,7 @@ "github.com/go-sql-driver/mysql", "github.com/go-xorm/core", "github.com/go-xorm/xorm", + "github.com/imdario/mergo", "github.com/labstack/echo", "github.com/labstack/echo/middleware", "github.com/mattn/go-sqlite3", diff --git a/Gopkg.toml b/Gopkg.toml index d3fbeb83e6..e9a32147c7 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -47,3 +47,7 @@ [[constraint]] name = "github.com/spf13/viper" version = "1.2.0" + +[[constraint]] + name = "github.com/imdario/mergo" + version = "0.3.6" diff --git a/models/list_items_create_update.go b/models/list_items_create_update.go index 6c4e1f888d..798f5da0e1 100644 --- a/models/list_items_create_update.go +++ b/models/list_items_create_update.go @@ -1,5 +1,9 @@ package models +import ( + "github.com/imdario/mergo" +) + // Create is the implementation to create a list task func (i *ListTask) Create(doer *User) (err error) { i.ID = 0 @@ -34,17 +38,14 @@ func (i *ListTask) Update() (err error) { return } - // If we dont have a text, use the one from the original task - if i.Text == "" { - i.Text = ot.Text - } - // For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand - if i.Done != ot.Done { - _, err = x.ID(i.ID).Cols("done").Update(i) - return + // Which is why we merge the actual task struct with the one we got from the user. + // The user struct ovverrides values in the actual one. + if err := mergo.Merge(&ot, i, mergo.WithOverride); err != nil { + return err } - _, err = x.ID(i.ID).Update(i) + _, err = x.ID(i.ID).Cols("text", "description", "done", "due_date_unix", "reminder_unix").Update(ot) + *i = ot return }