2018-10-23 16:18:34 +02:00
|
|
|
// *************************************************************************** }
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2019-01-08 12:48:27 +01:00
|
|
|
// Copyright (c) 2010-2019 Daniele Teti and the DMVCFramework Team
|
2018-10-23 16:18:34 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
|
|
|
|
unit EntitiesU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
MVCFramework.ActiveRecord,
|
|
|
|
System.Generics.Collections,
|
|
|
|
System.Classes;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTable('articles')]
|
2018-10-23 16:18:34 +02:00
|
|
|
TArticle = class(TMVCActiveRecord)
|
|
|
|
private
|
|
|
|
[MVCTableField('ID')]
|
|
|
|
fID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('code')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fCodice: string;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('description')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fDescrizione: string;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('price')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fPrezzo: Currency;
|
|
|
|
public
|
|
|
|
constructor Create; override;
|
|
|
|
destructor Destroy; override;
|
|
|
|
property ID: Integer read fID write fID;
|
2019-02-21 18:11:14 +01:00
|
|
|
property Code: string read fCodice write fCodice;
|
|
|
|
property Description: string read fDescrizione write fDescrizione;
|
|
|
|
property Price: Currency read fPrezzo write fPrezzo;
|
2018-10-23 16:18:34 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TOrder = class;
|
|
|
|
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTable('customers')]
|
2018-10-23 16:18:34 +02:00
|
|
|
TCustomer = class(TMVCActiveRecord)
|
|
|
|
private
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCPrimaryKey('id', [foAutoGenerated])]
|
2018-10-23 16:18:34 +02:00
|
|
|
fID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('code')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fCode: string;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('description')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fCompanyName: string;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('city')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fCity: string;
|
|
|
|
public
|
|
|
|
constructor Create; override;
|
|
|
|
destructor Destroy; override;
|
|
|
|
property ID: Integer read fID write fID;
|
|
|
|
property Code: string read fCode write fCode;
|
|
|
|
property CompanyName: string read fCompanyName write fCompanyName;
|
|
|
|
property City: string read fCity write fCity;
|
|
|
|
end;
|
|
|
|
|
2019-02-21 20:17:11 +01:00
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
[MVCTable('customers')]
|
|
|
|
TCustomerWithTransient = class(TMVCActiveRecord)
|
|
|
|
private
|
|
|
|
[MVCPrimaryKey('id', [foAutoGenerated])]
|
|
|
|
fID: Integer;
|
|
|
|
[MVCTableField('code', [foTransient])]
|
|
|
|
fCode: string;
|
|
|
|
[MVCTableField('description')]
|
|
|
|
fCompanyName: string;
|
|
|
|
[MVCTableField('city', [foTransient])]
|
|
|
|
fCity: string;
|
|
|
|
public
|
|
|
|
property ID: Integer read fID write fID;
|
|
|
|
property Code: string read fCode write fCode;
|
|
|
|
property CompanyName: string read fCompanyName write fCompanyName;
|
|
|
|
property City: string read fCity write fCity;
|
|
|
|
end;
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
[MVCNameCase(ncLowerCase)]
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTable('order_details')]
|
2018-10-23 16:18:34 +02:00
|
|
|
TOrderDetail = class(TMVCActiveRecord)
|
|
|
|
private
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCPrimaryKey('id', [foAutoGenerated])]
|
2018-10-23 16:18:34 +02:00
|
|
|
fID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('id_order')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fOrderID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('id_article')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fArticleID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('unit_price')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fPrice: Currency;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('discount')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fDiscount: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('quantity')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fQuantity: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('description')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fDescription: string;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('total')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fTotal: Currency;
|
|
|
|
public
|
|
|
|
constructor Create; override;
|
|
|
|
destructor Destroy; override;
|
|
|
|
property ID: Integer read fID write fID;
|
|
|
|
property OrderID: Integer read fOrderID write fOrderID;
|
|
|
|
property ArticleID: Integer read fArticleID write fArticleID;
|
|
|
|
property Price: Currency read fPrice write fPrice;
|
|
|
|
property Discount: Integer read fDiscount write fDiscount;
|
|
|
|
property Quantity: Integer read fQuantity write fQuantity;
|
|
|
|
property Description: string read fDescription write fDescription;
|
|
|
|
property Total: Currency read fTotal write fTotal;
|
|
|
|
end;
|
|
|
|
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTable('orders')]
|
2018-10-23 16:18:34 +02:00
|
|
|
TOrder = class(TMVCActiveRecord)
|
|
|
|
private
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCPrimaryKey('id', [foAutoGenerated])]
|
2018-10-23 16:18:34 +02:00
|
|
|
fID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('id_customer')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fCustomerID: Integer;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('order_date')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fOrderDate: TDate;
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTableField('total')]
|
2018-10-23 16:18:34 +02:00
|
|
|
fTotal: Currency;
|
|
|
|
public
|
|
|
|
constructor Create; override;
|
|
|
|
destructor Destroy; override;
|
|
|
|
property ID: Integer read fID write fID;
|
|
|
|
property CustomerID: Integer read fCustomerID write fCustomerID;
|
|
|
|
property OrderDate: TDate read fOrderDate write fOrderDate;
|
|
|
|
property Total: Currency read fTotal write fTotal;
|
|
|
|
end;
|
|
|
|
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTable('customers')]
|
2018-10-23 16:18:34 +02:00
|
|
|
TCustomerEx = class(TCustomer)
|
|
|
|
private
|
|
|
|
fOrders: TObjectList<TOrder>;
|
|
|
|
protected
|
|
|
|
procedure OnAfterLoad; override;
|
|
|
|
public
|
|
|
|
destructor Destroy; override;
|
|
|
|
property Orders: TObjectList<TOrder> read fOrders;
|
|
|
|
end;
|
|
|
|
|
|
|
|
[MVCNameCase(ncLowerCase)]
|
2019-02-21 18:11:14 +01:00
|
|
|
[MVCTable('customers')]
|
2018-10-23 16:18:34 +02:00
|
|
|
TCustomerWithLogic = class(TCustomer)
|
|
|
|
private
|
|
|
|
fIsLocatedInRome: Boolean;
|
|
|
|
protected
|
|
|
|
procedure OnAfterLoad; override;
|
|
|
|
procedure OnBeforeInsertOrUpdate; override;
|
|
|
|
procedure OnValidation; override;
|
|
|
|
public
|
|
|
|
property IsLocatedInRome: Boolean read fIsLocatedInRome;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils;
|
|
|
|
|
|
|
|
constructor TArticle.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TArticle.Destroy;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TCustomer.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TCustomer.Destroy;
|
|
|
|
begin
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TOrderDetail.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TOrderDetail.Destroy;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TOrder.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TOrder.Destroy;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TCustomerEx }
|
|
|
|
|
|
|
|
destructor TCustomerEx.Destroy;
|
|
|
|
begin
|
|
|
|
fOrders.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomerEx.OnAfterLoad;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
fOrders.Free;
|
2019-02-21 18:11:14 +01:00
|
|
|
fOrders := TOrder.Where<TOrder>('id_customer = ?', [Self.ID]);
|
2018-10-23 16:18:34 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
{ TCustomerWithLogic }
|
|
|
|
|
|
|
|
procedure TCustomerWithLogic.OnAfterLoad;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
fIsLocatedInRome := fCity = 'ROME';
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomerWithLogic.OnBeforeInsertOrUpdate;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
fCompanyName := fCompanyName.ToUpper;
|
|
|
|
fCity := fCity.ToUpper;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomerWithLogic.OnValidation;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
if fCompanyName.Trim.IsEmpty or fCity.Trim.IsEmpty or fCode.Trim.IsEmpty then
|
|
|
|
raise Exception.Create('CompanyName, City and Code are required');
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|