import scaffoldings

This commit is contained in:
Gani Georgiev
2022-08-05 06:00:38 +03:00
parent 95f9d685dc
commit f459dd8812
25 changed files with 1362 additions and 261 deletions

View File

@@ -128,7 +128,7 @@ func (dao *Dao) Delete(m models.Model) error {
// Save upserts (update or create if primary key is not set) the provided model.
func (dao *Dao) Save(m models.Model) error {
if m.HasId() {
if !m.IsNew() {
return dao.update(m)
}
@@ -140,6 +140,10 @@ func (dao *Dao) update(m models.Model) error {
return errors.New("ID is not set")
}
if m.GetCreated().IsZero() {
m.RefreshCreated()
}
m.RefreshUpdated()
if dao.BeforeUpdateFunc != nil {
@@ -195,6 +199,9 @@ func (dao *Dao) create(m models.Model) error {
if v, ok := any(m).(models.ColumnValueMapper); ok {
dataMap := v.ColumnValueMap()
if _, ok := dataMap["id"]; !ok {
dataMap["id"] = m.GetId()
}
_, err := dao.db.Insert(m.TableName(), dataMap).Execute()
if err != nil {
@@ -206,6 +213,9 @@ func (dao *Dao) create(m models.Model) error {
}
}
// clears the internal isNewFlag
m.UnmarkAsNew()
if dao.AfterCreateFunc != nil {
dao.AfterCreateFunc(dao, m)
}