Coverage for src/kwai/modules/portal/news/news_item_repository.py: 100%

6 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines an interface for a news item repository.""" 

2 

3from abc import ABC, abstractmethod 

4from typing import AsyncIterator 

5 

6from kwai.modules.portal.news.news_item import NewsItemEntity, NewsItemIdentifier 

7from kwai.modules.portal.news.news_item_query import NewsItemQuery 

8 

9 

10class NewsItemNotFoundException(Exception): 

11 """Raised when the news item can not be found.""" 

12 

13 

14class NewsItemRepository(ABC): 

15 """Interface for a news item repository.""" 

16 

17 @abstractmethod 

18 async def create(self, news_item: NewsItemEntity) -> NewsItemEntity: 

19 """Create a new news item entity. 

20 

21 Args: 

22 news_item: The news item to create 

23 

24 Returns: 

25 The news item entity with an identifier. 

26 """ 

27 raise NotImplementedError 

28 

29 @abstractmethod 

30 async def update(self, news_item: NewsItemEntity): 

31 """Update a news item entity. 

32 

33 Args: 

34 news_item: The news item to update. 

35 """ 

36 raise NotImplementedError 

37 

38 @abstractmethod 

39 async def delete(self, news_item: NewsItemEntity): 

40 """Delete a news item. 

41 

42 Args: 

43 news_item: The news item to delete. 

44 """ 

45 raise NotImplementedError 

46 

47 @abstractmethod 

48 def create_query(self) -> NewsItemQuery: 

49 """Create a query for querying news items.""" 

50 raise NotImplementedError 

51 

52 @abstractmethod 

53 async def get_by_id(self, id_: NewsItemIdentifier) -> NewsItemEntity: 

54 """Get the news item with the given id. 

55 

56 Args: 

57 id_: The id of the news item. 

58 

59 Returns: 

60 A news item entity. 

61 """ 

62 raise NotImplementedError 

63 

64 @abstractmethod 

65 async def get_all( 

66 self, 

67 query: NewsItemQuery | None = None, 

68 limit: int | None = None, 

69 offset: int | None = None, 

70 ) -> AsyncIterator[NewsItemEntity]: 

71 """Return all news items of a given query. 

72 

73 Args: 

74 query: The query to use for selecting the rows. 

75 limit: The maximum number of entities to return. 

76 offset: Skip the offset rows before beginning to return entities. 

77 

78 Yields: 

79 A list of news items. 

80 """ 

81 raise NotImplementedError