diff --git a/SchoolProject.Core/Features/Students/Commands/Validation/CreateStudentValidator.cs b/SchoolProject.Core/Features/Students/Commands/Validation/CreateStudentValidator.cs index d92cdea..4683736 100644 --- a/SchoolProject.Core/Features/Students/Commands/Validation/CreateStudentValidator.cs +++ b/SchoolProject.Core/Features/Students/Commands/Validation/CreateStudentValidator.cs @@ -11,12 +11,14 @@ public class CreateStudentValidator : AbstractValidator #region Fields private readonly IStudentService _studentService; private readonly IStringLocalizer _stringLocalizer; + private readonly IDepartmentService _departmentService; #endregion #region Constractor - public CreateStudentValidator(IStudentService studentService, IStringLocalizer stringLocalizer) + public CreateStudentValidator(IDepartmentService departmentService, IStudentService studentService, IStringLocalizer stringLocalizer) { _studentService = studentService; _stringLocalizer = stringLocalizer; + _departmentService = departmentService; ApplyValidationRules(); ApplyCustomValidationRules(); } @@ -37,15 +39,30 @@ public void ApplyValidationRules() .NotEmpty().WithMessage(_stringLocalizer[SharedResourcesKeys.NotEmpty]) .NotNull().WithMessage(_stringLocalizer[SharedResourcesKeys.Required]) .MaximumLength(100).WithMessage(_stringLocalizer[SharedResourcesKeys.MaximumLength100]); + + RuleFor(x => x.Address) + .NotEmpty().WithMessage(_stringLocalizer[SharedResourcesKeys.NotEmpty]) + .NotNull().WithMessage(_stringLocalizer[SharedResourcesKeys.Required]); } public void ApplyCustomValidationRules() { RuleFor(x => x.NameAr) .MustAsync(async (model, Key, CancellationToken) => !await _studentService.IsNameArExist(Key)) .WithMessage(_stringLocalizer[SharedResourcesKeys.IsExist]); + RuleFor(x => x.NameEn) .MustAsync(async (model, Key, CancellationToken) => !await _studentService.IsNameEnExist(Key)) .WithMessage(_stringLocalizer[SharedResourcesKeys.IsExist]); + + + When(x => x.DepartmentId != null, () => + { + RuleFor(x => x.DepartmentId) + .MustAsync(async (model, Key, CancellationToken) => await _departmentService.GetDepartmentByIdExist(Key)) + .WithMessage(_stringLocalizer[SharedResourcesKeys.DepartmentIdIsNotExist]); + }); + + } #endregion diff --git a/SchoolProject.Core/Features/Students/Queries/Handlers/StudentHandler.cs b/SchoolProject.Core/Features/Students/Queries/Handlers/StudentHandler.cs index 4e7c872..e2f20c0 100644 --- a/SchoolProject.Core/Features/Students/Queries/Handlers/StudentHandler.cs +++ b/SchoolProject.Core/Features/Students/Queries/Handlers/StudentHandler.cs @@ -6,9 +6,7 @@ using SchoolProject.Core.Features.Students.Responsies; using SchoolProject.Core.SharedResourcing; using SchoolProject.Core.Wrappers; -using SchoolProject.Data.Models; using SchoolProject.Service.Abstracts; -using System.Linq.Expressions; namespace SchoolProject.Core.Features.Students.Queries.Handlers { @@ -58,9 +56,19 @@ public async Task> Handle(GetStudentByIdQuery public async Task> Handle(GetStudentPaginatedListQuery request, CancellationToken cancellationToken) { - Expression> expression = e => new GetStudentPaginatedListResponse(e.StudID, e.Localize(e.NameAr, e.NameEn), e.Address, e.Localize(e.Department.DNameAr, e.Department.DNameEn)); + // way 1 + // Expression> expression = e => new GetStudentPaginatedListResponse(e.StudID, e.Localize(e.NameAr, e.NameEn), e.Address, e.Localize(e.Department.DNameAr, e.Department.DNameEn)); + // var paginatedList = await filterQuery.Select(expression)).ToPaginatedListAsync(request.PageNumber, request.PageSize); + + // way 2 + // var paginatedList = await filterQuery.Select(e => new GetStudentPaginatedListResponse(e.StudID, e.Localize(e.NameAr, e.NameEn), e.Address, e.Localize(e.Department.DNameAr, e.Department.DNameEn))).ToPaginatedListAsync(request.PageNumber, request.PageSize); + + // way 3 + // var paginatedList = await _mapper.ProjectTo(filterQuery, null).ToPaginatedListAsync(request.PageNumber, request.PageSize); + + var filterQuery = _studentService.FilterStudentPaginatedQuerable(request.OrderBy, request.Search); - var paginatedList = await filterQuery.Select(expression).ToPaginatedListAsync(request.PageNumber, request.PageSize); + var paginatedList = await _mapper.ProjectTo(filterQuery, null).ToPaginatedListAsync(request.PageNumber, request.PageSize); paginatedList.Meta = new { Count = paginatedList.Data.Count(), diff --git a/SchoolProject.Core/Features/Students/Responsies/GetStudentPaginatedListResponse.cs b/SchoolProject.Core/Features/Students/Responsies/GetStudentPaginatedListResponse.cs index dacf5cd..5d1198f 100644 --- a/SchoolProject.Core/Features/Students/Responsies/GetStudentPaginatedListResponse.cs +++ b/SchoolProject.Core/Features/Students/Responsies/GetStudentPaginatedListResponse.cs @@ -6,6 +6,10 @@ public class GetStudentPaginatedListResponse public string? Name { get; set; } public string? Address { get; set; } public string? DepartmentName { get; set; } + public GetStudentPaginatedListResponse() + { + + } public GetStudentPaginatedListResponse(int studID, string? name, string? address, string? departmentName) { StudID = studID; @@ -13,5 +17,7 @@ public GetStudentPaginatedListResponse(int studID, string? name, string? address Address = address; DepartmentName = departmentName; } + + } } diff --git a/SchoolProject.Core/Mapping/Students/CommandMapping/CreateStudentMapping.cs b/SchoolProject.Core/Mapping/Students/CommandMapping/CreateStudentMapping.cs index 7d22707..a87cf8b 100644 --- a/SchoolProject.Core/Mapping/Students/CommandMapping/CreateStudentMapping.cs +++ b/SchoolProject.Core/Mapping/Students/CommandMapping/CreateStudentMapping.cs @@ -3,7 +3,7 @@ namespace SchoolProject.Core.Mapping.Students { - public partial class DepartmentProfile + public partial class StudentProfile { public void CreateStudentMapping() { diff --git a/SchoolProject.Core/Mapping/Students/CommandMapping/EditStudentMapping.cs b/SchoolProject.Core/Mapping/Students/CommandMapping/EditStudentMapping.cs index 03b4ab8..5c0f456 100644 --- a/SchoolProject.Core/Mapping/Students/CommandMapping/EditStudentMapping.cs +++ b/SchoolProject.Core/Mapping/Students/CommandMapping/EditStudentMapping.cs @@ -3,7 +3,7 @@ namespace SchoolProject.Core.Mapping.Students { - public partial class DepartmentProfile + public partial class StudentProfile { public void EditStudentMapping() { diff --git a/SchoolProject.Core/Mapping/Students/QueryMapping/GetSingleStudentMapping.cs b/SchoolProject.Core/Mapping/Students/QueryMapping/GetSingleStudentMapping.cs index 8ad198f..41c94b2 100644 --- a/SchoolProject.Core/Mapping/Students/QueryMapping/GetSingleStudentMapping.cs +++ b/SchoolProject.Core/Mapping/Students/QueryMapping/GetSingleStudentMapping.cs @@ -3,7 +3,7 @@ namespace SchoolProject.Core.Mapping.Students { - public partial class DepartmentProfile + public partial class StudentProfile { public void GetSingleStudentMapping() { diff --git a/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListMapping.cs b/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListMapping.cs index a21d833..9f5a1bb 100644 --- a/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListMapping.cs +++ b/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListMapping.cs @@ -3,7 +3,7 @@ namespace SchoolProject.Core.Mapping.Students { - public partial class DepartmentProfile + public partial class StudentProfile { public void GetStudentListMapping() { diff --git a/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListPaginatedMapping.cs b/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListPaginatedMapping.cs new file mode 100644 index 0000000..5454562 --- /dev/null +++ b/SchoolProject.Core/Mapping/Students/QueryMapping/GetStudentListPaginatedMapping.cs @@ -0,0 +1,18 @@ +using SchoolProject.Core.Features.Students.Responsies; +using SchoolProject.Data.Models; + +namespace SchoolProject.Core.Mapping.Students +{ + public partial class StudentProfile + { + public void GetStudentListPaginatedMapping() + { + CreateMap() + .ForMember(dest => dest.DepartmentName, opt => opt.MapFrom(src => src.Localize(src.Department.DNameAr, src.Department.DNameEn))) + .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Localize(src.NameAr, src.NameEn))) + .ForMember(dest => dest.Address, opt => opt.MapFrom(src => src.Address)) + .ForMember(dest => dest.StudID, opt => opt.MapFrom(src => src.StudID)); + + } + } +} diff --git a/SchoolProject.Core/Mapping/Students/StudentProfile.cs b/SchoolProject.Core/Mapping/Students/StudentProfile.cs index 100a1f4..89abfa5 100644 --- a/SchoolProject.Core/Mapping/Students/StudentProfile.cs +++ b/SchoolProject.Core/Mapping/Students/StudentProfile.cs @@ -3,14 +3,15 @@ namespace SchoolProject.Core.Mapping.Students { - public partial class DepartmentProfile : Profile + public partial class StudentProfile : Profile { - public DepartmentProfile() + public StudentProfile() { GetStudentListMapping(); GetSingleStudentMapping(); CreateStudentMapping(); EditStudentMapping(); + GetStudentListPaginatedMapping(); } } } diff --git a/SchoolProject.Core/SharedResourcing/SharedResources.Ar.resx b/SchoolProject.Core/SharedResourcing/SharedResources.Ar.resx index 71d009a..c98b113 100644 --- a/SchoolProject.Core/SharedResourcing/SharedResources.Ar.resx +++ b/SchoolProject.Core/SharedResourcing/SharedResources.Ar.resx @@ -129,6 +129,12 @@ تم الحذف بنجاح + + رقم القسم + + + رقم القسم غير موجود + هو موجود @@ -138,6 +144,9 @@ الاسم + + الأسم + يجب أن يكون ممتلئاً diff --git a/SchoolProject.Core/SharedResourcing/SharedResources.En.resx b/SchoolProject.Core/SharedResourcing/SharedResources.En.resx index 8fa70f8..127c56a 100644 --- a/SchoolProject.Core/SharedResourcing/SharedResources.En.resx +++ b/SchoolProject.Core/SharedResourcing/SharedResources.En.resx @@ -129,6 +129,12 @@ Deleted Successfully + + Department Id + + + is not exist + Is Exist @@ -138,6 +144,9 @@ Name + + Name Arabic + Name Must not be Empty diff --git a/SchoolProject.Core/SharedResourcing/SharedResourcesKeys.cs b/SchoolProject.Core/SharedResourcing/SharedResourcesKeys.cs index 87bdb1a..36055bb 100644 --- a/SchoolProject.Core/SharedResourcing/SharedResourcesKeys.cs +++ b/SchoolProject.Core/SharedResourcing/SharedResourcesKeys.cs @@ -16,6 +16,10 @@ public static class SharedResourcesKeys public const string UnAuthorized = "UnAuthorized"; public const string BadRequest = "BadRequest"; public const string UnProcessableEntity = "UnProcessableEntity"; + public const string DepartmentIdIsNotExist = "DepartmentIdIsNotExist"; + public const string DepartmentId = "DepartmentId"; + public const string NameAr = "NameAr"; + } } diff --git a/SchoolProject.Service/Abstracts/IDepartmentService.cs b/SchoolProject.Service/Abstracts/IDepartmentService.cs index cb44436..461bb4e 100644 --- a/SchoolProject.Service/Abstracts/IDepartmentService.cs +++ b/SchoolProject.Service/Abstracts/IDepartmentService.cs @@ -5,5 +5,6 @@ namespace SchoolProject.Service.Abstracts public interface IDepartmentService { public Task GetDepartmentById(int id); + public Task GetDepartmentByIdExist(int Id); } } diff --git a/SchoolProject.Service/Implementations/DepartmentService.cs b/SchoolProject.Service/Implementations/DepartmentService.cs index e0eb3e6..5b7b16a 100644 --- a/SchoolProject.Service/Implementations/DepartmentService.cs +++ b/SchoolProject.Service/Implementations/DepartmentService.cs @@ -30,6 +30,12 @@ public async Task GetDepartmentById(int id) } + public async Task GetDepartmentByIdExist(int Id) + { + return await _departmentRepository.GetTableNoTracking() + .AnyAsync(x => x.DID.Equals(Id)); + } + #endregion } } diff --git a/Screenshot 2023-08-11 140818.png b/Screenshot 2023-08-11 140818.png new file mode 100644 index 0000000..83d06d7 Binary files /dev/null and b/Screenshot 2023-08-11 140818.png differ