From 574733e386e0d188901cdf269e3afe07b79ef0a9 Mon Sep 17 00:00:00 2001 From: Yami An Date: Mon, 20 Nov 2023 00:07:11 +0700 Subject: [PATCH] 231120 --- .../DeveloperTypeCreateRequest.cs} | 4 +-- .../DeveloperTypeUpdateRequest.cs | 8 +++++ .../Services/IDeveloperTypeService.cs | 6 ++-- .../Mappers/DeveloperTypeMapper.cs | 11 +++++-- .../Services/DeveloperTypeService.cs | 16 ++++++---- .../Validations/DeveloperTypeValidation.cs | 16 +++++----- .../Validations/DeveloperValidation.cs | 30 +++++++++---------- .../Controllers/DeveloperTypeController.cs | 12 ++++---- 8 files changed, 61 insertions(+), 42 deletions(-) rename src/YANLib.Application.Contracts/Requests/{DeveloperTypeRequest.cs => DeveloperType/DeveloperTypeCreateRequest.cs} (61%) create mode 100644 src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeUpdateRequest.cs diff --git a/src/YANLib.Application.Contracts/Requests/DeveloperTypeRequest.cs b/src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeCreateRequest.cs similarity index 61% rename from src/YANLib.Application.Contracts/Requests/DeveloperTypeRequest.cs rename to src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeCreateRequest.cs index 575e5e8..9e7dbbc 100644 --- a/src/YANLib.Application.Contracts/Requests/DeveloperTypeRequest.cs +++ b/src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeCreateRequest.cs @@ -1,6 +1,6 @@ -namespace YANLib.Requests; +namespace YANLib.Requests.DeveloperType; -public sealed class DeveloperTypeRequest +public sealed class DeveloperTypeCreateRequest { public required int Code { get; set; } diff --git a/src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeUpdateRequest.cs b/src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeUpdateRequest.cs new file mode 100644 index 0000000..1db3751 --- /dev/null +++ b/src/YANLib.Application.Contracts/Requests/DeveloperType/DeveloperTypeUpdateRequest.cs @@ -0,0 +1,8 @@ +namespace YANLib.Requests.DeveloperType; + +public sealed class DeveloperTypeUpdateRequest +{ + public string Name { get; set; } + + public bool? IsActive { get; set; } +} diff --git a/src/YANLib.Application.Contracts/Services/IDeveloperTypeService.cs b/src/YANLib.Application.Contracts/Services/IDeveloperTypeService.cs index d0023ba..8df30ea 100644 --- a/src/YANLib.Application.Contracts/Services/IDeveloperTypeService.cs +++ b/src/YANLib.Application.Contracts/Services/IDeveloperTypeService.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Services; -using YANLib.Requests; +using YANLib.Requests.DeveloperType; using YANLib.Responses; namespace YANLib.Services; @@ -12,9 +12,9 @@ public interface IDeveloperTypeService : IApplicationService public ValueTask Get(int code); - public ValueTask Create(DeveloperTypeRequest request); + public ValueTask Create(DeveloperTypeCreateRequest request); - public ValueTask Update(DeveloperTypeRequest request); + public ValueTask Update(int code, DeveloperTypeUpdateRequest request); public ValueTask SyncDbToRedis(); } diff --git a/src/YANLib.Application/Mappers/DeveloperTypeMapper.cs b/src/YANLib.Application/Mappers/DeveloperTypeMapper.cs index 95e2dad..41656ab 100644 --- a/src/YANLib.Application/Mappers/DeveloperTypeMapper.cs +++ b/src/YANLib.Application/Mappers/DeveloperTypeMapper.cs @@ -3,7 +3,7 @@ using Volo.Abp.AutoMapper; using YANLib.Entities; using YANLib.RedisDtos; -using YANLib.Requests; +using YANLib.Requests.DeveloperType; using YANLib.Responses; namespace YANLib.Mappers; @@ -22,11 +22,18 @@ public DeveloperTypeMapper() _ = CreateMap() .Ignore(d => d.Code); - _ = CreateMap() + _ = CreateMap() .ForMember(d => d.Id, o => o.MapFrom(s => s.Code)) .Ignore(d => d.CreatedAt) .Ignore(d => d.UpdatedAt); + _ = CreateMap<(int, DeveloperTypeUpdateRequest), DeveloperType>() + .ForMember(d => d.Id, o => o.MapFrom(s => s.Item1)) + .ForMember(d => d.Name, o => o.MapFrom(s => s.Item2.Name)) + .ForMember(d => d.IsActive, o => o.MapFrom(s => s.Item2.IsActive)) + .Ignore(d => d.CreatedAt) + .Ignore(d => d.UpdatedAt); + _ = CreateMap(); _ = CreateMap() diff --git a/src/YANLib.Application/Services/DeveloperTypeService.cs b/src/YANLib.Application/Services/DeveloperTypeService.cs index 59b5c71..a694183 100644 --- a/src/YANLib.Application/Services/DeveloperTypeService.cs +++ b/src/YANLib.Application/Services/DeveloperTypeService.cs @@ -8,7 +8,7 @@ using YANLib.Application.Redis.Services; using YANLib.Entities; using YANLib.RedisDtos; -using YANLib.Requests; +using YANLib.Requests.DeveloperType; using YANLib.Responses; using static System.DateTime; using static System.Threading.Tasks.Task; @@ -63,7 +63,7 @@ public async ValueTask Get(int code) } } - public async ValueTask Create(DeveloperTypeRequest request) + public async ValueTask Create(DeveloperTypeCreateRequest request) { try { @@ -74,7 +74,7 @@ public async ValueTask Create(DeveloperTypeRequest reques throw new BusinessException(EXIST_ID).WithData("Code", request.Code); } - var ent = ObjectMapper.Map(request); + var ent = ObjectMapper.Map(request); ent.CreatedAt = Now; @@ -94,12 +94,16 @@ public async ValueTask Create(DeveloperTypeRequest reques } } - public async ValueTask Update(DeveloperTypeRequest request) + public async ValueTask Update(int code, DeveloperTypeUpdateRequest request) { try { - var dto = await _redisService.Get(DeveloperTypeGroup, request.Code.ToString()) ?? throw new BusinessException(NOT_FOUND_DEV_TYPE).WithData("Code", request.Code); - var ent = ObjectMapper.Map(request); + var dto = await _redisService.Get(DeveloperTypeGroup, code.ToString()) ?? throw new BusinessException(NOT_FOUND_DEV_TYPE).WithData("Code", code); + + request.Name = request.Name.IsWhiteSpaceOrNull() ? dto.Name : request.Name; + request.IsActive ??= dto.IsActive; + + var ent = ObjectMapper.Map<(int, DeveloperTypeUpdateRequest), DeveloperType>((code, request)); ent.CreatedAt = dto.CreatedAt; ent.UpdatedAt = Now; diff --git a/src/YANLib.Application/Validations/DeveloperTypeValidation.cs b/src/YANLib.Application/Validations/DeveloperTypeValidation.cs index 444ebe9..e75edae 100644 --- a/src/YANLib.Application/Validations/DeveloperTypeValidation.cs +++ b/src/YANLib.Application/Validations/DeveloperTypeValidation.cs @@ -1,35 +1,35 @@ using FluentValidation; using System.Collections.Generic; using System.Linq; -using YANLib.Requests; +using YANLib.Requests.DeveloperType; using static YANLib.YANLibDomainErrorCodes; namespace YANLib.Validations; -public sealed class DeveloperTypeValidator : AbstractValidator +public sealed class DeveloperTypeCreateValidator : AbstractValidator { - public DeveloperTypeValidator() + public DeveloperTypeCreateValidator() { _ = RuleFor(x => x.Code).NotNull().NotEmpty().GreaterThanOrEqualTo(0).WithErrorCode(BAD_REQUEST_ID).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_ID); _ = RuleFor(x => x.Name).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); } } -public sealed class DeveloperTypeValidators : AbstractValidator> +public sealed class DeveloperTypeCreateValidators : AbstractValidator> { #region Constructors - public DeveloperTypeValidators() + public DeveloperTypeCreateValidators() { _ = RuleFor(x => x).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); - _ = RuleForEach(s => s).SetValidator(new DeveloperTypeValidator()); + _ = RuleForEach(s => s).SetValidator(new DeveloperTypeCreateValidator()); _ = RuleFor(x => x).Must(IsNotEmptyAndNull).WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); _ = RuleFor(x => x).Must(NameIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); } #endregion #region Methods - private bool IsNotEmptyAndNull(List requests) => requests.IsNotEmptyAndNull(); + private bool IsNotEmptyAndNull(List requests) => requests.IsNotEmptyAndNull(); - private bool NameIsNotWhiteSpace(List requests) => requests.Select(x => x.Name).AllNotWhiteSpaceAndNull(); + private bool NameIsNotWhiteSpace(List requests) => requests.Select(x => x.Name).AllNotWhiteSpaceAndNull(); #endregion } diff --git a/src/YANLib.Application/Validations/DeveloperValidation.cs b/src/YANLib.Application/Validations/DeveloperValidation.cs index 6cf8208..7703219 100644 --- a/src/YANLib.Application/Validations/DeveloperValidation.cs +++ b/src/YANLib.Application/Validations/DeveloperValidation.cs @@ -6,9 +6,9 @@ namespace YANLib.Validations; -public sealed class DeveloperValidator : AbstractValidator +public sealed class DeveloperCreateValidator : AbstractValidator { - public DeveloperValidator() + public DeveloperCreateValidator() { _ = RuleFor(x => x.Name).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); _ = RuleFor(x => x.IdCard).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_ID_CARD).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_ID_CARD); @@ -16,13 +16,13 @@ public DeveloperValidator() } } -public sealed class DeveloperValidators : AbstractValidator> +public sealed class DeveloperCreateValidators : AbstractValidator> { #region Constructors - public DeveloperValidators() + public DeveloperCreateValidators() { _ = RuleFor(x => x).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); - _ = RuleForEach(s => s).SetValidator(new DeveloperValidator()); + _ = RuleForEach(s => s).SetValidator(new DeveloperCreateValidator()); _ = RuleFor(x => x).Must(IsNotEmptyAndNull).WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); _ = RuleFor(x => x).Must(NameIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); _ = RuleFor(x => x).Must(IdCardIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_ID_CARD).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_ID_CARD); @@ -38,22 +38,22 @@ public DeveloperValidators() #endregion } -public sealed class DeveloperCertificateValidator : AbstractValidator +public sealed class DeveloperCreateCertificateValidator : AbstractValidator { - public DeveloperCertificateValidator() + public DeveloperCreateCertificateValidator() { _ = RuleFor(x => x.Name).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); _ = RuleFor(x => x.GPA).GreaterThan(0).WithErrorCode(BAD_REQUEST_GPA).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_GPA); } } -public sealed class DeveloperCertificateValidators : AbstractValidator> +public sealed class DeveloperCreateCertificateValidators : AbstractValidator> { #region Constructors - public DeveloperCertificateValidators() + public DeveloperCreateCertificateValidators() { _ = RuleFor(x => x).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); - _ = RuleForEach(s => s).SetValidator(new DeveloperCertificateValidator()); + _ = RuleForEach(s => s).SetValidator(new DeveloperCreateCertificateValidator()); _ = RuleFor(x => x).Must(IsNotEmptyAndNull).WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); _ = RuleFor(x => x).Must(NameIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); } @@ -66,22 +66,22 @@ public DeveloperCertificateValidators() #endregion } -public sealed class DeveloperDtoCertificateValidator : AbstractValidator +public sealed class DeveloperUpdateCertificateValidator : AbstractValidator { - public DeveloperDtoCertificateValidator() + public DeveloperUpdateCertificateValidator() { _ = RuleFor(x => x.Name).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); _ = RuleFor(x => x.GPA).GreaterThan(0).WithErrorCode(BAD_REQUEST_GPA).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_GPA); } } -public sealed class DeveloperDtoCertificateValidators : AbstractValidator> +public sealed class DeveloperUpdateCertificateValidators : AbstractValidator> { #region Constructors - public DeveloperDtoCertificateValidators() + public DeveloperUpdateCertificateValidators() { _ = RuleFor(x => x).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); - _ = RuleForEach(s => s).SetValidator(new DeveloperDtoCertificateValidator()); + _ = RuleForEach(s => s).SetValidator(new DeveloperUpdateCertificateValidator()); _ = RuleFor(x => x).Must(IsNotEmptyAndNull).WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); _ = RuleFor(x => x).Must(NameIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_NAME); } diff --git a/src/YANLib.HttpApi/Controllers/DeveloperTypeController.cs b/src/YANLib.HttpApi/Controllers/DeveloperTypeController.cs index a4ac99e..084e439 100644 --- a/src/YANLib.HttpApi/Controllers/DeveloperTypeController.cs +++ b/src/YANLib.HttpApi/Controllers/DeveloperTypeController.cs @@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Volo.Abp; -using YANLib.Requests; +using YANLib.Requests.DeveloperType; using YANLib.Services; namespace YANLib.Controllers; @@ -35,20 +35,20 @@ public async ValueTask Get(int code) [HttpPost] [SwaggerOperation(Summary = "Thêm mới định nghĩa Developer Type")] - public async ValueTask Create([Required] DeveloperTypeRequest request) + public async ValueTask Create([Required] DeveloperTypeCreateRequest request) { _logger.LogInformation("CreateDeveloperTypeController: {Request}", request.CamelSerialize()); return Ok(await _service.Create(request)); } - [HttpPut] + [HttpPut("{code}")] [SwaggerOperation(Summary = "Cập nhật định nghĩa Developer Type")] - public async ValueTask Update([Required] DeveloperTypeRequest request) + public async ValueTask Update(int code, [Required] DeveloperTypeUpdateRequest request) { - _logger.LogInformation("UpdateDeveloperTypeController: {Request}", request.CamelSerialize()); + _logger.LogInformation("UpdateDeveloperTypeController: {Code} - {Request}", code, request.CamelSerialize()); - return Ok(await _service.Update(request)); + return Ok(await _service.Update(code, request)); } [SwaggerOperation(Summary = "Đồng bộ tất cả định nghĩa Developer Types từ Database sang Redis")]