From 99b3cc2f5f1ae34988ea85805a50ba1508fe99fe Mon Sep 17 00:00:00 2001 From: Natasa Kasikovic~ Date: Sat, 30 Mar 2024 01:44:19 +0100 Subject: [PATCH] [Add] Implementation of Update and Create methods to TutorDAO class --- LangLang/Core/Model/DAO/TutorDAO.cs | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 LangLang/Core/Model/DAO/TutorDAO.cs diff --git a/LangLang/Core/Model/DAO/TutorDAO.cs b/LangLang/Core/Model/DAO/TutorDAO.cs new file mode 100644 index 00000000..df4a3438 --- /dev/null +++ b/LangLang/Core/Model/DAO/TutorDAO.cs @@ -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 _tutors; + private readonly Repository _repository; + + public TutorDAO() + { + _repository = new Repository("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 + + } +} +