adamkali / adamkali/Tavern-Backend
Make DB Requests To Be more convienient
- 主要言語
- Go
- スター
- 2
- フォーク
- 0
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
```
// TODO: Make DB Requests To Be more convienient
```
https://github.com/adamkali/Tavern-Backend/blob/40addcbb17c9f3ec0b39e10e2862b5a24d7f3c56/controllers/UserController.go#L23
I would like to add a Repository Struct like Csharp's Linq. This will help with database querying and help in debugging and avoiding errors and bugs. See below for a rough idea:
```golang
type ModelRepository[T IData] struct {
db gorm.DB
model T
models []T
}
func NewModelRepository[T IData](db grom.DB) {
return ModelRepository[T]{
db,
T.NewData().(T),
[]T{},
}
}
func (repo ModelRepository[T IData]) Sanitize(m T) {
// Clear out the data for every call.
}
```
In this way we could impliment:
```golang
type UserRepository = ModelRepository[models.User]
func NewUserRepository(db gorm.DB) {
return NewModelRepository[models.User](db)
}
```
Then the following call:
```golang
res := c.H.DB.Preload("PlayerPrefrences").Where("SOME WHERE CLAUSE").First(&user)
```
could then become something like:
```golang
var res models.User
err := repo.SomeWhereClause(&res)
```
I think this will be more beneficial in the long run, as we would only need to instantiate the new repo in the construction of the controller:
```golang
type UserController struct{ H BaseHandler[models.User] }
func NewUserController(DB *gorm.DB) *UserController {
return &UserController{
H: *NewHandler(DB, models.User{}, "User"),
}
}
```
becomes:
```golang
type UserController struct{ H BaseHandler[models.User] }
func NewUserController(DB *gorm.DB) *UserController {
return &UserController{
H: *NewHandler(NewUserRepo(db), models.User{}, "User"),
}
}
```
Obviously this will need to be tested and vetted. First push Bëor branch as production, and then `git checkout -b "Beor v1/RepositoryAbstraction"` can be made and pushed in a later version. Probably Bëor v1.1.
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
評価
この issue はまだ評価されていません。