Skip to content

Commit dbe8647

Browse files
Merge pull request #68 from DiyorMarket/AddLinksAllController
Add links all controller
2 parents 1ad6478 + f5a1994 commit dbe8647

17 files changed

+692
-45
lines changed

Inflow.Api/Controllers/CategoriesController.cs

+85-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ClosedXML.Excel;
2+
using Inflow.Api.Helper;
23
using Inflow.Domain.DTOs.Category;
34
using Inflow.Domain.DTOs.Product;
45
using Inflow.Domain.Interfaces.Services;
@@ -27,13 +28,30 @@ public CategoriesController(ICategoryService categoryService, IProductService pr
2728
_productService = productService;
2829
}
2930

30-
[HttpGet]
31-
public ActionResult<IEnumerable<CategoryDto>> GetCategories(
32-
[FromQuery] CategoryResourceParameters categoryResourceParameters)
31+
[HttpGet(Name = "GetCategories")]
32+
public IActionResult GetCategoriesAsync(
33+
[FromQuery] CategoryResourceParameters categoryResourceParameters)
3334
{
3435
var categories = _categoryService.GetCategories(categoryResourceParameters);
36+
var links = GetLinks(categoryResourceParameters, categories.HasNextPage, categories.HasPreviousPage);
37+
var metadata = new
38+
{
39+
categories.PageNumber,
40+
categories.PageSize,
41+
categories.HasNextPage,
42+
categories.HasPreviousPage,
43+
categories.TotalPages,
44+
categories.TotalCount
45+
};
46+
47+
var result = new
48+
{
49+
data = categories.Data,
50+
links,
51+
metadata
52+
};
3553

36-
return Ok(categories);
54+
return Ok(result);
3755
}
3856

3957
[HttpGet("{id}", Name = "GetCategoryById")]
@@ -169,5 +187,68 @@ private static DataTable GetCategoriesDataTable(IEnumerable<CategoryDto> categor
169187
return table;
170188
}
171189

190+
private List<ResourceLink> GetLinks(
191+
CategoryResourceParameters resourceParameters,
192+
bool hasNext,
193+
bool hasPrevious)
194+
{
195+
List<ResourceLink> links = new();
196+
197+
links.Add(new ResourceLink(
198+
"self",
199+
CreateCategoryResourceLink(resourceParameters, ResourceType.CurrentPage),
200+
"GET"));
201+
202+
if (hasNext)
203+
{
204+
links.Add(new ResourceLink(
205+
"next",
206+
CreateCategoryResourceLink(resourceParameters, ResourceType.NextPage),
207+
"GET"));
208+
}
209+
210+
if (hasPrevious)
211+
{
212+
links.Add(new ResourceLink(
213+
"previous",
214+
CreateCategoryResourceLink(resourceParameters, ResourceType.PreviousPage),
215+
"GET"));
216+
}
217+
218+
foreach (var link in links)
219+
{
220+
var lastIndex = link.Href.IndexOf("/api");
221+
if (lastIndex >= 0)
222+
{
223+
link.Href = "https://0wn6qg77-7258.asse.devtunnels.ms" + link.Href.Substring(lastIndex);
224+
}
225+
}
226+
227+
return links;
228+
}
229+
230+
private string? CreateCategoryResourceLink(CategoryResourceParameters resourceParameters, ResourceType type)
231+
{
232+
if (type == ResourceType.PreviousPage)
233+
{
234+
var parameters = resourceParameters with
235+
{
236+
PageNumber = resourceParameters.PageNumber - 1,
237+
};
238+
return Url.Link("GetCategories", parameters);
239+
}
240+
241+
if (type == ResourceType.NextPage)
242+
{
243+
var parameters = resourceParameters with
244+
{
245+
PageNumber = resourceParameters.PageNumber + 1,
246+
};
247+
return Url.Link("GetCategories", parameters);
248+
}
249+
250+
return Url.Link("GetCategories", resourceParameters);
251+
}
172252
}
173253
}
254+

Inflow.Api/Controllers/CustomersController.cs

+85-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ClosedXML.Excel;
2+
using Inflow.Api.Helper;
23
using Inflow.Domain.DTOs.Customer;
34
using Inflow.Domain.Interfaces.Services;
45
using Inflow.Domain.ResourceParameters;
@@ -22,13 +23,31 @@ public CustomersController(ICustomerService customerService)
2223
_customerService = customerService;
2324
}
2425

25-
[HttpGet]
26-
public ActionResult<IEnumerable<CustomerDto>> GetCustomersAsync(
27-
[FromQuery] CustomerResourceParameters customerResourceParameters)
26+
27+
[HttpGet(Name = "GetCustomers")]
28+
public IActionResult GetCategoriesAsync(
29+
[FromQuery] CustomerResourceParameters customerResourceParameters)
2830
{
2931
var customers = _customerService.GetCustomers(customerResourceParameters);
32+
var links = GetLinks(customerResourceParameters, customers.HasNextPage, customers.HasPreviousPage);
33+
var metadata = new
34+
{
35+
customers.PageNumber,
36+
customers.PageSize,
37+
customers.HasNextPage,
38+
customers.HasPreviousPage,
39+
customers.TotalPages,
40+
customers.TotalCount
41+
};
42+
43+
var result = new
44+
{
45+
data = customers.Data,
46+
links,
47+
metadata
48+
};
3049

31-
return Ok(customers);
50+
return Ok(result);
3251
}
3352

3453
[HttpGet("{id}", Name = "GetCustomerById")]
@@ -159,5 +178,67 @@ private static DataTable GetCustomersDataTable(IEnumerable<CustomerDto> customer
159178

160179
return table;
161180
}
181+
private List<ResourceLink> GetLinks(
182+
CustomerResourceParameters resourceParameters,
183+
bool hasNext,
184+
bool hasPrevious)
185+
{
186+
List<ResourceLink> links = new();
187+
188+
links.Add(new ResourceLink(
189+
"self",
190+
CreateCustomerResourceLink(resourceParameters, ResourceType.CurrentPage),
191+
"GET"));
192+
193+
if (hasNext)
194+
{
195+
links.Add(new ResourceLink(
196+
"next",
197+
CreateCustomerResourceLink(resourceParameters, ResourceType.NextPage),
198+
"GET"));
199+
}
200+
201+
if (hasPrevious)
202+
{
203+
links.Add(new ResourceLink(
204+
"previous",
205+
CreateCustomerResourceLink(resourceParameters, ResourceType.PreviousPage),
206+
"GET"));
207+
}
208+
209+
foreach (var link in links)
210+
{
211+
var lastIndex = link.Href.IndexOf("/api");
212+
if (lastIndex >= 0)
213+
{
214+
link.Href = "https://0wn6qg77-7258.asse.devtunnels.ms" + link.Href.Substring(lastIndex);
215+
}
216+
}
217+
218+
return links;
219+
}
220+
221+
private string? CreateCustomerResourceLink(CustomerResourceParameters resourceParameters, ResourceType type)
222+
{
223+
if (type == ResourceType.PreviousPage)
224+
{
225+
var parameters = resourceParameters with
226+
{
227+
PageNumber = resourceParameters.PageNumber - 1,
228+
};
229+
return Url.Link("GetCustomers", parameters);
230+
}
231+
232+
if (type == ResourceType.NextPage)
233+
{
234+
var parameters = resourceParameters with
235+
{
236+
PageNumber = resourceParameters.PageNumber + 1,
237+
};
238+
return Url.Link("GetCustomers", parameters);
239+
}
240+
241+
return Url.Link("GetCustomers", resourceParameters);
242+
}
162243
}
163244
}

Inflow.Api/Controllers/ProductsController.cs

+85-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AutoMapper;
22
using ClosedXML.Excel;
3+
using Inflow.Api.Helper;
34
using Inflow.Domain.DTOs.Product;
45
using Inflow.Domain.Entities;
56
using Inflow.Domain.Interfaces.Services;
@@ -27,15 +28,31 @@ public ProductsController(IProductService productService, IMapper mapper)
2728
_mapper = mapper;
2829
}
2930

30-
[HttpGet]
31-
public ActionResult<IEnumerable<ProductDto>> GetProductsAsync(
32-
[FromQuery] ProductResourceParameters productResourceParameters)
31+
[HttpGet(Name = "GetProducts")]
32+
public IActionResult GetProductsAsync(
33+
[FromQuery] ProductResourceParameters productResourceParameters)
3334
{
3435
var products = _productService.GetProducts(productResourceParameters);
36+
var links = GetLinks(productResourceParameters, products.HasNextPage, products.HasPreviousPage);
37+
var metadata = new
38+
{
39+
products.PageNumber,
40+
products.PageSize,
41+
products.HasNextPage,
42+
products.HasPreviousPage,
43+
products.TotalPages,
44+
products.TotalCount
45+
};
3546

36-
return Ok(products);
37-
}
47+
var result = new
48+
{
49+
data = products.Data,
50+
links,
51+
metadata
52+
};
3853

54+
return Ok(result);
55+
}
3956
[HttpGet("export/xls")]
4057
public ActionResult ExportProducts()
4158
{
@@ -219,5 +236,68 @@ private List<object> ConvertCategoriesToData(IEnumerable<ProductDto> products)
219236

220237
return data;
221238
}
239+
240+
private List<ResourceLink> GetLinks(
241+
ProductResourceParameters resourceParameters,
242+
bool hasNext,
243+
bool hasPrevious)
244+
{
245+
List<ResourceLink> links = new();
246+
247+
links.Add(new ResourceLink(
248+
"self",
249+
CreateProductResourceLink(resourceParameters, ResourceType.CurrentPage),
250+
"GET"));
251+
252+
if (hasNext)
253+
{
254+
links.Add(new ResourceLink(
255+
"next",
256+
CreateProductResourceLink(resourceParameters, ResourceType.NextPage),
257+
"GET"));
258+
}
259+
260+
if (hasPrevious)
261+
{
262+
links.Add(new ResourceLink(
263+
"previous",
264+
CreateProductResourceLink(resourceParameters, ResourceType.PreviousPage),
265+
"GET"));
266+
}
267+
268+
foreach (var link in links)
269+
{
270+
var lastIndex = link.Href.IndexOf("/api");
271+
if (lastIndex >= 0)
272+
{
273+
link.Href = "https://0wn6qg77-7258.asse.devtunnels.ms" + link.Href.Substring(lastIndex);
274+
}
275+
}
276+
277+
return links;
278+
}
279+
280+
private string? CreateProductResourceLink(ProductResourceParameters resourceParameters, ResourceType type)
281+
{
282+
if (type == ResourceType.PreviousPage)
283+
{
284+
var parameters = resourceParameters with
285+
{
286+
PageNumber = resourceParameters.PageNumber - 1,
287+
};
288+
return Url.Link("GetProducts", parameters);
289+
}
290+
291+
if (type == ResourceType.NextPage)
292+
{
293+
var parameters = resourceParameters with
294+
{
295+
PageNumber = resourceParameters.PageNumber + 1,
296+
};
297+
return Url.Link("GetProducts", parameters);
298+
}
299+
300+
return Url.Link("GetProducts", resourceParameters);
301+
}
222302
}
223303
}

0 commit comments

Comments
 (0)