-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
57 lines (40 loc) · 1.66 KB
/
crud.py
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
from typing import Union
from sqlalchemy.orm import Session
from models import CurrencyModel, ValueModel
from schemas import CurrencySchema, ValueSchema
class Crud:
def __init__(self, session: Session, schema):
self.session = session
self.schema = schema
def get(self, pk):
return self.session.get(self.schema, pk)
def update(self, data):
self.session.query(self.schema).filter_by(id=data['id']).update(**data)
def delete(self, pk):
self.session.query(self.schema).filter_by(id=pk).delete()
def insert(self, data: Union[CurrencySchema, ValueSchema]):
obj = self.schema(**data.dict())
self.session.add(obj)
self.session.commit()
return obj
class CurrenciesCrud(Crud):
def __init__(self, session: Session, schema):
super().__init__(session, schema)
def get_by_code(self, code):
return self.session.query(self.schema).filter_by(code=code).first()
def get_or_create(self, new_code: CurrencySchema):
obj = self.get_by_code(new_code.code)
if obj:
return obj
return self.insert(new_code)
class ValuesCrud(Crud):
def __init__(self, session: Session, schema):
super().__init__(session, schema)
def get_last_currency(self, currency_id: int):
return self.session.query(self.schema).filter_by(currency_id=currency_id).order_by(self.schema.id.desc()).first()
class MainCurrenciesCrud(CurrenciesCrud):
def __init__(self, session: Session):
super().__init__(session, CurrencyModel)
class MainValuesCrud(ValuesCrud):
def __init__(self, session: Session):
super().__init__(session, ValueModel)