From 055cf5997f7ca41a6d62e21c3136f836e5394bb9 Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 4 Apr 2024 08:35:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=98=A0=E5=B0=84=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E5=92=8C=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutoMapper/UsersMapConfiguration.cs | 17 +++ .../Controllers/UsersController.cs | 108 +++++++----------- src/HelloShop.IdentityService/Program.cs | 1 + .../Extensions/ModelBindingExtensionns.cs | 16 +++ .../HelloShop.ServiceDefaults.csproj | 1 + 5 files changed, 79 insertions(+), 64 deletions(-) create mode 100644 src/HelloShop.IdentityService/AutoMapper/UsersMapConfiguration.cs create mode 100644 src/HelloShop.ServiceDefaults/Extensions/ModelBindingExtensionns.cs diff --git a/src/HelloShop.IdentityService/AutoMapper/UsersMapConfiguration.cs b/src/HelloShop.IdentityService/AutoMapper/UsersMapConfiguration.cs new file mode 100644 index 0000000..11e0a24 --- /dev/null +++ b/src/HelloShop.IdentityService/AutoMapper/UsersMapConfiguration.cs @@ -0,0 +1,17 @@ +using AutoMapper; +using HelloShop.IdentityService.Entities; +using HelloShop.IdentityService.Models.Users; +using Microsoft.Extensions.Options; + +namespace HelloShop.IdentityService.AutoMapper; + +public class UsersMapConfiguration : Profile +{ + public UsersMapConfiguration() + { + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + } +} diff --git a/src/HelloShop.IdentityService/Controllers/UsersController.cs b/src/HelloShop.IdentityService/Controllers/UsersController.cs index 0108a10..3d610e4 100644 --- a/src/HelloShop.IdentityService/Controllers/UsersController.cs +++ b/src/HelloShop.IdentityService/Controllers/UsersController.cs @@ -1,4 +1,5 @@ -using HelloShop.IdentityService.Entities; +using AutoMapper; +using HelloShop.IdentityService.Entities; using HelloShop.IdentityService.EntityFrameworks; using HelloShop.IdentityService.Models.Users; using HelloShop.ServiceDefaults.Authorization; @@ -14,24 +15,15 @@ namespace HelloShop.IdentityService.Controllers { [Route("api/[controller]")] [ApiController] - public class UsersController(IdentityServiceDbContext dbContext) : ControllerBase + public class UsersController(IdentityServiceDbContext dbContext, IMapper mapper) : ControllerBase { [HttpGet] [Authorize(IdentityPermissions.Users.Default)] public async Task>> GetUsers([FromQuery] UserListRequest model) { - var userList= await dbContext.Set().Skip((model.PageNumber - 1) * model.PageSize).Take(model.PageSize).ToListAsync(); + var userList = await dbContext.Set().Skip((model.PageNumber - 1) * model.PageSize).Take(model.PageSize).ToListAsync(); - var result = userList.Select(e => new UserListItem - { - Id = e.Id, - UserName = e.UserName!, - PhoneNumber = e.PhoneNumber, - PhoneNumberConfirmed = e.PhoneNumberConfirmed, - Email = e.Email, - EmailConfirmed = e.EmailConfirmed, - CreationTime = e.CreationTime, - }).ToList(); + var result = mapper.Map>(userList); var responseModel = new PagedResponse(result, result.Count); @@ -51,51 +43,26 @@ namespace HelloShop.IdentityService.Controllers return Forbid(); } - User? user = dbContext.Set().Find(id); + User? user = await dbContext.Set().FindAsync(id); if (user == null) { return NotFound(); } - UserDetailsResponse responseModel = new() - { - Id = user.Id, - UserName = user.UserName!, - PhoneNumber = user.PhoneNumber, - PhoneNumberConfirmed = user.PhoneNumberConfirmed, - Email = user.Email, - EmailConfirmed = user.EmailConfirmed, - CreationTime = user.CreationTime, - }; - - return Ok(responseModel); + return mapper.Map(user); } [HttpPost] [Authorize(IdentityPermissions.Users.Create)] public async Task> PostUser([FromBody] UserCreateRequest model) { - var user = new User - { - UserName = model.UserName, - PhoneNumber = model.PhoneNumber, - Email = model.Email - }; + var user = mapper.Map(model); await dbContext.AddAsync(user); await dbContext.SaveChangesAsync(); - UserDetailsResponse responseModel = new() - { - Id = user.Id, - UserName = user.UserName, - PhoneNumber = user.PhoneNumber, - PhoneNumberConfirmed = user.PhoneNumberConfirmed, - Email = user.Email, - EmailConfirmed = user.EmailConfirmed, - CreationTime = user.CreationTime, - }; + var responseModel = mapper.Map(user); return CreatedAtAction(nameof(GetUser), new { id = user.Id }, responseModel); } @@ -107,44 +74,57 @@ namespace HelloShop.IdentityService.Controllers { if (model.Id != id) { - throw new ArgumentException("Id mismatch", nameof(model)); + return BadRequest(); } - var user = dbContext.Set().Find(id); + var user = mapper.Map(model); - if (user != null) + DbSet users = dbContext.Set(); + + users.Entry(user).State = EntityState.Modified; + + try { - user.UserName = model.UserName; - user.PhoneNumber = model.PhoneNumber; - user.Email = model.Email; + await dbContext.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!users.Any(e => e.Id == id)) + { + return NotFound(); + } + else + { + throw; + } } - await dbContext.SaveChangesAsync(); - - return Ok(); + return NoContent(); } [HttpDelete("{id}")] [Authorize(IdentityPermissions.Users.Delete)] public async Task DeleteUser(int id, [FromServices] IAuthorizationService authorizationService) { - var user = dbContext.Set().Find(id); + var user = await dbContext.Set().FindAsync(id); - if (user != null) + if (user is null) { - var result = await authorizationService.AuthorizeAsync(User, user, IdentityPermissions.Users.Delete); - - if (result.Succeeded) - { - dbContext.Remove(user); - - dbContext.SaveChanges(); - - return Ok(); - } + return NotFound(); } - return Unauthorized(); + var authorizationResult = await authorizationService.AuthorizeAsync(User, user, IdentityPermissions.Users.Delete); + + if (authorizationResult.Succeeded) + { + dbContext.Remove(user); + + await dbContext.SaveChangesAsync(); + + return NoContent(); + } + + return Forbid(); } [HttpDelete] diff --git a/src/HelloShop.IdentityService/Program.cs b/src/HelloShop.IdentityService/Program.cs index 91d0980..a6de747 100644 --- a/src/HelloShop.IdentityService/Program.cs +++ b/src/HelloShop.IdentityService/Program.cs @@ -57,6 +57,7 @@ builder.Services.AddDataSeedingProviders(); builder.Services.AddOpenApi(); builder.Services.AddPermissionDefinitions(); builder.Services.AddAuthorization().AddDistributedMemoryCache().AddHttpClient().AddHttpContextAccessor().AddTransient().AddCustomAuthorization(); +builder.Services.AddModelMapper(); var app = builder.Build(); diff --git a/src/HelloShop.ServiceDefaults/Extensions/ModelBindingExtensionns.cs b/src/HelloShop.ServiceDefaults/Extensions/ModelBindingExtensionns.cs new file mode 100644 index 0000000..f4602b3 --- /dev/null +++ b/src/HelloShop.ServiceDefaults/Extensions/ModelBindingExtensionns.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using System.Reflection; + +namespace HelloShop.ServiceDefaults.Extensions; + +public static class ModelBindingExtensionns +{ + public static IServiceCollection AddModelMapper(this IServiceCollection services, Assembly? assembly = null) + { + assembly ??= Assembly.GetCallingAssembly(); + + services.AddAutoMapper(assembly); + + return services; + } +} diff --git a/src/HelloShop.ServiceDefaults/HelloShop.ServiceDefaults.csproj b/src/HelloShop.ServiceDefaults/HelloShop.ServiceDefaults.csproj index f9f2ed2..8287707 100644 --- a/src/HelloShop.ServiceDefaults/HelloShop.ServiceDefaults.csproj +++ b/src/HelloShop.ServiceDefaults/HelloShop.ServiceDefaults.csproj @@ -8,6 +8,7 @@ +