1
+ using System . Collections . Generic ;
2
+ using System . Threading . Tasks ;
3
+ using GettingThingsDone . ApplicationCore . Helpers ;
4
+ using GettingThingsDone . Contracts . Dto ;
5
+ using GettingThingsDone . Contracts . Interface ;
6
+ using GettingThingsDone . WebApi . Controllers ;
7
+ using Microsoft . AspNetCore . Mvc ;
8
+ using Serilog ;
9
+ using Microsoft . AspNetCore . Mvc . Versioning ;
10
+
11
+
12
+ namespace GettingThingsDone . WebApiV2 . Controllers
13
+ {
14
+
15
+ [ ApiVersion ( "2.0" ) ]
16
+ [ Route ( "api/actions" ) ]
17
+ [ ApiController ]
18
+ public class ActionController : BaseController
19
+ {
20
+ private static class Routes
21
+ {
22
+ public const string GetActionById = nameof ( GetActionById ) ;
23
+ }
24
+
25
+ private readonly IActionService _actionService ;
26
+
27
+ public ActionController ( IActionService actionService )
28
+ {
29
+ _actionService = actionService ;
30
+ }
31
+
32
+ [ HttpGet ]
33
+ public async Task < ActionResult < List < ActionDto > > > GetAll ( )
34
+ {
35
+ Log . Information ( "Get all Actions!" ) ;
36
+ return FromValueServiceResult ( await _actionService . GetAll ( ) ) ;
37
+ }
38
+
39
+ [ HttpGet ( "{id}" , Name = Routes . GetActionById ) ]
40
+ public async Task < ActionResult < ActionDto > > GetById ( int id )
41
+ {
42
+ return FromValueServiceResult ( await _actionService . GetAction ( id ) ) ;
43
+ }
44
+
45
+ [ HttpPost ]
46
+ public async Task < IActionResult > Create ( [ FromBody ] ActionDto value )
47
+ {
48
+ Log . Warning ( "Action create {@value}" , value ) ;
49
+
50
+ if ( ! value . RepresentsNewEntity )
51
+ return BadRequest ( ) ;
52
+
53
+ var result = await _actionService . CreateOrUpdate ( value ) ;
54
+
55
+ if ( ! result . IsOk ( ) )
56
+ return FromServiceResult ( result ) ;
57
+
58
+ return CreatedAtRoute ( Routes . GetActionById , new { id = result . Value . Id } , result . Value ) ;
59
+ }
60
+
61
+ [ HttpPut ]
62
+ public async Task < IActionResult > Update ( [ FromBody ] ActionDto value )
63
+ {
64
+ if ( value . RepresentsNewEntity )
65
+ return BadRequest ( ) ;
66
+
67
+ var result = await _actionService . CreateOrUpdate ( value ) ;
68
+
69
+ if ( ! result . IsOk ( ) )
70
+ return FromServiceResult ( result ) ;
71
+
72
+ return NoContent ( ) ;
73
+ }
74
+
75
+ [ HttpDelete ( "{id}" ) ]
76
+ public async Task < IActionResult > Delete ( int id )
77
+ {
78
+ var result = await _actionService . Delete ( id ) ;
79
+
80
+ if ( ! result . IsOk ( ) )
81
+ return FromServiceResult ( result ) ;
82
+
83
+ return NoContent ( ) ;
84
+ }
85
+
86
+ [ HttpPut ( "{id}/move-to/{listId}" ) ]
87
+ public async Task < IActionResult > MoveToList ( int id , int listId )
88
+ {
89
+ var result = await _actionService . MoveToList ( id , listId ) ;
90
+
91
+ if ( ! result . IsOk ( ) )
92
+ return FromServiceResult ( result ) ;
93
+
94
+ return NoContent ( ) ;
95
+ }
96
+
97
+ [ HttpPut ( "{id}/assign-to/{projectId}" ) ]
98
+ public async Task < IActionResult > AssignToProject ( int id , int projectId )
99
+ {
100
+ var result = await _actionService . AssignToProject ( id , projectId ) ;
101
+
102
+ if ( ! result . IsOk ( ) )
103
+ return FromServiceResult ( result ) ;
104
+
105
+ return NoContent ( ) ;
106
+ }
107
+ }
108
+ }
0 commit comments