Working with model - Sharykhin/go-modular GitHub Wiki

If you didn't know how to initialize model read [article about it](Initialize new model)

To work with model first of all you have to import it

import model "go-modular/application/model/todo"

We've imported model "todo"

Then initialize new instance of it

todoModel := model.New()

It will create a new instance and now we will able to work with it

SAVE

todoModel.SetTitle("Buy flowers for my wife")
todoModel.SetIsDone(false)	
if err :=todoModel.Save(); err != nil {
	return err
}

It will create new row in database and set just created primary key to our model and we can update our row

UPDATE

todoModel.SetIsDone(true)
if err :=todoModel.Save(); err != nil {
	return err
}

Such as our row exists in database, this row will be updated.

DELETE

To Delete model you should call method Delete, but pay attention that the value of primary has to be existed.

if err := todoModel.Delete(); err != nil {
	return err
}

After that the value of primary key will be nil, but all other values won't be erased and if you call method Save it will create new row.