Skip to content

Commit

Permalink
Merge pull request #45 from kzi-nastava/feat/TutorDAO-Create-Update
Browse files Browse the repository at this point in the history
[Add] Implementation of Update and Create methods to TutorDAO class
  • Loading branch information
anasinik authored Mar 30, 2024
2 parents 6b79292 + 99b3cc2 commit 0f97c34
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 0f97c34

Please sign in to comment.