From 3e07610cfef325e6bc31b084b7350bb5a43dfd74 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Thu, 6 Dec 2018 10:19:19 +0100 Subject: [PATCH] Preload content on update and copy createdAt --- crm/service/content.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/crm/service/content.go b/crm/service/content.go index 5fea48051..83a499724 100644 --- a/crm/service/content.go +++ b/crm/service/content.go @@ -3,6 +3,7 @@ package service import ( "context" + "github.com/davecgh/go-spew/spew" "github.com/pkg/errors" "github.com/titpetric/factory" @@ -82,11 +83,28 @@ func (s *content) Create(mod *types.Content) (*types.Content, error) { return response, s.preload(response, "user", "fields") } -func (s *content) Update(mod *types.Content) (*types.Content, error) { - if mod.ID == 0 { - return nil, errors.New("Error when saving content, invalid ID") +func (s *content) Update(content *types.Content) (c *types.Content, err error) { + validate := func() error { + if content.ID == 0 { + return errors.New("Error updating content: invalid ID") + } else if c, err = s.repository.FindByID(content.ID); err != nil { + return errors.Wrap(err, "Error while loading content for update") + } else { + content.CreatedAt = c.CreatedAt + } + + return nil } - return s.repository.Update(mod) + + if err = validate(); err != nil { + return nil, err + } + + return c, s.db.Transaction(func() (err error) { + spew.Dump(content) + c, err = s.repository.Update(content) + return + }) } func (s *content) Fields(mod *types.Content) ([]*types.ContentColumn, error) {