-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathLinkService.cs
151 lines (121 loc) · 5.27 KB
/
LinkService.cs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using SampleWebApiAspNetCore.Domain.Models;
using SampleWebApiAspNetCore.Service.Helpers;
using SampleWebApiAspNetCore.Service.Services.Interfaces;
using System.Reflection;
namespace SampleWebApiAspNetCore.Service.Services.Implementations
{
public class LinkService<T> : ILinkService<T>
{
private readonly IUrlHelper _urlHelper;
public LinkService(IUrlHelperFactory urlHelperFactory, IActionContextAccessor actionContextAccessor)
{
_urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
}
public List<LinkDto> CreateLinksForCollection(QueryParameters queryParameters, int totalCount, ApiVersion version)
{
Type controllerType = (typeof(T));
MethodInfo[] methods = controllerType.GetMethods();
var links = new List<LinkDto>();
var getAllMethodName = GetMethod(methods, typeof(HttpGetAttribute), 0);
// self
links.Add(new LinkDto(_urlHelper.Link(getAllMethodName, new
{
pagecount = queryParameters.PageCount,
page = queryParameters.Page,
orderby = queryParameters.OrderBy
}), "self", "GET"));
links.Add(new LinkDto(_urlHelper.Link(getAllMethodName, new
{
pagecount = queryParameters.PageCount,
page = 1,
orderby = queryParameters.OrderBy
}), "first", "GET"));
links.Add(new LinkDto(_urlHelper.Link(getAllMethodName, new
{
pagecount = queryParameters.PageCount,
page = queryParameters.GetTotalPages(totalCount),
orderby = queryParameters.OrderBy
}), "last", "GET"));
if (queryParameters.HasNext(totalCount))
{
links.Add(new LinkDto(_urlHelper.Link(getAllMethodName, new
{
pagecount = queryParameters.PageCount,
page = queryParameters.Page + 1,
orderby = queryParameters.OrderBy
}), "next", "GET"));
}
if (queryParameters.HasPrevious())
{
links.Add(new LinkDto(_urlHelper.Link(getAllMethodName, new
{
pagecount = queryParameters.PageCount,
page = queryParameters.Page - 1,
orderby = queryParameters.OrderBy
}), "previous", "GET"));
}
var posturl = _urlHelper.Link(GetMethod(methods, typeof(HttpPostAttribute)), new { version = version.ToString() });
links.Add(
new LinkDto(posturl,
"create",
"POST"));
return links;
}
public object ExpandSingleFoodItem(object resource, int identifier, ApiVersion version)
{
var resourceToReturn = resource.ToDynamic() as IDictionary<string, object>;
var links = GetLinksForSingleItem(identifier, version);
resourceToReturn.Add("links", links);
return resourceToReturn;
}
private IEnumerable<LinkDto> GetLinksForSingleItem(int id, ApiVersion version)
{
Type myType = (typeof(T));
MethodInfo[] methods = myType.GetMethods();
var links = new List<LinkDto>();
var getLink = _urlHelper.Link(GetMethod(methods, typeof(HttpGetAttribute), 1), new { version = version.ToString(), id = id });
links.Add(new LinkDto(getLink, "self", "GET"));
var deleteLink = _urlHelper.Link(GetMethod(methods, typeof(HttpDeleteAttribute)), new { version = version.ToString(), id = id });
links.Add(
new LinkDto(deleteLink,
"delete",
"DELETE"));
var createLink = _urlHelper.Link(GetMethod(methods, typeof(HttpPostAttribute)), new { version = version.ToString() });
links.Add(
new LinkDto(createLink,
"create_food",
"POST"));
var updateLink = _urlHelper.Link(GetMethod(methods, typeof(HttpPutAttribute)), new { version = version.ToString(), id = id });
links.Add(
new LinkDto(updateLink,
"update_food",
"PUT"));
return links;
}
private string GetMethod(MethodInfo[] methods, Type type, int routeParamsLength = 0)
{
var filteredMethods = methods.Where(m => m.GetCustomAttributes(type, false).Length > 0).ToArray();
if (filteredMethods.Length == 0)
{
return "";
}
if (routeParamsLength == 0)
{
var toReturn = filteredMethods.FirstOrDefault();
return toReturn is not null ? toReturn.Name : "";
}
foreach (var method in filteredMethods)
{
var routeAttribs = method.GetCustomAttributes(typeof(RouteAttribute));
if (routeAttribs.Count() == routeParamsLength)
{
return method.Name;
}
}
return "";
}
}
}