Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add links all controller #68

Merged
merged 9 commits into from
Apr 8, 2024
89 changes: 85 additions & 4 deletions Inflow.Api/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ClosedXML.Excel;
using Inflow.Api.Helper;
using Inflow.Domain.DTOs.Category;
using Inflow.Domain.DTOs.Product;
using Inflow.Domain.Interfaces.Services;
Expand Down Expand Up @@ -27,13 +28,30 @@ public CategoriesController(ICategoryService categoryService, IProductService pr
_productService = productService;
}

[HttpGet]
public ActionResult<IEnumerable<CategoryDto>> GetCategories(
[FromQuery] CategoryResourceParameters categoryResourceParameters)
[HttpGet(Name = "GetCategories")]
public IActionResult GetCategoriesAsync(
[FromQuery] CategoryResourceParameters categoryResourceParameters)
{
var categories = _categoryService.GetCategories(categoryResourceParameters);
var links = GetLinks(categoryResourceParameters, categories.HasNextPage, categories.HasPreviousPage);
var metadata = new
{
categories.PageNumber,
categories.PageSize,
categories.HasNextPage,
categories.HasPreviousPage,
categories.TotalPages,
categories.TotalCount
};

var result = new
{
data = categories.Data,
links,
metadata
};

return Ok(categories);
return Ok(result);
}

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

private List<ResourceLink> GetLinks(
CategoryResourceParameters resourceParameters,
bool hasNext,
bool hasPrevious)
{
List<ResourceLink> links = new();

links.Add(new ResourceLink(
"self",
CreateCategoryResourceLink(resourceParameters, ResourceType.CurrentPage),
"GET"));

if (hasNext)
{
links.Add(new ResourceLink(
"next",
CreateCategoryResourceLink(resourceParameters, ResourceType.NextPage),
"GET"));
}

if (hasPrevious)
{
links.Add(new ResourceLink(
"previous",
CreateCategoryResourceLink(resourceParameters, ResourceType.PreviousPage),
"GET"));
}

foreach (var link in links)
{
var lastIndex = link.Href.IndexOf("/api");
if (lastIndex >= 0)
{
link.Href = "https://0wn6qg77-7258.asse.devtunnels.ms" + link.Href.Substring(lastIndex);
}
}

return links;
}

private string? CreateCategoryResourceLink(CategoryResourceParameters resourceParameters, ResourceType type)
{
if (type == ResourceType.PreviousPage)
{
var parameters = resourceParameters with
{
PageNumber = resourceParameters.PageNumber - 1,
};
return Url.Link("GetCategories", parameters);
}

if (type == ResourceType.NextPage)
{
var parameters = resourceParameters with
{
PageNumber = resourceParameters.PageNumber + 1,
};
return Url.Link("GetCategories", parameters);
}

return Url.Link("GetCategories", resourceParameters);
}
}
}

89 changes: 85 additions & 4 deletions Inflow.Api/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ClosedXML.Excel;
using Inflow.Api.Helper;
using Inflow.Domain.DTOs.Customer;
using Inflow.Domain.Interfaces.Services;
using Inflow.Domain.ResourceParameters;
Expand All @@ -22,13 +23,31 @@ public CustomersController(ICustomerService customerService)
_customerService = customerService;
}

[HttpGet]
public ActionResult<IEnumerable<CustomerDto>> GetCustomersAsync(
[FromQuery] CustomerResourceParameters customerResourceParameters)

[HttpGet(Name = "GetCustomers")]
public IActionResult GetCategoriesAsync(
[FromQuery] CustomerResourceParameters customerResourceParameters)
{
var customers = _customerService.GetCustomers(customerResourceParameters);
var links = GetLinks(customerResourceParameters, customers.HasNextPage, customers.HasPreviousPage);
var metadata = new
{
customers.PageNumber,
customers.PageSize,
customers.HasNextPage,
customers.HasPreviousPage,
customers.TotalPages,
customers.TotalCount
};

var result = new
{
data = customers.Data,
links,
metadata
};

return Ok(customers);
return Ok(result);
}

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

return table;
}
private List<ResourceLink> GetLinks(
CustomerResourceParameters resourceParameters,
bool hasNext,
bool hasPrevious)
{
List<ResourceLink> links = new();

links.Add(new ResourceLink(
"self",
CreateCustomerResourceLink(resourceParameters, ResourceType.CurrentPage),
"GET"));

if (hasNext)
{
links.Add(new ResourceLink(
"next",
CreateCustomerResourceLink(resourceParameters, ResourceType.NextPage),
"GET"));
}

if (hasPrevious)
{
links.Add(new ResourceLink(
"previous",
CreateCustomerResourceLink(resourceParameters, ResourceType.PreviousPage),
"GET"));
}

foreach (var link in links)
{
var lastIndex = link.Href.IndexOf("/api");
if (lastIndex >= 0)
{
link.Href = "https://0wn6qg77-7258.asse.devtunnels.ms" + link.Href.Substring(lastIndex);
}
}

return links;
}

private string? CreateCustomerResourceLink(CustomerResourceParameters resourceParameters, ResourceType type)
{
if (type == ResourceType.PreviousPage)
{
var parameters = resourceParameters with
{
PageNumber = resourceParameters.PageNumber - 1,
};
return Url.Link("GetCustomers", parameters);
}

if (type == ResourceType.NextPage)
{
var parameters = resourceParameters with
{
PageNumber = resourceParameters.PageNumber + 1,
};
return Url.Link("GetCustomers", parameters);
}

return Url.Link("GetCustomers", resourceParameters);
}
}
}
90 changes: 85 additions & 5 deletions Inflow.Api/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using ClosedXML.Excel;
using Inflow.Api.Helper;
using Inflow.Domain.DTOs.Product;
using Inflow.Domain.Entities;
using Inflow.Domain.Interfaces.Services;
Expand Down Expand Up @@ -27,15 +28,31 @@ public ProductsController(IProductService productService, IMapper mapper)
_mapper = mapper;
}

[HttpGet]
public ActionResult<IEnumerable<ProductDto>> GetProductsAsync(
[FromQuery] ProductResourceParameters productResourceParameters)
[HttpGet(Name = "GetProducts")]
public IActionResult GetProductsAsync(
[FromQuery] ProductResourceParameters productResourceParameters)
{
var products = _productService.GetProducts(productResourceParameters);
var links = GetLinks(productResourceParameters, products.HasNextPage, products.HasPreviousPage);
var metadata = new
{
products.PageNumber,
products.PageSize,
products.HasNextPage,
products.HasPreviousPage,
products.TotalPages,
products.TotalCount
};

return Ok(products);
}
var result = new
{
data = products.Data,
links,
metadata
};

return Ok(result);
}
[HttpGet("export/xls")]
public ActionResult ExportProducts()
{
Expand Down Expand Up @@ -219,5 +236,68 @@ private List<object> ConvertCategoriesToData(IEnumerable<ProductDto> products)

return data;
}

private List<ResourceLink> GetLinks(
ProductResourceParameters resourceParameters,
bool hasNext,
bool hasPrevious)
{
List<ResourceLink> links = new();

links.Add(new ResourceLink(
"self",
CreateProductResourceLink(resourceParameters, ResourceType.CurrentPage),
"GET"));

if (hasNext)
{
links.Add(new ResourceLink(
"next",
CreateProductResourceLink(resourceParameters, ResourceType.NextPage),
"GET"));
}

if (hasPrevious)
{
links.Add(new ResourceLink(
"previous",
CreateProductResourceLink(resourceParameters, ResourceType.PreviousPage),
"GET"));
}

foreach (var link in links)
{
var lastIndex = link.Href.IndexOf("/api");
if (lastIndex >= 0)
{
link.Href = "https://0wn6qg77-7258.asse.devtunnels.ms" + link.Href.Substring(lastIndex);
}
}

return links;
}

private string? CreateProductResourceLink(ProductResourceParameters resourceParameters, ResourceType type)
{
if (type == ResourceType.PreviousPage)
{
var parameters = resourceParameters with
{
PageNumber = resourceParameters.PageNumber - 1,
};
return Url.Link("GetProducts", parameters);
}

if (type == ResourceType.NextPage)
{
var parameters = resourceParameters with
{
PageNumber = resourceParameters.PageNumber + 1,
};
return Url.Link("GetProducts", parameters);
}

return Url.Link("GetProducts", resourceParameters);
}
}
}
Loading
Loading