Skip to content

Commit

Permalink
[Add] Implementation of Update and Create methods to TutorDAO class
Browse files Browse the repository at this point in the history
  • Loading branch information
natasakasikovic committed Mar 30, 2024
1 parent 1f4d725 commit 99b3cc2
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions LangLang/Core/Model/DAO/TutorDAO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using LangLang.Core.Model;
using LangLang.Core.Repository;
using LangLang.Core.Observer;
using System.Collections.Generic;


namespace LangLang.Core.DAO
{
public class TutorDAO : Subject
{
private readonly List<Tutor> _tutors;
private readonly Repository<Tutor> _repository;

public TutorDAO()
{
_repository = new Repository<Tutor>("tutors.csv");
_tutors = _repository.Load();
}
private int GenerateId()
{
if (_tutors.Count == 0) return 0;
return _tutors.Count + 1;
}

private Tutor Get(int id)
{
return _tutors.Find(t => t.Profile.Id == id);
}

public Tutor Add(Tutor tutor)
{
tutor.Profile.Id = GenerateId();
_tutors.Add(tutor);
_repository.Save(_tutors);
NotifyObservers();
return tutor;
}

public Tutor Update(Tutor tutor)
{
Tutor oldTutor = Get(tutor.Profile.Id);
if (oldTutor == null) { return null; }

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.PhoneNumber = tutor.Profile.PhoneNumber;
oldTutor.Profile.Email = tutor.Profile.Email;
oldTutor.Profile.Password = tutor.Profile.Password;
oldTutor.Profile.Role = tutor.Profile.Role;

_repository.Save(_tutors);
NotifyObservers();
return oldTutor;
}

// TODO: implement GetAllTutors and Remove method

}
}

0 comments on commit 99b3cc2

Please sign in to comment.