Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,95 +95,3 @@
</EditForm>
</div>
</div>

@code {
private CreateContractFormModel model = new();
private bool isSaving = false;
private bool isLoadingOperators = true;
private bool hasPermission = false;
private string? errorMessage;
private List<OperatorDropDownModel>? operators;

protected override async Task OnAfterRenderAsync(bool firstRender) {
if (!firstRender) {
await base.OnAfterRenderAsync(firstRender);
return;
}
await RequirePermission(PermissionSection.Contract, PermissionFunction.Create);
await LoadOperators();
}


private async Task LoadOperators()
{
try
{
isLoadingOperators = true;
var estateId = await GetEstateId();
var result = await Mediator.Send(new OperatorQueries.GetOperatorsForDropDownQuery(CorrelationIdHelper.New(), estateId));
if (result.IsSuccess)
{
operators = ModelFactory.ConvertFrom(result.Data);
}
}
finally
{
isLoadingOperators = false;
StateHasChanged();
}
}

private async Task HandleSubmit()
{
isSaving = true;
errorMessage = null;

try
{
var estateId = Guid.Parse("11111111-1111-1111-1111-111111111111");
var accessToken = "stubbed-token";

// Create contract
var createCommand = new Commands.CreateContractCommand(
CorrelationIdHelper.New(),
accessToken,
estateId,
model.Description!,
Guid.Parse(model.OperatorId!)
);

var createResult = await Mediator.Send(createCommand);

if (!createResult.IsSuccess)
{
errorMessage = createResult.Message ?? "Failed to create contract";
return;
}

// Navigate to contracts list with success
NavigationManager.NavigateTo("/contracts");
}
catch (Exception ex)
{
errorMessage = $"An error occurred: {ex.Message}";
}
finally
{
isSaving = false;
}
}

private void Cancel()
{
NavigationManager.NavigateTo("/contracts");
}

public class CreateContractFormModel
{
[Required(ErrorMessage = "Description is required")]
public string? Description { get; set; }

[Required(ErrorMessage = "Operator is required")]
public string? OperatorId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using EstateManagementUI.BlazorServer.Factories;
using EstateManagementUI.BlazorServer.Permissions;
using EstateManagementUI.BusinessLogic.Requests;
using System.ComponentModel.DataAnnotations;
using EstateManagementUI.BlazorServer.Models;

namespace EstateManagementUI.BlazorServer.Components.Pages.Contracts
{
public partial class New
{
private CreateContractFormModel model = new();
private bool isSaving = false;
private bool isLoadingOperators = true;
private bool hasPermission = false;
private string? errorMessage;
private List<OperatorDropDownModel>? operators;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
{
await base.OnAfterRenderAsync(firstRender);
return;
}
await RequirePermission(PermissionSection.Contract, PermissionFunction.Create);
await LoadOperators();
}


private async Task LoadOperators()
{
try
{
isLoadingOperators = true;
var estateId = await GetEstateId();
var result = await Mediator.Send(new OperatorQueries.GetOperatorsForDropDownQuery(CorrelationIdHelper.New(), estateId));
if (result.IsSuccess)
{
operators = ModelFactory.ConvertFrom(result.Data);
}
}
finally
{
isLoadingOperators = false;
StateHasChanged();
}
}

private async Task HandleSubmit()
{
isSaving = true;
errorMessage = null;

try {
var estateId = await this.GetEstateId();

// Create contract
var createCommand = new ContractCommands.CreateContractCommand(
CorrelationIdHelper.New(),
estateId,
model.Description!,
Guid.Parse(model.OperatorId!)
);

var createResult = await Mediator.Send(createCommand);

if (!createResult.IsSuccess)
{
errorMessage = createResult.Message ?? "Failed to create contract";
return;
}

// Navigate to contracts list with success
NavigationManager.NavigateTo("/contracts");
}
catch (Exception ex)
{
errorMessage = $"An error occurred: {ex.Message}";
}
finally
{
isSaving = false;
}
}

private void Cancel()
{
NavigationManager.NavigateTo("/contracts");
}

public class CreateContractFormModel
{
[Required(ErrorMessage = "Description is required")]
public string? Description { get; set; }

[Required(ErrorMessage = "Operator is required")]
public string? OperatorId { get; set; }
}
}
}
19 changes: 19 additions & 0 deletions EstateManagmentUI.BusinessLogic/Client/ContractMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Text;
using EstateManagementUI.BusinessLogic.BackendAPI.DataTransferObjects;
using TransactionProcessor.DataTransferObjects.Requests.Contract;

namespace EstateManagementUI.BusinessLogic.Client {
public partial interface IApiClient {
Expand All @@ -18,6 +19,9 @@ Task<Result<List<ContractModel>>> GetContracts(ContractQueries.GetContractsQuery
CancellationToken cancellationToken);
Task<Result<ContractModel>> GetContract(ContractQueries.GetContractQuery request,
CancellationToken cancellationToken);

Task<Result> CreateContract(ContractCommands.CreateContractCommand request,
CancellationToken cancellationToken);
}

public partial class ApiClient : IApiClient {
Expand Down Expand Up @@ -87,5 +91,20 @@ public async Task<Result<ContractModel>> GetContract(ContractQueries.GetContract

return Result.Success(contractModel);
}

public async Task<Result> CreateContract(ContractCommands.CreateContractCommand request,
CancellationToken cancellationToken) {
var token = await this.GetToken(cancellationToken);
if (token.IsFailed)
return ResultHelpers.CreateFailure(token);

var apiRequest = new CreateContractRequest() { Description = request.Description, OperatorId = request.OperatorId };

var apiResult = await this.TransactionProcessorClient.CreateContract(token.Data, request.EstateId, apiRequest, cancellationToken);
if (apiResult.IsFailed)
return ResultHelpers.CreateFailure(apiResult);

return Result.Success();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public async Task<Result<List<MerchantDeviceModel>>> Handle(MerchantQueries.GetM

public class ContractRequestHandler : IRequestHandler<ContractQueries.GetContractsQuery, Result<List<ContractModel>>>,
IRequestHandler<ContractQueries.GetContractQuery, Result<ContractModel>>,
IRequestHandler<Commands.CreateContractCommand, Result>,
IRequestHandler<ContractCommands.CreateContractCommand, Result>,
IRequestHandler<Commands.AddProductToContractCommand, Result>,
IRequestHandler<Commands.AddTransactionFeeForProductToContractCommand, Result>,
IRequestHandler<ContractQueries.GetRecentContractsQuery, Result<List<RecentContractModel>>>,
Expand All @@ -196,9 +196,9 @@ public async Task<Result<ContractModel>> Handle(ContractQueries.GetContractQuery
return await this.ApiClient.GetContract(request, cancellationToken);
}

public async Task<Result> Handle(Commands.CreateContractCommand request,
public async Task<Result> Handle(ContractCommands.CreateContractCommand request,
CancellationToken cancellationToken) {
return Result.Success();
return await this.ApiClient.CreateContract(request, cancellationToken);
}

public async Task<Result> Handle(Commands.AddProductToContractCommand request,
Expand Down
6 changes: 4 additions & 2 deletions EstateManagmentUI.BusinessLogic/Requests/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ public record UpdateOperatorCommand(CorrelationId CorrelationId,Guid EstateId, G
public record CreateOperatorCommand(CorrelationId CorrelationId, Guid EstateId, string Name, bool RequireCustomMerchantNumber, bool RequireCustomTerminalNumber) : IRequest<Result>;
}

public static class ContractCommands {
public record CreateContractCommand(CorrelationId CorrelationId, Guid EstateId, string Description, Guid OperatorId) : IRequest<Result>;
}

public static class Commands
{

public record CreateContractCommand(CorrelationId CorrelationId, string AccessToken, Guid EstateId, string Description, Guid OperatorId) : IRequest<Result>;
public record CreateMerchantUserCommand(CorrelationId CorrelationId, string AccessToken, Guid EstateId, Guid MerchantId, string EmailAddress, string Password) : IRequest<Result>;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, Cancellation
MerchantCommands.UpdateMerchantCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteUpdateMerchant(cmd)),
OperatorCommands.CreateOperatorCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteCreateOperator(cmd)),
OperatorCommands.UpdateOperatorCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteUpdateOperator(cmd)),
Commands.CreateContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteCreateContract(cmd)),
ContractCommands.CreateContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteCreateContract(cmd)),
Commands.AddProductToContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAddProductToContract(cmd)),
Commands.AddTransactionFeeForProductToContractCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAddTransactionFee(cmd)),
MerchantCommands.AssignContractToMerchantCommand cmd => Task.FromResult((TResponse)(object)this.ExecuteAssignContractToMerchant(cmd)),
Expand Down Expand Up @@ -196,7 +196,7 @@ private Result ExecuteUpdateOperator(OperatorCommands.UpdateOperatorCommand cmd)
return Result.Success();
}

private Result ExecuteCreateContract(Commands.CreateContractCommand cmd)
private Result ExecuteCreateContract(ContractCommands.CreateContractCommand cmd)
{
var contract = new ContractModel
{
Expand Down
Loading