Skip to content

Commit

Permalink
Merge pull request #28 from leungkimming/michael3
Browse files Browse the repository at this point in the history
enhance specflow, remove department enum
  • Loading branch information
leungkimming authored Apr 29, 2022
2 parents ad5c67e + 29a575b commit 9f93be8
Show file tree
Hide file tree
Showing 19 changed files with 278 additions and 176 deletions.
21 changes: 20 additions & 1 deletion API/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Service;
using Data;
using System.Net;
using Business;

namespace API {
[ApiController]
Expand All @@ -11,12 +12,22 @@ public class UserController : ControllerBase {
private readonly UserService _service;
private readonly ILogger<UserController> _logger;
private readonly IPaymentQuery _paymentQuery;
private readonly IDepartmentQuery _departmentQuery;

public UserController(ILogger<UserController> logger
, UserService service, IPaymentQuery paymentquery) {
, UserService service, IPaymentQuery paymentquery,
IDepartmentQuery _departmentquery) {
_service = service;
_logger = logger;
_paymentQuery = paymentquery;
_departmentQuery = _departmentquery;
}

[HttpGet(Name = "GetUserList")]
[AccessCodeAuthorize("AA01")]
public async Task<IActionResult> Get([FromQuery] GetUserRequestV1 request) {
var users = await _service.SearchAsyncV1(request);
return Ok(users);
}

[HttpPost]
Expand Down Expand Up @@ -59,5 +70,13 @@ public async Task<ActionResult<IEnumerable<PaymentSummary>>> GetPaymentOfFrequen

return Ok(paymentSummary);
}
[HttpGet("GetDepartmentList")]
[AccessCodeAuthorize("AA01")]
[ProducesResponseType(typeof(IEnumerable<Dep>), (int)HttpStatusCode.OK)]
public async Task<ActionResult<IEnumerable<Dep>>> GetDepartments() {
IEnumerable<Dep> departmentList = await _departmentQuery.GetDepartmentsAsync();

return Ok(departmentList);
}
}
}
1 change: 1 addition & 0 deletions API/RegisterModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected override void Load(ContainerBuilder builder) {
builder.RegisterType<UserService>().AsSelf();
builder.RegisterType<SystemParametersService>().AsSelf();
builder.RegisterType<PaymentQuery>().As<IPaymentQuery>();
builder.RegisterType<DepartmentQuery>().As<IDepartmentQuery>();

builder.RegisterAssemblyTypes(typeof(IMediator).Assembly)
.AsImplementedInterfaces();
Expand Down
2 changes: 1 addition & 1 deletion Business/Users/Events/OnPayslipAddedDomainEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

namespace Business {
public class OnPayslipAddedDomainEvent : BaseDomainEvent {
public class OnPayslipIssuedDomainEvent : BaseDomainEvent {
public Payslip Payslip { get; set; }
}
}
2 changes: 1 addition & 1 deletion Business/Users/Payslip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Payslip(int userId
public string? Letter { get; private set; }
public virtual User User { get; private set; }

public void Pay(float coefficientsSalary) {
public void CalculatePay(float coefficientsSalary) {
if (IsPaid)
throw new Exception("This Payslip has been paid.");

Expand Down
9 changes: 4 additions & 5 deletions Business/Users/User.Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ public void Update(string firstName
CoefficientsSalary = coefficientsSalary;
}

public void AddDepartment(Department department) {
public void JoinDepartment(Department department) {
Department = department;
//DepartmentId = departmentId;
}

public Payslip AddPayslip(DateTime date
public Payslip IssuePayslip(DateTime date
, float workingDays
, decimal bonus
, bool isPaid
Expand All @@ -52,13 +51,13 @@ public Payslip AddPayslip(DateTime date

var payslip = new Payslip(this.Id, date, workingDays, bonus);
if (isPaid) {
payslip.Pay(this.CoefficientsSalary);
payslip.CalculatePay(this.CoefficientsSalary);
}

PaySlips.Add(payslip);

if (isPaid && Address != null && payslip.TotalSalary > 0) {
var addEvent = new OnPayslipAddedDomainEvent()
var addEvent = new OnPayslipIssuedDomainEvent()
{
Payslip = payslip
};
Expand Down
16 changes: 13 additions & 3 deletions Client/Pages/NewUser.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/adduser"
@using Common
@using System.Text.Json

@inject HttpUtil _httpUtil

Expand All @@ -17,10 +18,10 @@
<FormItem Field="@nameof(_newuser.DepartmentName)">
<Template>
<label class="k-label k-form-label">Department:</label>
<TelerikDropDownList Data="@Enums.Departments"
<TelerikDropDownList Data="@deps"
@bind-Value="@_newuser.DepartmentName"
TextField="Name"
ValueField="Name"
TextField="DepartmentName"
ValueField="DepartmentName"
DefaultText="Select"
Id="DepartmentName"
Width="100%">
Expand Down Expand Up @@ -50,9 +51,18 @@
private string _message;
private bool _notspinning = true;
private bool _completed = false;
private Dep[] deps;

protected override async Task OnInitializedAsync() {
_newuser = new AddUserRequest();
var response = await _httpUtil.GetAsync("users/GetDepartmentList");
var content = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode) {
deps = JsonSerializer.Deserialize<Dep[]>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
} else {
_message = "Error: " + await response.Content.ReadAsStringAsync();
ShowErrorNotifications(_message);
}
_newuser.DepartmentName = "IT";
}
async Task Send() {
Expand Down
6 changes: 6 additions & 0 deletions Common/DTOs/Users/DepartmentViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

namespace Common {
public record Dep {
public string DepartmentName { get; set; }
}
}
6 changes: 6 additions & 0 deletions Common/DTOs/Users/GetUserList.Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ namespace Common {
public class GetUserRequest : GetAllDatasRequest {
public string? Search { get; set; }
}

public class GetUserRequestV1 : DTObaseRequest {
[Required(ErrorMessage = "Requires at least 1 character")]
[StringLength(20)]
public string Search { get; set; }
}
}
25 changes: 25 additions & 0 deletions Data/Query/DepartmentQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Data.SqlClient;
using Dapper;
using Microsoft.Extensions.Configuration;
using Common;

namespace Data {
public partial class DepartmentQuery : IDepartmentQuery {
private string _connectionString = string.Empty;

public DepartmentQuery(IConfiguration conf) {
_connectionString = conf.GetConnectionString("DDDConnectionString");
}

public async Task<IEnumerable<Dep>> GetDepartmentsAsync() {
using (var connection = new SqlConnection(_connectionString)) {
connection.Open();

IEnumerable<Dep> result = await connection.QueryAsync<Dep>(
@"select p.[Name] AS DepartmentName FROM [dbo].[Departments] p");

return result;
}
}
}
}
7 changes: 7 additions & 0 deletions Data/Query/IDepartmentQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Common;

namespace Data {
public interface IDepartmentQuery {
Task<IEnumerable<Dep>> GetDepartmentsAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace Service {
public class OnPayslipAddedDomainEventHandler
: INotificationHandler<OnPayslipAddedDomainEvent> {
: INotificationHandler<OnPayslipIssuedDomainEvent> {
public IUnitOfWork _unitOfWork;
public OnPayslipAddedDomainEventHandler(IUnitOfWork unitOfWork) {
this._unitOfWork = unitOfWork;
}
public async Task Handle(OnPayslipAddedDomainEvent notification, CancellationToken cancellationToken) {
public async Task Handle(OnPayslipIssuedDomainEvent notification, CancellationToken cancellationToken) {
string letter = $"To: {notification.Payslip.User.Address} \n"
+ $"Dear {notification.Payslip.User.UserName} \n"
+ $"Your Salary, amount to {notification.Payslip.TotalSalary} "
Expand Down
14 changes: 12 additions & 2 deletions Service/Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<AddUserResponse> AddNewAsync(AddUserRequest model) {
var user = _mapper.Map<User>(model);

if (dept != null) {
user.AddDepartment(dept);
user.JoinDepartment(dept);
}

await repository.AddAsync(user);
Expand All @@ -46,7 +46,7 @@ public async Task<AddPayslipResponse> AddUserPayslipAsync(AddPayslipRequest mode
if (StructuralComparisons.StructuralComparer.Compare(user.RowVersion, model.UserDTO.RowVersion) != 0) {
throw new RecordVersionException();
}
var payslip = user.AddPayslip(model.Date.Value
var payslip = user.IssuePayslip(model.Date.Value
, model.WorkingDays.Value
, model.Bonus
, model.IsPaid.Value);
Expand Down Expand Up @@ -78,6 +78,16 @@ public async Task<GetAllDatasResponse<UserInfoDTO>> SearchAsync(GetUserRequest r
searchResult.TotalCount = totalCount;
return searchResult;
}
public async Task<List<UserInfoDTO>> SearchAsyncV1(GetUserRequestV1 request) {
var x = _httpContext.User;
var repository = UnitOfWork.UserRepository();
var users = await repository
.ListAsyncwithDept(_ => _.UserName.Contains(request.Search));

var userDTOs = users.Select(_user => _mapper.Map<UserInfoDTO>(_user)).ToList();
return userDTOs;
}

public async Task<List<PayslipDTO>> SearchAsync(GetPayslipRequest request) {
var repository = UnitOfWork.AsyncRepository<Payslip>();
var payslips = await repository
Expand Down
27 changes: 16 additions & 11 deletions StoryTest/Features/CallAPI.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ Scenario: 1_Initialize
| 1 | AA01 |
| 2 | AB01 |
| 3 | AC01 |
And I have the "Business.Department" table with audit "true"
And I have the "Business.Department" table with audit "true" save as "D1"
| Name | Description | Manager |
| CS | CS | Raymond |
| PA | PA | Mimmi |
And DTO "D1" should contain a record save as "D2" that matches the following table
| Field | Value |
| Name | CS |
And I have the "Business.User" table with audit "true" save as "U0"
| UserName | FirstName | LastName | Address | BirthDate | CoefficientsSalary | Department |
| Mary | Mary | Wong | Central | CURRENT_DATE-30Y | 45.00 | @{D2} |
| 41776 | Tommy | Leung | KLN | CURRENT_DATE-25Y | 55.50 | @{D2} |

Scenario: 2_AddNewUser
Given I have the following "Common.AddUserRequest" DTO save as "U1"
Expand All @@ -31,24 +38,22 @@ Scenario: 2_AddNewUser
| DepartmentName | IT |

Scenario: 3_AddPayslip
When I get API "users?Search=Micl" with status code 200 and response DTO "Common.UserInfoDTO[]" save as "R9"
Then DTO "R9" should contain a record save as "R10" that matches the following table
| Field | Value |
| UserName | Micl |
| DepartmentName | IT |
Given I have the following "Common.AddPayslipRequest" DTO save as "P1"
| Field | Value |
| date | CURRENT_DATE-5M |
| workingDays | 10 |
| bonus | 100 |
| isPaid | true |
And I have the following "Common.GetUserRequest" DTO save as "U2"
| Field | Value |
| Search | Micl |
| RecordsPerPage | 10 |
| PageNo | 1 |
When I post DTO "U2" to API "users/getuserlist" with status code 200 and response save as "R2"
Then Response "R2" contains the "Common.GetAllDatasResponse`1[Common.UserInfoDTO]" DTO save as "R2DTO"
And I locate user "Micl" in DTO "R2DTO" and update to DTO "P1"
| UserDTO | @{R10} |
When I post DTO "P1" to API "users/AddPayslip" with status code 200 and response save as "R3"
Then Response "R3" contains the "Common.AddPayslipResponse" DTO save as "R3DTO"
And DTO "R3DTO" matches the following table
| Field | Value |
| TotalSalary | 75100 |
| Field | Value |
| TotalSalary | 75100 |
| LetterSentDate | CURRENT_DATE+0D |

Loading

0 comments on commit 9f93be8

Please sign in to comment.