Exemplo n.º 1
0
    def get_all_news(self) -> List[News]:
        """
        Grab all news in the system.

        Returns:
            A list of News objects sorted by timestamp.
        """
        sql = "SELECT id, timestamp, title, body FROM news ORDER BY timestamp DESC"
        cursor = self.execute(sql)
        return [
            News(
                result['id'],
                result['timestamp'],
                result['title'],
                result['body'],
            ) for result in cursor.fetchall()
        ]
Exemplo n.º 2
0
    def get_news(self, newsid: int) -> Optional[News]:
        """
        Given a news ID, grab that news entry from the DB.

        Parameters:
            newsid - Integer specifying news ID.

        Returns:
            A News object if the news entry was found or None otherwise.
        """
        sql = "SELECT timestamp, title, body FROM news WHERE id = :id"
        cursor = self.execute(sql, {'id': newsid})
        if cursor.rowcount != 1:
            # Couldn't find an entry with this ID
            return None

        result = cursor.fetchone()
        return News(
            newsid,
            result['timestamp'],
            result['title'],
            result['body'],
        )