Skip to content

Commit

Permalink
Replace the Id of model type from String to Guid
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 23, 2023
1 parent dab9419 commit eb0fecf
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Leo.Data.Domain/Entities/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class Customer : IAuditableEntity
{
public string? Id { get; set; }
public Guid Id { get; set; }

public string? Name { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions src/Leo.Data.Domain/Entities/CustomerDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{
public class CustomerDetail : IAuditableEntity
{
public string? Id { get; set; }
public Guid Id { get; set; }

public string? CustomerId { get; set; }
public Guid CustomerId { get; set; }

public DateTime? Date { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Leo.Web.Api/Controllers/CustomerDetailsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CustomerDetailsController(IMediator mediator, ILogger<CustomersController
}

[HttpGet("{id}")]
public async Task<CustomerDetailDto?> GetByIdAsync(string id)
public async Task<CustomerDetailDto?> GetByIdAsync(Guid id)
{
return await _mediator.Send(new GetCustomerDetailByIdRequest { Id = id }, HttpContext.RequestAborted).ConfigureAwait(false) ?? throw new NotFoundMessage();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Leo.Web.Api/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Task<List<CustomerDto>> GetAsync()
}

[HttpGet("{id}")]
public async Task<CustomerDto?> GetAsync(string id)
public async Task<CustomerDto?> GetAsync(Guid id)
{
return await _mediator.Send(new GetCustomerByIdRequest { Id = id }, this.HttpContext.RequestAborted).ConfigureAwait(false) ?? throw new NotFoundMessage();
}
Expand Down Expand Up @@ -60,7 +60,7 @@ await _mediator.Send(
}

[HttpGet("{id}/details")]
public Task<List<CustomerDetailDto>> GetByCustomerIdAsync(string id)
public Task<List<CustomerDetailDto>> GetByCustomerIdAsync(Guid id)
{
return _mediator.Send(new GetCustomerDetailsByCustomerIdRequest { CustomerId = id }, this.HttpContext.RequestAborted);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Leo.Web.Data.Services;
using Dapper;
using Leo.Web.Data.Services;
using Leo.Web.Data.SQLite.Repositories;
using Leo.Web.Data.SQLite.TypeHandlers;
using Microsoft.Extensions.DependencyInjection;

namespace Leo.Web.Data
Expand All @@ -8,6 +10,9 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddDataServices(this IServiceCollection services)
{
SqlMapper.AddTypeHandler(new GuidToStringHandler());
//SqlMapper.AddTypeHandler(new GuidAsBinaryHandler());

services.AddSingleton<IDbConnectionFactory, DbConnectionFactory>();
services.AddScoped<IDatabaseService, DatabaseService>();
services.AddScoped<ICustomerRepository, CustomerRepository>();
Expand Down
12 changes: 6 additions & 6 deletions src/Leo.Web.Data.SQLite/Repositories/CustomerDetailRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public CustomerDetailRepository(IDbConnectionFactory dbConnectionManager)
_dbConnectionManager = dbConnectionManager;
}

public async Task<string> CreateAsync(CustomerDetail detail)
public async Task<Guid> CreateAsync(CustomerDetail detail)
{
detail.Id = Guid.NewGuid().ToString();
detail.Id = Guid.NewGuid();

var parameters = new DynamicParameters();
parameters.Add("id", detail.Id, dbType: DbType.String);
Expand All @@ -37,21 +37,21 @@ public async Task<string> CreateAsync(CustomerDetail detail)
return detail.Id;
}

public async Task<CustomerDetail?> GetByIdAsync(string 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);
parameters.Add("id", id, DbType.String);
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(string customerId)
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);
parameters.Add("customer_id", customerId, DbType.String);
var cmdDef = new CommandDefinition(commandText, parameters);
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false);
return await conn.QueryAsync<CustomerDetail>(cmdDef).ConfigureAwait(false);
Expand Down
14 changes: 7 additions & 7 deletions src/Leo.Web.Data.SQLite/Repositories/CustomerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public CustomerRepository(IDbConnectionFactory dbConnectionManager)
_dbConnectionManager = dbConnectionManager;
}

public async Task<Customer?> GetAsync(string id)
public async Task<Customer?> GetAsync(Guid id)
{
var commandText = "SELECT * FROM customer "
+ "WHERE id = @id";
var parameters = new DynamicParameters();
parameters.Add("id", id, dbType: DbType.String);
parameters.Add("id", id, DbType.String);
var cmdDef = new CommandDefinition(commandText, parameters);
using var conn = await _dbConnectionManager.OpenAsync().ConfigureAwait(false);
return await conn.QueryFirstOrDefaultAsync<Customer>(cmdDef).ConfigureAwait(false);
Expand All @@ -32,23 +32,23 @@ public async Task<IEnumerable<Customer>> GetAsync()
return await conn.QueryAsync<Customer>(cmdDef).ConfigureAwait(false);
}

public async Task<string> CreateAsync(Customer customer)
public async Task<Guid> CreateAsync(Customer customer)
{
customer.Id = Guid.NewGuid().ToString();
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, dbType: DbType.String);
parameters.Add("id", customer.Id, DbType.String);
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); ;
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);
Expand All @@ -62,7 +62,7 @@ public async Task UpdateAsync(Customer customer)
+ "updated_at = @updated_at, updated_by = @updated_by "
+ "WHERE id=@id";
var parameters = new DynamicParameters();
parameters.Add("id", customer.Id, dbType: DbType.String);
parameters.Add("id", customer.Id, DbType.String);
parameters.Add("name", customer.Name);
parameters.Add("phone", customer.Phone);
parameters.Add("gender", customer.Gender);
Expand Down
18 changes: 18 additions & 0 deletions src/Leo.Web.Data.SQLite/TypeHandlers/GuidToStringHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Dapper;
using System.Data;

namespace Leo.Web.Data.SQLite.TypeHandlers
{
public sealed class GuidToStringHandler : SqlMapper.TypeHandler<Guid>
{
public override Guid Parse(object value)
{
return value == null ? Guid.Empty : Guid.Parse(value.ToString());
}

public override void SetValue(IDbDataParameter parameter, Guid value)
{
parameter.Value = value.ToString("D");
}
}
}
2 changes: 1 addition & 1 deletion src/Leo.Web.Data/Commands/CreateCustomerDetailRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Leo.Web.Data.Commands
{
public sealed class CreateCustomerDetailRequest : IRequest<string>
public sealed class CreateCustomerDetailRequest : IRequest<Guid>
{
public CustomerDetailDto? CustomerDetailDto { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Leo.Web.Data/Commands/CreateCustomerRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Leo.Web.Data.Commands
{
public sealed class CreateCustomerRequest : IRequest<string>
public sealed class CreateCustomerRequest : IRequest<Guid>
{
public CustomerDto CustomerDto { get; set; } = null!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Leo.Web.Data.Commands.Handlers
{
internal class CreateCustomerDetailRequestHandler : IRequestHandler<CreateCustomerDetailRequest, string>
internal class CreateCustomerDetailRequestHandler : IRequestHandler<CreateCustomerDetailRequest, Guid>
{
private readonly IUnitOfWork _uow;
private readonly IMapper _mapper;
Expand All @@ -16,7 +16,7 @@ public CreateCustomerDetailRequestHandler(IUnitOfWork unitOfWork, IMapper mapper
_mapper = mapper;
}

public Task<string> Handle(CreateCustomerDetailRequest request, CancellationToken cancellationToken)
public Task<Guid> Handle(CreateCustomerDetailRequest request, CancellationToken cancellationToken)
{
var detail = _mapper.Map<CustomerDetail>(request.CustomerDetailDto);
if (request.User != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Leo.Web.Data.Commands.Handlers
{
internal sealed class CreateCustomerRequestHandler : IRequestHandler<CreateCustomerRequest, string>
internal sealed class CreateCustomerRequestHandler : IRequestHandler<CreateCustomerRequest, Guid>
{
private readonly IUnitOfWork _uow;
private readonly IMapper _mapper;
Expand All @@ -16,7 +16,7 @@ public CreateCustomerRequestHandler(IUnitOfWork unitOfWork, IMapper mapper)
_mapper = mapper;
}

public Task<string> Handle(CreateCustomerRequest request, CancellationToken cancellationToken)
public Task<Guid> Handle(CreateCustomerRequest request, CancellationToken cancellationToken)
{
var customer = _mapper.Map<Customer>(request.CustomerDto);
if (request.User != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Leo.Web.Data/Queries/GetCustomerByIdRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Leo.Web.Data.Queries
{
public sealed class GetCustomerByIdRequest : IRequest<CustomerDto>
{
public string? Id { get; set; }
public Guid Id { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Leo.Web.Data/Queries/GetCustomerDetailByIdRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Leo.Web.Data.Queries
{
public sealed class GetCustomerDetailByIdRequest : IRequest<CustomerDetailDto>
{
public string? Id { get; set; }
public Guid Id { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Leo.Web.Data.Queries
{
public sealed class GetCustomerDetailsByCustomerIdRequest : IRequest<List<CustomerDetailDto>>
{
public string? CustomerId { get; set; }
public Guid CustomerId { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Leo.Web.Data/Repositories/ICustomerDetailRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Leo.Web.Data
{
public interface ICustomerDetailRepository
{
Task<IEnumerable<CustomerDetail>> GetByCustomerIdAsync(string customerId);
Task<IEnumerable<CustomerDetail>> GetByCustomerIdAsync(Guid customerId);

Task<CustomerDetail?> GetByIdAsync(string id);
Task<CustomerDetail?> GetByIdAsync(Guid id);

Task<string> CreateAsync(CustomerDetail detail);
Task<Guid> CreateAsync(CustomerDetail detail);
}
}
4 changes: 2 additions & 2 deletions src/Leo.Web.Data/Repositories/ICustomerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace Leo.Web.Data
{
public interface ICustomerRepository
{
Task<Customer?> GetAsync(string id);
Task<Customer?> GetAsync(Guid id);

Task<IEnumerable<Customer>> GetAsync();

Task<string> CreateAsync(Customer customer);
Task<Guid> CreateAsync(Customer customer);

Task UpdateAsync(Customer customer);
}
Expand Down

0 comments on commit eb0fecf

Please sign in to comment.