From dbc14740d92e383ed9fc01c09297ef7db002744c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=20=C5=A0inik?= Date: Sat, 27 Apr 2024 18:43:06 +0200 Subject: [PATCH] [refactor] Replace getters and setters with properties and perform some renaming --- LangLang/Core/Controller/LoginController.cs | 16 +-- LangLang/Core/Controller/StudentController.cs | 2 +- .../Core/Model/DAO/EnrollmentRequestDAO.cs | 2 +- LangLang/Core/Model/DAO/StudentDAO.cs | 4 +- LangLang/Core/Model/DAO/TutorDAO.cs | 2 +- LangLang/Core/Model/EnrollmentRequest.cs | 76 ++++-------- .../Model/Enums/{ERStatus.cs => Status.cs} | 2 +- LangLang/Core/Model/Profile.cs | 108 ++++-------------- LangLang/Core/Model/Student.cs | 35 ++---- LangLang/DTO/StudentDTO.cs | 4 +- LangLang/DTO/TutorDTO.cs | 2 +- LangLang/Data/enrollmentRequests.csv | 12 +- 12 files changed, 76 insertions(+), 189 deletions(-) rename LangLang/Core/Model/Enums/{ERStatus.cs => Status.cs} (81%) diff --git a/LangLang/Core/Controller/LoginController.cs b/LangLang/Core/Controller/LoginController.cs index 0ec39375..3ed6b426 100644 --- a/LangLang/Core/Controller/LoginController.cs +++ b/LangLang/Core/Controller/LoginController.cs @@ -9,8 +9,8 @@ namespace LangLang.Core.Controller public class LoginController { - StudentController studentController; - TutorController tutorController; + readonly StudentController studentController; + readonly TutorController tutorController; // TODO: add director public LoginController(StudentController studentController, TutorController tutorController) { @@ -22,13 +22,9 @@ public Profile GetProfileByCredentials(string email, string password) { try { - var profile = GetProfile(studentController.GetAllStudents(), email, password) - ?? GetProfile(tutorController.GetAllTutors(), email, password); - - if (profile == null) - { - throw new AuthenticationException("Invalid email address"); // there is no profile with given email - } + var profile = (GetProfile(studentController.GetAllStudents(), email, password) + ?? GetProfile(tutorController.GetAllTutors(), email, password)) + ?? throw new AuthenticationException("Invalid email address"); return profile; // profile with the given credentials exists } catch (AuthenticationException ex) @@ -53,7 +49,7 @@ public Profile GetProfileByCredentials(string email, string password) return user.Profile; } - + } } \ No newline at end of file diff --git a/LangLang/Core/Controller/StudentController.cs b/LangLang/Core/Controller/StudentController.cs index 23e08b23..d4f550ac 100644 --- a/LangLang/Core/Controller/StudentController.cs +++ b/LangLang/Core/Controller/StudentController.cs @@ -59,7 +59,7 @@ private bool HasStudentAttendedCourse(Student student, Course course, Enrollment if (enrollmentRequest.StudentId == student.Id && enrollmentRequest.CourseId == course.Id) { - if (enrollmentRequest.ERStatus == ERStatus.Accepted) + if (enrollmentRequest.Status == Status.Accepted) { return true; } diff --git a/LangLang/Core/Model/DAO/EnrollmentRequestDAO.cs b/LangLang/Core/Model/DAO/EnrollmentRequestDAO.cs index 7b19708c..cffd0535 100644 --- a/LangLang/Core/Model/DAO/EnrollmentRequestDAO.cs +++ b/LangLang/Core/Model/DAO/EnrollmentRequestDAO.cs @@ -44,7 +44,7 @@ public EnrollmentRequest Add(EnrollmentRequest enrollmentRequest) EnrollmentRequest oldRequest = GetEnrollmentRequestById(enrollmentRequest.Id); if (oldRequest == null) return null; - oldRequest.ERStatus = enrollmentRequest.ERStatus; + oldRequest.Status = enrollmentRequest.Status; _repository.Save(_enrollmentRequests); NotifyObservers(); diff --git a/LangLang/Core/Model/DAO/StudentDAO.cs b/LangLang/Core/Model/DAO/StudentDAO.cs index 67c4c129..3ec51f75 100644 --- a/LangLang/Core/Model/DAO/StudentDAO.cs +++ b/LangLang/Core/Model/DAO/StudentDAO.cs @@ -57,12 +57,12 @@ public Student AddStudent(Student student) oldStudent.Profile.Name = student.Profile.Name; oldStudent.Profile.LastName = student.Profile.LastName; oldStudent.Profile.Gender = student.Profile.Gender; - oldStudent.Profile.DateOfBirth = student.Profile.DateOfBirth; + oldStudent.Profile.BirthDate = student.Profile.BirthDate; oldStudent.Profile.PhoneNumber = student.Profile.PhoneNumber; oldStudent.Profile.Email = student.Profile.Email; oldStudent.Profile.Role = student.Profile.Role; oldStudent.Profile.Password = student.Profile.Password; - oldStudent.ProfessionalQualification = student.ProfessionalQualification; + oldStudent.Profession = student.Profession; _repository.Save(_students); NotifyObservers(); diff --git a/LangLang/Core/Model/DAO/TutorDAO.cs b/LangLang/Core/Model/DAO/TutorDAO.cs index ac8b4870..773c0dc3 100644 --- a/LangLang/Core/Model/DAO/TutorDAO.cs +++ b/LangLang/Core/Model/DAO/TutorDAO.cs @@ -58,7 +58,7 @@ public Dictionary Search(TutorController tutorController, DateTime d oldTutor.Profile.Name = tutor.Profile.Name; oldTutor.Profile.LastName = tutor.Profile.LastName; oldTutor.Profile.Gender = tutor.Profile.Gender; - oldTutor.Profile.DateOfBirth = tutor.Profile.DateOfBirth; + oldTutor.Profile.BirthDate = tutor.Profile.BirthDate; oldTutor.Profile.PhoneNumber = tutor.Profile.PhoneNumber; oldTutor.Profile.Email = tutor.Profile.Email; oldTutor.Profile.Password = tutor.Profile.Password; diff --git a/LangLang/Core/Model/EnrollmentRequest.cs b/LangLang/Core/Model/EnrollmentRequest.cs index d8985eeb..bb80c700 100644 --- a/LangLang/Core/Model/EnrollmentRequest.cs +++ b/LangLang/Core/Model/EnrollmentRequest.cs @@ -6,76 +6,50 @@ namespace LangLang.Core.Model { public class EnrollmentRequest : ISerializable { - private int _id; - private int _studentId; - private int _courseId; - private ERStatus _erStatus; - private DateTime _requestSentAt; + public int Id { get; set; } + public int StudentId { get; set; } + public int CourseId { get; set; } + public Status Status { get; set; } + public DateTime RequestSentAt { get; set; } + public DateTime LastModifiedTimestamp { get; set; } - public int Id - { - get { return _id; } - set { _id = value; } - } - - public int StudentId - { - get { return _studentId; } - set { _studentId = value; } - } - - public int CourseId - { - get { return _courseId; } - set { _courseId = value; } - } + public EnrollmentRequest() { } - public DateTime RequestSentAt - { - get { return _requestSentAt; } - set { _requestSentAt = value; } - } - - public ERStatus ERStatus - { - get { return _erStatus; } - set { _erStatus = value; } - } - - public EnrollmentRequest() {} - - public EnrollmentRequest(int id, int studentId, int courseId, ERStatus erStatus, DateTime requestSentAt) + public EnrollmentRequest(int id, int studentId, int courseId, Status erStatus, DateTime requestSentAt) { Id = id; StudentId = studentId; CourseId = courseId; - ERStatus = erStatus; + Status = erStatus; RequestSentAt = requestSentAt; } public void FromCSV(string[] values) { - if (!int.TryParse(values[0], out _id) - || !int.TryParse(values[1], out _studentId) - || !int.TryParse(values[2], out _courseId) - || !Enum.TryParse(values[3], out _erStatus) - || !DateTime.TryParseExact(values[4], "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out _requestSentAt)) - { - throw new FormatException("Error during parsing while reading from file"); - } + try { + RequestSentAt = DateTime.ParseExact(values[4], "yyyy-MM-dd", null); + LastModifiedTimestamp = DateTime.ParseExact(values[5], "yyyy-MM-dd", null); + } + catch { + throw new FormatException("Date is not in the correct format."); + } + + Id = int.Parse(values[0]); + StudentId = int.Parse(values[1]); + CourseId = int.Parse(values[2]); + Status = (Status)Enum.Parse(typeof(Status), values[3]); } public string[] ToCSV() { - string[] values = - { + return new string[] { Id.ToString(), StudentId.ToString(), CourseId.ToString(), - ERStatus.ToString(), - RequestSentAt.ToString("yyyy-MM-dd") + Status.ToString(), + RequestSentAt.ToString("yyyy-MM-dd"), + LastModifiedTimestamp.ToString("yyyy-MM-dd") }; - return values; } } } diff --git a/LangLang/Core/Model/Enums/ERStatus.cs b/LangLang/Core/Model/Enums/Status.cs similarity index 81% rename from LangLang/Core/Model/Enums/ERStatus.cs rename to LangLang/Core/Model/Enums/Status.cs index d1915f6c..d511cbc6 100644 --- a/LangLang/Core/Model/Enums/ERStatus.cs +++ b/LangLang/Core/Model/Enums/Status.cs @@ -1,7 +1,7 @@  namespace LangLang.Core.Model.Enums { - public enum ERStatus + public enum Status { Pending, Accepted, diff --git a/LangLang/Core/Model/Profile.cs b/LangLang/Core/Model/Profile.cs index a67cd5e4..9d280512 100644 --- a/LangLang/Core/Model/Profile.cs +++ b/LangLang/Core/Model/Profile.cs @@ -9,83 +9,25 @@ public interface IProfileHolder Profile Profile { get; } } - public class Profile { - // Attributes - private int _id; - private string _name; - private string _lastName; - private UserGender _gender; - private DateTime _dateOfBirth; - private string _phoneNumber; - private string _email; - private string _password; - private UserType _role; - - // Properties - public int Id - { - get { return _id; } - set { _id = value; } - } - - public string Name - { - get { return _name; } - set { _name = value; } - } - - public string LastName - { - get { return _lastName; } - set { _lastName = value; } - } - - public UserGender Gender - { - get { return _gender; } - set { _gender = value; } - } - - public DateTime DateOfBirth - { - get { return _dateOfBirth; } - set { _dateOfBirth = value; } - } - - public string PhoneNumber - { - get { return _phoneNumber; } - set { _phoneNumber = value; } - } - - public string Email - { - get { return _email; } - set { _email = value; } - } - - public string Password - { - get { return _password; } - set { _password = value; } - } - - public UserType Role - { - get { return _role; } - set { _role = value; } - } - - // Constructor - public Profile(int id, string name, string lastName, UserGender gender, DateTime dateOfBirth, string phoneNumber, string email, string password, UserType role) + public int Id { get; set; } + public string Name { get; set; } + public string LastName { get; set; } + public UserGender Gender { get; set; } + public DateTime BirthDate { get; set; } + public string PhoneNumber { get; set; } + public string Email { get; set; } + public string Password { get; set; } + public UserType Role { get; set; } + + public Profile(int id, string name, string lastName, UserGender gender, DateTime birthDate, string phoneNumber, string email, string password, UserType role) { Id = id; Name = name; LastName = lastName; Gender = gender; - DateOfBirth = dateOfBirth; + BirthDate = birthDate; PhoneNumber = phoneNumber; Email = email; Password = password; @@ -95,25 +37,19 @@ public Profile(int id, string name, string lastName, UserGender gender, DateTime public Profile() { } /// Constructor for initializing after parsing data loaded from file. - /// Thrown when one or more tokens (date, role or gender) are not in the correct format. + /// Thrown when date is not in the correct format. public Profile(string id, string name, string lastName, string gender, string dateOfBirth, string phoneNumber, string email, string password, string role) { - try - { - Id = int.Parse(id); - } - catch (FormatException ex) - { - throw new FormatException("Unable to convert the Id to integer. ", ex); - } - - if (!Enum.TryParse(gender, out _gender) - || !Enum.TryParse(role, out _role) - || !DateTime.TryParseExact(dateOfBirth, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out _dateOfBirth)) - { - throw new FormatException("One or more tokens are not in the correct format."); + try { + BirthDate = DateTime.ParseExact(dateOfBirth, "yyyy-MM-dd", null); + } + catch { + throw new FormatException("Date is not in the correct format."); } + Id = int.Parse(id); + Gender = (UserGender)Enum.Parse(typeof(UserGender), gender); + Role = (UserType)Enum.Parse(typeof(UserType), role); Name = name; LastName = lastName; PhoneNumber = phoneNumber; @@ -123,7 +59,7 @@ public Profile(string id, string name, string lastName, string gender, string da public override string ToString() { - return string.Join("|", new object[] { Id, Name, LastName, Gender, DateOfBirth.ToString("yyyy-MM-dd"), PhoneNumber, Email, Password, Role }); + return string.Join("|", new object[] { Id, Name, LastName, Gender, BirthDate.ToString("yyyy-MM-dd"), PhoneNumber, Email, Password, Role }); } } diff --git a/LangLang/Core/Model/Student.cs b/LangLang/Core/Model/Student.cs index 6fa1a92c..809207a9 100644 --- a/LangLang/Core/Model/Student.cs +++ b/LangLang/Core/Model/Student.cs @@ -7,49 +7,30 @@ namespace LangLang.Core.Model public class Student : ISerializable, IProfileHolder { - private Profile _profile; - private string _professionalQualification; - - public Profile Profile - { - get { return _profile; } - set { _profile = value; } - } - - public string ProfessionalQualification - { - get { return _professionalQualification; } - set { _professionalQualification = value; } - } - - public int Id - { - get { return Profile.Id; } - } + public Profile Profile { get; set; } + public string Profession { get; set; } + public int Id => Profile.Id; public Student() { } - public Student(int id, string name, string lastName, UserGender gender, DateTime dateOfBirth, string phoneNumber, string email, string password, UserType role, string professionalQualification) + public Student(int id, string name, string lastName, UserGender gender, DateTime dateOfBirth, string phoneNumber, string email, string password, UserType role, string profession) { Profile = new Profile(id, name, lastName, gender, dateOfBirth, phoneNumber, email, password, role); - ProfessionalQualification = professionalQualification; + Profession = profession; } public void FromCSV(string[] values) { Profile = new Profile(values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8]); - ProfessionalQualification = values[9]; + Profession = values[9]; } public string[] ToCSV() { - string[] csvValues = - { + return new string[] { Profile.ToString(), - ProfessionalQualification + Profession }; - - return csvValues; } } } diff --git a/LangLang/DTO/StudentDTO.cs b/LangLang/DTO/StudentDTO.cs index 9c7796c2..3bf8b820 100644 --- a/LangLang/DTO/StudentDTO.cs +++ b/LangLang/DTO/StudentDTO.cs @@ -206,9 +206,9 @@ public StudentDTO(Student student) gender = student.Profile.Gender; email = student.Profile.Email; password = student.Profile.Password; - birthDate = student.Profile.DateOfBirth; + birthDate = student.Profile.BirthDate; phoneNumber = student.Profile.PhoneNumber; - profession = student.ProfessionalQualification; + profession = student.Profession; } public string Error => throw new NotImplementedException(); diff --git a/LangLang/DTO/TutorDTO.cs b/LangLang/DTO/TutorDTO.cs index 71feca72..79f2dc73 100644 --- a/LangLang/DTO/TutorDTO.cs +++ b/LangLang/DTO/TutorDTO.cs @@ -319,7 +319,7 @@ public TutorDTO(Tutor tutor) Name = tutor.Profile.Name; LastName = tutor.Profile.LastName; Gender = tutor.Profile.Gender; - BirthDate = tutor.Profile.DateOfBirth; + BirthDate = tutor.Profile.BirthDate; PhoneNumber = tutor.Profile.PhoneNumber; Email = tutor.Profile.Email; Password = tutor.Profile.Password; diff --git a/LangLang/Data/enrollmentRequests.csv b/LangLang/Data/enrollmentRequests.csv index 2425818e..7ada731c 100644 --- a/LangLang/Data/enrollmentRequests.csv +++ b/LangLang/Data/enrollmentRequests.csv @@ -1,6 +1,6 @@ -0|0|2|Accepted|2024-03-20 -1|2|0|Accepted|2024-03-10 -2|0|1|Accepted|2024-02-02 -3|1|2|Accepted|2024-02-05 -4|0|0|Accepted|2024-07-07 -5|0|3|Accepted|2024-08-07 \ No newline at end of file +0|0|2|Accepted|2024-03-20|2024-03-20 +1|2|0|Accepted|2024-03-10|2024-03-10 +2|0|1|Accepted|2024-02-02|2024-02-02 +3|1|2|Accepted|2024-02-05|2024-02-05 +4|0|0|Accepted|2024-07-07|2024-07-07 +5|0|3|Accepted|2024-08-07|2024-08-07 \ No newline at end of file