11using CaseManagement . CMMN . AspNetCore . Extensions ;
2- using CaseManagement . CMMN . CaseFile ;
32using CaseManagement . CMMN . CaseFile . Commands ;
43using CaseManagement . CMMN . CaseFile . Exceptions ;
5- using CaseManagement . CMMN . Infrastructures ;
4+ using CaseManagement . CMMN . CaseFile . Queries ;
5+ using CaseManagement . CMMN . Common . Exceptions ;
6+ using MediatR ;
67using Microsoft . AspNetCore . Authorization ;
7- using Microsoft . AspNetCore . Http ;
88using Microsoft . AspNetCore . Mvc ;
9+ using Newtonsoft . Json ;
910using System . Net ;
11+ using System . Net . Http ;
12+ using System . Text ;
13+ using System . Threading ;
1014using System . Threading . Tasks ;
1115
1216namespace CaseManagement . CMMN . AspNetCore . Apis
1317{
1418 [ Route ( CMMNConstants . RouteNames . CaseFiles ) ]
1519 public class CaseFilesController : Controller
1620 {
17- private readonly ICaseFileService _caseFileService ;
21+ private readonly IMediator _mediator ;
1822
19- public CaseFilesController ( ICaseFileService caseFileService )
23+ public CaseFilesController ( IMediator mediator )
2024 {
21- _caseFileService = caseFileService ;
25+ _mediator = mediator ;
2226 }
2327
2428 [ HttpGet ( "count" ) ]
25- [ Authorize ( "get_statistic" ) ]
26- public async Task < IActionResult > Count ( )
29+ public async Task < IActionResult > Count ( CancellationToken token )
2730 {
28- var result = await _caseFileService . Count ( ) ;
31+ var result = await _mediator . Send ( new CountCaseFileQuery ( ) , token ) ;
2932 return new OkObjectResult ( result ) ;
3033 }
3134
32- [ HttpPost ( "me" ) ]
33- [ Authorize ( "me_add_casefile" ) ]
34- public async Task < IActionResult > AddMe ( [ FromBody ] AddCaseFileCommand parameter )
35- {
36- try
37- {
38- parameter . Owner = this . GetNameIdentifier ( ) ;
39- var result = await _caseFileService . Add ( parameter ) ;
40- return new ContentResult
41- {
42- StatusCode = ( int ) HttpStatusCode . Created ,
43- Content = result . ToString ( )
44- } ;
45- }
46- catch ( AggregateValidationException ex )
47- {
48- return this . ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
49- }
50- }
51-
5235 [ HttpPost ]
5336 [ Authorize ( "add_casefile" ) ]
54- public async Task < IActionResult > Add ( [ FromBody ] AddCaseFileCommand parameter )
37+ public async Task < IActionResult > Add ( [ FromBody ] AddCaseFileCommand parameter , CancellationToken token )
5538 {
5639 try
5740 {
58- var result = await _caseFileService . Add ( parameter ) ;
59- return new ContentResult
60- {
61- StatusCode = ( int ) HttpStatusCode . Created ,
62- Content = result . ToString ( )
63- } ;
64- }
65- catch ( AggregateValidationException ex )
66- {
67- return this . ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
68- }
69- }
70-
71- [ HttpPut ( "me/{id}" ) ]
72- [ Authorize ( "me_update_casefile" ) ]
73- public async Task < IActionResult > UpdateMe ( string id , [ FromBody ] UpdateCaseFileCommand parameter )
74- {
75- try
76- {
77- parameter . Id = id ;
78- parameter . Performer = this . GetNameIdentifier ( ) ;
79- await _caseFileService . UpdateMe ( parameter ) ;
80- return new OkResult ( ) ;
81- }
82- catch ( UnknownCaseFileException )
83- {
84- return new NotFoundResult ( ) ;
85- }
86- catch ( UnauthorizedCaseFileException )
87- {
88- return new UnauthorizedResult ( ) ;
41+ var result = await _mediator . Send ( parameter , token ) ;
42+ return new CreatedResult ( string . Empty , result ) ;
8943 }
9044 catch ( AggregateValidationException ex )
9145 {
@@ -95,50 +49,18 @@ public async Task<IActionResult> UpdateMe(string id, [FromBody] UpdateCaseFileCo
9549
9650 [ HttpPut ( "{id}" ) ]
9751 [ Authorize ( "update_casefile" ) ]
98- public async Task < IActionResult > Update ( string id , [ FromBody ] UpdateCaseFileCommand parameter )
52+ public async Task < IActionResult > Update ( string id , [ FromBody ] UpdateCaseFileCommand parameter , CancellationToken token )
9953 {
10054 try
10155 {
10256 parameter . Id = id ;
103- await _caseFileService . Update ( parameter ) ;
57+ await _mediator . Send ( parameter , token ) ;
10458 return new OkResult ( ) ;
10559 }
10660 catch ( UnknownCaseFileException )
10761 {
10862 return new NotFoundResult ( ) ;
10963 }
110- catch ( UnauthorizedCaseFileException )
111- {
112- return new UnauthorizedResult ( ) ;
113- }
114- catch ( AggregateValidationException ex )
115- {
116- return this . ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
117- }
118- }
119-
120- [ HttpGet ( "me/{id}/publish" ) ]
121- [ Authorize ( "me_publish_casefile" ) ]
122- public async Task < IActionResult > PublishMe ( string id )
123- {
124- try
125- {
126- var cmd = new PublishCaseFileCommand
127- {
128- Id = id ,
129- Performer = this . GetNameIdentifier ( )
130- } ;
131- var result = await _caseFileService . PublishMe ( cmd ) ;
132- return new OkObjectResult ( result ) ;
133- }
134- catch ( UnknownCaseFileException )
135- {
136- return new NotFoundResult ( ) ;
137- }
138- catch ( UnauthorizedCaseFileException )
139- {
140- return new UnauthorizedResult ( ) ;
141- }
14264 catch ( AggregateValidationException ex )
14365 {
14466 return this . ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
@@ -147,76 +69,48 @@ public async Task<IActionResult> PublishMe(string id)
14769
14870 [ HttpGet ( "{id}/publish" ) ]
14971 [ Authorize ( "publish_casefile" ) ]
150- public async Task < IActionResult > Publish ( string id )
72+ public async Task < IActionResult > Publish ( string id , CancellationToken token )
15173 {
15274 try
15375 {
15476 var cmd = new PublishCaseFileCommand
15577 {
15678 Id = id
15779 } ;
158- var result = await _caseFileService . Publish ( cmd ) ;
80+ var result = await _mediator . Send ( cmd , token ) ;
15981 return new OkObjectResult ( result ) ;
16082 }
16183 catch ( UnknownCaseFileException )
16284 {
16385 return new NotFoundResult ( ) ;
16486 }
165- catch ( UnauthorizedCaseFileException )
166- {
167- return new UnauthorizedResult ( ) ;
168- }
16987 catch ( AggregateValidationException ex )
17088 {
17189 return this . ToError ( ex . Errors , HttpStatusCode . BadRequest , Request ) ;
17290 }
17391 }
17492
175- [ HttpGet ( "search" ) ]
93+ [ HttpPost ( "search" ) ]
17694 [ Authorize ( "get_casefile" ) ]
177- public async Task < IActionResult > Search ( )
95+ public async Task < IActionResult > Search ( [ FromBody ] SearchCaseFileQuery query , CancellationToken token )
17896 {
179- var query = HttpContext . Request . Query . ToEnumerable ( ) ;
180- var result = await _caseFileService . Search ( query ) ;
97+ var result = await _mediator . Send ( query , token ) ;
18198 return new OkObjectResult ( result ) ;
18299 }
183100
184- [ HttpGet ( "me/{id}" ) ]
185- [ Authorize ( "me_get_casefile" ) ]
186- public async Task < IActionResult > GetMe ( string id )
187- {
188- try
189- {
190- var result = await _caseFileService . GetMe ( id , this . GetNameIdentifier ( ) ) ;
191- return new OkObjectResult ( result ) ;
192- }
193- catch ( UnknownCaseFileException )
194- {
195- return new NotFoundResult ( ) ;
196- }
197- catch ( UnauthorizedCaseFileException )
198- {
199- return new UnauthorizedResult ( ) ;
200- }
201- }
202-
203101 [ HttpGet ( "{id}" ) ]
204102 [ Authorize ( "get_casefile" ) ]
205- public async Task < IActionResult > Get ( string id )
103+ public async Task < IActionResult > Get ( string id , CancellationToken token )
206104 {
207105 try
208106 {
209- var result = await _caseFileService . Get ( id ) ;
107+ var result = await _mediator . Send ( new GetCaseFileQuery { Id = id } , token ) ;
210108 return new OkObjectResult ( result ) ;
211109 }
212110 catch ( UnknownCaseFileException )
213111 {
214112 return new NotFoundResult ( ) ;
215113 }
216- catch ( UnauthorizedCaseFileException )
217- {
218- return new UnauthorizedResult ( ) ;
219- }
220114 }
221115 }
222116}
0 commit comments