diff --git a/LangLang/Core/Controller/AppController.cs b/LangLang/Core/Controller/AppController.cs index b2c66bd2..621be0aa 100644 --- a/LangLang/Core/Controller/AppController.cs +++ b/LangLang/Core/Controller/AppController.cs @@ -8,16 +8,18 @@ public class AppController public readonly CourseController CourseController; public readonly StudentController StudentController; public readonly EnrollmentRequestController EnrollmentRequestController; + public readonly WithdrawalRequestController WithdrawalRequestController; public readonly ExamSlotController ExamSlotController; public readonly LoginController LoginController; public AppController() { - this.TutorController = new TutorController(); - this.CourseController = new CourseController(); - this.StudentController = new StudentController(); - this.EnrollmentRequestController = new EnrollmentRequestController(); - this.ExamSlotController = new ExamSlotController(); - this.LoginController = new LoginController(StudentController, TutorController); + TutorController = new(); + CourseController = new(); + StudentController = new(); + EnrollmentRequestController = new(); + WithdrawalRequestController = new(); + ExamSlotController = new(); + LoginController = new(StudentController, TutorController); } diff --git a/LangLang/Core/Controller/WithdrawalRequestController.cs b/LangLang/Core/Controller/WithdrawalRequestController.cs new file mode 100644 index 00000000..d0caf912 --- /dev/null +++ b/LangLang/Core/Controller/WithdrawalRequestController.cs @@ -0,0 +1,44 @@ + +using LangLang.Core.Model; +using LangLang.Core.Model.DAO; +using LangLang.Core.Observer; +using System.Collections.Generic; + +namespace LangLang.Core.Controller +{ + public class WithdrawalRequestController + { + private readonly WithdrawalRequestDAO _withdrawalRequests; + + public WithdrawalRequestController() + { + _withdrawalRequests = new(); + } + + public List GetAll() + { + return _withdrawalRequests.GetAllWithdrawalRequests(); + } + + public void Add(WithdrawalRequest withdrawalRequest) + { + _withdrawalRequests.Add(withdrawalRequest); + } + + public void Delete(int id) + { + _withdrawalRequests.Remove(id); + } + + public void Subscribe(IObserver observer) + { + _withdrawalRequests.Subscribe(observer); + } + + public List GetStudentRequests(int studentId, EnrollmentRequestController erController) + { + List allEnrollmentRequests = erController.GetAll(); + return _withdrawalRequests.GetStudentRequests(studentId, allEnrollmentRequests); + } + } +} diff --git a/LangLang/Core/Model/DAO/WithdrawalRequestDAO.cs b/LangLang/Core/Model/DAO/WithdrawalRequestDAO.cs new file mode 100644 index 00000000..41863268 --- /dev/null +++ b/LangLang/Core/Model/DAO/WithdrawalRequestDAO.cs @@ -0,0 +1,72 @@ + +using LangLang.Core.Observer; +using LangLang.Core.Repository; +using System.Collections.Generic; +using System.Linq; + + +namespace LangLang.Core.Model.DAO +{ + public class WithdrawalRequestDAO : Subject + { + private readonly Dictionary _withdrawalRequests; + private readonly Repository _repository; + + public WithdrawalRequestDAO() + { + _repository = new Repository("withdrawalRequests.csv"); + _withdrawalRequests = _repository.Load(); + } + + private int GenerateId() + { + if (_withdrawalRequests.Count == 0) return 0; + return _withdrawalRequests.Keys.Max() + 1; + } + + public WithdrawalRequest GetById(int id) + { + return _withdrawalRequests[id]; + } + + public List GetAllWithdrawalRequests() + { + return _withdrawalRequests.Values.ToList(); + } + + public WithdrawalRequest Add(WithdrawalRequest request) + { + request.Id= GenerateId(); + _withdrawalRequests.Add(request.Id, request); + _repository.Save(_withdrawalRequests); + NotifyObservers(); + return request; + } + + public WithdrawalRequest? Remove(int id) + { + WithdrawalRequest? request = GetById(id); + if (request == null) return null; + + _withdrawalRequests.Remove(request.Id); + _repository.Save(_withdrawalRequests); + NotifyObservers(); + return request; + } + + public List GetStudentRequests(int studentId, List allEnrollmentRequests) + { + List studentRequests = new(); + foreach (WithdrawalRequest request in GetAllWithdrawalRequests()) + { + EnrollmentRequest enrollmentRequest = allEnrollmentRequests[request.EnrollmentRequestId]; + if (enrollmentRequest.StudentId == studentId) + { + studentRequests.Add(request); + } + } + return studentRequests; + } + + } +} diff --git a/LangLang/Core/Model/WithdrawalRequest.cs b/LangLang/Core/Model/WithdrawalRequest.cs new file mode 100644 index 00000000..aba924a2 --- /dev/null +++ b/LangLang/Core/Model/WithdrawalRequest.cs @@ -0,0 +1,65 @@ + +using LangLang.Core.Model.Enums; +using LangLang.Core.Repository.Serialization; +using System; + +namespace LangLang.Core.Model +{ + // this class represents student's request to withdraw from course + public class WithdrawalRequest : ISerializable + { + public int Id { get; set; } + public int EnrollmentRequestId { get; set; } + public string Reason { get; set; } + public Status Status { get; private set; } + public DateTime RequestSentAt { get; set; } + public DateTime RequestReceivedAt { get; private set;} + + public WithdrawalRequest() {} + public WithdrawalRequest(int id, int enrollmentRequestId, string reason, Status status, DateTime requestSentAt, DateTime requestReceivedAt) + { + Id = id; + EnrollmentRequestId = enrollmentRequestId; + Reason = reason; + Status = status; + RequestSentAt = requestSentAt; + RequestReceivedAt = requestReceivedAt; + } + + public void UpdateStatus(Status status) + { + Status = status; + RequestReceivedAt = DateTime.Now; + } + + public void FromCSV(string[] values) + { + try + { + RequestSentAt = DateTime.ParseExact(values[4], "yyyy-MM-dd", null); + RequestReceivedAt = DateTime.ParseExact(values[5], "yyyy-MM-dd", null); + } + catch + { + throw new FormatException("Date is not in the correct format."); + } + + Id = int.Parse(values[0]); + EnrollmentRequestId = int.Parse(values[1]); + Reason = values[2]; + Status = (Status)Enum.Parse(typeof(Status), values[3]); + } + + public string[] ToCSV() + { + return new string[] { + Id.ToString(), + EnrollmentRequestId.ToString(), + Reason, + Status.ToString(), + RequestSentAt.ToString("yyyy-MM-dd"), + RequestReceivedAt.ToString("yyyy-MM-dd"), + }; + } + } +}