|
| 1 | +/* |
| 2 | + Create schema |
| 3 | +*/ |
| 4 | +IF SCHEMA_ID('web') IS NULL BEGIN |
| 5 | + EXECUTE('CREATE SCHEMA [web]'); |
| 6 | +END |
| 7 | +GO |
| 8 | + |
| 9 | +/* |
| 10 | + Create user to be used in the sample API solution |
| 11 | +*/ |
| 12 | +IF USER_ID('DotNetWebApp') IS NULL BEGIN |
| 13 | + CREATE USER [DotNetWebApp] WITH PASSWORD = 'a987REALLY#$%TRONGpa44w0rd'; |
| 14 | +END |
| 15 | + |
| 16 | +/* |
| 17 | + Grant execute permission to created users |
| 18 | +*/ |
| 19 | +GRANT EXECUTE ON SCHEMA::[web] TO [DotNetWebApp]; |
| 20 | +GO |
| 21 | + |
| 22 | +/* |
| 23 | + Return details on a specific customer |
| 24 | +*/ |
| 25 | +CREATE OR ALTER PROCEDURE web.get_customer |
| 26 | +@Id INT, |
| 27 | +@Json NVARCHAR(MAX) |
| 28 | +AS |
| 29 | +SET NOCOUNT ON; |
| 30 | +SELECT |
| 31 | + [CustomerID], |
| 32 | + [CustomerName], |
| 33 | + [PhoneNumber], |
| 34 | + [FaxNumber], |
| 35 | + [WebsiteURL], |
| 36 | + [DeliveryAddressLine1] AS 'Delivery.AddressLine1', |
| 37 | + [DeliveryAddressLine2] AS 'Delivery.AddressLine2', |
| 38 | + [DeliveryPostalCode] AS 'Delivery.PostalCode' |
| 39 | +FROM |
| 40 | + [Sales].[Customers] |
| 41 | +WHERE |
| 42 | + [CustomerID] = @Id |
| 43 | +FOR JSON PATH |
| 44 | +GO |
| 45 | + |
| 46 | +/* |
| 47 | + Delete a specific customer |
| 48 | +*/ |
| 49 | +CREATE OR ALTER PROCEDURE web.delete_customer |
| 50 | +@Id INT |
| 51 | +AS |
| 52 | +SET NOCOUNT ON; |
| 53 | +DELETE FROM [Sales].[Customers] WHERE CustomerId = @Id; |
| 54 | +SELECT * FROM (SELECT CustomerID = @Id) D FOR JSON AUTO; |
| 55 | +GO |
| 56 | + |
| 57 | +/* |
| 58 | + Update (Patch) a specific customer |
| 59 | +*/ |
| 60 | +CREATE OR ALTER PROCEDURE web.patch_customer |
| 61 | +@Id INT, |
| 62 | +@Json NVARCHAR(MAX) |
| 63 | +AS |
| 64 | +SET NOCOUNT ON; |
| 65 | +WITH [source] AS |
| 66 | +( |
| 67 | + SELECT * FROM OPENJSON(@Json) WITH ( |
| 68 | + [CustomerID] INT, |
| 69 | + [CustomerName] NVARCHAR(100), |
| 70 | + [PhoneNumber] NVARCHAR(20), |
| 71 | + [FaxNumber] NVARCHAR(20), |
| 72 | + [WebsiteURL] NVARCHAR(256), |
| 73 | + [DeliveryAddressLine1] NVARCHAR(60) '$.Delivery.AddressLine1', |
| 74 | + [DeliveryAddressLine2] NVARCHAR(60) '$.Delivery.AddressLine2', |
| 75 | + [DeliveryPostalCode] NVARCHAR(10) '$.Delivery.PostalCode' |
| 76 | + ) |
| 77 | +) |
| 78 | +UPDATE |
| 79 | + t |
| 80 | +SET |
| 81 | + t.[CustomerName] = COALESCE(s.[CustomerName], t.[CustomerName]), |
| 82 | + t.[PhoneNumber] = COALESCE(s.[PhoneNumber], t.[PhoneNumber]), |
| 83 | + t.[FaxNumber] = COALESCE(s.[FaxNumber], t.[FaxNumber]), |
| 84 | + t.[WebsiteURL] = COALESCE(s.[WebsiteURL], t.[WebsiteURL]), |
| 85 | + t.[DeliveryAddressLine1] = COALESCE(s.[DeliveryAddressLine1], t.[DeliveryAddressLine1]), |
| 86 | + t.[DeliveryAddressLine2] = COALESCE(s.[DeliveryAddressLine2], t.[DeliveryAddressLine2]), |
| 87 | + t.[DeliveryPostalCode] = COALESCE(s.[DeliveryPostalCode], t.[DeliveryPostalCode]) |
| 88 | +FROM |
| 89 | + [Sales].[Customers] t |
| 90 | +INNER JOIN |
| 91 | + [source] s ON t.[CustomerID] = s.[CustomerID] |
| 92 | +WHERE |
| 93 | + t.CustomerId = @Id; |
| 94 | + |
| 95 | +EXEC web.get_customer @Id; |
| 96 | +GO |
| 97 | + |
| 98 | +/* |
| 99 | + Create a new customer |
| 100 | +*/ |
| 101 | + |
| 102 | +CREATE OR ALTER PROCEDURE web.put_customer |
| 103 | +@Json NVARCHAR(MAX) |
| 104 | +AS |
| 105 | +SET NOCOUNT ON; |
| 106 | +DECLARE @CustomerId INT = NEXT VALUE FOR Sequences.CustomerID; |
| 107 | +WITH [source] AS |
| 108 | +( |
| 109 | + SELECT * FROM OPENJSON(@Json) WITH ( |
| 110 | + [CustomerName] NVARCHAR(100), |
| 111 | + [PhoneNumber] NVARCHAR(20), |
| 112 | + [FaxNumber] NVARCHAR(20), |
| 113 | + [WebsiteURL] NVARCHAR(256), |
| 114 | + [DeliveryAddressLine1] NVARCHAR(60) '$.Delivery.AddressLine1', |
| 115 | + [DeliveryAddressLine2] NVARCHAR(60) '$.Delivery.AddressLine2', |
| 116 | + [DeliveryPostalCode] NVARCHAR(10) '$.Delivery.PostalCode' |
| 117 | + ) |
| 118 | +) |
| 119 | +INSERT INTO [Sales].[Customers] |
| 120 | +( |
| 121 | + CustomerID, |
| 122 | + CustomerName, |
| 123 | + BillToCustomerID, |
| 124 | + CustomerCategoryID, |
| 125 | + PrimaryContactPersonID, |
| 126 | + DeliveryMethodID, |
| 127 | + DeliveryCityID, |
| 128 | + PostalCityID, |
| 129 | + AccountOpenedDate, |
| 130 | + StandardDiscountPercentage, |
| 131 | + IsStatementSent, |
| 132 | + IsOnCreditHold, |
| 133 | + PaymentDays, |
| 134 | + PhoneNumber, |
| 135 | + FaxNumber, |
| 136 | + WebsiteURL, |
| 137 | + DeliveryAddressLine1, |
| 138 | + DeliveryAddressLine2, |
| 139 | + DeliveryPostalCode, |
| 140 | + PostalAddressLine1, |
| 141 | + PostalAddressLine2, |
| 142 | + PostalPostalCode, |
| 143 | + LastEditedBy |
| 144 | +) |
| 145 | +SELECT |
| 146 | + @CustomerId, |
| 147 | + CustomerName, |
| 148 | + @CustomerId, |
| 149 | + 5, -- Computer Shop |
| 150 | + 1, -- No contact person |
| 151 | + 1, -- Post Delivery |
| 152 | + 28561, -- Redmond |
| 153 | + 28561, -- Redmond |
| 154 | + SYSUTCDATETIME(), |
| 155 | + 0.00, |
| 156 | + 0, |
| 157 | + 0, |
| 158 | + 30, |
| 159 | + PhoneNumber, |
| 160 | + FaxNumber, |
| 161 | + WebsiteURL, |
| 162 | + DeliveryAddressLine1, |
| 163 | + DeliveryAddressLine2, |
| 164 | + DeliveryPostalCode, |
| 165 | + DeliveryAddressLine1, |
| 166 | + DeliveryAddressLine2, |
| 167 | + DeliveryPostalCode, |
| 168 | + 1 |
| 169 | +FROM |
| 170 | + [source] |
| 171 | +; |
| 172 | + |
| 173 | +EXEC web.get_customer @CustomerId; |
| 174 | +GO |
| 175 | + |
| 176 | +CREATE OR ALTER PROCEDURE web.get_customers |
| 177 | +AS |
| 178 | +SET NOCOUNT ON; |
| 179 | +-- Cast is needed to corretly inform pyodbc of output type is NVARCHAR(MAX) |
| 180 | +-- Needed if generated json is bigger then 4000 bytes and thus pyodbc trucates it |
| 181 | +-- https://stackoverflow.com/questions/49469301/pyodbc-truncates-the-response-of-a-sql-server-for-json-query |
| 182 | +SELECT CAST(( |
| 183 | + SELECT |
| 184 | + [CustomerID], |
| 185 | + [CustomerName] |
| 186 | + FROM |
| 187 | + [Sales].[Customers] |
| 188 | + FOR JSON PATH) AS NVARCHAR(MAX)) AS JsonResult |
| 189 | +GO |
| 190 | + |
0 commit comments