-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBusiness_Rules.sql
165 lines (137 loc) · 3.72 KB
/
Business_Rules.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Shane Business Rules
-- You can only have one user who rents/owns the property at any given time.
Create Function fn_IsOccupiedByOnlyOne()
Returns INT
As
Begin
Declare @Ret INT = 0
If Exists (
Select *
From tblPROPERTY_USER PU
Join tblPROPERTY P On P.PropertyID = PU.PropertyID
Join tblUSER U On U.UserID = PU.UserID
Join tblEvent E On E.PropertyUserID = PU.PropertyUserID
Join tblEvent_Type ET On ET.EventTypeID = E.EventTypeID
Where (ET.EventTypeName = 'Purchase property' Or ET.EventTypeName = 'Rent property')
And GETDATE() Between PU.BeginDate And PU.EndDate
Group By P.PropertyID
Having COUNT(Distinct U.UserID) > 1
) Set @Ret = 1
Return @Ret
End
Go
Alter Table tblEvent
Add Constraint IsOccupiedByOnlyOne
Check (dbo.fn_IsOccupiedByOnlyOne() = 0)
Go
-- Listers cannot list a property as both for sale and for rent.
Create Function fn_IsListedSingleType()
Returns INT
As
Begin
Declare @Ret INT = 0
If Exists (
Select *
From tblEVENT_TYPE ET
Join tblEVENT E On E.EventTypeID = ET.EventTypeID
Join tblPROPERTY_USER PU On PU.PropertyUserID = E.PropertyUserID
Join tblProperty P On P.PropertyID = PU.PropertyID
Where E.EventName = 'List for rent'
Or E.EventName = 'List for purchase'
Group By P.PropertyID
Having COUNT(Distinct E.EventID) > 1
) Set @Ret = 1
Return @Ret
End
Go
Alter Table tblProperty
Add Constraint IsListedSingleType
Check (dbo.fn_IsListedSingleType() = 0)
Go
-- Anthony Business Rules
--Business rule: Users below the age of 18 are not valid users
CREATE FUNCTION fn_No_User_below_18()
RETURNS INT
AS
BEGIN
DECLARE @RET INT = 0
IF EXISTS(SELECT *
FROM tblUSER
WHERE BirthDate > DATEADD(day, -18*365, GETDATE()))
BEGIN
SET @RET = 1
END
RETURN @RET
END
GO
ALTER TABLE tblUSER
ADD CONSTRAINT ck_no_User_below_18
CHECK(dbo.fn_No_User_below_18() = 0)
GO
---Business rule: Users’ monthly income must be larger than the price[Anthony]
CREATE FUNCTION fn_income_LargerThan_Price()
RETURNS INT
AS
BEGIN
DECLARE @RET INT = 0
IF EXISTS(SELECT *
FROM tblUSER U
JOIN tblPROPERTY_USER PU on U.UserID = PU.UserID
JOIN tblEVENT E on PU.PropertyUserID = E.PropertyUserID
JOIN tblEVENT_TYPE ET on E.EventTypeID = ET.EventTypeID
WHERE ET.EventTypeName = 'Rent property'
AND U.MonthlyIncome < E.Price
OR ET.EventTypeName = 'Purchase property'
AND E.Price > U.MonthlyIncome * 50)
BEGIN
SET @RET = 1
END
RETURN @RET
END
GO
ALTER TABLE tblEVENT
ADD CONSTRAINT ck_income_LargerThan_Price
CHECK(dbo.fn_income_LargerThan_Price() = 0)
GO
-- William Business Rules
--User below the age of 21 cannot buy property that is over 1M dollars
CREATE FUNCTION dbo.fn_NoKids_BuyMansion()
RETURNS INT
AS
BEGIN
DECLARE @RET INT = 0
IF EXISTS (SELECT * FROM tblEvent E
JOIN tblEVENT_TYPE ET ON ET.EventTypeID = E.EventTypeID
JOIN tblPROPERTY_USER PU ON PU.PropertyUserID = E.PropertyUserID
JOIN tblUSER U ON U.UserID = PU.UserID
WHERE U.BirthDate > DateAdd(Year, -21, GetDate())
AND ET.EventTypeID = 'Purchase property'
AND E.Price > 1000000)
BEGIN
SET @RET = 1
END
RETURN @RET
END
GO
ALTER TABLE tblEVENT
ADD CONSTRAINT CK_NoKids_BuyMansion
CHECK (dbo.fn_NoKids_BuyMansion() = 0)
GO
-- A property above 300 years are not valid to be listed
CREATE FUNCTION fn_NoAncientBuilding()
RETURNS INT
AS
BEGIN
DECLARE @RET INT = 0
IF EXISTS (SELECT * FROM tblPROPERTY
WHERE YEAR(GetDate()) - PropBuiltYear >= 300 )
BEGIN
SET @RET = 1
END
RETURN @RET
END
GO
ALTER TABLE tblPROPERTY
ADD CONSTRAINT CK_NoAncientBuilding
CHECK (dbo.fn_NoAncientBuilding() = 0)
GO