-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
313 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/Leo.Web.Data.SQLite/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Leo.Web.Data.SqlServer/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Leo.Web.Data.Services; | ||
using Leo.Web.Data.SqlServer.Repositories; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Leo.Web.Data.SqlServer | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDataServices(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<IDbConnectionFactory, DbConnectionFactory>(); | ||
services.AddScoped<IDatabaseService, DatabaseService>(); | ||
services.AddScoped<ICustomerRepository, CustomerRepository>(); | ||
services.AddScoped<ICustomerDetailRepository, CustomerDetailRepository>(); | ||
services.AddScoped<IUnitOfWork, UnitOfWork>(); | ||
services.AddCQRS(); | ||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Leo.Web.Data\Leo.Web.Data.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Alyio.Extensions" Version="2.3.0" /> | ||
<PackageReference Include="Dapper" Version="2.1.21" /> | ||
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
59 changes: 59 additions & 0 deletions
59
src/Leo.Web.Data.SqlServer/Repositories/CustomerDetailRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Dapper; | ||
using Leo.Data.Domain.Entities; | ||
|
||
namespace Leo.Web.Data.SqlServer.Repositories | ||
{ | ||
internal sealed class CustomerDetailRepository : ICustomerDetailRepository | ||
{ | ||
private readonly IDbConnectionFactory _dbConnectionManager; | ||
|
||
public CustomerDetailRepository(IDbConnectionFactory dbConnectionManager) | ||
{ | ||
_dbConnectionManager = dbConnectionManager; | ||
} | ||
|
||
public async Task<Guid> CreateAsync(CustomerDetail detail) | ||
{ | ||
detail.Id = Guid.NewGuid(); | ||
|
||
var parameters = new DynamicParameters(); | ||
parameters.Add("id", detail.Id); | ||
parameters.Add("customer_id", detail.CustomerId); | ||
parameters.Add("date", detail.Date); | ||
parameters.Add("item", detail.Item); | ||
parameters.Add("count", detail.Count); | ||
parameters.Add("height", detail.Height); | ||
parameters.Add("weight", detail.Weight); | ||
parameters.Add("created_at", detail.CreatedAt); | ||
parameters.Add("created_by", detail.CreatedBy); | ||
var commandText = "INSERT INTO customer_detail (id, customer_id, date, item, count, height, weight," | ||
+ "created_at, created_by) " | ||
+ "VALUES (@id, @customer_id, @date, @item, @count, @height, @weight, " | ||
+ "@created_at, @created_by)"; | ||
var cmdDef = new CommandDefinition(commandText, parameters); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
await conn.ExecuteAsync(cmdDef).ConfigureAwait(false); | ||
return detail.Id; | ||
} | ||
|
||
public async Task<CustomerDetail?> GetByIdAsync(Guid id) | ||
{ | ||
var commandText = "SELECT * FROM customer_detail WHERE id = @id"; | ||
var parameters = new DynamicParameters(); | ||
parameters.Add("id", id); | ||
var cmdDef = new CommandDefinition(commandText, parameters); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
return await conn.QueryFirstOrDefaultAsync<CustomerDetail>(cmdDef).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<IEnumerable<CustomerDetail>> GetByCustomerIdAsync(Guid customerId) | ||
{ | ||
var commandText = "SELECT * FROM customer_detail WHERE customer_id = @customer_id"; | ||
var parameters = new DynamicParameters(); | ||
parameters.Add("customer_id", customerId); | ||
var cmdDef = new CommandDefinition(commandText, parameters); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
return await conn.QueryAsync<CustomerDetail>(cmdDef).ConfigureAwait(false); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/Leo.Web.Data.SqlServer/Repositories/CustomerRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Dapper; | ||
using Leo.Data.Domain.Entities; | ||
|
||
namespace Leo.Web.Data.SqlServer.Repositories | ||
{ | ||
internal sealed class CustomerRepository : ICustomerRepository | ||
{ | ||
private readonly IDbConnectionFactory _dbConnectionManager; | ||
|
||
public CustomerRepository(IDbConnectionFactory dbConnectionManager) | ||
{ | ||
_dbConnectionManager = dbConnectionManager; | ||
} | ||
|
||
public async Task<Customer?> GetAsync(Guid id) | ||
{ | ||
var commandText = "SELECT * FROM customer " | ||
+ "WHERE id = @id"; | ||
var parameters = new DynamicParameters(); | ||
parameters.Add("id", id); | ||
var cmdDef = new CommandDefinition(commandText, parameters); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
return await conn.QueryFirstOrDefaultAsync<Customer>(cmdDef).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<IEnumerable<Customer>> GetAsync() | ||
{ | ||
var commandText = "SELECT * FROM customer"; | ||
var cmdDef = new CommandDefinition(commandText: commandText); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
return await conn.QueryAsync<Customer>(cmdDef).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<Guid> CreateAsync(Customer customer) | ||
{ | ||
customer.Id = Guid.NewGuid(); | ||
|
||
var commandText = "INSERT INTO customer (id, name, phone, gender, birthday, cardno," | ||
+ "created_at, created_by) " | ||
+ "VALUES (@id, @name, @phone, @gender, @birthday, @cardno, " | ||
+ "@created_at, @created_by)"; | ||
var parameters = new DynamicParameters(); | ||
parameters.Add("id", customer.Id); | ||
parameters.Add("name", customer.Name); | ||
parameters.Add("phone", customer.Phone); | ||
parameters.Add("gender", customer.Gender); | ||
parameters.Add("birthday", customer.Birthday); | ||
parameters.Add("cardno", customer.CardNo); | ||
parameters.Add("created_at", customer.CreatedAt); | ||
parameters.Add("created_by", customer.CreatedBy); ; | ||
var cmdDef = new CommandDefinition(commandText, parameters); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
await conn.ExecuteAsync(cmdDef).ConfigureAwait(false); | ||
return customer.Id; | ||
} | ||
|
||
public async Task UpdateAsync(Customer customer) | ||
{ | ||
var commandText = "UPDATE customer " | ||
+ "SET name=@name, phone=@phone, gender=@gender, birthday=@birthday, cardno=@cardno, " | ||
+ "updated_at = @updated_at, updated_by = @updated_by " | ||
+ "WHERE id=@id"; | ||
var parameters = new DynamicParameters(); | ||
parameters.Add("id", customer.Id); | ||
parameters.Add("name", customer.Name); | ||
parameters.Add("phone", customer.Phone); | ||
parameters.Add("gender", customer.Gender); | ||
parameters.Add("birthday", customer.Birthday); | ||
parameters.Add("cardno", customer.CardNo); | ||
parameters.Add("updated_at", customer.UpdatedAt); | ||
parameters.Add("updated_by", customer.UpdatedBy); | ||
var cmdDef = new CommandDefinition(commandText, parameters); | ||
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false); | ||
await conn.ExecuteAsync(cmdDef).ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- USE [Customers]; | ||
-- GO | ||
|
||
DROP TABLE IF EXISTS [dbo].[customer]; | ||
GO | ||
CREATE TABLE [dbo].[customer] | ||
( | ||
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, | ||
[name] NVARCHAR(50) NOT NULL, | ||
[phone] NVARCHAR(20) NOT NULL, | ||
[gender] NVARCHAR(10) NULL, | ||
[birthday] DATETIME NULL, | ||
[cardno] NVARCHAR(50) NULL, | ||
[created_at] DATETIME NULL, | ||
[created_by] NVARCHAR(50) NULL, | ||
[updated_at] DATETIME NULL, | ||
[updated_by] NVARCHAR(50) NULL | ||
); | ||
GO | ||
|
||
DROP TABLE IF EXISTS [dbo].[customer_detail]; | ||
GO | ||
CREATE TABLE [dbo].[customer_detail] | ||
( | ||
[Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY, | ||
[customer_id] UNIQUEIDENTIFIER NOT NULL, | ||
[date] DATE NOT NULL, | ||
[item] NVARCHAR(50) NOT NULL, | ||
[count] INT NULL, | ||
[height] NUMERIC(18, 2) NULL, | ||
[weight] NUMERIC(18, 2) NULL, | ||
[created_at] DATETIME NULL, | ||
[created_by] NVARCHAR(50) NULL, | ||
[updated_at] DATETIME NULL, | ||
[updated_by] NVARCHAR(50) NULL | ||
); | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Leo.Web.Data.Services | ||
{ | ||
internal class DatabaseService : IDatabaseService | ||
{ | ||
public Task InitializeAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Leo.Web.Data.SqlServer/Services/DbConnectionFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Data.Common; | ||
|
||
namespace Leo.Web.Data.Services | ||
{ | ||
internal class DbConnectionFactory : IDbConnectionFactory | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public DbConnectionFactory(IConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public DbConnection Open() | ||
{ | ||
var conn = GetDbConnection(); | ||
conn.Open(); | ||
return conn; | ||
} | ||
|
||
public async Task<DbConnection> OpenAsync() | ||
{ | ||
var conn = GetDbConnection(); | ||
await conn.OpenAsync().ConfigureAwait(false); | ||
|
||
return conn; | ||
} | ||
|
||
private SqlConnection GetDbConnection() | ||
{ | ||
return new SqlConnection(_configuration.GetConnectionString("mssql")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Leo.Web.Data.Services | ||
{ | ||
internal sealed class UnitOfWork : IUnitOfWork | ||
{ | ||
public UnitOfWork( | ||
ICustomerRepository customerRepository, | ||
ICustomerDetailRepository customerDetailRepository) | ||
{ | ||
CustomerRepository = customerRepository; | ||
CustomerDetailRepository = customerDetailRepository; | ||
} | ||
|
||
public ICustomerRepository CustomerRepository { get; } | ||
|
||
public ICustomerDetailRepository CustomerDetailRepository { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters