From 7d3298f5e13957c54b6b06ec788e3d0edc7ac155 Mon Sep 17 00:00:00 2001 From: Aaron Po Date: Thu, 20 Aug 2026 21:31:38 -0400 Subject: [PATCH] archive old stored procs/functions --- .../02-functions/UDF_GetCountryIdByCode.sql | 15 ++++ .../UDF_GetStateProvinceIdByCode.sql | 13 ++++ .../01-UserAccount/USP_CreateUserAccount.sql | 31 ++++++++ .../01-UserAccount/USP_DeleteUserAccount.sql | 20 +++++ .../01-UserAccount/USP_GetAllUserAccounts.sql | 15 ++++ .../USP_GetUserAccountByEmail.sql | 20 +++++ .../01-UserAccount/USP_GetUserAccountById.sql | 20 +++++ .../USP_GetUserAccountByUsername.sql | 20 +++++ .../01-UserAccount/USP_UpdateUserAccount.sql | 28 +++++++ .../USP_GetUserCredentialByUserAccountId.sql | 18 +++++ .../02-Auth/USP_InvalidateUserCredential.sql | 28 +++++++ .../03-crud/02-Auth/USP_RegisterUser.sql | 50 ++++++++++++ .../02-Auth/USP_RotateUserCredential.sql | 31 ++++++++ .../USP_AddUserVerification.sql | 27 +++++++ .../03-crud/04-Location/USP_CreateCity.sql | 38 +++++++++ .../03-crud/04-Location/USP_CreateCountry.sql | 24 ++++++ .../04-Location/USP_CreateStateProvince.sql | 32 ++++++++ .../05-Breweries/USP_CreateBrewery.sql | 59 ++++++++++++++ .../05-Breweries/USP_DeleteBrewery.sql | 17 ++++ .../05-Breweries/USP_GetAllBreweries.sql | 28 +++++++ .../05-Breweries/USP_GetBreweryById.sql | 9 +++ .../05-Breweries/USP_UpdateBrewery.sql | 77 +++++++++++++++++++ 22 files changed, 620 insertions(+) create mode 100644 archive/sql-stored-procs-functions/02-functions/UDF_GetCountryIdByCode.sql create mode 100644 archive/sql-stored-procs-functions/02-functions/UDF_GetStateProvinceIdByCode.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_CreateUserAccount.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_DeleteUserAccount.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetAllUserAccounts.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByEmail.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountById.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByUsername.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_UpdateUserAccount.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/02-Auth/USP_GetUserCredentialByUserAccountId.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/02-Auth/USP_InvalidateUserCredential.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RegisterUser.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RotateUserCredential.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/03-UserVerification/USP_AddUserVerification.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCity.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCountry.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateStateProvince.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_CreateBrewery.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_DeleteBrewery.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetAllBreweries.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetBreweryById.sql create mode 100644 archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_UpdateBrewery.sql diff --git a/archive/sql-stored-procs-functions/02-functions/UDF_GetCountryIdByCode.sql b/archive/sql-stored-procs-functions/02-functions/UDF_GetCountryIdByCode.sql new file mode 100644 index 00000000..c0ce88f1 --- /dev/null +++ b/archive/sql-stored-procs-functions/02-functions/UDF_GetCountryIdByCode.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/02-functions/UDF_GetStateProvinceIdByCode.sql b/archive/sql-stored-procs-functions/02-functions/UDF_GetStateProvinceIdByCode.sql new file mode 100644 index 00000000..baea6063 --- /dev/null +++ b/archive/sql-stored-procs-functions/02-functions/UDF_GetStateProvinceIdByCode.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_CreateUserAccount.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_CreateUserAccount.sql new file mode 100644 index 00000000..e8cc9351 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_CreateUserAccount.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_DeleteUserAccount.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_DeleteUserAccount.sql new file mode 100644 index 00000000..58f7b238 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_DeleteUserAccount.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetAllUserAccounts.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetAllUserAccounts.sql new file mode 100644 index 00000000..e23d90a0 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetAllUserAccounts.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByEmail.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByEmail.sql new file mode 100644 index 00000000..3ac92b53 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByEmail.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountById.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountById.sql new file mode 100644 index 00000000..0a83ebf5 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountById.sql @@ -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 diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByUsername.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByUsername.sql new file mode 100644 index 00000000..2b8919d5 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_GetUserAccountByUsername.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_UpdateUserAccount.sql b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_UpdateUserAccount.sql new file mode 100644 index 00000000..be30d16e --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/01-UserAccount/USP_UpdateUserAccount.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_GetUserCredentialByUserAccountId.sql b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_GetUserCredentialByUserAccountId.sql new file mode 100644 index 00000000..e962cb26 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_GetUserCredentialByUserAccountId.sql @@ -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; \ No newline at end of file diff --git a/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_InvalidateUserCredential.sql b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_InvalidateUserCredential.sql new file mode 100644 index 00000000..b655f2f2 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_InvalidateUserCredential.sql @@ -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; \ No newline at end of file diff --git a/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RegisterUser.sql b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RegisterUser.sql new file mode 100644 index 00000000..4c2a4597 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RegisterUser.sql @@ -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 diff --git a/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RotateUserCredential.sql b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RotateUserCredential.sql new file mode 100644 index 00000000..3408b182 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/02-Auth/USP_RotateUserCredential.sql @@ -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; \ No newline at end of file diff --git a/archive/sql-stored-procs-functions/03-crud/03-UserVerification/USP_AddUserVerification.sql b/archive/sql-stored-procs-functions/03-crud/03-UserVerification/USP_AddUserVerification.sql new file mode 100644 index 00000000..bdfe538d --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/03-UserVerification/USP_AddUserVerification.sql @@ -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 diff --git a/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCity.sql b/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCity.sql new file mode 100644 index 00000000..e95a4960 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCity.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCountry.sql b/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCountry.sql new file mode 100644 index 00000000..0f60e353 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateCountry.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateStateProvince.sql b/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateStateProvince.sql new file mode 100644 index 00000000..893e52e2 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/04-Location/USP_CreateStateProvince.sql @@ -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; diff --git a/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_CreateBrewery.sql b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_CreateBrewery.sql new file mode 100644 index 00000000..999c1588 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_CreateBrewery.sql @@ -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 diff --git a/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_DeleteBrewery.sql b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_DeleteBrewery.sql new file mode 100644 index 00000000..b4bf3905 --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_DeleteBrewery.sql @@ -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 diff --git a/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetAllBreweries.sql b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetAllBreweries.sql new file mode 100644 index 00000000..208be24f --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetAllBreweries.sql @@ -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 diff --git a/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetBreweryById.sql b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetBreweryById.sql new file mode 100644 index 00000000..25425efc --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_GetBreweryById.sql @@ -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 \ No newline at end of file diff --git a/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_UpdateBrewery.sql b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_UpdateBrewery.sql new file mode 100644 index 00000000..3f3ba76c --- /dev/null +++ b/archive/sql-stored-procs-functions/03-crud/05-Breweries/USP_UpdateBrewery.sql @@ -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