add NewsService
This commit is contained in:
parent
02134f61f5
commit
51f1a5a2d8
@ -1,5 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime, date
|
||||||
|
|
||||||
from from_root import from_root
|
from from_root import from_root
|
||||||
|
|
||||||
@ -9,7 +9,9 @@ from src.ez_lan_manager.services.DatabaseService import DatabaseService
|
|||||||
|
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
|
from src.ez_lan_manager.services.NewsService import NewsService
|
||||||
from src.ez_lan_manager.services.UserService import UserService
|
from src.ez_lan_manager.services.UserService import UserService
|
||||||
|
from src.ez_lan_manager.types.News import News
|
||||||
from src.ez_lan_manager.types.Transaction import Transaction
|
from src.ez_lan_manager.types.Transaction import Transaction
|
||||||
from src.ez_lan_manager.types.User import User
|
from src.ez_lan_manager.types.User import User
|
||||||
|
|
||||||
@ -21,20 +23,17 @@ if __name__ == "__main__":
|
|||||||
db_config = configuration_service.get_database_configuration()
|
db_config = configuration_service.get_database_configuration()
|
||||||
db_service = DatabaseService(db_config)
|
db_service = DatabaseService(db_config)
|
||||||
user_service = UserService(db_service)
|
user_service = UserService(db_service)
|
||||||
accounting_service = AccountingService(db_service, user_service)
|
accounting_service = AccountingService(db_service)
|
||||||
#user_service.create_user("Mamfred", "Peter@peterson.com", "MamaHalloDoo")
|
news_service = NewsService(db_service)
|
||||||
# db_service.add_transaction(Transaction(
|
print(news_service.get_latest_news())
|
||||||
# user_id=19,
|
|
||||||
# value=50,
|
# news_service.add_news(News(
|
||||||
# is_debit=True,
|
# news_id=None,
|
||||||
# reference="Ein teures Bier",
|
# title=f"TITLE{randint(0, 9999)}",
|
||||||
# transaction_date=datetime.now()
|
# subtitle="",
|
||||||
|
# content="",
|
||||||
|
# author=user_service.get_user(19),
|
||||||
|
# news_date=date(2024, 8, 30)
|
||||||
# ))
|
# ))
|
||||||
#print(accounting_service.remove_balance(19, 150, "EinsFuffzig"))
|
|
||||||
# print(db_service.create_user(f"TestUser{randint(0, 9999)}", f"TestMail{randint(0, 9999)}", "pw123"))
|
|
||||||
# print(db_service.update_user(
|
|
||||||
# User(user_id=19, user_name='TestUser838', user_mail='TestMail3142', user_password='pw123', user_first_name=None, user_last_name=None,
|
|
||||||
# user_birth_day=None, is_active=False, is_team_member=False, is_admin=False, created_at=datetime(2024, 8, 19, 10, 10, 39),
|
|
||||||
# last_updated_at=datetime(2024, 8, 19, 10, 10, 39), balance=0)
|
|
||||||
#
|
|
||||||
# ))
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import logging
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from src.ez_lan_manager.services.DatabaseService import DatabaseService
|
from src.ez_lan_manager.services.DatabaseService import DatabaseService
|
||||||
from src.ez_lan_manager.services.UserService import UserService
|
|
||||||
from src.ez_lan_manager.types.Transaction import Transaction
|
from src.ez_lan_manager.types.Transaction import Transaction
|
||||||
|
|
||||||
logger = logging.getLogger(__name__.split(".")[-1])
|
logger = logging.getLogger(__name__.split(".")[-1])
|
||||||
@ -11,9 +10,8 @@ class InsufficientFundsError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class AccountingService:
|
class AccountingService:
|
||||||
def __init__(self, db_service: DatabaseService, user_service: UserService) -> None:
|
def __init__(self, db_service: DatabaseService) -> None:
|
||||||
self._db_service = db_service
|
self._db_service = db_service
|
||||||
self._user_service = user_service
|
|
||||||
|
|
||||||
def add_balance(self, user_id: int, balance_to_add: int, reference: str) -> int:
|
def add_balance(self, user_id: int, balance_to_add: int, reference: str) -> int:
|
||||||
self._db_service.add_transaction(Transaction(
|
self._db_service.add_transaction(Transaction(
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
from datetime import date
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import mariadb
|
import mariadb
|
||||||
from mariadb import Cursor
|
from mariadb import Cursor
|
||||||
|
|
||||||
from src.ez_lan_manager.types.ConfigurationTypes import DatabaseConfiguration
|
from src.ez_lan_manager.types.ConfigurationTypes import DatabaseConfiguration
|
||||||
|
from src.ez_lan_manager.types.News import News
|
||||||
from src.ez_lan_manager.types.Transaction import Transaction
|
from src.ez_lan_manager.types.Transaction import Transaction
|
||||||
from src.ez_lan_manager.types.User import User
|
from src.ez_lan_manager.types.User import User
|
||||||
|
|
||||||
@ -142,3 +144,37 @@ class DatabaseService:
|
|||||||
reference=transaction_raw[5]
|
reference=transaction_raw[5]
|
||||||
))
|
))
|
||||||
return transactions
|
return transactions
|
||||||
|
|
||||||
|
def add_news(self, news: News) -> None:
|
||||||
|
cursor = self._get_cursor()
|
||||||
|
try:
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO news (news_content, news_title, news_subtitle, news_author, news_date) "
|
||||||
|
"VALUES (?, ?, ?, ?, ?)",
|
||||||
|
(news.content, news.title, news.subtitle, news.author.user_id, news.news_date)
|
||||||
|
)
|
||||||
|
self._connection.commit()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Error adding Transaction: {e}")
|
||||||
|
|
||||||
|
def get_news(self, dt_start: date, dt_end: date) -> list[News]:
|
||||||
|
results = []
|
||||||
|
cursor = self._get_cursor()
|
||||||
|
try:
|
||||||
|
cursor.execute("SELECT * FROM news INNER JOIN users ON news.news_author = users.user_id WHERE news_date BETWEEN ? AND ?;", (dt_start, dt_end))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Error fetching news: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
for news_raw in cursor.fetchall():
|
||||||
|
user = self._map_db_result_to_user(news_raw[6:])
|
||||||
|
results.append(News(
|
||||||
|
news_id=news_raw[0],
|
||||||
|
title=news_raw[2],
|
||||||
|
subtitle=news_raw[3],
|
||||||
|
author=user,
|
||||||
|
content=news_raw[1],
|
||||||
|
news_date=news_raw[5]
|
||||||
|
))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|||||||
31
src/ez_lan_manager/services/NewsService.py
Normal file
31
src/ez_lan_manager/services/NewsService.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import logging
|
||||||
|
from datetime import date, datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from src.ez_lan_manager.services.DatabaseService import DatabaseService
|
||||||
|
from src.ez_lan_manager.types.News import News
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__.split(".")[-1])
|
||||||
|
|
||||||
|
class NewsService:
|
||||||
|
def __init__(self, db_service: DatabaseService) -> None:
|
||||||
|
self._db_service = db_service
|
||||||
|
|
||||||
|
def add_news(self, news: News) -> None:
|
||||||
|
if news.news_id is not None:
|
||||||
|
logger.warning("Can not add news with ID, ignoring...")
|
||||||
|
return
|
||||||
|
self._db_service.add_news(news)
|
||||||
|
|
||||||
|
def get_news(self, dt_start: Optional[date] = None, dt_end: Optional[date] = None) -> list[News]:
|
||||||
|
if not dt_end:
|
||||||
|
dt_end = date.today()
|
||||||
|
if not dt_start:
|
||||||
|
dt_start = date(1900, 1, 1)
|
||||||
|
return self._db_service.get_news(dt_start, dt_end)
|
||||||
|
|
||||||
|
def get_latest_news(self) -> Optional[News]:
|
||||||
|
try:
|
||||||
|
return self.get_news(None, date.today())[0]
|
||||||
|
except IndexError:
|
||||||
|
logger.debug("There are no news to fetch")
|
||||||
15
src/ez_lan_manager/types/News.py
Normal file
15
src/ez_lan_manager/types/News.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import date
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from src.ez_lan_manager.types.User import User
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class News:
|
||||||
|
news_id: Optional[int]
|
||||||
|
title: str
|
||||||
|
subtitle: str
|
||||||
|
content: str
|
||||||
|
author: User
|
||||||
|
news_date: date
|
||||||
Loading…
Reference in New Issue
Block a user