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
126 lines (115 loc) · 4.3 KB
/
ApiController.cs
File metadata and controls
126 lines (115 loc) · 4.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Reflection;
using JSONAPI.Core;
namespace JSONAPI.Http
{
//TODO: Authorization checking framework, maybe?
public class ApiController<T> : System.Web.Http.ApiController
where T : class
{
protected virtual IMaterializer MaterializerFactory()
{
return null;
}
protected virtual TM MaterializerFactory<TM>()
where TM : IMaterializer
{
return (TM)this.MaterializerFactory();
}
/// <summary>
/// Override this method to provide an IQueryable set of objects of type T. If this
/// method is not overridden, an empty List<T> will be returned.
/// </summary>
/// <param name="materializer"></param>
/// <returns></returns>
protected virtual IQueryable<T> QueryableFactory(IMaterializer materializer = null)
{
return (new List<T>()).AsQueryable<T>();
}
//[System.Web.OData.EnableQuery] // Do this yourself!
/// <summary>
/// Default Get method implementation. Returns the result of
/// Note: You can easily add OData query support by overriding this method and decorating
/// it with the [System.Web.OData.EnableQuery] attribute.
/// </summary>
/// <returns></returns>
public virtual IQueryable<T> Get()
{
IMaterializer materializer = MaterializerFactory();
IQueryable<T> es = QueryableFactory(materializer);
return es;
}
public virtual async Task<IEnumerable<T>> Get(string id)
{
IMaterializer materializer = MaterializerFactory();
List<T> results = new List<T>();
string[] arrIds;
if (id.Contains(","))
{
arrIds = id.Split(',');
}
else
{
arrIds = new string[] { id };
}
foreach (string singleid in arrIds)
{
T hit = await materializer.GetByIdAsync<T>(singleid);
if (hit != null)
{
results.Add(hit);
}
}
return results;
}
/// <summary>
/// In this base class, the Post operation is essentially a no-op. It returns a materialized
/// copy of the object (which is meaningless unless the materializer implements
/// some logic that does something to it), but fulfills the JSONAPI requirement
/// that the POST operation return the POSTed object. It should probably be
/// overridden in any implementation.
/// </summary>
/// <param name="postedObj"></param>
/// <returns></returns>
public virtual Task<IList<T>> Post([FromBody] IList<T> postedObjs)
{
foreach(T postedObj in postedObjs)
{
IMaterializer materializer = this.MaterializerFactory();
}
return Task.FromResult(postedObjs);
}
/// <summary>
/// Similar to Post, this method doesn't do much. It calls MaterializeUpdateAsync() on the
/// input and returns it. It should probably always be overridden.
/// </summary>
/// <param name="id"></param>
/// <param name="payload"></param>
/// <returns></returns>
public virtual async Task<IList<T>> Patch(string id, IList<T> putObjs)
{
IMaterializer materializer = this.MaterializerFactory();
IList<T> materialList = new List<T>();
foreach (T putObj in putObjs)
{
materialList.Add(await materializer.MaterializeUpdateAsync<T>(putObj));
}
return materialList;
}
/// <summary>
/// A no-op method. This should be overriden in subclasses if Delete is to be supported.
/// </summary>
/// <param name="id"></param>
public virtual Task Delete(string id)
{
return Task.FromResult(0);
}
}
}