-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implements BaseRepository and BaseService
- Loading branch information
1 parent
45fa040
commit 1bd7d83
Showing
6 changed files
with
60 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
from typing import TypeVar, Generic, Optional | ||
|
||
db = SQLAlchemy() | ||
import app | ||
|
||
T = TypeVar('T') | ||
|
||
class BaseRepository: | ||
def __init__(self, model): | ||
|
||
class BaseRepository(Generic[T]): | ||
def __init__(self, model: T): | ||
self.model = model | ||
|
||
def create(self, **kwargs): | ||
instance = self.model(**kwargs) | ||
db.session.add(instance) | ||
db.session.commit() | ||
def create(self, **kwargs) -> T: | ||
instance: T = self.model(**kwargs) | ||
app.db.session.add(instance) | ||
app.db.session.commit() | ||
return instance | ||
|
||
def get_by_id(self, id): | ||
return self.model.query.get(id) | ||
def get_by_id(self, id: int) -> Optional[T]: | ||
instance: Optional[T] = self.model.query.get(id) | ||
return instance | ||
|
||
def update(self, id, **kwargs): | ||
instance = self.get_by_id(id) | ||
def update(self, id: int, **kwargs) -> Optional[T]: | ||
instance: Optional[T] = self.get_by_id(id) | ||
if instance: | ||
for key, value in kwargs.items(): | ||
setattr(instance, key, value) | ||
db.session.commit() | ||
app.db.session.commit() | ||
return instance | ||
return None | ||
|
||
def delete(self, id): | ||
instance = self.get_by_id(id) | ||
def delete(self, id: int) -> bool: | ||
instance: Optional[T] = self.get_by_id(id) | ||
if instance: | ||
db.session.delete(instance) | ||
db.session.commit() | ||
app.db.session.delete(instance) | ||
app.db.session.commit() | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters