Skip to content

Commit d5de1cf

Browse files
Part 6.
Part 6.
1 parent 83060e2 commit d5de1cf

7 files changed

+176
-4
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace UserManagement.Application.User.Commands
2+
{
3+
using AutoMapper;
4+
using MediatR;
5+
using System;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using UserManagement.Application.Common.BaseClass;
9+
using UserManagement.Application.Common.Interfaces;
10+
using UserManagement.Domain.UnitOfWork;
11+
public class AddUserCommand : IRequest<int>
12+
{
13+
public string FirstName { get; set; }
14+
public string LastName { get; set; }
15+
public DateTime DOB { get; set; }
16+
public string Gender { get; set; }
17+
public string EmailAddress { get; set; }
18+
public string PhoneNumber { get; set; }
19+
public string City { get; set; }
20+
public string State { get; set; }
21+
public string Zip { get; set; }
22+
public string Country { get; set; }
23+
public class AddNewUserHandler : ApplicationBase, IRequestHandler<AddUserCommand, int>
24+
{
25+
public AddNewUserHandler(IConfigConstants constant, IMapper mapper, IUnitOfWork unitOfWork)
26+
: base(constant, unitOfWork, mapper)
27+
{
28+
}
29+
30+
public async Task<int> Handle(AddUserCommand request, CancellationToken cancellationToken)
31+
{
32+
var user = new UserManagement.Domain.Entities.User
33+
{
34+
FirstName = request.FirstName,
35+
LastName = request.LastName,
36+
DOB = request.DOB,
37+
Gender = request.Gender.ToUpper(),
38+
EmailAddress = request.EmailAddress,
39+
PhoneNumber = request.PhoneNumber,
40+
City = request.City,
41+
State = request.State,
42+
Zip = request.Zip,
43+
Country = request.Country
44+
};
45+
this.UnitOfWork.StartTransaction();
46+
var res = UnitOfWork.Users.AddUser(user).Result;
47+
this.UnitOfWork.Commit();
48+
return await Task.Run(() => res, cancellationToken);
49+
}
50+
}
51+
}
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace UserManagement.Application.User.Commands
2+
{
3+
using FluentValidation;
4+
using System.Linq;
5+
using UserManagement.Application.Common.Interfaces;
6+
public class AddUserCommandValidator : AbstractValidator<AddUserCommand>
7+
{
8+
public AddUserCommandValidator(IConfigConstants constant)
9+
{
10+
this.RuleFor(v => v.FirstName).NotEmpty().WithMessage(constant.MSG_USER_NULLFIRSTNAME);
11+
this.RuleFor(v => v.LastName).NotEmpty().WithMessage(constant.MSG_USER_NULLLASTNAME);
12+
this.RuleFor(v => v.DOB).NotEmpty().WithMessage(constant.MSG_USER_NULLDOB);
13+
this.RuleFor(v => v.Gender).Must(x => (new string[] { "M", "F", "m", "f" }).Contains(x)).WithMessage(constant.MSG_USER_NULLGENDER);
14+
this.RuleFor(v => v.EmailAddress).NotEmpty().WithMessage(constant.MSG_USER_NULLEMAILADDR);
15+
this.RuleFor(v => v.PhoneNumber).NotEmpty().WithMessage(constant.MSG_USER_NULLPHNUM);
16+
this.RuleFor(v => v.City).NotEmpty().WithMessage(constant.MSG_USER_NULLCITY);
17+
this.RuleFor(v => v.State).NotEmpty().WithMessage(constant.MSG_USER_NULLSTATE);
18+
this.RuleFor(v => v.Country).NotEmpty().WithMessage(constant.MSG_USER_NULLCOUNTRY);
19+
}
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace UserManagement.Application.User.Commands
2+
{
3+
using AutoMapper;
4+
using MediatR;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using UserManagement.Application.Common.BaseClass;
8+
using UserManagement.Application.Common.Interfaces;
9+
using UserManagement.Domain.UnitOfWork;
10+
public class DeleteUserCommand : IRequest<bool>
11+
{
12+
public int UserID { get; set; }
13+
public class DeleteUserHandler : ApplicationBase, IRequestHandler<DeleteUserCommand, bool>
14+
{
15+
public DeleteUserHandler(IConfigConstants constant, IMapper mapper, IUnitOfWork unitOfWork)
16+
: base(constant, unitOfWork, mapper)
17+
{
18+
}
19+
20+
public async Task<bool> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
21+
{
22+
this.UnitOfWork.StartTransaction();
23+
var res = UnitOfWork.Users.DeleteUser(request.UserID).Result;
24+
this.UnitOfWork.Commit();
25+
return await Task.Run(() => res, cancellationToken);
26+
}
27+
}
28+
}
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace UserManagement.Application.User.Commands
2+
{
3+
using FluentValidation;
4+
using UserManagement.Application.Common.Interfaces;
5+
public class DeleteUserCommandValidator : AbstractValidator<DeleteUserCommand>
6+
{
7+
public DeleteUserCommandValidator(IConfigConstants constant)
8+
{
9+
this.RuleFor(v => v.UserID).GreaterThan(0).WithMessage(constant.MSG_USER_NULLUSERID);
10+
}
11+
}
12+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace UserManagement.Application.User.Commands
2+
{
3+
using AutoMapper;
4+
using MediatR;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using UserManagement.Application.Common.BaseClass;
8+
using UserManagement.Application.Common.Exceptions;
9+
using UserManagement.Application.Common.Interfaces;
10+
using UserManagement.Domain.UnitOfWork;
11+
public class UpdateUserCommand : IRequest<bool>
12+
{
13+
public int UserID { get; set; }
14+
public string PhoneNumber { get; set; }
15+
public string City { get; set; }
16+
public string State { get; set; }
17+
public string Zip { get; set; }
18+
public string Country { get; set; }
19+
public class UpdateUserHandler : ApplicationBase, IRequestHandler<UpdateUserCommand, bool>
20+
{
21+
public UpdateUserHandler(IConfigConstants constant, IMapper mapper, IUnitOfWork unitOfWork)
22+
: base(constant, unitOfWork, mapper)
23+
{
24+
}
25+
26+
public async Task<bool> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
27+
{
28+
var user = await this.UnitOfWork.Users.GetUser(request.UserID);
29+
if (user == null)
30+
{
31+
throw new NotFoundException($"The User ID {request.UserID} is not found");
32+
}
33+
34+
user.PhoneNumber = request.PhoneNumber;
35+
user.City = request.City;
36+
user.State = request.State;
37+
user.Zip = request.Zip;
38+
user.Country = request.Country;
39+
this.UnitOfWork.StartTransaction();
40+
var res = UnitOfWork.Users.UpdateUser(user).Result;
41+
this.UnitOfWork.Commit();
42+
return await Task.Run(() => res, cancellationToken);
43+
}
44+
}
45+
}
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace UserManagement.Application.User.Commands
2+
{
3+
using FluentValidation;
4+
using UserManagement.Application.Common.Interfaces;
5+
public class UpdateUserCommandValidator : AbstractValidator<UpdateUserCommand>
6+
{
7+
public UpdateUserCommandValidator(IConfigConstants constant)
8+
{
9+
this.RuleFor(v => v.UserID).GreaterThan(0).WithMessage(constant.MSG_USER_NULLUSERID);
10+
this.RuleFor(v => v.PhoneNumber).NotEmpty().WithMessage(constant.MSG_USER_NULLPHNUM);
11+
this.RuleFor(v => v.City).NotEmpty().WithMessage(constant.MSG_USER_NULLCITY);
12+
this.RuleFor(v => v.State).NotEmpty().WithMessage(constant.MSG_USER_NULLSTATE);
13+
this.RuleFor(v => v.Country).NotEmpty().WithMessage(constant.MSG_USER_NULLCOUNTRY);
14+
}
15+
}
16+
}

UserManagement.Application/UserManagement.Application.csproj

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

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

0 commit comments

Comments
 (0)