mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
feat: basic organization endpoint
This commit is contained in:
11
WebApp/DTOs/OrganisationSummaryDto.cs
Normal file
11
WebApp/DTOs/OrganisationSummaryDto.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using WebApp.Entities;
|
||||||
|
|
||||||
|
namespace WebApp.DTOs;
|
||||||
|
public record class OrganisationSummaryDto
|
||||||
|
(
|
||||||
|
[Required] int? OrganisationId,
|
||||||
|
[Required][StringLength(50)] string Name,
|
||||||
|
[StringLength(500)] string Description,
|
||||||
|
[Required][StringLength(200)] string Website
|
||||||
|
);
|
||||||
@@ -41,10 +41,10 @@ namespace WebApp.Endpoints
|
|||||||
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
|
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
|
||||||
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
|
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
|
||||||
// przed jego wysłaniem!
|
// przed jego wysłaniem!
|
||||||
Eve.ToEventDetailsDto();
|
|
||||||
if (org is null || org.OrganisationId != Eve.OrganisationId) Eve.EventRegistrations = [];
|
if (org is null || org.OrganisationId != Eve.OrganisationId) Eve.EventRegistrations = [];
|
||||||
|
EventDetailsDto EveDto = Eve.ToEventDetailsDto();
|
||||||
|
|
||||||
return Results.Ok(Eve); //EventDetailsDto
|
return Results.Ok(EveDto); //EventDetailsDto
|
||||||
})
|
})
|
||||||
.WithName(GetEventEndpointName);
|
.WithName(GetEventEndpointName);
|
||||||
|
|
||||||
|
|||||||
42
WebApp/Endpoints/OrganizationsEndpoints.cs
Normal file
42
WebApp/Endpoints/OrganizationsEndpoints.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WebApp.Data;
|
||||||
|
using WebApp.Entities;
|
||||||
|
using WebApp.Mapping;
|
||||||
|
|
||||||
|
|
||||||
|
namespace WebApp.Endpoints;
|
||||||
|
|
||||||
|
public static class OrganizationsEndpoints
|
||||||
|
{
|
||||||
|
const string GetOrganizationEndpointName = "GetOrganization";
|
||||||
|
|
||||||
|
public static RouteGroupBuilder MapOrganizationsEndpoints(this WebApplication app)
|
||||||
|
{
|
||||||
|
var group = app.MapGroup("api/organizations")
|
||||||
|
.WithParameterValidation();
|
||||||
|
|
||||||
|
// GET /organizations
|
||||||
|
group.MapGet("/",
|
||||||
|
async (ApplicationDbContext dbContext, HttpContext httpContext) =>
|
||||||
|
await dbContext.Organisations
|
||||||
|
//.Include(Eve => Eve.Organisation)
|
||||||
|
.OrderByDescending(Org => Org.OrganisationId)
|
||||||
|
.Select(Org => Org.ToOrgSummaryDto()) //OrgSummaryDto
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToListAsync());
|
||||||
|
|
||||||
|
// GET /organizations/1
|
||||||
|
group.MapGet("/{id}",
|
||||||
|
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
||||||
|
{
|
||||||
|
Organisation? Org = await dbContext.Organisations.FindAsync(id);
|
||||||
|
|
||||||
|
if (Org is null) return Results.NotFound();
|
||||||
|
|
||||||
|
return Results.Ok(Org.ToOrgSummaryDto()); //OrgSummaryDto
|
||||||
|
})
|
||||||
|
.WithName(GetOrganizationEndpointName);
|
||||||
|
|
||||||
|
return group;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
WebApp/Mapping/OrganizationMapping.cs
Normal file
27
WebApp/Mapping/OrganizationMapping.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WebApp.DTOs;
|
||||||
|
using WebApp.Entities;
|
||||||
|
|
||||||
|
namespace WebApp.Mapping;
|
||||||
|
|
||||||
|
public static class OrganizationMapping
|
||||||
|
{
|
||||||
|
// obecnie zbędne
|
||||||
|
//public static Organisation ToEntity(this OrganisationSummaryDto ODto)
|
||||||
|
//{
|
||||||
|
// return new Organisation()
|
||||||
|
// {
|
||||||
|
// OrganisationId = ODto.OrganisationId!.Value,
|
||||||
|
// };
|
||||||
|
//}
|
||||||
|
|
||||||
|
public static OrganisationSummaryDto ToOrgSummaryDto(this Organisation org)
|
||||||
|
{
|
||||||
|
return new OrganisationSummaryDto(
|
||||||
|
org.OrganisationId,
|
||||||
|
org.Name,
|
||||||
|
org.Description,
|
||||||
|
org.Website
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -51,6 +51,6 @@ app.UseRouting(); // Enables routing to match incoming request to endpoints
|
|||||||
|
|
||||||
// Map Minimal API Endpoints
|
// Map Minimal API Endpoints
|
||||||
app.MapEventsEndpoints();
|
app.MapEventsEndpoints();
|
||||||
|
app.MapOrganizationsEndpoints();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
Reference in New Issue
Block a user