-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.sql
289 lines (274 loc) · 6.45 KB
/
init.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use master
begin try
alter database TradingViewIdeas set single_user with rollback immediate
drop database TradingViewIdeas
end try
begin catch end catch
create database TradingViewIdeas
go
use TradingViewIdeas
go
/* TABLES */
create table [User]
(
Id int primary key identity,
Username nvarchar(256) not null unique,
PasswordHash nvarchar(256) not null,
[Role] nvarchar(256) default 'User',
)
create table Idea
(
Id int primary key identity,
Title nvarchar(256),
Link nvarchar(512) unique,
[Description] nvarchar(max),
PicturePath nvarchar(256),
PublishedDate datetime2,
SymbolId int,
MarketId int,
)
create table Author
(
Id int primary key identity,
[Name] nvarchar(256),
Link nvarchar(512) unique,
)
create table IdeaAuthor
(
Id int primary key identity,
IdeaId int,
AuthorId int,
)
create table Symbol
(
Id int primary key identity,
[Name] nvarchar(256),
[Description] nvarchar(256),
Link nvarchar(512) unique,
)
create table Market
(
Id int primary key identity,
[Name] nvarchar(256) unique,
IsSelected bit default 0,
)
go
alter table Idea add
constraint FK_Idea_Symbol foreign key (SymbolId)
references Symbol(Id) on delete cascade,
constraint FK_Idea_Market foreign key (MarketId)
references Market(Id) on delete cascade
alter table IdeaAuthor add
constraint FK_IdeaAuthor_Idea foreign key (IdeaId)
references Idea(Id) on delete cascade,
constraint FK_IdeaAuthor_Author foreign key (AuthorId)
references Author(Id) on delete cascade
alter table IdeaAuthor add
constraint UQ_IdeaAuthorUnique unique (IdeaId, AuthorId)
go
/* PROCEDURES */
/* USER */
create procedure CreateUser
@Username nvarchar(256),
@PasswordHash nvarchar(256),
@Role nvarchar(256),
@Id int output
as
begin
insert into [User] values (@Username, @PasswordHash, @Role)
set @Id = SCOPE_IDENTITY()
end
go
create procedure SelectUserByUsername @Username nvarchar(256) as
select * from [User] where Username = @Username
go
/* IDEA */
create procedure CreateIdea
@Title nvarchar(256),
@Link nvarchar(512),
@Description nvarchar(max),
@PicturePath nvarchar(256),
@PublishedDate datetime2,
@SymbolId int,
@MarketId int,
@Id int output
as
begin
insert into Idea values (@Title, @Link, @Description, @PicturePath, @PublishedDate, @SymbolId, @MarketId)
set @Id = SCOPE_IDENTITY()
end
go
create procedure UpdateIdea
@Id int,
@Title nvarchar(256),
@Link nvarchar(512),
@Description nvarchar(max),
@PicturePath nvarchar(256),
@PublishedDate datetime2,
@SymbolId int,
@MarketId int
as
update Idea set
Title = @Title,
Link = @Link,
[Description] = @Description,
PicturePath = @PicturePath,
PublishedDate = @PublishedDate,
SymbolId = @SymbolId,
MarketId = @MarketId
where Id = @Id
go
create procedure DeleteIdea @Id int as
delete from Idea where Id = @Id
go
create view IdeaJoined as
select
Idea.[Id],
Idea.[Title],
Idea.[Link],
Idea.[Description],
Idea.[PicturePath],
Idea.[PublishedDate],
Symbol.[Id] as SymbolId,
Symbol.[Name] as SymbolName,
Symbol.[Description] as SymbolDescription,
Symbol.[Link] as SymbolLink,
Market.[Id] as MarketId,
Market.[Name] as MarketName,
Market.[IsSelected] as MarketIsSelected,
Author.[Id] as AuthorId,
Author.[Name] as AuthorName,
Author.[Link] as AuthorLink
from Idea
inner join Symbol on Idea.SymbolId = Symbol.Id
inner join Market on Idea.MarketId = Market.Id
left join IdeaAuthor on Idea.Id = IdeaAuthor.IdeaId
left join Author on IdeaAuthor.AuthorId = Author.Id
go
create procedure SelectIdea @Id int as
select * from IdeaJoined where Id = @Id
go
create procedure SelectIdeas as
select * from IdeaJoined
go
/* AUTHOR */
create procedure CreateAuthor
@Name nvarchar(256),
@Link nvarchar(512),
@Id int output
as
begin
insert into Author values (@Name, @Link)
set @Id = SCOPE_IDENTITY()
end
go
create procedure UpdateAuthor
@Id int,
@Name nvarchar(256),
@Link nvarchar(512)
as
update Author set
[Name] = @Name,
Link = @Link
where Id = @Id
go
create procedure DeleteAuthor @Id int as
delete from Author where Id = @Id
go
create procedure SelectAuthors as
select * from Author
go
/* IDEA_AUTHOR */
create procedure CreateIdeaAuthor
@IdeaId int,
@AuthorId int,
@Id int output
as
begin
insert into IdeaAuthor values (@IdeaId, @AuthorId)
set @Id = SCOPE_IDENTITY()
end
go
create procedure DeleteIdeaAuthorByIdeaId @IdeaId int as
delete from IdeaAuthor where IdeaAuthor.IdeaId = @IdeaId
go
create procedure DeleteIdeaAuthorByAuthorId @AuthorId int as
delete from IdeaAuthor where IdeaAuthor.AuthorId = @AuthorId
go
/* SYMBOL */
create procedure CreateSymbol
@Name nvarchar(256),
@Description nvarchar(256),
@Link nvarchar(512),
@Id int output
as
begin
insert into Symbol values (@Name, @Description, @Link)
set @Id = SCOPE_IDENTITY()
end
go
create procedure UpdateSymbol
@Id int,
@Name nvarchar(256),
@Description nvarchar(256),
@Link nvarchar(512)
as
update Symbol set
[Name] = @Name,
[Description] = @Description,
Link = @Link
where Id = @Id
go
create procedure DeleteSymbol @Id int as
delete from Symbol where Id = @Id
go
create procedure SelectSymbols as
select * from Symbol
go
/* MARKET */
create procedure CreateMarket
@Name nvarchar(256),
@IsSelected bit,
@Id int output
as
begin
insert into Market values (@Name, @IsSelected)
set @Id = SCOPE_IDENTITY()
end
go
create procedure SetSelectedMarket
@Id int,
@IsSelected bit
as
update Market set IsSelected = @IsSelected where Id = @Id
go
create procedure SelectMarkets as
select * from Market
go
/* OTHER */
create procedure DeleteAllContent as
begin
delete from Idea
delete from IdeaAuthor
delete from Author
delete from Symbol
dbcc checkident('Idea', reseed, 0)
dbcc checkident('IdeaAuthor', reseed, 0)
dbcc checkident('Author', reseed, 0)
dbcc checkident('Symbol', reseed, 0)
end
go
/* INSERTIONS */
/* USERS */
declare @Id int
exec CreateUser 'admin', '$2a$09$Q4KC0FpkkMtQTYNAmtA/Q.wRZurIxw4xMi3Z5SDmmVpmSQxh6OYw.', 'ADMIN', @Id output
exec CreateUser 'user', '$2a$12$XIOs1Mas/1VDxO4jUe5rG.VGNVZrilvwwwqiUR2fEBFnOXsGqSyNq', 'USER', @Id output
go
/* MARKETS */
declare @Id int
exec CreateMarket 'Commodities', 0, @Id output
exec CreateMarket 'Crypto', 0, @Id output
exec CreateMarket 'Currencies', 0, @Id output
exec CreateMarket 'Indices', 0, @Id output
exec CreateMarket 'Stocks', 0, @Id output
go