Skip to content

Commit 1221011

Browse files
committed
first commit
1 parent 9c01d6b commit 1221011

11 files changed

+339
-0
lines changed

.vscode/launch.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/azure-sql-db-dotnet-rest-api.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
"stopAtEntry": false,
17+
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18+
"serverReadyAction": {
19+
"action": "openExternally",
20+
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
21+
},
22+
"env": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
},
25+
"sourceFileMap": {
26+
"/Views": "${workspaceFolder}/Views"
27+
}
28+
},
29+
{
30+
"name": ".NET Core Attach",
31+
"type": "coreclr",
32+
"request": "attach",
33+
"processId": "${command:pickProcess}"
34+
}
35+
]
36+
}

.vscode/tasks.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/azure-sql-db-dotnet-rest-api.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/azure-sql-db-dotnet-rest-api.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/azure-sql-db-dotnet-rest-api.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
}
41+
]
42+
}

Controllers/ControllerQuery.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using System.Text.Json;
8+
using System.Data;
9+
using Microsoft.Data.SqlClient;
10+
using Dapper;
11+
using Microsoft.Extensions.Configuration;
12+
13+
namespace AzureSamples.AzureSQL.Controllers
14+
{
15+
public class ControllerQuery : ControllerBase
16+
{
17+
private readonly ILogger<CustomersController> _logger;
18+
private readonly IConfiguration _config;
19+
20+
public ControllerQuery(IConfiguration config, ILogger<CustomersController> logger)
21+
{
22+
_logger = logger;
23+
_config = config;
24+
}
25+
26+
protected async Task<JsonDocument> Query(string verb, Type entity)
27+
{
28+
JsonDocument result = null;
29+
30+
if (!(new string[] {"get", "put", "patch", "delete"}).Contains(verb.ToLower()))
31+
{
32+
throw new ArgumentException($"verb '{verb}' not supported", nameof(verb));
33+
}
34+
35+
string entityName = entity.Name.Replace("Controller", string.Empty).ToLower();
36+
string procedure = $"web.{verb}_{entityName}";
37+
_logger.LogDebug($"Executing {procedure}");
38+
39+
using(var conn = new SqlConnection(_config.GetConnectionString("DefaultConnection"))) {
40+
var qr = await conn.ExecuteScalarAsync<string>(procedure, commandType: CommandType.StoredProcedure);
41+
result = JsonDocument.Parse(qr);
42+
};
43+
44+
if (result == null)
45+
result = JsonDocument.Parse("[]");
46+
47+
return result;
48+
}
49+
}
50+
}

Controllers/CustomerController.cs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.Data.SqlClient;
8+
using Dapper;
9+
10+
namespace AzureSamples.AzureSQL.Controllers
11+
{
12+
[ApiController]
13+
[Route("[controller]")]
14+
public class CustomerController : ControllerBase
15+
{
16+
private static readonly string[] Summaries = new[]
17+
{
18+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
19+
};
20+
21+
private readonly ILogger<CustomerController> _logger;
22+
23+
public CustomerController(ILogger<CustomerController> logger)
24+
{
25+
_logger = logger;
26+
}
27+
28+
[HttpGet]
29+
public IEnumerable<dynamic> Get()
30+
{
31+
return null;
32+
}
33+
}
34+
}

Controllers/CustomersController.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using System.Text.Json;
8+
using System.Data;
9+
using Microsoft.Data.SqlClient;
10+
using Dapper;
11+
using Microsoft.Extensions.Configuration;
12+
13+
namespace AzureSamples.AzureSQL.Controllers
14+
{
15+
[ApiController]
16+
[Route("[controller]")]
17+
public class CustomersController : ControllerQuery
18+
{
19+
public CustomersController(IConfiguration config, ILogger<CustomersController> logger):
20+
base(config, logger) {}
21+
22+
[HttpGet]
23+
public async Task<JsonDocument> Get()
24+
{
25+
return await this.Query("get", this.GetType());
26+
}
27+
}
28+
}

Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace AzureSamples.AzureSQL
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}

Properties/launchSettings.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:17959",
8+
"sslPort": 44341
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "customers",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"azure_sql_db_dotnet_rest_api": {
21+
"commandName": "Project",
22+
"launchBrowser": true,
23+
"launchUrl": "customers",
24+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}

Startup.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
12+
using Microsoft.Extensions.Logging;
13+
14+
namespace AzureSamples.AzureSQL
15+
{
16+
public class Startup
17+
{
18+
public Startup(IConfiguration configuration)
19+
{
20+
Configuration = configuration;
21+
}
22+
23+
public IConfiguration Configuration { get; }
24+
25+
// This method gets called by the runtime. Use this method to add services to the container.
26+
public void ConfigureServices(IServiceCollection services)
27+
{
28+
services.AddControllers();
29+
}
30+
31+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
33+
{
34+
if (env.IsDevelopment())
35+
{
36+
app.UseDeveloperExceptionPage();
37+
}
38+
39+
app.UseHttpsRedirection();
40+
41+
app.UseRouting();
42+
43+
app.UseAuthorization();
44+
45+
app.UseEndpoints(endpoints =>
46+
{
47+
endpoints.MapControllers();
48+
});
49+
}
50+
}
51+
}

appsettings.Development.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Microsoft": "Information"
7+
}
8+
},
9+
"ConnectionStrings": {
10+
"DefaultConnection": "Server=dmmssqlsrv.database.windows.net;Database=WideWorldImportersStandard;UID=PythonWebApp;PWD=a987REALLY#$%TRONGpa44w0rd"
11+
}
12+
}

appsettings.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*",
10+
"ConnectionStrings": {
11+
"DefaultConnection": "Server=.database.windows.net;Database=;UID=;PWD="
12+
}
13+
}

azure-sql-db-dotnet-rest-api.csproj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<RootNamespace>AzureSamples.AzureSQL</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Dapper" Version="2.0.30" />
13+
<PackageReference Include="Microsoft.Data.SqlClient" Version="1.1.0" />
14+
<PackageReference Include="System.Text.Json" Version="4.7.0" />
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)