Skip to content

Commit

Permalink
articles_crud migrated to services container
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Mar 29, 2024
1 parent 6142c2d commit b41e245
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 256 deletions.
57 changes: 22 additions & 35 deletions samples/articles_crud_server/BusinessObjects.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,51 @@
interface

uses
MVCFramework.Serializer.Commons, MVCFramework.Nullables;
MVCFramework.Serializer.Commons, MVCFramework.Nullables,
MVCFramework.ActiveRecord;

type
TBaseBO = class
TBaseBO = class(TMVCActiveRecord)
private
[MVCTableField('ID', [foPrimaryKey])]
FID: Integer;
procedure SetID(const Value: Integer);
public
procedure CheckInsert; virtual;
procedure CheckUpdate; virtual;
procedure CheckDelete; virtual;
property ID: Integer read FID write SetID;
end;

[MVCNameCase(ncLowerCase)]
[MVCTable('ARTICOLI')]
[MVCNamedSQLQuery(
'search_by_text',
'SELECT * FROM ARTICOLI WHERE DESCRIZIONE CONTAINING ? ORDER BY ID')
]
TArticle = class(TBaseBO)
private
FPrice: Currency;
[MVCTableField('CODICE')]
FCode: string;
[MVCTableField('DESCRIZIONE')]
FDescription: String;
[MVCTableField('PREZZO')]
FPrice: Currency;
[MVCTableField('UPDATED_AT')]
FUpdatedAt: TDateTime;
[MVCTableField('CREATED_AT')]
FCreatedAt: TDateTime;
procedure SetCode(const Value: string);
procedure SetDescription(const Value: String);
procedure SetPrice(const Value: Currency);
procedure SetCreatedAt(const Value: TDateTime);
procedure SetUpdatedAt(const Value: TDateTime);
protected
procedure OnBeforeInsertOrUpdate; override;
procedure OnBeforeUpdate; override;
procedure OnBeforeDelete; override;
public
procedure CheckInsert; override;
procedure CheckUpdate; override;
procedure CheckDelete; override;
[MVCColumn('CODICE')]
property Code: string read FCode write SetCode;
[MVCColumn('DESCRIZIONE')]
property Description: String read FDescription write SetDescription;
[MVCColumn('PREZZO')]
property Price: Currency read FPrice write SetPrice;
[MVCColumn('CREATED_AT')]
property CreatedAt: TDateTime read FCreatedAt write SetCreatedAt;
[MVCColumn('UPDATED_AT')]
property UpdatedAt: TDateTime read FUpdatedAt write SetUpdatedAt;
end;

Expand All @@ -53,46 +58,28 @@ implementation

{ TBaseBO }

procedure TBaseBO.CheckDelete;
begin

end;

procedure TBaseBO.CheckInsert;
begin

end;

procedure TBaseBO.CheckUpdate;
begin

end;

procedure TBaseBO.SetID(const Value: Integer);
begin
FID := Value;
end;

{ TArticolo }

procedure TArticle.CheckDelete;
procedure TArticle.OnBeforeDelete;
begin
inherited;
if Price <= 5 then
raise Exception.Create('Cannot delete an article with a price below 5 euros (yes, it is a silly check)');
end;

procedure TArticle.CheckInsert;
procedure TArticle.OnBeforeInsertOrUpdate;
begin
inherited;
if not TRegEx.IsMatch(Code, '^C[0-9]{2,4}') then
raise Exception.Create('Article code must be in the format "CXX or CXXX or CXXXX"');
end;

procedure TArticle.CheckUpdate;
procedure TArticle.OnBeforeUpdate;
begin
inherited;
CheckInsert;
if Price <= 2 then
raise Exception.Create('We cannot sell so low cost pizzas!');
end;
Expand Down
3 changes: 3 additions & 0 deletions samples/articles_crud_server/Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ EServiceException = class(Exception)

end;

const
CON_DEF_NAME = 'MyConnX';

implementation

end.
121 changes: 45 additions & 76 deletions samples/articles_crud_server/Controllers.Articles.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ interface
mvcframework.Commons,
mvcframework.Serializer.Commons,
System.Generics.Collections,
Controllers.Base, BusinessObjects;
Controllers.Base, BusinessObjects, Services;

type

[MVCDoc('Resource that manages articles CRUD')]
[MVCPath('/articles')]
TArticlesController = class(TBaseController)
private
fArticlesService: IArticlesService;
public
[MVCInject]
constructor Create(ArticlesService: IArticlesService); reintroduce;

[MVCDoc('Returns the list of articles')]
[MVCPath]
[MVCHTTPMethod([httpGET])]
Expand All @@ -23,17 +28,17 @@ TArticlesController = class(TBaseController)
[MVCDoc('Returns the list of articles')]
[MVCPath('/searches')]
[MVCHTTPMethod([httpGET])]
procedure GetArticlesByDescription(const [MVCFromQueryString('q', '')] Search: String);
function GetArticlesByDescription(const [MVCFromQueryString('q', '')] Search: String): IMVCObjectDictionary;

[MVCDoc('Returns the article with the specified id')]
[MVCPath('/meta')]
[MVCHTTPMethod([httpGET])]
procedure GetArticleMeta;
function GetArticleMeta: IMVCObjectDictionary;

[MVCDoc('Returns the article with the specified id')]
[MVCPath('/($id)')]
[MVCHTTPMethod([httpGET])]
procedure GetArticleByID(id: Integer);
function GetArticleByID(id: Integer): IMVCObjectDictionary;

[MVCDoc('Deletes the article with the specified id')]
[MVCPath('/($id)')]
Expand Down Expand Up @@ -61,116 +66,80 @@ implementation
{ TArticlesController }

uses
Services,
Commons,
mvcframework.Serializer.Intf,
System.SysUtils;

constructor TArticlesController.Create(ArticlesService: IArticlesService);
begin
inherited Create;
fArticlesService := ArticlesService;
end;

procedure TArticlesController.CreateArticle(const Article: TArticle);
begin
GetArticlesService.Add(Article);
fArticlesService.Add(Article);
Render201Created('/articles/' + Article.id.ToString, 'Article Created');
end;

procedure TArticlesController.CreateArticles(const ArticleList: TObjectList<TArticle>);
var
lArticle: TArticle;
begin
GetArticlesService.StartTransaction;
try
for lArticle in ArticleList do
begin
GetArticlesService.Add(lArticle);
end;
GetArticlesService.Commit;
except
GetArticlesService.Rollback;
raise;
end;
Render(201, 'Articles Created');
// fArticlesService.StartTransaction;
// try
// for lArticle in ArticleList do
// begin
// fArticlesService.Add(lArticle);
// end;
// fArticlesService.Commit;
// except
// fArticlesService.Rollback;
// raise;
// end;
// Render(201, 'Articles Created');
end;

procedure TArticlesController.DeleteArticleByID(id: Integer);
var
Article: TArticle;
lArticle: TArticle;
begin
GetArticlesService.StartTransaction;
try
Article := GetArticlesService.GetByID(id);
try
GetArticlesService.Delete(Article);
finally
Article.Free;
end;
GetArticlesService.Commit;
except
GetArticlesService.Rollback;
raise;
end;
lArticle := fArticlesService.GetByID(id);
fArticlesService.Delete(lArticle);
end;

procedure TArticlesController.GetArticles;
begin
Render(ObjectDict().Add('data', GetArticlesService.GetAll));
Render(ObjectDict().Add('data', fArticlesService.GetAll));
end;

procedure TArticlesController.GetArticlesByDescription(const Search: String);
var
lDict: IMVCObjectDictionary;
function TArticlesController.GetArticlesByDescription(const Search: String): IMVCObjectDictionary;
begin
try
if Search = '' then
begin
lDict := ObjectDict().Add('data', GetArticlesService.GetAll);
end
else
begin
lDict := ObjectDict().Add('data', GetArticlesService.GetArticles(Search));
end;
Render(lDict);
except
on E: EServiceException do
begin
raise EMVCException.Create(E.Message, '', 0, 404);
end
else
raise;
if Search = '' then
begin
Result := ObjectDict().Add('data', fArticlesService.GetAll);
end
else
begin
Result := ObjectDict().Add('data', fArticlesService.GetArticles(Search));
end;
end;

procedure TArticlesController.UpdateArticleByID(const Article: TArticle; const id: Integer);
begin
Article.id := id;
GetArticlesService.Update(Article);
fArticlesService.Update(Article);
Render(200, 'Article Updated');
end;

procedure TArticlesController.GetArticleByID(id: Integer);
function TArticlesController.GetArticleByID(id: Integer): IMVCObjectDictionary;
begin
try
Render(ObjectDict().Add('data', GetArticlesService.GetByID(id)));
except
on E: EServiceException do
begin
raise EMVCException.Create(E.Message, '', 0, 404);
end
else
raise;
end;
Result := ObjectDict().Add('data', fArticlesService.GetByID(id));
end;

procedure TArticlesController.GetArticleMeta;
function TArticlesController.GetArticleMeta: IMVCObjectDictionary;
begin
try
Render(ObjectDict().Add('data', GetArticlesService.GetMeta));
except
on E: EServiceException do
begin
raise EMVCException.Create(E.Message, '', 0, 404);
end
else
raise;
end;
Result := ObjectDict().Add('data', fArticlesService.GetMeta);
end;

end.
40 changes: 4 additions & 36 deletions samples/articles_crud_server/Controllers.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,30 @@
interface

uses
MVCFramework, MVCFramework.Commons, Services, MainDM;
MVCFramework, MVCFramework.Commons, Services;

type
TBaseController = class abstract(TMVCController)
strict private
FDM: TdmMain;
FArticlesService: TArticlesService;
function GetDataModule: TdmMain;
strict protected
function GetArticlesService: TArticlesService;
public
destructor Destroy; override;

end;

[MVCPath('/private')]
TPrivateController = class(TBaseController)
public
[MVCPath('/articles')]
[MVCHTTPMethods([httpDELETE])]
procedure DeleteAllArticles;
procedure DeleteAllArticles(ArticlesService: IArticlesService);
end;

implementation

uses
System.SysUtils;

{ TBaseController }

destructor TBaseController.Destroy;
begin
FArticlesService.Free;
FDM.Free;
inherited;
end;

function TBaseController.GetArticlesService: TArticlesService;
begin
if not Assigned(FArticlesService) then
FArticlesService := TArticlesService.Create(GetDataModule);
Result := FArticlesService;
end;

function TBaseController.GetDataModule: TdmMain;
begin
if not Assigned(FDM) then
FDM := TdmMain.Create(nil);
Result := FDM;
end;

{ TPrivateController }

procedure TPrivateController.DeleteAllArticles;
procedure TPrivateController.DeleteAllArticles(ArticlesService: IArticlesService);
begin
GetArticlesService.DeleteAllArticles();
ArticlesService.DeleteAllArticles();
end;

end.
Loading

0 comments on commit b41e245

Please sign in to comment.