Skip to content

Commit 83060e2

Browse files
Part 5
Part 5
1 parent 7bce405 commit 83060e2

File tree

8 files changed

+158
-0
lines changed

8 files changed

+158
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace UserManagement.Application.Common.BaseClass
2+
{
3+
using AutoMapper;
4+
using UserManagement.Application.Common.Interfaces;
5+
using UserManagement.Domain.UnitOfWork;
6+
public class ApplicationBase
7+
{
8+
public IUnitOfWork UnitOfWork { get; set; }
9+
public IConfigConstants ConfigConstants { get; set; }
10+
public IMapper Mapper { get; set; }
11+
12+
public ApplicationBase(IConfigConstants configConstants, IUnitOfWork unitOfWork, IMapper mapper)
13+
{
14+
ConfigConstants = configConstants;
15+
UnitOfWork = unitOfWork;
16+
Mapper = mapper;
17+
}
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using AutoMapper;
2+
using FluentValidation;
3+
using MediatR;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System.Reflection;
6+
using UserManagement.Application.Common.Behaviors;
7+
using UserManagement.Application.Common.Behaviours;
8+
9+
namespace UserManagement.Application
10+
{
11+
public static class DependencyInjection
12+
{
13+
public static IServiceCollection AddApplication(this IServiceCollection services)
14+
{
15+
services.AddAutoMapper(Assembly.GetExecutingAssembly());
16+
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
17+
services.AddMediatR(Assembly.GetExecutingAssembly());
18+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>));
19+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
20+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
21+
return services;
22+
}
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace UserManagement.Application.User.DTO
2+
{
3+
using AutoMapper;
4+
using System;
5+
using UserManagement.Application.Common.Mappings;
6+
public class UserDTO : IMapFrom<Domain.Entities.User>
7+
{
8+
public int UserID { get; set; }
9+
public string Salutation { get; set; }
10+
public string FirstName { get; set; }
11+
public string LastName { get; set; }
12+
public DateTime DOB { get; set; }
13+
public int Age { get; set; }
14+
public string Gender { get; set; }
15+
public string EmailAddress { get; set; }
16+
public string PhoneNumber { get; set; }
17+
public string City { get; set; }
18+
public string State { get; set; }
19+
public string Zip { get; set; }
20+
public string Country { get; set; }
21+
public void Mapping(Profile profile)
22+
{
23+
profile.CreateMap<Domain.Entities.User, UserDTO>()
24+
.ForMember(d => d.Salutation, opt => opt.MapFrom(s => s.Gender.ToUpper() == "M" ? "Hi Sir!" : "Hi Ma'am!"))
25+
.ForMember(d => d.Age, opt => opt.MapFrom(s => DateTime.Today.Year - s.DOB.Year))
26+
;
27+
profile.CreateMap<UserDTO, Domain.Entities.User>();
28+
}
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace UserManagement.Application.User.Queries
2+
{
3+
using AutoMapper;
4+
using MediatR;
5+
using System.Collections.Generic;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using UserManagement.Application.Common.BaseClass;
9+
using UserManagement.Application.Common.Interfaces;
10+
using UserManagement.Application.User.DTO;
11+
using UserManagement.Application.User.VM;
12+
using UserManagement.Domain.UnitOfWork;
13+
public class GetAllUserQuery : IRequest<UserVM>
14+
{
15+
public class GetAllUserHandler : ApplicationBase, IRequestHandler<GetAllUserQuery, UserVM>
16+
{
17+
public GetAllUserHandler(IConfigConstants constant, IMapper mapper, IUnitOfWork unitOfWork)
18+
: base(constant, unitOfWork, mapper)
19+
{
20+
}
21+
22+
public async Task<UserVM> Handle(GetAllUserQuery request, CancellationToken cancellationToken)
23+
{
24+
var res = Mapper.Map(UnitOfWork.Users.GetAllUsers().Result, new List<UserDTO>());
25+
return await Task.FromResult(new UserVM() { UserList = res });
26+
}
27+
}
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace UserManagement.Application.User.Queries
2+
{
3+
using AutoMapper;
4+
using MediatR;
5+
using System.Collections.Generic;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using UserManagement.Application.Common.BaseClass;
9+
using UserManagement.Application.Common.Interfaces;
10+
using UserManagement.Application.User.DTO;
11+
using UserManagement.Application.User.VM;
12+
using UserManagement.Domain.UnitOfWork;
13+
14+
public class GetSingleUserQuery : IRequest<UserVM>
15+
{
16+
public int UserID { get; set; }
17+
public class GetSingleUserHandler : ApplicationBase, IRequestHandler<GetSingleUserQuery, UserVM>
18+
{
19+
public GetSingleUserHandler(IConfigConstants constant, IMapper mapper, IUnitOfWork unitOfWork)
20+
: base(constant, unitOfWork, mapper)
21+
{
22+
}
23+
24+
public async Task<UserVM> Handle(GetSingleUserQuery request, CancellationToken cancellationToken)
25+
{
26+
var res = this.Mapper.Map(this.UnitOfWork.Users.GetUser(request.UserID).Result, new UserDTO());
27+
return await Task.FromResult(new UserVM() { UserList = new List<UserDTO> { res } });
28+
}
29+
}
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace UserManagement.Application.User.Queries
2+
{
3+
using FluentValidation;
4+
using UserManagement.Application.Common.Interfaces;
5+
public class GetSingleUserQueryValidator : AbstractValidator<GetSingleUserQuery>
6+
{
7+
public GetSingleUserQueryValidator(IConfigConstants constant)
8+
{
9+
this.RuleFor(v => v.UserID).GreaterThan(0).WithMessage(constant.MSG_USER_NULLUSERID);
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace UserManagement.Application.User.VM
2+
{
3+
using System.Collections.Generic;
4+
using UserManagement.Application.User.DTO;
5+
public class UserVM
6+
{
7+
public IList<UserDTO> UserList { get; set; }
8+
}
9+
}

UserManagement.Application/UserManagement.Application.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919
<ProjectReference Include="..\UserManagement.Domain\UserManagement.Domain.csproj" />
2020
</ItemGroup>
2121

22+
<ItemGroup>
23+
<Folder Include="User\Commands\" />
24+
</ItemGroup>
25+
2226
</Project>

0 commit comments

Comments
 (0)