Skip to content

Instantly share code, notes, and snippets.

@adejorosam
Created June 24, 2023 18:39
Show Gist options
  • Save adejorosam/b52d17c0d30e7058539184290a880936 to your computer and use it in GitHub Desktop.
Save adejorosam/b52d17c0d30e7058539184290a880936 to your computer and use it in GitHub Desktop.
/// <summary>
/// Create an NGN investment
/// </summary>
[HttpPost("ngn")]
[ProducesResponseType(typeof(InvestmentExtendedResponse), 201)]
[ProducesResponseType(typeof(GenericResponse), 400)]
[ProducesResponseType(typeof(GenericResponse), 404)]
public async Task<IActionResult> CreateNgn([FromBody] CreateNgnInvestmentRequest req)
{
var user = await _userService.FindById(req.OwnerId);
var staffId = User.GetCallerId();
var staff = await _staffService.FindById(staffId);
if (staff is null)
return NotFound("Staff account not found.");
if (user is null)
return NotFound("User account not found.");
Investment investment;
if (req.SourceInvestmentId.HasValue)
{
var sourceInvestment = await _investmentService.FindById(req.SourceInvestmentId.Value);
if (sourceInvestment is null)
return NotFound("Source investment does not exist");
if (sourceInvestment.Status != InvestmentStatus.Liquidated)
return BadRequest("You can only reinvest a liquidated investment");
if (req.BusinessId.HasValue)
{
var business = await _businessService.FindById(req.BusinessId.Value);
if (business is null)
return NotFound("Business profile not found.");
if (business.OwnerId != req.OwnerId)
return BadRequest("Business profile does not match the user.");
investment = await _investmentService.CreateCorporateNgn(user.Id, user.FullName(), business.Id,
business.BusinessName, req.PrincipalAmount, req.PayoutFrequency, req.TenorInDays, req.InterestRate, sourceInvestment, staff);
}
else
{
investment = await _investmentService.CreatePersonalNgn(user.Id, user.FullName(), req.PrincipalAmount,
req.PayoutFrequency, req.TenorInDays, req.InterestRate, sourceInvestment, staff);
}
}
else
{
if (req.BusinessId.HasValue)
{
var business = await _businessService.FindById(req.BusinessId.Value);
if (business is null)
return NotFound("Business profile not found.");
if (business.OwnerId != req.OwnerId)
return BadRequest("Business profile does not match the user.");
investment = await _investmentService.CreateCorporateNgn(user.Id, user.FullName(), business.Id,
business.BusinessName, req.PrincipalAmount, req.PayoutFrequency, req.TenorInDays, req.InterestRate);
}
else
{
investment = await _investmentService.CreatePersonalNgn(user.Id, user.FullName(), req.PrincipalAmount,
req.PayoutFrequency, req.TenorInDays, req.InterestRate);
}
}
return Created(_mapper.Map<InvestmentExtendedResponse>(investment));
}
@imanoel01
Copy link

Yeah, the logic for creating investment is a bit repetitive. We can extract it into a separate method.

[HttpPost("ngn")]
[ProducesResponseType(typeof(InvestmentExtendedResponse), 201)]
[ProducesResponseType(typeof(GenericResponse), 400)]
[ProducesResponseType(typeof(GenericResponse), 404)]
public async Task CreateNgn([FromBody] CreateNgnInvestmentRequest req)
{
var user = await _userService.FindById(req.OwnerId);
var staffId = User.GetCallerId();
var staff = await _staffService.FindById(staffId);

if (staff is null)
    return NotFound("Staff account not found.");

if (user is null)
    return NotFound("User account not found.");

Investment investment;

if (req.SourceInvestmentId.HasValue)
{
    var sourceInvestment = await _investmentService.FindById(req.SourceInvestmentId.Value);

    if (sourceInvestment is null)
        return NotFound("Source investment does not exist");

    if (sourceInvestment.Status != InvestmentStatus.Liquidated)
        return BadRequest("You can only reinvest a liquidated investment");

    investment = await CreateInvestment(user, req, sourceInvestment, staff);
}
else
{
    investment = await CreateInvestment(user, req, null, staff);
}

return Created(_mapper.Map<InvestmentExtendedResponse>(investment));

}

private async Task CreateInvestment(User user, CreateNgnInvestmentRequest req, Investment sourceInvestment, Staff staff)
{
Investment investment;

if (req.BusinessId.HasValue)
{
    var business = await _businessService.FindById(req.BusinessId.Value);

    if (business is null)
        return NotFound("Business profile not found.");

    if (business.OwnerId != req.OwnerId)
        return BadRequest("Business profile does not match the user.");

    investment = await _investmentService.CreateCorporateNgn(user.Id, user.FullName(), business.Id,
        business.BusinessName, req.PrincipalAmount, req.PayoutFrequency, req.TenorInDays, req.InterestRate, sourceInvestment, staff);
}
else
{
    investment = await _investmentService.CreatePersonalNgn(user.Id, user.FullName(), req.PrincipalAmount,
        req.PayoutFrequency, req.TenorInDays, req.InterestRate, sourceInvestment, staff);
}

return investment;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment