I am creating application which get the data from the database and pass them to frontend as JSON.
I think it can be a good idea to create separate structs
for the data which I get from the database and the data which I pass to REST services. I am right or it is my mistake ?
In this case I need to map the structs from one layer to another. I do it in the database layer now:
func (ds *DataStore) AddUnit(_unit models.Unit) {
unit := Unit{}
unit.Name = _unit.Name
unit.Description = _unit.Description
db.Create(&unit)
}
func (ds *DataStore) UpdateUnit(id int, _unit models.Unit) models.Unit {
unit := Unit{}
db.First(&unit, id)
unit.Name = _unit.Name
unit.Description = _unit.Description
db.Save(&unit)
return _unit
}
Now I have this code in each method for each entity in my code. May be there is some way or library to extract this mapping functionality and describe it using declarative way in the separate file ?