-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_entity_separation_test.go
More file actions
61 lines (49 loc) · 1.17 KB
/
Copy pathmodel_entity_separation_test.go
File metadata and controls
61 lines (49 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package example_test
import (
"testing"
"github.com/gin-gonic/gin"
"github.com/philiphil/restman/orm"
"github.com/philiphil/restman/orm/entity"
"github.com/philiphil/restman/orm/gormrepository"
"github.com/philiphil/restman/route"
"github.com/philiphil/restman/router"
)
//If needed we can separate an Entity from a Model
// Entity is the struct that will be used to interact with the database
// Model is the struct that will be used to interact with the API
// Entity needs to implement the entity.Entity interface
type Entity struct {
ID int
Name string
}
func (e Entity) GetId() entity.ID {
return entity.CastId(e.ID)
}
func (e Entity) SetId(id any) entity.Entity {
e.ID = int(entity.CastId(id))
return e
}
// Model must implement the Model interface
type Model struct {
Entity
}
func (t Model) ToEntity() Entity {
e := Entity{
ID: t.ID,
Name: t.Name,
}
return e
}
func (t Model) FromEntity(entity Entity) any {
t.ID = entity.ID
t.Name = entity.Name
return t
}
func testEntityModel(m *testing.M) {
api := router.NewApiRouter(
*orm.NewORM(gormrepository.NewRepository[Model](getDB())),
route.DefaultApiRoutes(),
)
r := gin.New()
api.AllowRoutes(r)
}