forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiController.cs
More file actions
99 lines (91 loc) · 3.78 KB
/
Copy pathApiController.cs
File metadata and controls
99 lines (91 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSONAPI.EntityFramework.Http
{
public class ApiController<T,TC> : JSONAPI.Http.ApiController<T>
where T : class // hmm...see http://stackoverflow.com/a/6451237/489116
where TC : DbContext
{
private EntityFrameworkMaterializer _materializer = null;
protected override JSONAPI.Core.IMaterializer MaterializerFactory()
{
if (_materializer == null)
{
DbContext context = (DbContext)Activator.CreateInstance(typeof(TC));
_materializer = new JSONAPI.EntityFramework.EntityFrameworkMaterializer(context);
}
return _materializer;
}
protected override TM MaterializerFactory<TM>()
{
return base.MaterializerFactory<TM>();
}
protected override IQueryable<T> QueryableFactory(Core.IMaterializer materializer = null)
{
if (materializer == null)
{
materializer = MaterializerFactory();
}
return ((EntityFrameworkMaterializer)materializer).DbContext.Set<T>();
}
public override async Task<IList<T>> Post(IList<T> postedObjs)
{
var materializer = this.MaterializerFactory<EntityFrameworkMaterializer>();
List<T> materialList = new List<T>();
foreach (T postedObj in postedObjs)
{
DbContext context = materializer.DbContext;
var material = await materializer.MaterializeUpdateAsync(postedObj);
if (context.Entry<T>(material).State == EntityState.Added)
{
await context.SaveChangesAsync();
materialList.Add(material);
}
else
{
// POST should only create an object--if the EntityState is Unchanged or Modified, this is an illegal operation.
var e = new System.Web.Http.HttpResponseException(System.Net.HttpStatusCode.BadRequest);
//e.InnerException = new ArgumentException("The POSTed object already exists!"); // Can't do this, I guess...
throw e;
}
}
return materialList;
}
public override async Task<IList<T>> Put(string id, IList<T> putObjs)
{
var materializer = this.MaterializerFactory<EntityFrameworkMaterializer>();
DbContext context = materializer.DbContext;
List<T> materialList = new List<T>();
foreach (T putObj in putObjs)
{
var material = await materializer.MaterializeUpdateAsync(putObj);
materialList.Add(material);
}
await context.SaveChangesAsync();
return materialList;
}
public override async Task Delete(string id)
{
var materializer = this.MaterializerFactory<EntityFrameworkMaterializer>();
DbContext context = materializer.DbContext;
T target = await materializer.GetByIdAsync<T>(id);
context.Set<T>().Remove(target);
await context.SaveChangesAsync();
await base.Delete(id);
}
protected override void Dispose(bool disposing)
{
//FIXME: Unsure what to do with the "disposing" parameter here...what does it mean??
if (_materializer != null)
{
_materializer.DbContext.Dispose();
}
_materializer = null;
base.Dispose(disposing);
}
}
}