CREATE DATABASE BLUEFIREDATABASE
USE BLUEFIREDATABASE

---------------------------------------------------------------------------------------------------
--Reset Identity

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

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


CREATE TABLE [Admin](
	AdminID INT PRIMARY KEY IDENTITY(1,1) NOT NULL, 
	AdminName VARCHAR(50),
	AdminPassword VARCHAR(50)
)
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(50),
	UserName VARCHAR(50),
	DOB VARCHAR(50),
	Gender VARCHAR(50),
	[Address] VARCHAR(50),
	Phone VARCHAR(50),
	Email VARCHAR(50)
)
--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 (50)
)

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 (50),
	AddressDetail VARCHAR(50)
)
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(50),
	CityID INT FOREIGN KEY REFERENCES city(cityId),
	JobLevel VARCHAR(50),
	Deadline VARCHAR (50),
	JobDescription VARCHAR(50),
)
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(50),
	PrevCompanyName VARCHAR(50),
	PrevPosition VARCHAR(50),
	UniversityName VARCHAR(50),
	CvLink VARCHAR(50),
	PortofolioLink VARCHAR(50),
	StatusEntry VARCHAR(50)
)
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
----------------------------------
---------STORED PROCEDURES----------
----------------------------------

--Done
CREATE PROC sp_getAllCity
AS
BEGIN
SET NOCOUNT ON
	SELECT  CityID,CountryID,CityName,AddressDetail FROM City
END
GO
--Done
CREATE PROC sp_insertAdmin
	@password VARCHAR(50),
	@name VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [Admin] (AdminName,AdminPassword)
VALUES (@name,@password)
END
GO
--Done
CREATE PROC sp_insertUser
	@password VARCHAR(50),
	@name VARCHAR(50),
	@DOB VARCHAR(50),
	@gender VARCHAR(50),
	@address VARCHAR(50),
	@phone VARCHAR(50),
	@email VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [User] ([Password],UserName,DOB,Gender,Address,Phone,Email)
VALUES (@password, @name, @DOB, @gender, @address, @phone, @email)
END
GO
--Done
CREATE PROC sp_loginUser
	@name VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
	SELECT UserName FROM [User]
	WHERE ([User].UserName=@name)
END
--Done
GO
CREATE PROC sp_loginAdmin
	@name VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
	SELECT AdminName FROM [Admin]
	WHERE ([Admin].AdminName=@name)
END

---Done
GO
CREATE PROC sp_insertJob
	@name VARCHAR(50),
	@cityId INT,
	@jobLevel VARCHAR(50),
	@deadline VARCHAR (50),
	@description VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO Job (JobName,CityID,JobLevel,Deadline,JobDescription)
VALUES(@name, @cityId, @jobLevel, @deadline, @description)
END
GO
--Done
CREATE PROC sp_deleteJob
	@jobId INT
AS
BEGIN
SET NOCOUNT ON
DELETE FROM Job
WHERE JobID = @jobId
END
GO
--Done
CREATE PROC sp_insertBookmark
	@userId INT,
	@jobId INT
AS 
BEGIN
SET NOCOUNT ON
INSERT INTO UserBookmark (UserID,JobID)
VALUES(@userId, @jobId)
END
-----------------------------------------
GO
--Done
CREATE PROC sp_getBookmark
	@userId INT
AS
BEGIN
SET NOCOUNT ON
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
SET NOCOUNT ON
	DELETE FROM UserBookmark WHERE BookmarkID = @bookmarkID
END
GO
--DONE 
CREATE PROC sp_insertEntry
	@userId INT,
	@jobId INT,
	@email VARCHAR(50),
	@prevCompanyName VARCHAR(50),
	@prevPosition VARCHAR(50),
	@universityName VARCHAR(50),
	@cvLink VARCHAR(50),
	@portofolioLink VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO ApplicationEntry (UserID,JobID,Email,PrevCompanyName,PrevPosition,UniversityName,CvLink,PortofolioLink,StatusEntry)
VALUES(@userId, @jobId, @email, @prevCompanyName, @prevPosition, @universityName, @cvLink, @portofolioLink, 'pending')

END
GO
--DONE
CREATE PROC sp_getAllCountry
AS
BEGIN
SET NOCOUNT ON
SELECT CountryName,CountryID 
FROM Country
END
GO
--DONE
CREATE PROC sp_getAllJob
AS
BEGIN
SET NOCOUNT ON
SELECT JobName,JobDescription,JobLevel,JobID,Deadline,CityID FROM Job
END
GO
--Done
CREATE PROC sp_denyApplicationEntry
@applicationID INT
AS
BEGIN
SET NOCOUNT ON
	UPDATE ApplicationEntry SET StatusEntry='Denied'
	WHERE EntryID = @applicationID
END
GO
--Done
CREATE PROC sp_getJob
@jobId INT
AS
BEGIN
SET NOCOUNT ON
	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

--Done
GO
CREATE PROC sp_hiredApplicationEntry
@applicationID INT
AS
BEGIN
SET NOCOUNT ON
	UPDATE ApplicationEntry SET StatusEntry='Accepted'
	WHERE EntryID = @applicationID
END
GO
--Done
CREATE PROC sp_getAllEntry
--	@userid INT
AS
BEGIN
SET NOCOUNT ON
	SELECT EntryID,u.UserID,JobID,ae.Email,PrevCompanyName,PrevPosition,UniversityName,CvLink,PortofolioLink,StatusEntry FROM ApplicationEntry ae
	JOIN [User] u
	ON ae.UserID=u.UserID
--	WHERE UserID = @userID
END
----------------------------------------
GO
--Done
CREATE PROC sp_getListJob
AS
BEGIN
SET NOCOUNT ON
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(50),
	@cityId INT,
	@jobLevel VARCHAR(50),
	@deadline VARCHAR (50),
	@description VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON
	UPDATE Job SET JobName=@name,CityID=@cityId,JobLevel=@jobLevel,Deadline=@deadline,JobDescription=@description
	WHERE JobID=@jobId
END
GO