Skip to content

Commit

Permalink
Add Validation of Deapartment
Browse files Browse the repository at this point in the history
  • Loading branch information
MuntherAliAbuAssi committed Aug 24, 2023
1 parent b0899fb commit c5509a1
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public class CreateStudentValidator : AbstractValidator<CreateStudentCommand>
#region Fields
private readonly IStudentService _studentService;
private readonly IStringLocalizer<SharedResources> _stringLocalizer;
private readonly IDepartmentService _departmentService;
#endregion
#region Constractor
public CreateStudentValidator(IStudentService studentService, IStringLocalizer<SharedResources> stringLocalizer)
public CreateStudentValidator(IDepartmentService departmentService, IStudentService studentService, IStringLocalizer<SharedResources> stringLocalizer)
{
_studentService = studentService;
_stringLocalizer = stringLocalizer;
_departmentService = departmentService;
ApplyValidationRules();
ApplyCustomValidationRules();
}
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -58,9 +56,19 @@ public async Task<Response<GetSingleStudentResponse>> Handle(GetStudentByIdQuery

public async Task<PaginatedResult<GetStudentPaginatedListResponse>> Handle(GetStudentPaginatedListQuery request, CancellationToken cancellationToken)
{
Expression<Func<Student, GetStudentPaginatedListResponse>> 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<Func<Student, GetStudentPaginatedListResponse>> 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<GetStudentPaginatedListResponse>(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<GetStudentPaginatedListResponse>(filterQuery, null).ToPaginatedListAsync(request.PageNumber, request.PageSize);
paginatedList.Meta = new
{
Count = paginatedList.Data.Count(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ 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;
Name = name;
Address = address;
DepartmentName = departmentName;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SchoolProject.Core.Mapping.Students
{
public partial class DepartmentProfile
public partial class StudentProfile
{
public void CreateStudentMapping()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SchoolProject.Core.Mapping.Students
{
public partial class DepartmentProfile
public partial class StudentProfile
{
public void EditStudentMapping()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SchoolProject.Core.Mapping.Students
{
public partial class DepartmentProfile
public partial class StudentProfile
{
public void GetSingleStudentMapping()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SchoolProject.Core.Mapping.Students
{
public partial class DepartmentProfile
public partial class StudentProfile
{
public void GetStudentListMapping()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Student, GetStudentPaginatedListResponse>()
.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));

}
}
}
5 changes: 3 additions & 2 deletions SchoolProject.Core/Mapping/Students/StudentProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
9 changes: 9 additions & 0 deletions SchoolProject.Core/SharedResourcing/SharedResources.Ar.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
<data name="Deleted" xml:space="preserve">
<value>تم الحذف بنجاح</value>
</data>
<data name="DepartmentId" xml:space="preserve">
<value>رقم القسم</value>
</data>
<data name="DepartmentIdIsNotExist" xml:space="preserve">
<value>رقم القسم غير موجود </value>
</data>
<data name="IsExist" xml:space="preserve">
<value>هو موجود </value>
</data>
Expand All @@ -138,6 +144,9 @@
<data name="Name" xml:space="preserve">
<value>الاسم</value>
</data>
<data name="NameAr" xml:space="preserve">
<value>الأسم </value>
</data>
<data name="NotEmpty" xml:space="preserve">
<value>يجب أن يكون ممتلئاً</value>
</data>
Expand Down
9 changes: 9 additions & 0 deletions SchoolProject.Core/SharedResourcing/SharedResources.En.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
<data name="Deleted" xml:space="preserve">
<value>Deleted Successfully</value>
</data>
<data name="DepartmentId" xml:space="preserve">
<value>Department Id</value>
</data>
<data name="DepartmentIdIsNotExist" xml:space="preserve">
<value>is not exist</value>
</data>
<data name="IsExist" xml:space="preserve">
<value>Is Exist</value>
</data>
Expand All @@ -138,6 +144,9 @@
<data name="Name" xml:space="preserve">
<value>Name</value>
</data>
<data name="NameAr" xml:space="preserve">
<value>Name Arabic</value>
</data>
<data name="NotEmpty" xml:space="preserve">
<value>Name Must not be Empty</value>
</data>
Expand Down
4 changes: 4 additions & 0 deletions SchoolProject.Core/SharedResourcing/SharedResourcesKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";


}
}
1 change: 1 addition & 0 deletions SchoolProject.Service/Abstracts/IDepartmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace SchoolProject.Service.Abstracts
public interface IDepartmentService
{
public Task<Department> GetDepartmentById(int id);
public Task<bool> GetDepartmentByIdExist(int Id);
}
}
6 changes: 6 additions & 0 deletions SchoolProject.Service/Implementations/DepartmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public async Task<Department> GetDepartmentById(int id)

}

public async Task<bool> GetDepartmentByIdExist(int Id)
{
return await _departmentRepository.GetTableNoTracking()
.AnyAsync(x => x.DID.Equals(Id));
}

#endregion
}
}
Binary file added Screenshot 2023-08-11 140818.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c5509a1

Please sign in to comment.