Skip to content

Commit

Permalink
Merge pull request #218 from kzi-nastava/feat/withdrawalrequest-class
Browse files Browse the repository at this point in the history
[feat] Implement WithdrawalRequest functionality
  • Loading branch information
natasakasikovic authored Apr 29, 2024
2 parents 497ff30 + 5986dfc commit 9672364
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 6 deletions.
14 changes: 8 additions & 6 deletions LangLang/Core/Controller/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
44 changes: 44 additions & 0 deletions LangLang/Core/Controller/WithdrawalRequestController.cs
Original file line number Diff line number Diff line change
@@ -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<WithdrawalRequest> 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<WithdrawalRequest> GetStudentRequests(int studentId, EnrollmentRequestController erController)
{
List<EnrollmentRequest> allEnrollmentRequests = erController.GetAll();
return _withdrawalRequests.GetStudentRequests(studentId, allEnrollmentRequests);
}
}
}
72 changes: 72 additions & 0 deletions LangLang/Core/Model/DAO/WithdrawalRequestDAO.cs
Original file line number Diff line number Diff line change
@@ -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<int, WithdrawalRequest> _withdrawalRequests;
private readonly Repository<WithdrawalRequest> _repository;

public WithdrawalRequestDAO()
{
_repository = new Repository<WithdrawalRequest>("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<WithdrawalRequest> 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<WithdrawalRequest> GetStudentRequests(int studentId, List<EnrollmentRequest> allEnrollmentRequests)
{
List<WithdrawalRequest> studentRequests = new();
foreach (WithdrawalRequest request in GetAllWithdrawalRequests())
{
EnrollmentRequest enrollmentRequest = allEnrollmentRequests[request.EnrollmentRequestId];
if (enrollmentRequest.StudentId == studentId)
{
studentRequests.Add(request);
}
}
return studentRequests;
}

}
}
65 changes: 65 additions & 0 deletions LangLang/Core/Model/WithdrawalRequest.cs
Original file line number Diff line number Diff line change
@@ -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() {}

Check warning on line 18 in LangLang/Core/Model/WithdrawalRequest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Reason' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
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"),
};
}
}
}

0 comments on commit 9672364

Please sign in to comment.