CREATE DATABASE BLUEFIREDATABASE
USE BLUEFIREDATABASE

---------------------------------------------------------------------------------------------------
--LOGIN

--SELECT * FROM [Admin]
--WHERE AdminName = 'Budi' AND [Admin].Password = 'budi321'

--SELECT * FROM [User]
--WHERE UserName='Budi' AND [User].Password='budi321'

--data.UserName == $('[name="username"]').val() AND data.UserPassword == $('[name="password"]').val()
--location.href('')


--Reset Identity

--    DBCC CHECKIDENT ('Emp', RESEED, 1)  


----------------------------------------------------------------------------------------------------


CREATE TABLE [Admin](
	AdminID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, 
	AdminName VARCHAR(MAX),
	AdminPassword VARCHAR(MAX)
)
INSERT INTO [Admin] VALUES ('admin', 'admin123');
INSERT INTO [Admin] VALUES ('adminHero', 'admin321');
GO
CREATE TABLE [User](
	UserID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
	[Password] VARCHAR(MAX),
	UserName VARCHAR(MAX),
	DOB VARCHAR(MAX),
	Gender VARCHAR(MAX),
	[Address] VARCHAR(MAX),
	Phone VARCHAR(MAX),
	Email VARCHAR(MAX)
)
--DBCC CHECKIDENT ('[User]', RESEED, 0)  

INSERT INTO [User] VALUES('kevin123','Kevin','11/11/2000','Male','SS Street','082211323456','Kevin@gmail.com')
INSERT INTO [User] VALUES('budi321','Budi','08/12/2001','Male','M Street','082211234533','Budi@gmail.com')
INSERT INTO [User] VALUES('Julia111','Julia','3/1/1999','Female','K Street','089098123849','Julia@gmail.com')
GO
CREATE TABLE Country(
	CountryID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	CountryName VARCHAR (MAX)
)


INSERT INTO Country VALUES('Indonesia')
INSERT INTO Country VALUES('Singapore')
INSERT INTO Country VALUES('India')
GO
CREATE TABLE City(
	CityID INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
	CountryID INT FOREIGN KEY REFERENCES Country(CountryID),
	CityName VARCHAR (MAX),
	AddressDetail VARCHAR(MAX)
)
INSERT INTO City VALUES(1,'Jakarta','Jakarta Selatan')
INSERT INTO City VALUES(1,'Jakarta','Jakarta Selatan')
INSERT INTO City VALUES(2,'Singapore','Singapore')
INSERT INTO City VALUES(3,'Bengaluru','Bengaluru')
GO
CREATE TABLE Job(
	JobID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
	JobName VARCHAR(MAX),
	CityID INT FOREIGN KEY REFERENCES city(cityId),
	JobLevel VARCHAR(MAX),
	Deadline VARCHAR (MAX),
	JobDescription VARCHAR(MAX),
)
INSERT INTO Job VALUES('IT Infrastructure',1,'Easy','12/12/2012','need computer knowledge')
INSERT INTO Job VALUES('Trust and Safety Associate',2,'Medium','12/01/2013','cyber knowledge')
INSERT INTO Job VALUES('HR System Analyst',1,'Medium','8/1/2012','Communication knowledge')
GO
CREATE TABLE UserBookmark(
	BookmarkID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
	UserID INT FOREIGN KEY REFERENCES [User](userId) ON UPDATE CASCADE ON DELETE CASCADE,
	JobID INT FOREIGN KEY REFERENCES Job(jobId) ON UPDATE CASCADE ON DELETE CASCADE
)
INSERT INTO UserBookmark VALUES(1,1)
INSERT INTO UserBookmark VALUES(1,2)
INSERT INTO UserBookmark VALUES(2,2)
INSERT INTO UserBookmark VALUES(2,3)
INSERT INTO UserBookmark VALUES(3,3)
GO
CREATE TABLE ApplicationEntry(
	EntryID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
	UserID INT FOREIGN KEY REFERENCES [User](userId) ON UPDATE CASCADE ON DELETE CASCADE,
	JobID INT FOREIGN KEY REFERENCES Job(jobId) ON UPDATE CASCADE ON DELETE CASCADE,
	Email VARCHAR(MAX),
	PrevCompanyName VARCHAR(MAX),
	PrevPosition VARCHAR(MAX),
	UniversityName VARCHAR(MAX),
	CvLink VARCHAR(MAX),
	PortofolioLink VARCHAR(MAX),
	StatusEntry VARCHAR(MAX)
)
INSERT INTO ApplicationEntry VALUES(1,1,'K@k.com','Grab','Enterprise Security Lead','ITS','www.cvUser1.com','www.portUser1.com','pending')
INSERT INTO ApplicationEntry VALUES(2,2,'B@b.coms','Uber','Head of Data Science','Sunib','www.cvUser2.com','www.portUser2.com','pending')
GO
---------------------------
SELECT*FROM [Admin]
SELECT*FROM [User]
SELECT*FROM Country
SELECT*FROM City
SELECT*FROM Job
SELECT*FROM UserBookmark
SELECT*FROM ApplicationEntry
GO
----------------------------------
--Done
CREATE PROC sp_getAllCity
AS
BEGIN
	SELECT * FROM City
END
GO
--Done
CREATE PROC sp_insertAdmin
	@password VARCHAR(MAX),
	@name VARCHAR(MAX)
AS
BEGIN
INSERT INTO [Admin] 
VALUES (@name,@password)
END
GO
--Done
CREATE PROC sp_insertUser
	@password VARCHAR(MAX),
	@name VARCHAR(MAX),
	@DOB VARCHAR(MAX),
	@gender VARCHAR(MAX),
	@address VARCHAR(MAX),
	@phone VARCHAR(MAX),
	@email VARCHAR(MAX)
AS
BEGIN
INSERT INTO [User] VALUES (@password, @name, @DOB, @gender, @address, @phone, @email)
END
GO
--Done
CREATE PROC sp_loginUser
	@name VARCHAR(MAX)
AS
BEGIN
	SELECT * FROM [User]
	WHERE ([User].UserName=@name)
END
--Done
GO
CREATE PROC sp_loginAdmin
	@name VARCHAR(MAX)
AS
BEGIN
	SELECT * FROM [Admin]
	WHERE ([Admin].AdminName=@name)
END

---Done
GO
CREATE PROC sp_insertJob
	@name VARCHAR(MAX),
	@cityId INT,
	@jobLevel VARCHAR(MAX),
	@deadline VARCHAR (MAX),
	@description VARCHAR(MAX)
AS
BEGIN
INSERT INTO Job 
VALUES(@name, @cityId, @jobLevel, @deadline, @description)
END
GO
--Done
CREATE PROC sp_deleteJob
	@jobId INT
AS
BEGIN
DELETE FROM Job
WHERE JobID = @jobId
END
GO
--Done Without real ID
CREATE PROC sp_insertBookmark
	@userId INT,
	@jobId INT
AS 
BEGIN
INSERT INTO UserBookmark 
VALUES(@userId, @jobId)
END
-----------------------------------------
GO
--Done Without real ID
CREATE PROC sp_getBookmark
	@userId INT
AS
BEGIN
SELECT ub.BookmarkID,j.JobID,JobName,CountryName,CityName, COUNT (EntryID)  AS ApplicantCount, (CASE WHEN CONVERT(DATE,Deadline) > GETDATE() THEN 1 ELSE 0 END ) As IsOpen FROM Job j
JOIN UserBookmark ub
ON j.jobId = ub.jobId
LEFT JOIN ApplicationEntry ae
ON ae.JobID=j.JobID
JOIN City ci
ON ci.CityID = j.CityID
JOIN Country co
ON ci.CountryID = co.CountryID
WHERE ub.userId = @userId

GROUP BY ub.BookmarkID,j.JobID,JobName,CountryName,CityName,Deadline
END
GO
--Done
CREATE PROC sp_deleteBookmark
@bookmarkID INT
AS
BEGIN
	DELETE FROM UserBookmark WHERE BookmarkID = @bookmarkID
END
GO
--DONE Without real Id
CREATE PROC sp_insertEntry
	@userId INT,
	@jobId INT,
	@email VARCHAR(MAX),
	@prevCompanyName VARCHAR(MAX),
	@prevPosition VARCHAR(MAX),
	@universityName VARCHAR(MAX),
	@cvLink VARCHAR(MAX),
	@portofolioLink VARCHAR(MAX)
AS
BEGIN
INSERT INTO ApplicationEntry VALUES(@userId, @jobId, @email, @prevCompanyName, @prevPosition, @universityName, @cvLink, @portofolioLink, 'pending')

END
GO
--DONE
CREATE PROC sp_getAllCountry
AS
BEGIN
SELECT * 
FROM Country
END
GO
--DONE
CREATE PROC sp_getAllJob
AS
BEGIN
SELECT * FROM Job
END
GO
--Done
CREATE PROC sp_denyApplicationEntry
@applicationID INT
AS
BEGIN
	UPDATE ApplicationEntry SET StatusEntry='Denied'
	WHERE EntryID = @applicationID
END
GO
--Done
CREATE PROC sp_getJob
@jobId INT
AS
BEGIN
	SELECT j.JobID,JobName,co.CountryID,CountryName,ci.CityID,CityName,AddressDetail, Deadline,JobLevel,JobDescription, COUNT (EntryID) AS ApplicantCount
	FROM [Job] j
	LEFT JOIN ApplicationEntry ae
	ON ae.JobID=j.JobID
	JOIN City ci
	ON ci.CityID = j.CityID
	JOIN Country co
	ON ci.CountryID = co.CountryID
	WHERE j.JobID = @jobID
	GROUP BY j.JobID,JobName,co.CountryID,CountryName,ci.CityID,CityName,Deadline,JobLevel,JobDescription,AddressDetail
END
GO
CREATE PROC sp_hiredApplicationEntry
@applicationID INT
AS
BEGIN
	UPDATE ApplicationEntry SET StatusEntry='Accepted'
	WHERE EntryID = @applicationID
END
GO
--done without real ID
CREATE PROC sp_getAllEntry
--	@userid INT
AS
BEGIN
	SELECT * FROM ApplicationEntry ae
	JOIN [User] u
	ON ae.UserID=u.UserID
--	WHERE UserID = @userID
END
----------------------------------------
GO
--Done
CREATE PROC sp_getListJob
AS
BEGIN
SELECT j.JobID,JobName,co.CountryID,CountryName,ci.CityID, CityName, COUNT (EntryID)  AS ApplicantCount, (CASE WHEN CONVERT(DATE,Deadline) > GETDATE() THEN 1 ELSE 0 END ) As IsOpen
FROM Job j
LEFT JOIN ApplicationEntry ae
ON ae.JobID=j.JobID
JOIN City ci
ON ci.CityID = j.CityID
JOIN Country co
ON ci.CountryID = co.CountryID
GROUP BY j.JobID,JobName,co.CountryID,CountryName,ci.CityID,CityName,Deadline
END
GO
--Done
CREATE PROC sp_updateJob
	@jobId INT,
	@name VARCHAR(MAX),
	@cityId INT,
	@jobLevel VARCHAR(MAX),
	@deadline VARCHAR (MAX),
	@description VARCHAR(MAX)
AS
BEGIN
	UPDATE Job SET JobName=@name,CityID=@cityId,JobLevel=@jobLevel,Deadline=@deadline,JobDescription=@description
	WHERE JobID=@jobId
END
GO