diff --git a/src/EdaSample.Services.Customer/Controllers/CustomersController.cs b/src/EdaSample.Services.Customer/Controllers/CustomersController.cs index 0709909..d0dfed3 100644 --- a/src/EdaSample.Services.Customer/Controllers/CustomersController.cs +++ b/src/EdaSample.Services.Customer/Controllers/CustomersController.cs @@ -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; @@ -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 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(sql, new { id }); + if (customer == null) + { + return NotFound(); + } + + return Ok(customer); + } + } + + // 创建新的客户信息 [HttpPost] public async Task 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); + } } } } diff --git a/src/EdaSample.Services.Customer/EdaSample.Services.Customer.csproj b/src/EdaSample.Services.Customer/EdaSample.Services.Customer.csproj index cf6c3ca..51aa45e 100644 --- a/src/EdaSample.Services.Customer/EdaSample.Services.Customer.csproj +++ b/src/EdaSample.Services.Customer/EdaSample.Services.Customer.csproj @@ -13,6 +13,7 @@ + diff --git a/src/EdaSample.Services.Customer/Model/Customer.cs b/src/EdaSample.Services.Customer/Model/Customer.cs index fe89052..d27b661 100644 --- a/src/EdaSample.Services.Customer/Model/Customer.cs +++ b/src/EdaSample.Services.Customer/Model/Customer.cs @@ -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; } } diff --git a/src/EdaSample.Services.Customer/appsettings.Development.json b/src/EdaSample.Services.Customer/appsettings.Development.json index fa8ce71..faffd9b 100644 --- a/src/EdaSample.Services.Customer/appsettings.Development.json +++ b/src/EdaSample.Services.Customer/appsettings.Development.json @@ -6,5 +6,8 @@ "System": "Information", "Microsoft": "Information" } + }, + "mssql": { + "connectionString": "Server=localhost\\sqlexpress;Database=EdaSample;User Id=eda;Password=eda;" } } diff --git a/src/EdaSample.Services.Customer/appsettings.json b/src/EdaSample.Services.Customer/appsettings.json index 069402d..26bb0ac 100644 --- a/src/EdaSample.Services.Customer/appsettings.json +++ b/src/EdaSample.Services.Customer/appsettings.json @@ -11,8 +11,5 @@ "Default": "Warning" } } - }, - "mssql": { - "connectionString": "Server=localhost\\sqlexpress;Database=EdaSample;User Id=eda;Password=eda;" } }