-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAssetModel.py
More file actions
60 lines (42 loc) · 1.8 KB
/
Copy pathAssetModel.py
File metadata and controls
60 lines (42 loc) · 1.8 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
from .BaseDataModel import BaseDataModel
from .db_schemas import Asset
from .enums import DataBaseEnum
from bson import ObjectId
from sqlalchemy.future import select
from sqlalchemy import delete
class AssetModel(BaseDataModel):
def __init__(self, db_client):
super().__init__(db_client)
self.db_client = self.db_client
@classmethod
async def create_instance(cls, db_client):
# we create this method and don't use the __init__ because we need to do async calls and __init__ can't be async
instance = cls(db_client)
return instance
async def create_asset(self, asset: Asset):
async with self.db_client() as session:
async with session.begin():
session.add(asset)
await session.commit()
await session.refresh(asset)
return asset
async def get_all_project_assets(self, asset_project_id: str, asset_type: str):
async with self.db_client() as session:
async with session.begin():
stmt = select(Asset).where(
Asset.asset_project_id == asset_project_id,
Asset.asset_type == asset_type
)
result = await session.execute(stmt)
assets = result.scalars().all()
return assets
async def get_asset_record_by_id(self, asset_project_id: str, asset_id: int):
async with self.db_client() as session:
async with session.begin():
stmt = select(Asset).where(
Asset.asset_project_id == asset_project_id,
Asset.asset_id == asset_id
)
result = await session.execute(stmt)
asset = result.scalar_one_or_none()
return asset