WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
namespace GraphQLServer.SetupGraphQL;
|
||||
|
||||
public record EventInput(
|
||||
string Name,
|
||||
DateOnly EventDate);
|
||||
@@ -0,0 +1,50 @@
|
||||
using Events.EF.Data.MSSQL;
|
||||
using Events.EF.Models;
|
||||
|
||||
namespace GraphQLServer.SetupGraphQL;
|
||||
|
||||
public partial class Mutations
|
||||
{
|
||||
public async Task<Event> AddEvent([Service] EventsContext ctx, EventInput input)
|
||||
{
|
||||
var item = new Event
|
||||
{
|
||||
Name = input.Name,
|
||||
EventDate = input.EventDate
|
||||
};
|
||||
|
||||
ctx.Events.Add(item);
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<Event?> UpdateEvent([Service] EventsContext ctx, int id, EventInput input)
|
||||
{
|
||||
var item = await ctx.Events.FindAsync(id);
|
||||
if (item is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
item.Name = input.Name;
|
||||
item.EventDate = input.EventDate;
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteEvent([Service] EventsContext ctx, int id)
|
||||
{
|
||||
var item = await ctx.Events.FindAsync(id);
|
||||
if (item is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx.Events.Remove(item);
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace GraphQLServer.SetupGraphQL;
|
||||
|
||||
public partial class Mutations
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Events.EF.Models;
|
||||
|
||||
namespace GraphQLServer.SetupGraphQL;
|
||||
|
||||
public class PeoplePage
|
||||
{
|
||||
public required int TotalCount { get; init; }
|
||||
|
||||
public required int PageNumber { get; init; }
|
||||
|
||||
public required int PageSize { get; init; }
|
||||
|
||||
public required IReadOnlyList<Person> Items { get; init; }
|
||||
}
|
||||
74
Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Queries.cs
Normal file
74
Events.GraphQL/Events.GraphQLServer/SetupGraphQL/Queries.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Events.EF.Data.MSSQL;
|
||||
using Events.EF.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GraphQLServer.SetupGraphQL;
|
||||
|
||||
public class Queries
|
||||
{
|
||||
[UsePaging]
|
||||
[UseProjection]
|
||||
[UseFiltering]
|
||||
[UseSorting]
|
||||
public IQueryable<Country> GetCountries([Service] EventsContext ctx) => ctx.Countries.AsNoTracking();
|
||||
|
||||
[UsePaging]
|
||||
[UseProjection]
|
||||
[UseFiltering]
|
||||
[UseSorting]
|
||||
public IQueryable<Sport> GetSports([Service] EventsContext ctx) => ctx.Sports.AsNoTracking();
|
||||
|
||||
[UsePaging]
|
||||
[UseProjection]
|
||||
[UseFiltering]
|
||||
[UseSorting]
|
||||
public IQueryable<Person> GetPeople([Service] EventsContext ctx) => ctx.People.AsNoTracking();
|
||||
|
||||
public async Task<PeoplePage> GetPeoplePage(
|
||||
[Service] EventsContext ctx,
|
||||
int pageNumber = 1,
|
||||
int pageSize = 10)
|
||||
{
|
||||
pageNumber = Math.Max(1, pageNumber);
|
||||
pageSize = Math.Clamp(pageSize, 1, 100);
|
||||
|
||||
var baseQuery = ctx.People
|
||||
.AsNoTracking()
|
||||
.OrderBy(p => p.Id);
|
||||
|
||||
var totalCount = await baseQuery.CountAsync();
|
||||
|
||||
var items = await baseQuery
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
|
||||
return new PeoplePage
|
||||
{
|
||||
TotalCount = totalCount,
|
||||
PageNumber = pageNumber,
|
||||
PageSize = pageSize,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
|
||||
[UsePaging]
|
||||
[UseProjection]
|
||||
[UseFiltering]
|
||||
[UseSorting]
|
||||
public IQueryable<Event> GetEvents([Service] EventsContext ctx) => ctx.Events.AsNoTracking();
|
||||
|
||||
[UsePaging]
|
||||
[UseProjection]
|
||||
[UseFiltering]
|
||||
[UseSorting]
|
||||
public IQueryable<Event> GetEventsForDate([Service] EventsContext ctx, DateOnly date) =>
|
||||
ctx.Events.AsNoTracking()
|
||||
.Where(e => e.EventDate == date);
|
||||
|
||||
[UsePaging]
|
||||
[UseProjection]
|
||||
[UseFiltering]
|
||||
[UseSorting]
|
||||
public IQueryable<Registration> GetRegistrations([Service] EventsContext ctx) => ctx.Registrations.AsNoTracking();
|
||||
}
|
||||
Reference in New Issue
Block a user