2016-02-14 18:15:56 +01:00
|
|
|
unit DAL;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.JSON;
|
|
|
|
|
|
|
|
type
|
|
|
|
IPeopleDAL = interface
|
|
|
|
['{3E534A3E-EAEB-44ED-B74E-EFBBAAAE11B4}']
|
|
|
|
function GetPeople: TJSONArray;
|
|
|
|
procedure AddPerson(FirstName, LastName: String; Age: Integer);
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure DeleteByGUID(GUID: String);
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TPeopleDAL = class(TInterfacedObject, IPeopleDAL)
|
|
|
|
private const
|
|
|
|
DATAFILE: String = 'people.data';
|
|
|
|
public
|
|
|
|
function GetPeople: TJSONArray;
|
|
|
|
procedure AddPerson(FirstName, LastName: String; Age: Integer);
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure DeleteByGUID(GUID: String);
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TServicesFactory = class sealed
|
|
|
|
class function GetPeopleDAL: IPeopleDAL;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2016-04-24 19:08:21 +02:00
|
|
|
System.IOUtils, System.SysUtils, System.SyncObjs;
|
2016-02-14 18:15:56 +01:00
|
|
|
|
2016-04-24 19:08:21 +02:00
|
|
|
var
|
|
|
|
// Hey! The storage is a simple json file, so some synchronization is needed
|
|
|
|
_CS: TCriticalSection = nil;
|
|
|
|
|
|
|
|
{ TSimpleDAL }
|
2016-02-14 18:15:56 +01:00
|
|
|
|
|
|
|
procedure TPeopleDAL.AddPerson(FirstName, LastName: String; Age: Integer);
|
|
|
|
var
|
|
|
|
LJPeople: TJSONArray;
|
|
|
|
LJPerson: TJSONObject;
|
|
|
|
begin
|
2016-04-24 19:08:21 +02:00
|
|
|
_CS.Enter;
|
2016-02-14 18:15:56 +01:00
|
|
|
try
|
2016-04-24 19:08:21 +02:00
|
|
|
LJPeople := GetPeople;
|
|
|
|
try
|
|
|
|
LJPerson := TJSONObject.Create;
|
|
|
|
LJPeople.AddElement(LJPerson);
|
2016-11-27 23:17:20 +01:00
|
|
|
LJPerson
|
|
|
|
.AddPair('first_name', FirstName)
|
|
|
|
.AddPair('last_name', LastName)
|
|
|
|
.AddPair('age', TJSONNumber.Create(Age))
|
|
|
|
.AddPair('guid', TGuid.NewGuid.ToString);
|
2016-04-24 19:08:21 +02:00
|
|
|
TFile.WriteAllText(DATAFILE, LJPeople.ToJSON);
|
|
|
|
finally
|
|
|
|
LJPeople.Free;
|
|
|
|
end;
|
2016-02-14 18:15:56 +01:00
|
|
|
finally
|
2016-04-24 19:08:21 +02:00
|
|
|
_CS.Leave;
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class function TServicesFactory.GetPeopleDAL: IPeopleDAL;
|
|
|
|
begin
|
|
|
|
Result := TPeopleDAL.Create;
|
|
|
|
end;
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure TPeopleDAL.DeleteByGUID(GUID: String);
|
|
|
|
var
|
|
|
|
LJPeople: TJSONArray;
|
|
|
|
I: Integer;
|
|
|
|
begin
|
|
|
|
_CS.Enter;
|
|
|
|
try
|
|
|
|
LJPeople := GetPeople;
|
|
|
|
try
|
|
|
|
for I := 0 to LJPeople.Count - 1 do
|
|
|
|
begin
|
|
|
|
if LJPeople.Items[I].GetValue<TJSONString>('guid').Value = GUID then
|
|
|
|
begin
|
|
|
|
LJPeople.Remove(I);
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
TFile.WriteAllText(DATAFILE, LJPeople.ToJSON);
|
|
|
|
finally
|
|
|
|
LJPeople.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
_CS.Leave;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-02-14 18:15:56 +01:00
|
|
|
function TPeopleDAL.GetPeople: TJSONArray;
|
|
|
|
var
|
|
|
|
LData: string;
|
|
|
|
begin
|
2016-04-24 19:08:21 +02:00
|
|
|
_CS.Enter;
|
|
|
|
try
|
|
|
|
if TFile.Exists(DATAFILE) then
|
|
|
|
LData := TFile.ReadAllText(DATAFILE).Trim;
|
|
|
|
if not LData.IsEmpty then
|
|
|
|
begin
|
|
|
|
Result := TJSONObject.ParseJSONValue(LData) as TJSONArray;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := TJSONArray.Create;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
_CS.Leave;
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-04-24 19:08:21 +02:00
|
|
|
initialization
|
|
|
|
|
|
|
|
_CS := TCriticalSection.Create;
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
_CS.Free;
|
|
|
|
|
2016-02-14 18:15:56 +01:00
|
|
|
end.
|