File tree Expand file tree Collapse file tree 5 files changed +133
-0
lines changed
Expand file tree Collapse file tree 5 files changed +133
-0
lines changed Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . ComponentModel . DataAnnotations ;
4+ using System . Resources ;
5+ using Microsoft . AspNetCore . Mvc ;
6+
7+
8+ namespace lib
9+ {
10+ public interface IMyResourceService
11+ {
12+ IDictionary < string , string > GetValidationErrors ( ModelStateDictionary modelState ) ;
13+ string GetErrorMessage ( string errorKey ) ;
14+ }
15+
16+ public class MyResourceService : IMyResourceService
17+ {
18+ private readonly ResourceManager _messagesResourceManager ;
19+ private readonly ResourceManager _errorMessagesResourceManager ;
20+
21+ public MyResourceService ( )
22+ {
23+ _messagesResourceManager = new ResourceManager ( typeof ( Messages ) ) ;
24+ _errorMessagesResourceManager = new ResourceManager ( typeof ( ErrorMessages ) ) ;
25+ }
26+
27+ public IDictionary < string , string > GetValidationErrors ( ModelStateDictionary modelState )
28+ {
29+ var errors = new Dictionary < string , string > ( ) ;
30+ foreach ( var field in modelState )
31+ {
32+ if ( field . Value . ValidationState == ModelValidationState . Invalid )
33+ {
34+ foreach ( var error in field . Value . Errors )
35+ {
36+ string errorMessage = GetErrorMessage ( error . ErrorMessage ) ;
37+ errors . Add ( field . Key , errorMessage ) ;
38+ }
39+ }
40+ }
41+
42+ return errors ;
43+ }
44+
45+ public string GetErrorMessage ( string errorKey )
46+ {
47+ return _errorMessagesResourceManager . GetString ( errorKey ) ;
48+ }
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Mvc ;
2+ using lib ;
3+
4+ namespace someservice . Controllers ;
5+
6+ [ ApiController ]
7+ [ Route ( "[controller]" ) ]
8+ public class ValuesController : ControllerBase
9+ {
10+ [ ApiController ]
11+ [ Route ( "api/[controller]" ) ]
12+ public class ValuesController : ControllerBase
13+ {
14+ private readonly IMyResourceService _resourceService ;
15+
16+ public ValuesController ( IMyResourceService resourceService )
17+ {
18+ _resourceService = resourceService ;
19+ }
20+
21+ [ HttpPost ]
22+ public IActionResult Post ( [ FromBody ] InputModel inputModel )
23+ {
24+ try
25+ {
26+ // Perform validation and processing
27+ if ( ! ModelState . IsValid )
28+ {
29+ // Handle validation errors
30+ var validationErrors = _resourceService . GetValidationErrors ( ModelState ) ;
31+ return BadRequest ( validationErrors ) ;
32+ }
33+
34+ // Process the input model
35+ // ...
36+
37+ return Ok ( "Success" ) ;
38+ }
39+ catch ( Exception ex )
40+ {
41+ // Handle exceptions
42+ string errorMessage = _resourceService . GetErrorMessage ( "SomeErrorKey" ) ;
43+ return StatusCode ( 500 , errorMessage ) ;
44+ }
45+ }
46+ }
47+ }
Original file line number Diff line number Diff line change 1+ using System . ComponentModel . DataAnnotations ;
2+
3+ namespace someservice . Models
4+ {
5+ public class InputModel
6+ {
7+ [ Required ( ErrorMessageResourceType = typeof ( lib . ValidationMessages ) , ErrorMessageResourceName = "Required" ) ]
8+ public string Name { get ; set ; }
9+
10+ [ EmailAddress ( ErrorMessageResourceType = typeof ( lib . ValidationMessages ) , ErrorMessageResourceName = "Email" ) ]
11+ public string Email { get ; set ; }
12+
13+ [ StringLength ( 50 , ErrorMessageResourceType = typeof ( lib . ValidationMessages ) , ErrorMessageResourceName = "Length" ) ]
14+ public string Address { get ; set ; }
15+ }
16+ }
Original file line number Diff line number Diff line change 77 </PropertyGroup >
88
99 <ItemGroup >
10+ <PackageReference Include =" Microsoft.AspNetCore.App" Version =" 2.2.8" />
11+ <PackageReference Include =" Microsoft.AspNetCore.Http" Version =" 2.2.2" />
12+ <PackageReference Include =" Microsoft.AspNetCore.Mvc" Version =" 2.2.0" />
13+ <PackageReference Include =" Microsoft.AspNetCore.Routing" Version =" 2.2.2" />
1014 <PackageReference Include =" Swashbuckle.AspNetCore" Version =" 6.5.0" />
1115 </ItemGroup >
1216
17+ <ItemGroup >
18+ <ProjectReference Include =" ..\lib\lib.csproj" />
19+ </ItemGroup >
20+
1321</Project >
Original file line number Diff line number Diff line change 1+ using Xunit ;
2+
3+ namespace tests ;
4+
5+ public class UnitTest1
6+ {
7+ [ Fact ]
8+ public void Test1 ( )
9+ {
10+
11+ }
12+ }
You can’t perform that action at this time.
0 commit comments