Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use DateTimeOffset for AgreementReadModel #544

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 12 additions & 14 deletions backend/Api/Agreements/AgreementController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace Api.Projects;
namespace Api.Agreements;

[Authorize]
[Route("/v0/{orgUrlKey}/agreements")]
Expand Down Expand Up @@ -36,17 +36,17 @@ public async Task<ActionResult<AgreementReadModel>> GetAgreement([FromRoute] str
AgreementId: agreement.Id,
EngagementId: agreement.EngagementId,
CustomerId: agreement.CustomerId,
StartDate: agreement.StartDate,
EndDate: agreement.EndDate,
NextPriceAdjustmentDate: agreement.NextPriceAdjustmentDate,
StartDate: agreement.StartDate is null ? null : new DateTimeOffset((DateTime)agreement.StartDate, TimeSpan.Zero),
EndDate: new DateTimeOffset(agreement.EndDate, TimeSpan.Zero) ,
NextPriceAdjustmentDate: agreement.NextPriceAdjustmentDate is null ? null : new DateTimeOffset((DateTime)agreement.NextPriceAdjustmentDate, TimeSpan.Zero),
PriceAdjustmentIndex: agreement.PriceAdjustmentIndex,
Notes: agreement.Notes,
Options: agreement.Options,
PriceAdjustmentProcess: agreement.PriceAdjustmentProcess,
Files: agreement.Files.Select(f => new FileReferenceReadModel(
FileName: f.FileName,
BlobName: f.BlobName,
UploadedOn: f.UploadedOn,
UploadedOn: new DateTimeOffset(f.UploadedOn, TimeSpan.Zero),
UploadedBy: f.UploadedBy ?? "Unknown"
)).ToList()
);
Expand All @@ -63,7 +63,7 @@ public async Task<ActionResult<List<AgreementReadModel>>> GetAgreementsByEngagem

var agreements = await agreementsRepository.GetAgreementsByEngagementId(engagementId, ct);

if (agreements is null || !agreements.Any()) return NotFound();
if (agreements.Count == 0) return NotFound();

var responseModels = agreements.Select(agreement => new AgreementReadModel(
AgreementId: agreement.Id,
Expand All @@ -89,7 +89,7 @@ public async Task<ActionResult<List<AgreementReadModel>>> GetAgreementsByEngagem
}

[HttpGet]
[Route("get/customer/{customerId}")]
[Route("get/customer/{customerId:int}")]
public async Task<ActionResult<List<AgreementReadModel>>> GetAgreementsByCustomer([FromRoute] string orgUrlKey,
[FromRoute] int customerId, CancellationToken ct)
{
Expand All @@ -98,7 +98,7 @@ public async Task<ActionResult<List<AgreementReadModel>>> GetAgreementsByCustome

var agreements = await agreementsRepository.GetAgreementsByCustomerId(customerId, ct);

if (agreements is null || !agreements.Any()) return NotFound();
if (agreements.Count == 0) return NotFound();

var responseModels = agreements.Select(agreement => new AgreementReadModel(
AgreementId: agreement.Id,
Expand Down Expand Up @@ -146,15 +146,15 @@ public async Task<ActionResult<AgreementReadModel>> Post([FromRoute] string orgU
Customer? customer = null;
if (body.CustomerId != null)
{
customer = await context.Customer.FindAsync(body.CustomerId.Value);
customer = await context.Customer.FindAsync([body.CustomerId.Value], ct);
if (customer == null)
return BadRequest("Customer not found");
}

Engagement? engagement = null;
if (body.EngagementId != null)
{
engagement = await context.Project.FindAsync(body.EngagementId.Value);
engagement = await context.Project.FindAsync([body.EngagementId.Value], ct);
if (engagement is null)
return BadRequest("Engagement not found");
}
Expand Down Expand Up @@ -232,10 +232,9 @@ public async Task<ActionResult<AgreementReadModel>> Put([FromRoute] string orgUr
if (agreement is null)
return NotFound("Agreement not found");

Customer? customer = null;
if (body.CustomerId is not null)
{
customer = await context.Customer.FindAsync(body.CustomerId);
var customer = await context.Customer.FindAsync([body.CustomerId], cancellationToken: ct);
if (customer is null)
return BadRequest("Customer not found");

Expand All @@ -248,10 +247,9 @@ public async Task<ActionResult<AgreementReadModel>> Put([FromRoute] string orgUr
agreement.Customer = null;
}

Engagement? engagement = null;
if (body.EngagementId is not null)
{
engagement = await context.Project.FindAsync(body.EngagementId);
var engagement = await context.Project.FindAsync([body.EngagementId], cancellationToken: ct);
if (engagement is null)
return BadRequest("Engagement not found");

Expand Down
18 changes: 10 additions & 8 deletions backend/Api/Agreements/AgreementModels.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
// ReSharper disable NotAccessedPositionalProperty.Global
// ReSharper disable ClassNeverInstantiated.Global

namespace Api.Agreements;

public record AgreementReadModel(
int AgreementId,
string? Name,
int? CustomerId,
int? EngagementId,
DateTime? StartDate,
DateTime EndDate,
DateTime? NextPriceAdjustmentDate,
DateTimeOffset? StartDate,
DateTimeOffset EndDate,
DateTimeOffset? NextPriceAdjustmentDate,
string? PriceAdjustmentIndex,
string? Notes,
string? Options,
Expand All @@ -19,7 +21,7 @@ List<FileReferenceReadModel> Files
public record FileReferenceReadModel(
string FileName,
string BlobName,
DateTime UploadedOn,
DateTimeOffset UploadedOn,
string? UploadedBy
);

Expand All @@ -43,7 +45,7 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
{
yield return new ValidationResult(
"At least one of CustomerId or EngagementId must be provided.",
new[] { nameof(CustomerId), nameof(EngagementId) });
[nameof(CustomerId), nameof(EngagementId)]);
}
}
}
Expand All @@ -54,4 +56,4 @@ public record FileReferenceWriteModel(
string BlobName,
DateTime UploadedOn,
string? UploadedBy
);
);
Loading