def get_all_by(query_params: dict, paginate=True) -> Dict[str, Union[int, List[Tour]]]: if not paginate: result = TourRepository.get_all_by_query(**query_params) return { 'total': result.get('total'), 'tours': result.get('tours'), } result = TourRepository.paginate_with(**query_params) return { 'total': result.get('total'), 'tours': result.get('tours'), 'page': result.get('page'), 'page_size': result.get('page_size') }
def validate_uniqueness_except_current_tour_id(tour_id: int, field_name: str = None, **kwargs): tour = TourRepository.get_by(**kwargs) if tour is not None and tour.id != tour_id: key, value = next(iter(kwargs.items())) field_name = field_name if field_name else key raise ValidationError(field_name=field_name, message=ValidateErrorCode.IS_UNIQUE) return tour
def validate_non_existence(field_name: str = None, **kwargs): tour = TourRepository.get_by(**kwargs) if tour is None: key, value = next(iter(kwargs.items())) field_name = field_name if field_name else key raise ValidationError(field_name=field_name, message=ValidateErrorCode.NOT_EXIST) return tour
def validate_uniqueness(field_name: str = None, **kwargs): tour = TourRepository.get_by(**kwargs) if tour is not None: key, value = next(iter(kwargs.items())) field_name = field_name if field_name else key raise ValidationError(field_name=field_name, message=ValidateErrorCode.IS_UNIQUE) return tour
def update_by(tour_id: int, **payload): payload = accepts_logic(payload=payload, temp={'tour_id': tour_id}, schema=TourUpdateBy) TourRepository.update_by(tour_id, **payload)
def get_all_tour_payment_information(tour_id): tour = TourRepository.get_by(id=tour_id) if tour is None: raise TourNotFoundException return list(tour.tour_payment_informations)
def create(payload: dict) -> Tour: payload = accepts_logic(payload=payload, schema=TourCreate) tour = TourRepository.create(payload) return tour
def is_existed(**kwargs) -> Tour: tour = TourRepository.get_by(**kwargs) return tour
def is_existed_except_by_id(tour_id: int, **kwargs) -> bool: tour = TourRepository.get_by(**kwargs) if not tour or tour.id == tour_id: return False return True