Skip to content

Commit

Permalink
fix: Refactor services and repositories (dataset)
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Oct 15, 2024
1 parent b5e36c8 commit 33ccd60
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
38 changes: 27 additions & 11 deletions app/modules/dataset/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def is_synchronized(self, dataset_id: int) -> bool:
return True
return False

'''
Synchronised dataset
'''
def get_synchronized_datasets(self) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
Expand All @@ -90,18 +93,35 @@ def get_synchronized_datasets(self) -> List[DataSet]:
.all()
)

def get_unsynchronized_datasets(self) -> List[DataSet]:
def get_synchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.is_(None))
.filter(DataSet.user_id == current_user_id, DSMetaData.dataset_doi.isnot(None))
.order_by(self.model.created_at.desc())
.all()
)

def get_synchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
def get_synchronized_dataset_by_user(self, current_user_id: int, dataset_id: int) -> DataSet:
return (
self.model.query.join(DSMetaData)
.filter(DataSet.user_id == current_user_id, DSMetaData.dataset_doi.isnot(None))
.filter(DataSet.user_id == current_user_id, DataSet.id == dataset_id, DSMetaData.dataset_doi.isnot(None))
.first()
)

def count_synchronized_datasets(self) -> int:
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.isnot(None))
.count()
)

'''
Unsynchronised dataset
'''
def get_unsynchronized_datasets(self) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.is_(None))
.order_by(self.model.created_at.desc())
.all()
)
Expand All @@ -121,20 +141,16 @@ def get_unsynchronized_dataset_by_user(self, current_user_id: int, dataset_id: i
.first()
)

def count_synchronized_datasets(self):
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.isnot(None))
.count()
)

def count_unsynchronized_datasets(self):
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.is_(None))
.count()
)

'''
Top X datasets...
'''
def latest_synchronized(self) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
Expand Down
2 changes: 1 addition & 1 deletion app/modules/dataset/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def subdomain_index(doi):
def get_unsynchronized_dataset(dataset_id):

# Get dataset
dataset = dataset_service.get_unsynchronized_dataset(current_user.id, dataset_id)
dataset = dataset_service.get_unsynchronized_dataset_by_user(current_user.id, dataset_id)

if not dataset:
abort(404)
Expand Down
32 changes: 24 additions & 8 deletions app/modules/dataset/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,46 @@ def move_feature_models(self, dataset: DataSet):
def is_synchronized(self, dataset_id: int) -> bool:
return self.repository.is_synchronized(dataset_id)

'''
Synchronised dataset
'''
def get_synchronized_datasets(self) -> List[DataSet]:
return self.repository.get_synchronized_datasets()

def get_unsynchronized_datasets(self) -> List[DataSet]:
return self.repository.get_unsynchronized_datasets()

def get_synchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return self.repository.get_synchronized_datasets_by_user(current_user_id)

def get_synchronized_dataset_by_user(self, current_user_id: int, dataset_id: int) -> DataSet:
return self.repository.get_synchronized_dataset_by_user(current_user_id, dataset_id)

def count_synchronized_datasets(self) -> int:
return self.repository.count_synchronized_datasets()

'''
Unsynchronised dataset
'''
def get_unsynchronized_datasets(self) -> List[DataSet]:
return self.repository.get_unsynchronized_datasets()

def get_unsynchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return self.repository.get_unsynchronized_datasets_by_user(current_user_id)

def get_unsynchronized_dataset(self, current_user_id: int, dataset_id: int) -> DataSet:
return self.repository.get_unsynchronized_dataset(current_user_id, dataset_id)
def get_unsynchronized_dataset_by_user(self, current_user_id: int, dataset_id: int) -> DataSet:
return self.repository.get_unsynchronized_dataset_by_user(current_user_id, dataset_id)

def count_unsynchronized_datasets(self) -> int:
return self.repository.count_unsynchronized_datasets()

'''
Top X datasets...
'''

def latest_synchronized(self) -> List[DataSet]:
return self.repository.latest_synchronized()

def get_top_5_datasets_by_feature_model_count(self) -> List[DataSet]:
return self.repository.get_top_5_datasets_by_feature_model_count()

def count_synchronized_datasets(self) -> int:
return self.repository.count_synchronized_datasets()

def count_feature_models(self, dataset_id: int) -> int:
dataset = self.repository.get_by_id(dataset_id)
return dataset.feature_model_count
Expand Down

0 comments on commit 33ccd60

Please sign in to comment.