Skip to content

Commit ecfee52

Browse files
committed
Added validation to API project and changed Function name.
1 parent 09b1fb4 commit ecfee52

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

ClaimsSolution.API/Controllers/ClaimsController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task<IActionResult> SubmitClaim([FromBody] SubmitClaimRequestDto re
2525
return Accepted(response);
2626
}
2727

28-
[HttpGet("{userId:guid}/{claimId:guid}")]
28+
[HttpGet("GetClaimByUserAndClaimId/{userId:guid}/{claimId:guid}")]
2929
[ProducesResponseType(typeof(ClaimResponseDto), StatusCodes.Status200OK)]
3030
[ProducesResponseType(StatusCodes.Status404NotFound)]
3131
public async Task<IActionResult> GetClaim(Guid userId, Guid claimId, CancellationToken ct)
@@ -40,7 +40,7 @@ public async Task<IActionResult> GetClaim(Guid userId, Guid claimId, Cancellatio
4040
return Ok(claim);
4141
}
4242

43-
[HttpGet("{userId:guid}")]
43+
[HttpGet("GetUserClaims/{userId:guid}")]
4444
[ProducesResponseType(typeof(IEnumerable<ClaimResponseDto>), StatusCodes.Status200OK)]
4545
public async Task<IActionResult> GetUserClaims(Guid userId, CancellationToken ct)
4646
{
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,50 @@
1-
namespace ClaimsSolution.API.Dtos.RequestDtos
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ClaimsSolution.API.Dtos.RequestDtos
24
{
35
public class SubmitClaimRequestDto
46
{
7+
[Required(ErrorMessage = "UserId is required")]
58
public Guid UserId { get; init; }
9+
10+
[Required(ErrorMessage = "PhoneNumber is required")]
11+
[RegularExpression(@"^\d{10}$|^\+?\d{1,3}[-.\s]?\d{1,14}$",
12+
ErrorMessage = "PhoneNumber must be a valid phone number format")]
613
public string PhoneNumber { get; init; } = string.Empty;
14+
15+
[Required(ErrorMessage = "Ssn is required")]
16+
[RegularExpression(@"^\d{3}-?\d{2}-?\d{4}$",
17+
ErrorMessage = "Ssn must be a valid Social Security Number (XXX-XX-XXXX or XXXXXXXXX)")]
718
public string Ssn { get; init; } = string.Empty;
19+
20+
[Required(ErrorMessage = "IncidentDate is required")]
21+
[DataType(DataType.Date)]
22+
[CustomValidation(typeof(SubmitClaimRequestDto), nameof(ValidateIncidentDate))]
823
public DateTime IncidentDate { get; init; }
24+
25+
[Required(ErrorMessage = "VehicleMake is required")]
26+
[StringLength(50, MinimumLength = 1,
27+
ErrorMessage = "VehicleMake must be between 1 and 50 characters")]
928
public string VehicleMake { get; init; } = string.Empty;
29+
30+
[Required(ErrorMessage = "VehicleModel is required")]
31+
[StringLength(50, MinimumLength = 1,
32+
ErrorMessage = "VehicleModel must be between 1 and 50 characters")]
1033
public string VehicleModel { get; init; } = string.Empty;
34+
35+
public static ValidationResult? ValidateIncidentDate(DateTime incidentDate, ValidationContext context)
36+
{
37+
if (incidentDate == default)
38+
{
39+
return new ValidationResult("IncidentDate cannot be empty");
40+
}
41+
42+
if (incidentDate.Date > DateTime.UtcNow.Date)
43+
{
44+
return new ValidationResult("IncidentDate cannot be in the future");
45+
}
46+
47+
return ValidationResult.Success;
48+
}
1149
}
1250
}

ClaimsSolution.API/Services/ClaimService.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ public async Task<SubmitClaimResponseDto> SubmitAsync(SubmitClaimRequestDto requ
5454

5555
public async Task<ClaimResponseDto?> GetClaimByIdAsync(Guid userId, Guid claimId, CancellationToken ct)
5656
{
57-
_logger.LogInformation($"Retrieving claim {claimId} for user {userId}");
58-
5957
var claimEntity = await _claimRepository.GetClaimByIdAsync(userId, claimId, ct);
6058

6159
if (claimEntity == null)
6260
{
63-
_logger.LogWarning($"Claim {claimId} not found for user {userId}");
6461
return null;
6562
}
6663

@@ -69,11 +66,9 @@ public async Task<SubmitClaimResponseDto> SubmitAsync(SubmitClaimRequestDto requ
6966

7067
public async Task<IEnumerable<ClaimResponseDto>> GetClaimsByUserIdAsync(Guid userId, CancellationToken ct)
7168
{
72-
_logger.LogInformation($"Retrieving all claims for user {userId}");
73-
7469
var claimEntities = await _claimRepository.GetClaimsByUserIdAsync(userId, ct);
7570

76-
return claimEntities.Select(MapToResponseDto).ToList();
71+
return [.. claimEntities.Select(MapToResponseDto)];
7772
}
7873

7974
private static ClaimResponseDto MapToResponseDto(ClaimEntity claimEntity)
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<TargetFramework>net8.0</TargetFramework>
54
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
65
<OutputType>Exe</OutputType>
76
<ImplicitUsings>enable</ImplicitUsings>
87
<Nullable>enable</Nullable>
98
</PropertyGroup>
10-
119
<ItemGroup>
1210
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1311
<PackageReference Include="Azure.Storage.Blobs" Version="12.24.0" />
@@ -17,7 +15,6 @@
1715
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.1.0" />
1816
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="2.0.0" />
1917
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.2" />
20-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.6.0" />
2118
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.7.0" />
2219
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.2" />
2320
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.5" />
@@ -27,11 +24,11 @@
2724
<ItemGroup>
2825
<None Update="host.json">
2926
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
28+
<!-- ADDED -->
3029
</None>
3130
</ItemGroup>
32-
3331
<ItemGroup>
3432
<ProjectReference Include="..\ClaimsSolution.Common\ClaimsSolution.Common.csproj" />
3533
</ItemGroup>
36-
37-
</Project>
34+
</Project>

ClaimsSolution.ClaimsProcessor/Functions/ProcessClaimFunction.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.Azure.Functions.Worker;
66
using Microsoft.Extensions.Logging;
77
using System.Text.Json;
8-
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
98

109
namespace ClaimsSolution.ClaimsProcessor.Functions
1110
{
@@ -21,7 +20,6 @@ public ProcessClaimFunction(ILogger<ProcessClaimFunction> logger, IClaimReposito
2120
}
2221

2322
[Function(nameof(IngestClaims))]
24-
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(ClaimSubmittedMessage))]
2523
public async Task IngestClaims(
2624
[QueueTrigger("claims-queue", Connection = "AzureWebJobsStorage")] QueueMessage message,
2725
FunctionContext context)
@@ -36,7 +34,6 @@ public async Task IngestClaims(
3634
}
3735

3836
await ProcessClaimAsync(claimMessage, context.CancellationToken);
39-
4037
_logger.LogInformation($"Claim {claimMessage.ClaimId} processed successfully");
4138
}
4239

@@ -59,7 +56,6 @@ private async Task ProcessClaimAsync(ClaimSubmittedMessage claimMessage, Cancell
5956
};
6057

6158
await _claimRepository.AddClaimAsync(claimEntity, ct);
62-
6359
_logger.LogInformation($"Claim {claimMessage.ClaimId} saved to repository");
6460
}
6561
}

0 commit comments

Comments
 (0)