|
| 1 | +using EstateManagement.Database.Contexts; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | + |
| 6 | +namespace EstateReportingAPI.Controllers |
| 7 | +{ |
| 8 | + using Shared.EntityFramework; |
| 9 | + |
| 10 | + [ExcludeFromCodeCoverage] |
| 11 | + [Route(FactSettlementsController.ControllerRoute)] |
| 12 | + [ApiController] |
| 13 | + public class FactSettlementsController : ControllerBase |
| 14 | + { |
| 15 | + #region Others |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// The controller name |
| 19 | + /// </summary> |
| 20 | + public const String ControllerName = "settlements"; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The controller route |
| 24 | + /// </summary> |
| 25 | + private const String ControllerRoute = "api/facts/" + FactSettlementsController.ControllerName; |
| 26 | + |
| 27 | + #endregion |
| 28 | + |
| 29 | + private readonly IDbContextFactory<EstateManagementGenericContext> ContextFactory; |
| 30 | + |
| 31 | + public FactSettlementsController(IDbContextFactory<EstateManagementGenericContext> contextFactory) |
| 32 | + { |
| 33 | + this.ContextFactory = contextFactory; |
| 34 | + } |
| 35 | + |
| 36 | + private const String ConnectionStringIdentifier = "EstateReportingReadModel"; |
| 37 | + |
| 38 | + [HttpGet] |
| 39 | + [Route("todayssettlement")] |
| 40 | + public async Task<IActionResult> TodaysSettlement([FromHeader] Guid estateId, [FromQuery] DateTime comparisonDate, CancellationToken cancellationToken) |
| 41 | + { |
| 42 | + EstateManagementGenericContext? context = await this.ContextFactory.GetContext(estateId, FactSettlementsController.ConnectionStringIdentifier, cancellationToken); |
| 43 | + |
| 44 | + // First we need to get a value of todays sales |
| 45 | + Decimal todaysSettlement = (from s in context.Settlements |
| 46 | + join f in context.MerchantSettlementFees on s.SettlementReportingId equals f.SettlementReportingId |
| 47 | + where f.IsSettled && s.SettlementDate == DateTime.Now.Date |
| 48 | + select f.CalculatedValue).Sum(); |
| 49 | + |
| 50 | + Int32 todaysSettlementCount = (from s in context.Settlements |
| 51 | + join f in context.MerchantSettlementFees on s.SettlementReportingId equals f.SettlementReportingId |
| 52 | + where f.IsSettled && s.SettlementDate == DateTime.Now.Date |
| 53 | + select f.CalculatedValue).Count(); |
| 54 | + |
| 55 | + Decimal comparisonSettlement = (from s in context.Settlements |
| 56 | + join f in context.MerchantSettlementFees on s.SettlementReportingId equals f.SettlementReportingId |
| 57 | + where f.IsSettled && s.SettlementDate == comparisonDate |
| 58 | + select f.CalculatedValue).Sum(); |
| 59 | + |
| 60 | + Int32 comparisonSettlementCount = (from s in context.Settlements |
| 61 | + join f in context.MerchantSettlementFees on s.SettlementReportingId equals f.SettlementReportingId |
| 62 | + where f.IsSettled && s.SettlementDate == comparisonDate |
| 63 | + select f.CalculatedValue).Count(); |
| 64 | + |
| 65 | + var response = new |
| 66 | + { |
| 67 | + TodaysSettlementValue = todaysSettlement, |
| 68 | + TodaysSettlementCount = todaysSettlementCount, |
| 69 | + ComparisonSettlement = comparisonSettlement, |
| 70 | + ComparisonSettlementCount = comparisonSettlementCount |
| 71 | + }; |
| 72 | + |
| 73 | + return this.Ok(response); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments