Skip to content

Commit d5d0407

Browse files
committed
implemented all verbs
1 parent 33b2b30 commit d5d0407

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

Controllers/ControllerQuery.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ControllerQuery(IConfiguration config, ILogger<CustomersController> logge
2323
_config = config;
2424
}
2525

26-
protected async Task<JsonDocument> Query(string verb, Type entity)
26+
protected async Task<JsonElement> Query(string verb, Type entity, int? id = null, JsonElement payload = default(JsonElement))
2727
{
2828
JsonDocument result = null;
2929

@@ -37,14 +37,31 @@ protected async Task<JsonDocument> Query(string verb, Type entity)
3737
_logger.LogDebug($"Executing {procedure}");
3838

3939
using(var conn = new SqlConnection(_config.GetConnectionString("DefaultConnection"))) {
40-
var qr = await conn.ExecuteScalarAsync<string>(procedure, commandType: CommandType.StoredProcedure);
41-
result = JsonDocument.Parse(qr);
40+
DynamicParameters parameters = new DynamicParameters();
41+
42+
if (payload.ValueKind != default(JsonValueKind))
43+
{
44+
var json = JsonSerializer.Serialize(payload);
45+
parameters.Add("Json", json);
46+
}
47+
48+
if (id.HasValue)
49+
parameters.Add("Id", id.Value);
50+
51+
var qr = await conn.ExecuteScalarAsync<string>(
52+
sql: procedure,
53+
param: parameters,
54+
commandType: CommandType.StoredProcedure
55+
);
56+
57+
if (qr != null)
58+
result = JsonDocument.Parse(qr);
4259
};
4360

4461
if (result == null)
4562
result = JsonDocument.Parse("[]");
4663

47-
return result;
64+
return result.RootElement;
4865
}
4966
}
5067
}

Controllers/CustomerController.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,40 @@
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.Extensions.Logging;
7-
using Microsoft.Data.SqlClient;
8-
using Dapper;
7+
using System.Text.Json;
8+
using Microsoft.Extensions.Configuration;
99

1010
namespace AzureSamples.AzureSQL.Controllers
1111
{
1212
[ApiController]
1313
[Route("[controller]")]
14-
public class CustomerController : ControllerBase
14+
public class CustomerController : ControllerQuery
1515
{
16-
private static readonly string[] Summaries = new[]
16+
public CustomerController(IConfiguration config, ILogger<CustomersController> logger):
17+
base(config, logger) {}
18+
19+
[HttpGet("{customerId}")]
20+
public async Task<JsonElement> Get(int customerId)
1721
{
18-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
19-
};
22+
return await this.Query("get", this.GetType(), customerId);
23+
}
2024

21-
private readonly ILogger<CustomerController> _logger;
25+
[HttpPut]
26+
public async Task<JsonElement> Put([FromBody]JsonElement payload)
27+
{
28+
return await this.Query("put", this.GetType(), payload: payload);
29+
}
2230

23-
public CustomerController(ILogger<CustomerController> logger)
31+
[HttpPatch("{customerId}")]
32+
public async Task<JsonElement> Patch([FromBody]JsonElement payload, int customerId)
2433
{
25-
_logger = logger;
34+
return await this.Query("patch", this.GetType(), customerId, payload);
2635
}
2736

28-
[HttpGet]
29-
public IEnumerable<dynamic> Get()
37+
[HttpDelete("{customerId}")]
38+
public async Task<JsonElement> Delete(int customerId)
3039
{
31-
return null;
40+
return await this.Query("delete", this.GetType(), customerId);
3241
}
3342
}
3443
}

Controllers/CustomersController.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.Extensions.Logging;
77
using System.Text.Json;
8-
using System.Data;
9-
using Microsoft.Data.SqlClient;
10-
using Dapper;
118
using Microsoft.Extensions.Configuration;
129

1310
namespace AzureSamples.AzureSQL.Controllers
@@ -20,7 +17,7 @@ public CustomersController(IConfiguration config, ILogger<CustomersController> l
2017
base(config, logger) {}
2118

2219
[HttpGet]
23-
public async Task<JsonDocument> Get()
20+
public async Task<JsonElement> Get()
2421
{
2522
return await this.Query("get", this.GetType());
2623
}

0 commit comments

Comments
 (0)