Skip to content

Commit

Permalink
Merge pull request #192 from kzi-nastava/refactor/properties
Browse files Browse the repository at this point in the history
[refactor] Replace getters and setters with properties and perform some renaming
  • Loading branch information
anasinik authored Apr 28, 2024
2 parents 9ca1f91 + dbc1474 commit 33ddd50
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 189 deletions.
16 changes: 6 additions & 10 deletions LangLang/Core/Controller/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand All @@ -53,7 +49,7 @@ public Profile GetProfileByCredentials(string email, string password)

return user.Profile;
}

}

}
2 changes: 1 addition & 1 deletion LangLang/Core/Controller/StudentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion LangLang/Core/Model/DAO/EnrollmentRequestDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions LangLang/Core/Model/DAO/StudentDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion LangLang/Core/Model/DAO/TutorDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Dictionary<int, Tutor> 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;
Expand Down
76 changes: 25 additions & 51 deletions LangLang/Core/Model/EnrollmentRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace LangLang.Core.Model.Enums
{
public enum ERStatus
public enum Status
{
Pending,
Accepted,
Expand Down
108 changes: 22 additions & 86 deletions LangLang/Core/Model/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
/// <exception cref="FormatException">Thrown when one or more tokens (date, role or gender) are not in the correct format.</exception>
/// <exception cref="FormatException">Thrown when date is not in the correct format.</exception>
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;
Expand All @@ -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 });
}

}
Expand Down
35 changes: 8 additions & 27 deletions LangLang/Core/Model/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Loading

0 comments on commit 33ddd50

Please sign in to comment.