delphimvcframework/samples/serversideviews/DAL.pas

203 lines
4.2 KiB
ObjectPascal
Raw Normal View History

2016-11-27 23:17:20 +01:00
unit DAL;
interface
uses
2017-05-25 16:57:49 +02:00
System.JSON,
MVCFramework.SystemJSONUtils,
System.Generics.Collections,
MVCFramework.Serializer.Commons;
2016-11-27 23:17:20 +01:00
type
2017-05-25 16:57:49 +02:00
[MVCNameCase(ncLowerCase)]
TPerson = class
private
FFirstName: string;
FLastName: string;
FAge: Integer;
FItems: string;
FGUID: string;
procedure SetFirstName(const Value: string);
procedure SetLastName(const Value: string);
procedure SetAge(const Value: Integer);
procedure SetGUID(const Value: string);
procedure SetItems(const Value: string);
public
[MVCNameAs('first_name')]
property FirstName: string read FFirstName write SetFirstName;
[MVCNameAs('last_name')]
property LastName: string read FLastName write SetLastName;
property Age: Integer read FAge write SetAge;
property Items: string read FItems write SetItems;
property GUID: string read FGUID write SetGUID;
end;
TPeople = class(TObjectList<TPerson>)
end;
IPeopleDAL = interface
2016-11-27 23:17:20 +01:00
['{3E534A3E-EAEB-44ED-B74E-EFBBAAAE11B4}']
2017-05-25 16:57:49 +02:00
function GetPeople: TPeople;
procedure AddPerson(FirstName, LastName: string; Age: Integer; Items: TArray<string>);
procedure DeleteByGUID(GUID: string);
function GetPersonByGUID(GUID: string): TPerson;
2016-11-27 23:17:20 +01:00
end;
TPeopleDAL = class(TInterfacedObject, IPeopleDAL)
private const
DATAFILE: string = 'people.data';
public
2017-05-25 16:57:49 +02:00
function GetPeople: TPeople;
procedure AddPerson(FirstName, LastName: string; Age: Integer; Items: TArray<string>);
procedure DeleteByGUID(GUID: string);
function GetPersonByGUID(GUID: string): TPerson;
2016-11-27 23:17:20 +01:00
end;
TServicesFactory = class sealed
class function GetPeopleDAL: IPeopleDAL;
end;
implementation
uses
2017-05-25 16:57:49 +02:00
System.SyncObjs,
System.IOUtils,
MVCFramework.Serializer.Defaults,
System.SysUtils;
2016-11-27 23:17:20 +01:00
var
// Hey! The storage is a simple json file, so some synchronization is needed
_CS: TCriticalSection = nil;
{ TSimpleDAL }
2017-05-25 16:57:49 +02:00
procedure TPeopleDAL.AddPerson(FirstName, LastName: string; Age: Integer; Items: TArray<string>);
2016-11-27 23:17:20 +01:00
var
2017-05-25 16:57:49 +02:00
lPeople: TPeople;
lPerson: TPerson;
2016-11-27 23:17:20 +01:00
begin
_CS.Enter;
try
2017-05-25 16:57:49 +02:00
lPeople := GetPeople;
2016-11-27 23:17:20 +01:00
try
2017-05-25 16:57:49 +02:00
lPerson := TPerson.Create;
lPeople.Add(lPerson);
lPerson.FirstName := FirstName;
lPerson.LastName := LastName;
lPerson.Age := Age;
lPerson.Items := string.Join(',', Items);
lPerson.GUID := TGuid.NewGuid.ToString;
TFile.WriteAllText(DATAFILE, GetDefaultSerializer.SerializeCollection(lPeople));
2016-11-27 23:17:20 +01:00
finally
2017-05-25 16:57:49 +02:00
lPeople.Free;
2016-11-27 23:17:20 +01:00
end;
finally
_CS.Leave;
end;
end;
class function TServicesFactory.GetPeopleDAL: IPeopleDAL;
begin
Result := TPeopleDAL.Create;
end;
2017-05-25 16:57:49 +02:00
procedure TPeopleDAL.DeleteByGUID(GUID: string);
2016-11-27 23:17:20 +01:00
var
2017-05-25 16:57:49 +02:00
LJPeople: TPeople;
2016-11-27 23:17:20 +01:00
I: Integer;
begin
_CS.Enter;
try
2017-05-25 16:57:49 +02:00
LJPeople := GetPeople;
try
for I := 0 to LJPeople.Count - 1 do
begin
if LJPeople[I].GUID = GUID then
2016-11-27 23:17:20 +01:00
begin
2017-05-25 16:57:49 +02:00
LJPeople.Delete(i);
break;
2016-11-27 23:17:20 +01:00
end;
end;
2017-05-25 16:57:49 +02:00
TFile.WriteAllText(DATAFILE, GetDefaultSerializer.SerializeCollection(LJPeople));
finally
LJPeople.Free;
end;
2016-11-27 23:17:20 +01:00
finally
_CS.Leave;
end;
end;
2017-05-25 16:57:49 +02:00
function TPeopleDAL.GetPeople: TPeople;
2016-11-27 23:17:20 +01:00
var
LData: string;
begin
_CS.Enter;
try
2017-05-25 16:57:49 +02:00
Result := TPeople.Create;
2016-11-27 23:17:20 +01:00
if TFile.Exists(DATAFILE) then
LData := TFile.ReadAllText(DATAFILE).Trim;
if not LData.IsEmpty then
begin
2017-05-25 16:57:49 +02:00
GetDefaultSerializer.DeserializeCollection(LData, Result, TPerson);
2016-11-27 23:17:20 +01:00
end;
finally
_CS.Leave;
end;
end;
2017-05-25 16:57:49 +02:00
function TPeopleDAL.GetPersonByGUID(GUID: string): TPerson;
2016-11-27 23:17:20 +01:00
var
2017-05-25 16:57:49 +02:00
lPeople: TPeople;
lPerson: TPerson;
2016-11-27 23:17:20 +01:00
begin
Result := nil;
lPeople := GetPeople;
2017-05-25 16:57:49 +02:00
for lPerson in lPeople do
begin
if lPerson.GUID = GUID then
2016-11-27 23:17:20 +01:00
begin
2017-05-25 16:57:49 +02:00
Result := lPeople.Extract(lPerson);
Break;
2016-11-27 23:17:20 +01:00
end;
2017-05-25 16:57:49 +02:00
2016-11-27 23:17:20 +01:00
end;
end;
2017-05-25 16:57:49 +02:00
{ TPerson }
procedure TPerson.SetAge(const Value: Integer);
begin
FAge := Value;
end;
procedure TPerson.SetFirstName(const Value: string);
begin
FFirstName := Value;
end;
procedure TPerson.SetGUID(const Value: string);
begin
FGUID := Value;
end;
procedure TPerson.SetItems(const Value: string);
begin
FItems := Value;
end;
procedure TPerson.SetLastName(const Value: string);
begin
FLastName := Value;
end;
2016-11-27 23:17:20 +01:00
initialization
_CS := TCriticalSection.Create;
finalization
_CS.Free;
end.