mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 18:14:44 +00:00
archive old stored procs/functions
This commit is contained in:
15
archive/sql-stored-procs-functions/02-functions/UDF_GetCountryIdByCode.sql
vendored
Normal file
15
archive/sql-stored-procs-functions/02-functions/UDF_GetCountryIdByCode.sql
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
CREATE OR ALTER FUNCTION dbo.UDF_GetCountryIdByCode(
|
||||||
|
@CountryCode NVARCHAR(2)
|
||||||
|
)
|
||||||
|
RETURNS UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
DECLARE
|
||||||
|
@CountryId UNIQUEIDENTIFIER;
|
||||||
|
|
||||||
|
SELECT @CountryId = CountryID
|
||||||
|
FROM dbo.Country
|
||||||
|
WHERE ISO3166_1 = @CountryCode;
|
||||||
|
|
||||||
|
RETURN @CountryId;
|
||||||
|
END;
|
||||||
13
archive/sql-stored-procs-functions/02-functions/UDF_GetStateProvinceIdByCode.sql
vendored
Normal file
13
archive/sql-stored-procs-functions/02-functions/UDF_GetStateProvinceIdByCode.sql
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
CREATE OR ALTER FUNCTION dbo.UDF_GetStateProvinceIdByCode(
|
||||||
|
@StateProvinceCode NVARCHAR(6)
|
||||||
|
)
|
||||||
|
RETURNS UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
DECLARE
|
||||||
|
@StateProvinceId UNIQUEIDENTIFIER;
|
||||||
|
SELECT @StateProvinceId = StateProvinceID
|
||||||
|
FROM dbo.StateProvince
|
||||||
|
WHERE ISO3166_2 = @StateProvinceCode;
|
||||||
|
RETURN @StateProvinceId;
|
||||||
|
END;
|
||||||
31
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_CreateUserAccount.sql
vendored
Normal file
31
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_CreateUserAccount.sql
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE usp_CreateUserAccount(
|
||||||
|
@UserAccountId UNIQUEIDENTIFIER OUTPUT,
|
||||||
|
@Username VARCHAR(64),
|
||||||
|
@FirstName NVARCHAR(128),
|
||||||
|
@LastName NVARCHAR(128),
|
||||||
|
@DateOfBirth DATETIME,
|
||||||
|
@Email VARCHAR(128)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
DECLARE
|
||||||
|
@Inserted TABLE
|
||||||
|
(
|
||||||
|
UserAccountID UNIQUEIDENTIFIER
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO UserAccount
|
||||||
|
(Username,
|
||||||
|
FirstName,
|
||||||
|
LastName,
|
||||||
|
DateOfBirth,
|
||||||
|
Email)
|
||||||
|
OUTPUT INSERTED.UserAccountID INTO @Inserted
|
||||||
|
VALUES (@Username, @FirstName, @LastName, @DateOfBirth, @Email);
|
||||||
|
|
||||||
|
SELECT @UserAccountId = UserAccountID
|
||||||
|
FROM @Inserted;
|
||||||
|
END;
|
||||||
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_DeleteUserAccount.sql
vendored
Normal file
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_DeleteUserAccount.sql
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE usp_DeleteUserAccount(
|
||||||
|
@UserAccountId UNIQUEIDENTIFIER
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM UserAccount WHERE UserAccountId = @UserAccountId)
|
||||||
|
BEGIN
|
||||||
|
RAISERROR
|
||||||
|
('UserAccount with the specified ID does not exist.', 16,
|
||||||
|
1);
|
||||||
|
ROLLBACK TRANSACTION
|
||||||
|
RETURN
|
||||||
|
END
|
||||||
|
|
||||||
|
DELETE
|
||||||
|
FROM UserAccount
|
||||||
|
WHERE UserAccountId = @UserAccountId;
|
||||||
|
END;
|
||||||
15
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetAllUserAccounts.sql
vendored
Normal file
15
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetAllUserAccounts.sql
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE usp_GetAllUserAccounts
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON;
|
||||||
|
SELECT UserAccountID,
|
||||||
|
Username,
|
||||||
|
FirstName,
|
||||||
|
LastName,
|
||||||
|
Email,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
DateOfBirth,
|
||||||
|
Timer
|
||||||
|
FROM dbo.UserAccount;
|
||||||
|
END;
|
||||||
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByEmail.sql
vendored
Normal file
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByEmail.sql
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE usp_GetUserAccountByEmail(
|
||||||
|
@Email VARCHAR(128)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
SELECT UserAccountID,
|
||||||
|
Username,
|
||||||
|
FirstName,
|
||||||
|
LastName,
|
||||||
|
Email,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
DateOfBirth,
|
||||||
|
Timer
|
||||||
|
FROM dbo.UserAccount
|
||||||
|
WHERE Email = @Email;
|
||||||
|
END;
|
||||||
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountById.sql
vendored
Normal file
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountById.sql
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE USP_GetUserAccountById(
|
||||||
|
@UserAccountId UNIQUEIDENTIFIER
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
SELECT UserAccountID,
|
||||||
|
Username,
|
||||||
|
FirstName,
|
||||||
|
LastName,
|
||||||
|
Email,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
DateOfBirth,
|
||||||
|
Timer
|
||||||
|
FROM dbo.UserAccount
|
||||||
|
WHERE UserAccountID = @UserAccountId;
|
||||||
|
END
|
||||||
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByUsername.sql
vendored
Normal file
20
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByUsername.sql
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE usp_GetUserAccountByUsername(
|
||||||
|
@Username VARCHAR(64)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
SELECT UserAccountID,
|
||||||
|
Username,
|
||||||
|
FirstName,
|
||||||
|
LastName,
|
||||||
|
Email,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
DateOfBirth,
|
||||||
|
Timer
|
||||||
|
FROM dbo.UserAccount
|
||||||
|
WHERE Username = @Username;
|
||||||
|
END;
|
||||||
28
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_UpdateUserAccount.sql
vendored
Normal file
28
archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_UpdateUserAccount.sql
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE usp_UpdateUserAccount(
|
||||||
|
@Username VARCHAR(64),
|
||||||
|
@FirstName NVARCHAR(128),
|
||||||
|
@LastName NVARCHAR(128),
|
||||||
|
@DateOfBirth DATETIME,
|
||||||
|
@Email VARCHAR(128),
|
||||||
|
@UserAccountId UNIQUEIDENTIFIER
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
UPDATE UserAccount
|
||||||
|
SET Username = @Username,
|
||||||
|
FirstName = @FirstName,
|
||||||
|
LastName = @LastName,
|
||||||
|
DateOfBirth = @DateOfBirth,
|
||||||
|
Email = @Email
|
||||||
|
WHERE UserAccountId = @UserAccountId;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@@ROWCOUNT = 0
|
||||||
|
BEGIN
|
||||||
|
THROW
|
||||||
|
50001, 'UserAccount with the specified ID does not exist.', 1;
|
||||||
|
END
|
||||||
|
END;
|
||||||
18
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_GetUserCredentialByUserAccountId.sql
vendored
Normal file
18
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_GetUserCredentialByUserAccountId.sql
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_GetActiveUserCredentialByUserAccountId(
|
||||||
|
@UserAccountId UNIQUEIDENTIFIER
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
SELECT UserCredentialId,
|
||||||
|
UserAccountId,
|
||||||
|
Hash,
|
||||||
|
IsRevoked,
|
||||||
|
CreatedAt,
|
||||||
|
RevokedAt
|
||||||
|
FROM dbo.UserCredential
|
||||||
|
WHERE UserAccountId = @UserAccountId
|
||||||
|
AND IsRevoked = 0;
|
||||||
|
END;
|
||||||
28
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_InvalidateUserCredential.sql
vendored
Normal file
28
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_InvalidateUserCredential.sql
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_InvalidateUserCredential(
|
||||||
|
@UserAccountId_ UNIQUEIDENTIFIER
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
EXEC dbo.USP_GetUserAccountByID @UserAccountId = @UserAccountId_;
|
||||||
|
IF
|
||||||
|
@@ROWCOUNT = 0
|
||||||
|
THROW 50001, 'User account not found', 1;
|
||||||
|
|
||||||
|
-- invalidate all other credentials by setting them to revoked
|
||||||
|
UPDATE dbo.UserCredential
|
||||||
|
SET IsRevoked = 1,
|
||||||
|
RevokedAt = GETDATE()
|
||||||
|
WHERE UserAccountId = @UserAccountId_
|
||||||
|
AND IsRevoked != 1;
|
||||||
|
|
||||||
|
|
||||||
|
COMMIT TRANSACTION;
|
||||||
|
END;
|
||||||
50
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RegisterUser.sql
vendored
Normal file
50
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RegisterUser.sql
vendored
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_RegisterUser(
|
||||||
|
@Username VARCHAR(64),
|
||||||
|
@FirstName NVARCHAR(128),
|
||||||
|
@LastName NVARCHAR(128),
|
||||||
|
@DateOfBirth DATETIME,
|
||||||
|
@Email VARCHAR(128),
|
||||||
|
@Hash NVARCHAR(MAX)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
DECLARE
|
||||||
|
@UserAccountId_ UNIQUEIDENTIFIER;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
EXEC usp_CreateUserAccount
|
||||||
|
@UserAccountId = @UserAccountId_ OUTPUT,
|
||||||
|
@Username = @Username,
|
||||||
|
@FirstName = @FirstName,
|
||||||
|
@LastName = @LastName,
|
||||||
|
@DateOfBirth = @DateOfBirth,
|
||||||
|
@Email = @Email;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@UserAccountId_ IS NULL
|
||||||
|
BEGIN
|
||||||
|
THROW
|
||||||
|
50000, 'Failed to create user account.', 1;
|
||||||
|
END
|
||||||
|
|
||||||
|
INSERT INTO dbo.UserCredential
|
||||||
|
(UserAccountId, Hash)
|
||||||
|
VALUES (@UserAccountId_, @Hash);
|
||||||
|
|
||||||
|
IF
|
||||||
|
@@ROWCOUNT = 0
|
||||||
|
BEGIN
|
||||||
|
THROW
|
||||||
|
50002, 'Failed to create user credential.', 1;
|
||||||
|
END
|
||||||
|
COMMIT TRANSACTION;
|
||||||
|
|
||||||
|
SELECT @UserAccountId_ AS UserAccountId;
|
||||||
|
END
|
||||||
31
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RotateUserCredential.sql
vendored
Normal file
31
archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RotateUserCredential.sql
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_RotateUserCredential(
|
||||||
|
@UserAccountId_ UNIQUEIDENTIFIER,
|
||||||
|
@Hash NVARCHAR(MAX)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
EXEC USP_GetUserAccountByID @UserAccountId = @UserAccountId_
|
||||||
|
|
||||||
|
IF @@ROWCOUNT = 0
|
||||||
|
THROW 50001, 'User account not found', 1;
|
||||||
|
|
||||||
|
|
||||||
|
-- invalidate all other credentials -- set them to revoked
|
||||||
|
UPDATE dbo.UserCredential
|
||||||
|
SET IsRevoked = 1,
|
||||||
|
RevokedAt = GETDATE()
|
||||||
|
WHERE UserAccountId = @UserAccountId_;
|
||||||
|
|
||||||
|
INSERT INTO dbo.UserCredential
|
||||||
|
(UserAccountId, Hash)
|
||||||
|
VALUES (@UserAccountId_, @Hash);
|
||||||
|
|
||||||
|
|
||||||
|
END;
|
||||||
27
archive/sql-stored-procs-functions/03-crud/03-UserVerification/USP_AddUserVerification.sql
vendored
Normal file
27
archive/sql-stored-procs-functions/03-crud/03-UserVerification/USP_AddUserVerification.sql
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_CreateUserVerification @UserAccountID_ UNIQUEIDENTIFIER,
|
||||||
|
@VerificationDateTime DATETIME = NULL
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@VerificationDateTime IS NULL
|
||||||
|
SET @VerificationDateTime = GETDATE();
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
EXEC USP_GetUserAccountByID @UserAccountId = @UserAccountID_;
|
||||||
|
IF
|
||||||
|
@@ROWCOUNT = 0
|
||||||
|
THROW 50001, 'Could not find a user with that id', 1;
|
||||||
|
|
||||||
|
INSERT INTO dbo.UserVerification
|
||||||
|
(UserAccountId, VerificationDateTime)
|
||||||
|
VALUES (@UserAccountID_, @VerificationDateTime);
|
||||||
|
|
||||||
|
COMMIT TRANSACTION;
|
||||||
|
END
|
||||||
38
archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCity.sql
vendored
Normal file
38
archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCity.sql
vendored
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_CreateCity(
|
||||||
|
@CityName NVARCHAR(100),
|
||||||
|
@StateProvinceCode NVARCHAR(6)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION
|
||||||
|
DECLARE
|
||||||
|
@StateProvinceId UNIQUEIDENTIFIER = dbo.UDF_GetStateProvinceIdByCode(@StateProvinceCode);
|
||||||
|
IF
|
||||||
|
@StateProvinceId IS NULL
|
||||||
|
BEGIN
|
||||||
|
THROW
|
||||||
|
50001, 'State/province does not exist', 1;
|
||||||
|
END
|
||||||
|
|
||||||
|
IF
|
||||||
|
EXISTS (SELECT 1
|
||||||
|
FROM dbo.City
|
||||||
|
WHERE CityName = @CityName
|
||||||
|
AND StateProvinceID = @StateProvinceId)
|
||||||
|
BEGIN
|
||||||
|
|
||||||
|
THROW
|
||||||
|
50002, 'City already exists.', 1;
|
||||||
|
END
|
||||||
|
|
||||||
|
INSERT INTO dbo.City
|
||||||
|
(StateProvinceID, CityName)
|
||||||
|
VALUES (@StateProvinceId, @CityName);
|
||||||
|
COMMIT TRANSACTION
|
||||||
|
END;
|
||||||
24
archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCountry.sql
vendored
Normal file
24
archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCountry.sql
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_CreateCountry(
|
||||||
|
@CountryName NVARCHAR(100),
|
||||||
|
@ISO3166_1 NVARCHAR(2)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
IF
|
||||||
|
EXISTS (SELECT 1
|
||||||
|
FROM dbo.Country
|
||||||
|
WHERE ISO3166_1 = @ISO3166_1)
|
||||||
|
THROW 50001, 'Country already exists', 1;
|
||||||
|
|
||||||
|
INSERT INTO dbo.Country
|
||||||
|
(CountryName, ISO3166_1)
|
||||||
|
VALUES (@CountryName, @ISO3166_1);
|
||||||
|
COMMIT TRANSACTION;
|
||||||
|
END;
|
||||||
32
archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateStateProvince.sql
vendored
Normal file
32
archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateStateProvince.sql
vendored
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_CreateStateProvince(
|
||||||
|
@StateProvinceName NVARCHAR(100),
|
||||||
|
@ISO3166_2 NVARCHAR(6),
|
||||||
|
@CountryCode NVARCHAR(2)
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
IF
|
||||||
|
EXISTS (SELECT 1
|
||||||
|
FROM dbo.StateProvince
|
||||||
|
WHERE ISO3166_2 = @ISO3166_2)
|
||||||
|
RETURN;
|
||||||
|
|
||||||
|
DECLARE
|
||||||
|
@CountryId UNIQUEIDENTIFIER = dbo.UDF_GetCountryIdByCode(@CountryCode);
|
||||||
|
IF
|
||||||
|
@CountryId IS NULL
|
||||||
|
BEGIN
|
||||||
|
THROW
|
||||||
|
50001, 'Country does not exist', 1;
|
||||||
|
|
||||||
|
END
|
||||||
|
|
||||||
|
INSERT INTO dbo.StateProvince
|
||||||
|
(StateProvinceName, ISO3166_2, CountryID)
|
||||||
|
VALUES (@StateProvinceName, @ISO3166_2, @CountryId);
|
||||||
|
END;
|
||||||
59
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_CreateBrewery.sql
vendored
Normal file
59
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_CreateBrewery.sql
vendored
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_CreateBrewery(
|
||||||
|
@BreweryName NVARCHAR(256),
|
||||||
|
@Description NVARCHAR(512),
|
||||||
|
@PostedByID UNIQUEIDENTIFIER,
|
||||||
|
@CityID UNIQUEIDENTIFIER,
|
||||||
|
@AddressLine1 NVARCHAR(256),
|
||||||
|
@AddressLine2 NVARCHAR(256) = NULL,
|
||||||
|
@PostalCode NVARCHAR(20),
|
||||||
|
@Coordinates GEOGRAPHY = NULL
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@BreweryName IS NULL
|
||||||
|
THROW 50001, 'Brewery name cannot be null.', 1;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@Description IS NULL
|
||||||
|
THROW 50002, 'Brewery description cannot be null.', 1;
|
||||||
|
|
||||||
|
IF
|
||||||
|
NOT EXISTS (SELECT 1
|
||||||
|
FROM dbo.UserAccount
|
||||||
|
WHERE UserAccountID = @PostedByID)
|
||||||
|
THROW 50404, 'User not found.', 1;
|
||||||
|
|
||||||
|
IF
|
||||||
|
NOT EXISTS (SELECT 1
|
||||||
|
FROM dbo.City
|
||||||
|
WHERE CityID = @CityID)
|
||||||
|
THROW 50404, 'City not found.', 1;
|
||||||
|
|
||||||
|
DECLARE
|
||||||
|
@NewBreweryID UNIQUEIDENTIFIER = NEWID();
|
||||||
|
DECLARE
|
||||||
|
@NewBrewerLocationID UNIQUEIDENTIFIER = NEWID();
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
INSERT INTO dbo.BreweryPost
|
||||||
|
(BreweryPostID, BreweryName, Description, PostedByID)
|
||||||
|
VALUES (@NewBreweryID, @BreweryName, @Description, @PostedByID);
|
||||||
|
|
||||||
|
INSERT INTO dbo.BreweryPostLocation
|
||||||
|
(BreweryPostLocationID, BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates)
|
||||||
|
VALUES (@NewBrewerLocationID, @NewBreweryID, @CityID, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates);
|
||||||
|
|
||||||
|
COMMIT TRANSACTION;
|
||||||
|
|
||||||
|
SELECT @NewBreweryID AS BreweryPostID,
|
||||||
|
@NewBrewerLocationID AS BreweryPostLocationID;
|
||||||
|
|
||||||
|
END
|
||||||
17
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_DeleteBrewery.sql
vendored
Normal file
17
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_DeleteBrewery.sql
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_DeleteBrewery @BreweryPostID UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
IF
|
||||||
|
NOT EXISTS (SELECT 1
|
||||||
|
FROM dbo.BreweryPost
|
||||||
|
WHERE BreweryPostID = @BreweryPostID)
|
||||||
|
THROW 50404, 'Brewery not found.', 1;
|
||||||
|
|
||||||
|
-- BreweryPostLocation and BreweryPostPhoto cascade-delete with their parent BreweryPost.
|
||||||
|
DELETE
|
||||||
|
FROM dbo.BreweryPost
|
||||||
|
WHERE BreweryPostID = @BreweryPostID;
|
||||||
|
END
|
||||||
28
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetAllBreweries.sql
vendored
Normal file
28
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetAllBreweries.sql
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_GetAllBreweries(
|
||||||
|
@Limit INT = NULL,
|
||||||
|
@Offset INT = NULL
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
|
||||||
|
SELECT bp.BreweryPostID,
|
||||||
|
bp.PostedByID,
|
||||||
|
bp.BreweryName,
|
||||||
|
bp.Description,
|
||||||
|
bp.CreatedAt,
|
||||||
|
bp.UpdatedAt,
|
||||||
|
bp.Timer,
|
||||||
|
bpl.BreweryPostLocationID,
|
||||||
|
bpl.CityID,
|
||||||
|
bpl.AddressLine1,
|
||||||
|
bpl.AddressLine2,
|
||||||
|
bpl.PostalCode,
|
||||||
|
bpl.Coordinates
|
||||||
|
FROM dbo.BreweryPost bp
|
||||||
|
LEFT JOIN dbo.BreweryPostLocation bpl
|
||||||
|
ON bp.BreweryPostID = bpl.BreweryPostID
|
||||||
|
ORDER BY bp.CreatedAt DESC
|
||||||
|
OFFSET ISNULL(@Offset, 0) ROWS FETCH NEXT ISNULL(@Limit, 2147483647) ROWS ONLY;
|
||||||
|
END
|
||||||
9
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetBreweryById.sql
vendored
Normal file
9
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetBreweryById.sql
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_GetBreweryById @BreweryPostID UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SELECT *
|
||||||
|
FROM BreweryPost bp
|
||||||
|
INNER JOIN BreweryPostLocation bpl
|
||||||
|
ON bp.BreweryPostID = bpl.BreweryPostID
|
||||||
|
WHERE bp.BreweryPostID = @BreweryPostID;
|
||||||
|
END
|
||||||
77
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_UpdateBrewery.sql
vendored
Normal file
77
archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_UpdateBrewery.sql
vendored
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_UpdateBrewery(
|
||||||
|
@BreweryPostID UNIQUEIDENTIFIER,
|
||||||
|
@BreweryName NVARCHAR(256),
|
||||||
|
@Description NVARCHAR(512),
|
||||||
|
@BreweryPostLocationID UNIQUEIDENTIFIER = NULL,
|
||||||
|
@CityID UNIQUEIDENTIFIER = NULL,
|
||||||
|
@AddressLine1 NVARCHAR(256) = NULL,
|
||||||
|
@AddressLine2 NVARCHAR(256) = NULL,
|
||||||
|
@PostalCode NVARCHAR(20) = NULL,
|
||||||
|
@Coordinates GEOGRAPHY = NULL
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET
|
||||||
|
NOCOUNT ON;
|
||||||
|
SET
|
||||||
|
XACT_ABORT ON;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@BreweryName IS NULL
|
||||||
|
THROW 50001, 'Brewery name cannot be null.', 1;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@Description IS NULL
|
||||||
|
THROW 50002, 'Brewery description cannot be null.', 1;
|
||||||
|
|
||||||
|
IF
|
||||||
|
NOT EXISTS (SELECT 1
|
||||||
|
FROM dbo.BreweryPost
|
||||||
|
WHERE BreweryPostID = @BreweryPostID)
|
||||||
|
THROW 50404, 'Brewery not found.', 1;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@CityID IS NOT NULL AND NOT EXISTS (SELECT 1
|
||||||
|
FROM dbo.City
|
||||||
|
WHERE CityID = @CityID)
|
||||||
|
THROW 50404, 'City not found.', 1;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
TRANSACTION;
|
||||||
|
|
||||||
|
UPDATE dbo.BreweryPost
|
||||||
|
SET BreweryName = @BreweryName,
|
||||||
|
Description = @Description,
|
||||||
|
UpdatedAt = GETDATE()
|
||||||
|
WHERE BreweryPostID = @BreweryPostID;
|
||||||
|
|
||||||
|
IF
|
||||||
|
@CityID IS NULL
|
||||||
|
BEGIN
|
||||||
|
-- No location supplied: clear any existing location for this brewery.
|
||||||
|
DELETE
|
||||||
|
FROM dbo.BreweryPostLocation
|
||||||
|
WHERE BreweryPostID = @BreweryPostID;
|
||||||
|
END
|
||||||
|
ELSE
|
||||||
|
IF EXISTS (SELECT 1
|
||||||
|
FROM dbo.BreweryPostLocation
|
||||||
|
WHERE BreweryPostID = @BreweryPostID)
|
||||||
|
BEGIN
|
||||||
|
UPDATE dbo.BreweryPostLocation
|
||||||
|
SET CityID = @CityID,
|
||||||
|
AddressLine1 = @AddressLine1,
|
||||||
|
AddressLine2 = @AddressLine2,
|
||||||
|
PostalCode = @PostalCode,
|
||||||
|
Coordinates = @Coordinates
|
||||||
|
WHERE BreweryPostID = @BreweryPostID;
|
||||||
|
END
|
||||||
|
ELSE
|
||||||
|
BEGIN
|
||||||
|
INSERT INTO dbo.BreweryPostLocation
|
||||||
|
(BreweryPostLocationID, BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates)
|
||||||
|
VALUES (ISNULL(@BreweryPostLocationID, NEWID()), @BreweryPostID, @CityID, @AddressLine1, @AddressLine2,
|
||||||
|
@PostalCode, @Coordinates);
|
||||||
|
END
|
||||||
|
COMMIT TRANSACTION;
|
||||||
|
END
|
||||||
Reference in New Issue
Block a user