generated from kzi-nastava/dotnet-wpf-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from kzi-nastava/feat/withdrawalrequest-class
[feat] Implement WithdrawalRequest functionality
- Loading branch information
Showing
4 changed files
with
189 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
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"), | ||
}; | ||
} | ||
} | ||
} |