Skip to content

Commit

Permalink
Merge pull request #62 from kzi-nastava/feat/StudentDAO-fix
Browse files Browse the repository at this point in the history
Feat/student dao fix
  • Loading branch information
natasakasikovic authored Mar 30, 2024
2 parents 6312e0c + 0c43b5d commit 32d9998
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
40 changes: 30 additions & 10 deletions LangLang/Core/Model/DAO/StudentDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ namespace LangLang.Core.Model.DAO
**/
public class StudentDAO : Subject
{
private readonly List<Student> _students;
private readonly Dictionary<int, Student> _students;
private readonly Repository<Student> _repository;


public StudentDAO()
{
_repository = new Repository<Student>("student.csv");
_repository = new Repository<Student>("students.csv");
_students = _repository.Load();
}

private int GenerateId()
{
if (_students.Count == 0) return 0;
return _students.Last().Profile.Id + 1;
return _students.Count + 1;
}

public Student? GetStudentById(int id)
{
return _students[id];
}

public Dictionary<int, Student> GetAllStudents()
{
return _students;
}

public Student AddStudent(Student student)
{
student.Profile.Id = GenerateId();
_students.Add(student);
_students.Add(student.Profile.Id, student);
_repository.Save(_students);
NotifyObservers();
return student;
Expand All @@ -42,7 +52,14 @@ public Student AddStudent(Student student)
Student oldStudent = GetStudentById(student.Profile.Id);
if (oldStudent == null) return null;

oldStudent.Profile = student.Profile;
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.PhoneNumber = student.Profile.PhoneNumber;
oldStudent.Profile.Email = student.Profile.Email;
oldStudent.Profile.Role = student.Profile.Role;
oldStudent.Profile.Password = student.Profile.Password;
oldStudent.CanModifyInfo = student.CanModifyInfo;
oldStudent.ProfessionalQualification = student.ProfessionalQualification;

Expand All @@ -51,12 +68,15 @@ public Student AddStudent(Student student)
return oldStudent;
}

public Student GetStudentById(int id)
public Student? RemoveStudent(int id)
{
return _students.Find(s => s.Profile.Id == id);
}
Student student = GetStudentById(id);
if (student == null) return null;

// TODO: implement RemoveStudent(int id), GetAllStudent()
_students.Remove(student.Profile.Id);
_repository.Save(_students);
NotifyObservers();
return student;
}
}

}
6 changes: 6 additions & 0 deletions LangLang/Core/Model/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public bool CanModifyInfo
get { return _canModifyInfo; }
set { _canModifyInfo = value; }
}

public int Id
{
get { return Profile.Id; }
}

public Student() { }

Check warning on line 36 in LangLang/Core/Model/Student.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_profile' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 36 in LangLang/Core/Model/Student.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_professionalQualification' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

public Student(int id, string name, string lastName, UserGender gender, DateTime dateOfBirth, string phoneNumber, string email, string password, UserType role, string professionalQualification)

Check warning on line 38 in LangLang/Core/Model/Student.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_profile' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
Expand Down

0 comments on commit 32d9998

Please sign in to comment.