1
1
using System . Linq ;
2
- using JsonApiDotNetCore . Extensions ;
2
+ using System . Threading . Tasks ;
3
+ using JsonApiDotNetCore . Data ;
3
4
using JsonApiDotNetCore . Models ;
4
5
using JsonApiDotNetCore . Services ;
5
6
using Microsoft . AspNetCore . Mvc ;
6
- using Microsoft . EntityFrameworkCore ;
7
+ using Microsoft . AspNetCore . Routing ;
7
8
using Microsoft . Extensions . Logging ;
8
9
using Newtonsoft . Json ;
9
10
@@ -12,58 +13,56 @@ namespace JsonApiDotNetCore.Controllers
12
13
public class JsonApiController < T > : JsonApiController < T , int > where T : class , IIdentifiable < int >
13
14
{
14
15
public JsonApiController (
15
- ILoggerFactory loggerFactory ,
16
- DbContext context ,
17
- IJsonApiContext jsonApiContext )
18
- : base ( loggerFactory , context , jsonApiContext )
16
+ IJsonApiContext jsonApiContext ,
17
+ IEntityRepository < T , int > entityRepository ,
18
+ ILoggerFactory loggerFactory )
19
+ : base ( jsonApiContext , entityRepository , loggerFactory )
19
20
{ }
20
21
}
21
22
22
23
public class JsonApiController < T , TId > : Controller where T : class , IIdentifiable < TId >
23
24
{
24
- private readonly DbContext _context ;
25
- private readonly DbSet < T > _dbSet ;
25
+ private readonly IEntityRepository < T , TId > _entities ;
26
26
private readonly IJsonApiContext _jsonApiContext ;
27
27
private readonly ILogger _logger ;
28
28
29
29
public JsonApiController (
30
- ILoggerFactory loggerFactory ,
31
- DbContext context ,
32
- IJsonApiContext jsonApiContext )
30
+ IJsonApiContext jsonApiContext ,
31
+ IEntityRepository < T , TId > entityRepository ,
32
+ ILoggerFactory loggerFactory )
33
33
{
34
- _context = context ;
35
- _dbSet = context . GetDbSet < T > ( ) ;
36
34
_jsonApiContext = jsonApiContext ;
35
+ _entities = entityRepository ;
37
36
38
37
_logger = loggerFactory . CreateLogger < JsonApiController < T , TId > > ( ) ;
39
38
_logger . LogTrace ( $@ "JsonApiController activated with ContextGraph:
40
39
{ JsonConvert . SerializeObject ( jsonApiContext . ContextGraph ) } " ) ;
41
40
}
42
41
43
42
public JsonApiController (
44
- DbContext context ,
45
- IJsonApiContext jsonApiContext )
43
+ IJsonApiContext jsonApiContext ,
44
+ IEntityRepository < T , TId > entityRepository )
46
45
{
47
- _context = context ;
48
- _dbSet = context . GetDbSet < T > ( ) ;
49
46
_jsonApiContext = jsonApiContext ;
47
+ _entities = entityRepository ;
50
48
}
51
49
52
50
[ HttpGet ]
53
51
public virtual IActionResult Get ( )
54
52
{
55
53
ApplyContext ( ) ;
56
54
57
- var entities = _dbSet . ToList ( ) ;
55
+ var entities = _entities . Get ( ) . ToList ( ) ;
56
+
58
57
return Ok ( entities ) ;
59
58
}
60
59
61
60
[ HttpGet ( "{id}" ) ]
62
- public virtual IActionResult Get ( TId id )
61
+ public virtual async Task < IActionResult > GetAsync ( TId id )
63
62
{
64
63
ApplyContext ( ) ;
65
64
66
- var entity = _dbSet . FirstOrDefault ( e => e . Id . Equals ( id ) ) ;
65
+ var entity = await _entities . GetAsync ( id ) ;
67
66
68
67
if ( entity == null )
69
68
return NotFound ( ) ;
@@ -72,7 +71,7 @@ public virtual IActionResult Get(TId id)
72
71
}
73
72
74
73
[ HttpGet ( "{id}/{relationshipName}" ) ]
75
- public virtual IActionResult GetRelationship ( TId id , string relationshipName )
74
+ public virtual async Task < IActionResult > GetRelationshipAsync ( TId id , string relationshipName )
76
75
{
77
76
ApplyContext ( ) ;
78
77
@@ -81,9 +80,7 @@ public virtual IActionResult GetRelationship(TId id, string relationshipName)
81
80
if ( relationshipName == null )
82
81
return NotFound ( ) ;
83
82
84
- var entity = _dbSet
85
- . Include ( relationshipName )
86
- . FirstOrDefault ( e => e . Id . Equals ( id ) ) ;
83
+ var entity = await _entities . GetAndIncludeAsync ( id , relationshipName ) ;
87
84
88
85
if ( entity == null )
89
86
return NotFound ( ) ;
@@ -100,41 +97,29 @@ public virtual IActionResult GetRelationship(TId id, string relationshipName)
100
97
}
101
98
102
99
[ HttpPost ]
103
- public virtual IActionResult Post ( [ FromBody ] T entity )
100
+ public virtual async Task < IActionResult > PostAsync ( [ FromBody ] T entity )
104
101
{
105
102
ApplyContext ( ) ;
106
103
107
104
if ( entity == null )
108
105
return BadRequest ( ) ;
109
106
110
- _dbSet . Add ( entity ) ;
111
- _context . SaveChanges ( ) ;
107
+ await _entities . CreateAsync ( entity ) ;
112
108
113
109
return Created ( HttpContext . Request . Path , entity ) ;
114
110
}
115
111
116
112
[ HttpPatch ( "{id}" ) ]
117
- public virtual IActionResult Patch ( int id , [ FromBody ] T entity )
113
+ public virtual async Task < IActionResult > PatchAsync ( TId id , [ FromBody ] T entity )
118
114
{
119
115
ApplyContext ( ) ;
120
116
121
117
if ( entity == null )
122
118
return BadRequest ( ) ;
123
119
124
- var oldEntity = _dbSet . FirstOrDefault ( e => e . Id . Equals ( id ) ) ;
125
- if ( oldEntity == null )
126
- return NotFound ( ) ;
127
-
128
- var requestEntity = _jsonApiContext . RequestEntity ;
129
-
130
- requestEntity . Attributes . ForEach ( attr =>
131
- {
132
- attr . SetValue ( oldEntity , attr . GetValue ( entity ) ) ;
133
- } ) ;
120
+ var updatedEntity = await _entities . UpdateAsync ( id , entity ) ;
134
121
135
- _context . SaveChanges ( ) ;
136
-
137
- return Ok ( oldEntity ) ;
122
+ return Ok ( updatedEntity ) ;
138
123
}
139
124
140
125
// [HttpPatch("{id}/{relationship}")]
@@ -144,16 +129,14 @@ public virtual IActionResult Patch(int id, [FromBody] T entity)
144
129
// }
145
130
146
131
[ HttpDelete ( "{id}" ) ]
147
- public virtual IActionResult Delete ( TId id )
132
+ public virtual async Task < IActionResult > DeleteAsync ( TId id )
148
133
{
149
134
ApplyContext ( ) ;
150
135
151
- var entity = _dbSet . FirstOrDefault ( e => e . Id . Equals ( id ) ) ;
152
- if ( entity == null )
153
- return NotFound ( ) ;
136
+ var wasDeleted = await _entities . DeleteAsync ( id ) ;
154
137
155
- _dbSet . Remove ( entity ) ;
156
- _context . SaveChanges ( ) ;
138
+ if ( ! wasDeleted )
139
+ return NotFound ( ) ;
157
140
158
141
return Ok ( ) ;
159
142
}
@@ -166,6 +149,7 @@ public virtual IActionResult Delete(TId id)
166
149
167
150
private void ApplyContext ( )
168
151
{
152
+ var routeData = HttpContext . GetRouteData ( ) ;
169
153
_jsonApiContext . RequestEntity = _jsonApiContext . ContextGraph . GetContextEntity ( typeof ( T ) ) ;
170
154
_jsonApiContext . ApplyContext ( HttpContext ) ;
171
155
}
0 commit comments