2014-06-27 15:30:39 +02:00
unit Services;
interface
uses
2019-06-24 20:59:33 +02:00
System. Generics. Collections,
BusinessObjects,
MainDM,
System. SysUtils,
2020-09-23 23:33:30 +02:00
Commons, JsonDataObjects;
2014-06-27 15:30:39 +02:00
type
TServiceBase = class abstract
strict protected
FDM: TdmMain;
public
constructor Create( AdmMain: TdmMain) ; virtual ;
procedure Commit;
procedure Rollback;
procedure StartTransaction;
end ;
TArticlesService = class( TServiceBase)
public
function GetAll: TObjectList< TArticle> ;
2020-09-23 23:33:30 +02:00
function GetArticles( const aTextSearch: string ) : TObjectList< TArticle> ;
2014-06-27 15:30:39 +02:00
function GetByID( const AID: Integer ) : TArticle;
procedure Delete( AArticolo: TArticle) ;
2020-01-24 10:09:14 +01:00
procedure DeleteAllArticles;
2014-06-27 15:30:39 +02:00
procedure Add( AArticolo: TArticle) ;
2014-06-27 16:38:49 +02:00
procedure Update( AArticolo: TArticle) ;
2020-09-23 23:33:30 +02:00
function GetMeta: TJSONObject;
2014-06-27 15:30:39 +02:00
end ;
implementation
uses
2019-06-24 20:59:33 +02:00
FireDAC. Stan. Option,
FireDAC. Comp . Client,
FireDAC. Stan. Param,
MVCFramework. FireDAC. Utils,
MVCFramework. DataSet. Utils,
2017-04-24 00:19:53 +02:00
MVCFramework. Serializer. Commons;
2014-06-27 15:30:39 +02:00
{ TArticoliService }
procedure TArticlesService. Add( AArticolo: TArticle) ;
var
Cmd: TFDCustomCommand;
begin
AArticolo. CheckInsert;
Cmd : = FDM. updArticles. Commands[ arInsert] ;
2017-04-24 00:19:53 +02:00
TFireDACUtils. ObjectToParameters( Cmd. Params, AArticolo, 'NEW_' ) ;
2017-05-12 00:25:46 +02:00
Cmd. Execute;
AArticolo. ID : = Cmd. ParamByName( 'ID' ) . AsInteger;
2014-06-27 15:30:39 +02:00
end ;
procedure TArticlesService. Delete( AArticolo: TArticle) ;
var
Cmd: TFDCustomCommand;
begin
AArticolo. CheckDelete;
Cmd : = FDM. updArticles. Commands[ arDelete] ;
2017-04-24 00:19:53 +02:00
TFireDACUtils. ObjectToParameters( Cmd. Params, AArticolo, 'OLD_' ) ;
2014-06-27 15:30:39 +02:00
Cmd. Execute;
end ;
2020-01-24 10:09:14 +01:00
procedure TArticlesService. DeleteAllArticles;
begin
FDM. Connection. ExecSQL( 'delete from articoli' ) ;
end ;
2014-06-27 15:30:39 +02:00
function TArticlesService. GetAll: TObjectList< TArticle> ;
begin
2019-06-24 20:59:33 +02:00
FDM. dsArticles. Open( 'SELECT * FROM ARTICOLI ORDER BY ID' , [ ] ) ;
2014-06-27 15:30:39 +02:00
Result : = FDM. dsArticles. AsObjectList< TArticle> ;
FDM. dsArticles. Close;
end ;
2019-06-24 20:59:33 +02:00
function TArticlesService. GetArticles(
2020-09-23 23:33:30 +02:00
const aTextSearch: string ) : TObjectList< TArticle> ;
2019-06-24 20:59:33 +02:00
begin
FDM. dsArticles. Open( 'SELECT * FROM ARTICOLI WHERE DESCRIZIONE CONTAINING ? ORDER BY ID' , [ aTextSearch] ) ;
try
Result : = FDM. dsArticles. AsObjectList< TArticle> ( )
finally
FDM. dsArticles. Close;
end ;
end ;
2014-06-27 15:30:39 +02:00
function TArticlesService. GetByID( const AID: Integer ) : TArticle;
begin
FDM. dsArticles. Open( 'SELECT * FROM ARTICOLI WHERE ID = :ID' , [ AID] ) ;
try
if not FDM. dsArticles. Eof then
Result : = FDM. dsArticles. AsObject< TArticle>
else
raise EServiceException. Create( 'Article not found' ) ;
finally
FDM. dsArticles. Close;
end ;
end ;
2020-09-23 23:33:30 +02:00
function TArticlesService. GetMeta: TJSONObject;
begin
2020-11-05 15:42:31 +01:00
FDM. dsArticles. Open( 'SELECT ID, CODICE as CODE, DESCRIZIONE as DESCRIPTION, PREZZO as PRICE, CREATED_AT as CREATEDAT, UPDATED_AT as UPDATEDAT FROM ARTICOLI WHERE TRUE = FALSE' ) ;
2020-09-23 23:33:30 +02:00
Result : = FDM. dsArticles. MetadataAsJSONObject( ) ;
end ;
2014-06-27 16:38:49 +02:00
procedure TArticlesService. Update( AArticolo: TArticle) ;
var
Cmd: TFDCustomCommand;
begin
AArticolo. CheckUpdate;
Cmd : = FDM. updArticles. Commands[ arUpdate] ;
2017-04-24 00:19:53 +02:00
TFireDACUtils. ObjectToParameters( Cmd. Params, AArticolo, 'NEW_' ) ;
2014-06-27 16:38:49 +02:00
Cmd. ParamByName( 'OLD_ID' ) . AsInteger : = AArticolo. ID;
2014-06-30 12:32:43 +02:00
Cmd. Execute;
2014-06-27 16:38:49 +02:00
if Cmd. RowsAffected < > 1 then
raise Exception. Create( 'Article not found' ) ;
end ;
2014-06-27 15:30:39 +02:00
{ TServiceBase }
procedure TServiceBase. Commit;
begin
FDM. Connection. Commit;
end ;
constructor TServiceBase. Create( AdmMain: TdmMain) ;
begin
inherited Create;
FDM : = AdmMain;
end ;
procedure TServiceBase. Rollback;
begin
FDM. Connection. Rollback;
end ;
procedure TServiceBase. StartTransaction;
begin
FDM. Connection. StartTransaction;
end ;
end .