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

changes to improve pagedlist and adding new endpoints for naturalareas #50

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
changes to improve pagedlist and ading new endpoints for naturalareas
  • Loading branch information
Mteheran committed Mar 19, 2023
commit 83048db054f71393bb8ce517d97da13a111df8d5
5 changes: 4 additions & 1 deletion api/DBContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class DBContext : DbContext
public DbSet<CategoryNaturalArea> CategoryNaturalAreas { get; set; }
public DbSet<NaturalArea> NaturalAreas { get; set; }

public DBContext(DbContextOptions<DBContext> options) : base(options) { }
public DBContext(DbContextOptions<DBContext> options) : base(options)
{
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}

protected override void OnModelCreating(ModelBuilder builder)
{
Expand Down
5 changes: 3 additions & 2 deletions api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "1.0.1",
Title = "API Colombia ",
Title = "API Colombia",
Description = "Open and free API that contains general information about Colombia",
TermsOfService = new Uri("https://github.com/Mteheran/api-colombia"),
Contact = new OpenApiContact
Expand All @@ -39,7 +39,8 @@

builder.Configuration.AddEnvironmentVariables();

builder.Services.AddNpgsql<DBContext>(builder.Configuration.GetConnectionString("DefaultConnection"));
builder.Services.AddNpgsql<DBContext>(
builder.Configuration.GetConnectionString("DefaultConnection"));

builder.Services.Configure<JsonOptions>(options =>
{
Expand Down
23 changes: 23 additions & 0 deletions api/Routes/CategoryNaturalAreaRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ public static void RegisterCategoryNaturalAreaAPI(WebApplication app)
.WithMetadata(new SwaggerOperationAttribute(
summary: CategoryNaturalAreaEndpoint.MESSAGE_BYID_SUMMARY,
description: CategoryNaturalAreaEndpoint.MESSAGE_BYID_DESCRIPTION));

app.MapGet($"{API_CATEGORY_ROUTE_COMPLETE}/{{id}}/NaturalAreas", async (int id, DBContext db) =>
{
if (id <= 0)
{
return Results.BadRequest();
}

var region = await db.CategoryNaturalAreas
.Include(p=> p.NaturalAreas)
.ThenInclude(p=> p.Department)
.SingleOrDefaultAsync(p => p.Id == id);

if (region is null)
{
return Results.NotFound();
}

return Results.Ok(region);
})
.WithMetadata(new SwaggerOperationAttribute(
summary: CategoryNaturalAreaEndpoint.MESSAGE_BYID_SUMMARY,
description: CategoryNaturalAreaEndpoint.MESSAGE_BYID_DESCRIPTION));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions api/Routes/CityRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void RegisterCityAPI(WebApplication app)


app.MapGet($"{API_CITY_ROUTE_COMPLETE}/pagedList",
async (PaginationModel pagination, DBContext db) =>
async ([AsParameters] PaginationModel pagination, DBContext db) =>
{

if (pagination.Page<= 0 || pagination.PageSize <= 0)
Expand All @@ -89,7 +89,7 @@ public static void RegisterCityAPI(WebApplication app)

var cities = db.Cities.Skip((pagination.Page - 1) * pagination.PageSize).Take(pagination.PageSize);

if (await cities?.CountAsync() == 0)
if (!await cities?.AnyAsync())
{
return Results.NotFound();
}
Expand All @@ -107,7 +107,7 @@ public static void RegisterCityAPI(WebApplication app)
})
.WithMetadata(new SwaggerOperationAttribute(
summary: CityEndpointMetadataMessages.MESSAGE_CITY_PAGEDLIST_SUMMARY,
description: CityEndpointMetadataMessages.MESSAGE_CITY_SEARCH_DESCRIPTION
description: CityEndpointMetadataMessages.MESSAGE_CITY_PAGEDLIST_DESCRIPTION
));


Expand Down
2 changes: 1 addition & 1 deletion api/Routes/DepartmentRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static void RegisterDepartmentAPI(WebApplication app)
));

app.MapGet($"{API_DEPARTMENT_ROUTE_COMPLETE}/pagedList",
async (PaginationModel pagination, DBContext db) =>
async ([AsParameters] PaginationModel pagination, DBContext db) =>
{

if (pagination.Page <= 0 || pagination.PageSize <= 0)
Expand Down
75 changes: 74 additions & 1 deletion api/Routes/NaturalAreaRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Swashbuckle.AspNetCore.Annotations;
using Microsoft.EntityFrameworkCore;
using static api.Utils.Messages.EndpointMetadata;
using api.Models;
using Microsoft.AspNetCore.Mvc;

namespace api.Routes
{
Expand All @@ -29,7 +31,8 @@ public static void RegisterNaturalAreaAPI(WebApplication app)
}

var naturalArea = await db.NaturalAreas
.Include(p=> p.CategoryNaturalArea)
.Include(p => p.CategoryNaturalArea)
.Include(p => p.Department)
.SingleOrDefaultAsync(p => p.Id == id);

if (naturalArea is null)
Expand All @@ -42,6 +45,76 @@ public static void RegisterNaturalAreaAPI(WebApplication app)
.WithMetadata(new SwaggerOperationAttribute(
summary: NaturalAreaEndpoint.MESSAGE_BYID_SUMMARY,
description: NaturalAreaEndpoint.MESSAGE_BYID_DESCRIPTION));


app.MapGet($"{API_NATURALAREA_ROUTE_COMPLETE}/name/{{name}}", async (string name, DBContext db) =>
{
var naturalAreas = await db.NaturalAreas
.Include(p=> p.CategoryNaturalArea).IgnoreAutoIncludes()
.Include(p=> p.Department).IgnoreAutoIncludes()
.Where(x => x.Name!.ToUpper().Equals(name.Trim().ToUpper())).ToListAsync();

if (naturalAreas is null)
{
return Results.NotFound();
}

return Results.Ok(naturalAreas);
})
.WithMetadata(new SwaggerOperationAttribute(
summary: NaturalAreaEndpoint.MESSAGE_BYNAME_SUMMARY,
description: NaturalAreaEndpoint.MESSAGE_BYNAME_DESCRIPTION
));

app.MapGet($"{API_NATURALAREA_ROUTE_COMPLETE}/search/{{keyword}}", (string keyword, DBContext db) =>
{
string wellFormedKeyword = keyword.Trim().ToUpper().Normalize();
var naturalAreas = db.NaturalAreas.ToList();

var departments = Functions.FilterObjectListPropertiesByKeyword<NaturalArea>(naturalAreas, wellFormedKeyword);

if (departments.Count == 0)
{
return Results.NotFound();
}

return Results.Ok(departments);
})
.WithMetadata(new SwaggerOperationAttribute(
summary: NaturalAreaEndpoint.MESSAGE_SEARCH_SUMMARY,
description: NaturalAreaEndpoint.MESSAGE_SEARCH_DESCRIPTION
));

app.MapGet($"{API_NATURALAREA_ROUTE_COMPLETE}/pagedList",
async ([AsParameters] PaginationModel pagination, DBContext db) =>
{

if (pagination.Page <= 0 || pagination.PageSize <= 0)
{
return Results.BadRequest();
}

var naturalAreaPaged = db.NaturalAreas.Skip((pagination.Page - 1) * pagination.PageSize).Take(pagination.PageSize);

if (!await naturalAreaPaged?.AnyAsync())
{
return Results.NotFound();
}

var paginationResponse = new PaginationResponseModel<NaturalArea>
{
Page = pagination.Page,
PageSize = pagination.PageSize,
TotalRecords = await db.NaturalAreas.CountAsync(),
Data = await naturalAreaPaged.ToListAsync(),
};

return Results.Ok(paginationResponse);
})
.WithMetadata(new SwaggerOperationAttribute(
summary: NaturalAreaEndpoint.MESSAGE_PAGEDLIST_SUMMARY,
description: NaturalAreaEndpoint.MESSAGE_PAGEDLIST_DESCRIPTION
));
}
}
}
2 changes: 1 addition & 1 deletion api/Routes/PresidentRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void RegisterPresidentApi(WebApplication app)
));

app.MapGet($"{API_PRESIDENT_ROUTE_COMPLETE}/pagedList",
async (PaginationModel pagination, DBContext db) =>
async ([AsParameters] PaginationModel pagination, DBContext db) =>
{

if (pagination.Page <= 0 || pagination.PageSize <= 0)
Expand Down
2 changes: 1 addition & 1 deletion api/Routes/TouristicAttactionRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void RegisterTuristicAttactionAPI(WebApplication app)
description: TouristAttractionEndpointMetadata.MESSAGE_TOURIST_ATTRACTION_SEARCH_DESCRIPTION));

app.MapGet($"{API_TOURISTIC_ROUTE_COMPLETE}/pagedList",
async (PaginationModel pagination, DBContext db) =>
async ([AsParameters] PaginationModel pagination, DBContext db) =>
{

if (pagination.Page <= 0 || pagination.PageSize <= 0)
Expand Down
6 changes: 6 additions & 0 deletions api/Utils/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public struct NaturalAreaEndpoint
public const string MESSAGE_LIST_DESCRIPTION = " This endpoint returns a list of natural areas in Colombia";
public const string MESSAGE_BYID_SUMMARY = "Natural area information by Id";
public const string MESSAGE_BYID_DESCRIPTION = "This endpoint returns the information for the natural area with the provided id";
public const string MESSAGE_BYNAME_SUMMARY = "Natural place by name";
public const string MESSAGE_BYNAME_DESCRIPTION = "This endpoint returns an specific natural places by the provided name";
public const string MESSAGE_SEARCH_SUMMARY = "Search natural places by keyword ";
public const string MESSAGE_SEARCH_DESCRIPTION = "This endpoint returns a list of touristic attractions any of the following fields(Name, Description,LastName,Latitude, Longitude) match the provided keyword ";
public const string MESSAGE_PAGEDLIST_SUMMARY = "List of natural places using pagination - api/v1/naturalarea/pagedList?page=1&pagesize=10";
public const string MESSAGE_PAGEDLIST_DESCRIPTION = "This endpoint returns a list of natural places in Colombia using pagination including page, pagesize, total records and data, example api/v1/naturalarea/pagedList?page=1&pagesize=10";
}

}
Expand Down
8 changes: 4 additions & 4 deletions api/Utils/PaginationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace api.Utils

public class PaginationModel
{
public string? SortBy { get; init; }
public SortDirection SortDirection { get; init; }
//public string? SortBy { get; init; }
//public SortDirection SortDirection { get; init; }
public int Page { get; init; }
public int PageSize { get; set; }

Expand All @@ -27,8 +27,8 @@ public class PaginationModel

var result = new PaginationModel
{
SortBy = context.Request.Query[sortByKey],
SortDirection = sortDirection,
//SortBy = context.Request.Query[sortByKey],
//SortDirection = sortDirection,
Page = page,
PageSize = pageSize

Expand Down
2 changes: 1 addition & 1 deletion api/api.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down