Skip to content

Commit 23f52e1

Browse files
add create user
1 parent 1fe9e45 commit 23f52e1

File tree

7 files changed

+58
-36
lines changed

7 files changed

+58
-36
lines changed

Controllers/UsersController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MediatR;
2+
using Microsoft.AspNetCore.Http.HttpResults;
23
using Microsoft.AspNetCore.Mvc;
34
using PaymentCoreServiceApi.Features.Users.Commands;
45

@@ -18,7 +19,7 @@ public UsersController(IMediator mediator)
1819
[HttpPost]
1920
public async Task<IActionResult> Create([FromBody] CreateUserCommand command)
2021
{
21-
var userId = await _mediator.Send(command);
22-
return CreatedAtAction(nameof(Create), new { id = userId }, userId);
22+
var user = await _mediator.Send(command);
23+
return Ok(user);
2324
}
2425
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace PaymentCoreServiceApi.Core.Interfaces.Repositories.IUnitOfWork;
22

3-
public interface IUnitOfWork
3+
public interface IDbUnitOfWork
44
{
55
Task CommitAsync(CancellationToken cancellationToken = default);
6+
void Commit();
67
}

Core/Interfaces/Repositories/Write/IBaseWriteOnlyRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using PaymentCoreServiceApi.Core.Interfaces.Repositories.IUnitOfWork;
2+
13
namespace PaymentCoreServiceApi.Core.Interfaces.Repositories.Write
24
{
3-
public interface IBaseWriteOnlyRepository<TEntity> where TEntity : class
5+
public interface IBaseWriteOnlyRepository<TEntity> : IDbUnitOfWork where TEntity : class
46
{
57
Task<TEntity> AddAsync(TEntity entity, string[]? excludeProperties = null);
68
Task<List<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities);

Features/Users/Commands/CreateUserCommandHandler.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,32 @@ public CreateUserCommandHandler(IUserWriteRepository userWriteRepository)
1515

1616
public async Task<User> Handle(CreateUserCommand request, CancellationToken cancellationToken)
1717
{
18-
var user = new User
18+
try
1919
{
20-
NickName = request.NickName,
21-
Avatar = request.Avatar,
22-
Gender = request.Gender,
23-
BirthDate = request.BirthDate,
24-
Age = request.Age,
25-
Email = request.Email,
26-
UserName = request.UserName,
27-
Password = request.Password,
28-
PhoneNumber = request.PhoneNumber,
29-
Address = request.Address,
30-
Active = true,
31-
};
20+
var user = new User
21+
{
22+
NickName = request.NickName,
23+
Avatar = request.Avatar,
24+
Gender = request.Gender,
25+
BirthDate = request.BirthDate,
26+
Age = request.Age,
27+
Email = request.Email,
28+
UserName = request.UserName,
29+
Password = request.Password, // Note: In production, hash this password
30+
PhoneNumber = request.PhoneNumber,
31+
Address = request.Address,
32+
Active = true
33+
};
3234

33-
34-
var result = await _userWriteRepository.AddAsync(user);
35+
// Add the user without auto-commit
36+
var result = await _userWriteRepository.AddAsync(user);
37+
await _userWriteRepository.CommitAsync();
38+
return result;
39+
}
40+
catch (Exception ex)
41+
{
42+
throw;
43+
}
3544

36-
return result;
3745
}
38-
39-
public IUserWriteRepository UserWriteRepository => _userWriteRepository;
4046
}

Infrastructure/Repositories/UnitOfWork/UnitOfWork.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33

44
namespace PaymentCoreServiceApi.Infrastructure.Repositories.UnitOfWork
55
{
6-
public class UnitOfWork(AppDbContext context) : IUnitOfWork, IDisposable
6+
public class UnitOfWork(AppDbContext context) : IDbUnitOfWork, IDisposable
77
{
88
private readonly AppDbContext _context = context ?? throw new ArgumentNullException(nameof(context));
99

10-
public async Task CommitAsync(CancellationToken cancellationToken = default)
11-
{
12-
await _context.SaveChangesAsync(cancellationToken);
13-
}
10+
public async Task CommitAsync(CancellationToken cancellationToken = default)
11+
{
12+
await _context.SaveChangesAsync(cancellationToken);
13+
}
1414

15-
public void Dispose()
16-
{
17-
_context.Dispose();
15+
public void Commit()
16+
{
17+
_context.SaveChanges();
18+
}
19+
20+
public void Dispose()
21+
{
22+
_context.Dispose();
1823
}
1924
}
2025
}

Infrastructure/Repositories/Write/EfBaseWriteOnlyRepository.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using PaymentCoreServiceApi.Core.Entities.BaseModel;
22
using PaymentCoreServiceApi.Core.Interfaces.Repositories.Write;
3+
using PaymentCoreServiceApi.Core.Interfaces.Repositories.IUnitOfWork;
34
using PaymentCoreServiceApi.Infrastructure.DbContexts;
45
using Microsoft.EntityFrameworkCore;
56

@@ -14,45 +15,39 @@ public class EfBaseWriteOnlyRepository<TEntity>(AppDbContext context) : IBaseWri
1415
public async Task<TEntity> AddAsync(TEntity entity, string[]? excludeProperties = null)
1516
{
1617
await DbSet.AddAsync(entity);
17-
await Context.SaveChangesAsync();
1818
return entity;
1919
}
2020

2121
public async Task<List<TEntity>> AddRangeAsync(IEnumerable<TEntity> entities)
2222
{
2323
var entityBases = entities as TEntity[] ?? entities.ToArray();
2424
await DbSet.AddRangeAsync(entityBases);
25-
await Context.SaveChangesAsync();
2625
return entityBases.ToList();
2726
}
2827

2928
public async Task<TEntity> UpdateAsync(TEntity entity, string[]? excludeProperties = null)
3029
{
3130
DbSet.Update(entity);
32-
await Context.SaveChangesAsync();
3331
return entity;
3432
}
3533

3634
public async Task<List<TEntity>> UpdateBatchAsync(IEnumerable<TEntity> entities, string[]? excludeProperties = null)
3735
{
3836
var entityBases = entities as TEntity[] ?? entities.ToArray();
3937
DbSet.UpdateRange(entityBases);
40-
await Context.SaveChangesAsync();
4138
return entityBases.ToList();
4239
}
4340

4441
public async Task<TEntity> DeleteAsync(TEntity entity)
4542
{
4643
DbSet.Remove(entity);
47-
await Context.SaveChangesAsync();
4844
return entity;
4945
}
5046

5147
public async Task<List<TEntity>> DeleteBatchAsync(IEnumerable<TEntity> entities)
5248
{
5349
IEnumerable<TEntity> entityBases = entities.ToList();
5450
DbSet.RemoveRange(entityBases);
55-
await Context.SaveChangesAsync();
5651
return entityBases.ToList();
5752
}
5853

@@ -70,4 +65,14 @@ public void Dispose()
7065
{
7166
Context.Dispose();
7267
}
68+
69+
public async Task CommitAsync(CancellationToken cancellationToken = default)
70+
{
71+
await Context.SaveChangesAsync(cancellationToken);
72+
}
73+
74+
public void Commit()
75+
{
76+
Context.SaveChanges();
77+
}
7378
}

Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using Microsoft.EntityFrameworkCore;
33
using PaymentCoreServiceApi.Infrastructure.DbContexts;
44
using PaymentCoreServiceApi.Core.Interfaces.Repositories.Write;
5+
using PaymentCoreServiceApi.Core.Interfaces.Repositories.IUnitOfWork;
56
using PaymentCoreServiceApi.Infrastructure.Repositories.Write;
7+
using PaymentCoreServiceApi.Infrastructure.Repositories.UnitOfWork;
68
using MediatR;
79

810
var builder = WebApplication.CreateBuilder(args);

0 commit comments

Comments
 (0)