generated from kzi-nastava/dotnet-wpf-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add] Implementation of Update and Create methods to TutorDAO class
- Loading branch information
1 parent
1f4d725
commit 99b3cc2
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} | ||
|