Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Simply a customs agent

![CI](https://github.com/BaboucheOne/BuggyBot/actions/workflows/python-formatter.yml/badge.svg)
![Tests](https://github.com/BaboucheOne/BuggyBot/actions/workflows/pytest.yml/badge.svg)
![DockerBuild](https://github.com/BaboucheOne/BuggyBot/actions/workflows/docker-build.yml/badge.svg)
![DockerDeployment](https://github.com/BaboucheOne/BuggyBot/actions/workflows/build-push-docker-image.yml/badge.svg)

Expand Down
12 changes: 6 additions & 6 deletions bot/application/student/student_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ async def add_student(self, add_student_request: AddStudentRequest):
add_student_request.program_code,
)

if self.__does_student_exists(student.ni) or self.__does_student_registered(
student.ni
):
if self.__does_student_registered(student.ni):
raise StudentAlreadyRegisteredException(ni=student.ni)

if self.__does_student_exists(student.ni):
raise StudentAlreadyExistsException(ni=student.ni)

self.__student_repository.add_student(student)
Expand Down Expand Up @@ -196,9 +197,6 @@ async def unregister_student(
method="unregister_student",
)

if not Utility.does_user_exist_on_server(unregister_student_request.discord_id):
raise UserNotInServerException(unregister_student_request.discord_id)

discord_user_id = DiscordUserId(unregister_student_request.discord_id)

if not self.__does_discord_user_id_already_registered_an_account(
Expand All @@ -213,6 +211,7 @@ async def unregister_student(
self.__student_repository.unregister_student(
student.ni, DiscordUserId(DiscordUserId.INVALID_DISCORD_ID)
)

await self.notify_on_student_unregistered(student.discord_user_id)

async def force_unregister_student(
Expand All @@ -233,6 +232,7 @@ async def force_unregister_student(
self.__student_repository.unregister_student(
student_ni, DiscordUserId(DiscordUserId.INVALID_DISCORD_ID)
)

await self.notify_on_student_unregistered(student.discord_user_id)

async def remove_member(self, member: Member):
Expand Down
101 changes: 101 additions & 0 deletions bot/infra/student/in_memory_student_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import copy
from typing import List

from bot.config.logger.logger import Logger
from bot.config.service_locator import ServiceLocator
from bot.domain.student.attribut.discord_user_id import DiscordUserId
from bot.domain.student.attribut.ni import NI
from bot.domain.student.student import Student
from bot.domain.student.student_repository import StudentRepository
from bot.infra.student.assembler.student_assembler import StudentAssembler
from bot.infra.student.exception.cannot_add_student_exception import (
CannotAddStudentException,
)
from bot.infra.student.exception.cannot_unregister_student_exception import (
CannotUnregisterStudentException,
)
from bot.infra.student.exception.cannot_update_student_exception import (
CannotUpdateStudentException,
)
from bot.infra.student.exception.student_not_found_exception import (
StudentNotFoundException,
)
from bot.infra.student.exception.cannot_register_student_exception import (
CannotRegisterStudentException,
)


class InMemoryStudentRepository(StudentRepository):

def __init__(self, student_collection: List[Student]):
self.__student_collection: List[Student] = student_collection
self.__student_assembler: StudentAssembler = StudentAssembler()
self.__logger: Logger = ServiceLocator.get_dependency(Logger)

def find_student_by_discord_user_id(
self, discord_user_id: DiscordUserId
) -> Student:
for student in self.__student_collection:
if student.discord_user_id.value == discord_user_id.value:
return copy.deepcopy(student)
raise StudentNotFoundException(discord_id=discord_user_id)

def find_students_by_discord_user_id(
self, discord_user_id: DiscordUserId
) -> List[Student]:
found_students = [
copy.deepcopy(student)
for student in self.__student_collection
if student.discord_user_id.value == discord_user_id.value
]
return found_students

def find_student_by_ni(self, ni: NI) -> Student:
for student in self.__student_collection:
if student.ni.value == ni.value:
return copy.deepcopy(student)
raise StudentNotFoundException(ni=ni)

def add_student(self, student: Student):
if any(s.ni.value == student.ni.value for s in self.__student_collection):
raise CannotAddStudentException(student)
self.__student_collection.append(student)
self.__logger.info(
f"L'étudiant {repr(student)} a bien été ajouté à la base de données.",
method="add_student",
)

def update_student(self, student: Student):
for idx, existing_student in enumerate(self.__student_collection):
if existing_student.ni.value == student.ni.value:
self.__student_collection[idx] = student
self.__logger.info(
f"{repr(student)} a bien été mis à jour dans la base de données.",
method="update_student",
)
return
raise CannotUpdateStudentException(student)

def register_student(self, ni: NI, discord_user_id: DiscordUserId):
for student in self.__student_collection:
if student.ni.value == ni.value:
student.discord_user_id = discord_user_id
self.__logger.info(
f"L'étudiant {repr(ni)} {repr(discord_user_id)} a bien été enregistré.",
method="register_student",
)
return
raise CannotRegisterStudentException(ni)

def unregister_student(self, ni: NI, discord_user_id: DiscordUserId):
for student in self.__student_collection:
if student.ni.value == ni.value:
student.discord_user_id = DiscordUserId(
DiscordUserId.INVALID_DISCORD_ID
)
self.__logger.info(
f"L'étudiant {repr(ni)} {repr(discord_user_id)} en cache a bien été désenregistré.",
method="unregister_student",
)
return
raise CannotUnregisterStudentException(ni)
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
black==24.4.2
ruff==0.4.10
pytest==8.3.5
pytest-asyncio==0.26.0
Loading
Loading