Skip to content

Commit

Permalink
Completed the sample code for chapter 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
daxnet committed Dec 21, 2017
1 parent b2f5bb9 commit e8962f0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
50 changes: 45 additions & 5 deletions src/EdaSample.Services.Customer/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using EdaSample.Common.Events;
using Dapper;
using EdaSample.Common.Events;
using EdaSample.Services.Customer.Events;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -11,19 +14,56 @@ namespace EdaSample.Services.Customer.Controllers
[Route("api/[controller]")]
public class CustomersController : Controller
{
private readonly IConfiguration configuration;
private readonly string connectionString;
private readonly IEventBus eventBus;

public CustomersController(IEventBus eventBus)
public CustomersController(IConfiguration configuration,
IEventBus eventBus)
{
this.configuration = configuration;
this.connectionString = configuration["mssql:connectionString"];
this.eventBus = eventBus;
}


// 获取指定ID的客户信息
[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
const string sql = "SELECT [CustomerId] AS Id, [CustomerName] AS Name FROM [dbo].[Customers] WHERE [CustomerId]=@id";
using (var connection = new SqlConnection(connectionString))
{
var customer = await connection.QueryFirstOrDefaultAsync<Model.Customer>(sql, new { id });
if (customer == null)
{
return NotFound();
}

return Ok(customer);
}
}

// 创建新的客户信息
[HttpPost]
public async Task<IActionResult> Create([FromBody] dynamic model)
{
var customerName = (string)model.Name;
await this.eventBus.PublishAsync(new CustomerCreatedEvent(customerName));
return Ok();
var name = (string)model.Name;
if (string.IsNullOrEmpty(name))
{
return BadRequest();
}

const string sql = "INSERT INTO [dbo].[Customers] ([CustomerId], [CustomerName]) VALUES (@Id, @Name)";
using (var connection = new SqlConnection(connectionString))
{
var customer = new Model.Customer(name);
await connection.ExecuteAsync(sql, customer);

await this.eventBus.PublishAsync(new CustomerCreatedEvent(name));

return Created(Url.Action("Get", new { id = customer.Id }), customer.Id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.4" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/EdaSample.Services.Customer/Model/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Customer(Guid id, string name)
this.Name = name;
}

public Guid Id { get; }
public Guid Id { get; set; }

public string Name { get; set; }
}
Expand Down
3 changes: 3 additions & 0 deletions src/EdaSample.Services.Customer/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"System": "Information",
"Microsoft": "Information"
}
},
"mssql": {
"connectionString": "Server=localhost\\sqlexpress;Database=EdaSample;User Id=eda;Password=eda;"
}
}
3 changes: 0 additions & 3 deletions src/EdaSample.Services.Customer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
"Default": "Warning"
}
}
},
"mssql": {
"connectionString": "Server=localhost\\sqlexpress;Database=EdaSample;User Id=eda;Password=eda;"
}
}

0 comments on commit e8962f0

Please sign in to comment.