Skip to content

Commit

Permalink
Added session 7
Browse files Browse the repository at this point in the history
  • Loading branch information
glen-84 committed Aug 20, 2024
1 parent 49a173b commit 64f0813
Show file tree
Hide file tree
Showing 96 changed files with 1,408 additions and 2,300 deletions.
5 changes: 3 additions & 2 deletions code/session-7/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "5.0.0",
"version": "8.0.7",
"commands": [
"dotnet-ef"
]
],
"rollForward": false
}
}
}
34 changes: 0 additions & 34 deletions code/session-7/.vscode/launch.json

This file was deleted.

24 changes: 0 additions & 24 deletions code/session-7/.vscode/tasks.json

This file was deleted.

56 changes: 22 additions & 34 deletions code/session-7/ConferencePlanner.sln
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphQL", "GraphQL\GraphQL.csproj", "{48385280-56F1-4937-9655-E6A79184740B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{48385280-56F1-4937-9655-E6A79184740B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Debug|x64.ActiveCfg = Debug|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Debug|x64.Build.0 = Debug|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Debug|x86.ActiveCfg = Debug|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Debug|x86.Build.0 = Debug|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Release|Any CPU.Build.0 = Release|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Release|x64.ActiveCfg = Release|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Release|x64.Build.0 = Release|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Release|x86.ActiveCfg = Release|Any CPU
{48385280-56F1-4937-9655-E6A79184740B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphQL", "GraphQL\GraphQL.csproj", "{D96823B9-86D3-4D54-A803-F1D43AEBE1FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D96823B9-86D3-4D54-A803-F1D43AEBE1FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D96823B9-86D3-4D54-A803-F1D43AEBE1FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D96823B9-86D3-4D54-A803-F1D43AEBE1FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D96823B9-86D3-4D54-A803-F1D43AEBE1FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions code/session-7/GraphQL/Attendees/AttendeeDataLoaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ConferencePlanner.GraphQL.Data;
using Microsoft.EntityFrameworkCore;

namespace ConferencePlanner.GraphQL.Attendees;

public static class AttendeeDataLoaders
{
[DataLoader]
public static async Task<IReadOnlyDictionary<int, Attendee>> AttendeeByIdAsync(
IReadOnlyList<int> ids,
ApplicationDbContext dbContext,
CancellationToken cancellationToken)
{
return await dbContext.Attendees
.Where(a => ids.Contains(a.Id))
.ToDictionaryAsync(a => a.Id, cancellationToken);
}
}
3 changes: 3 additions & 0 deletions code/session-7/GraphQL/Attendees/AttendeeExceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ConferencePlanner.GraphQL.Attendees;

public sealed class AttendeeNotFoundException() : Exception("Attendee not found.");
92 changes: 40 additions & 52 deletions code/session-7/GraphQL/Attendees/AttendeeMutations.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,56 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ConferencePlanner.GraphQL.Common;
using ConferencePlanner.GraphQL.Data;
using HotChocolate;
using HotChocolate.Types;
using HotChocolate.Subscriptions;
using Microsoft.EntityFrameworkCore;

namespace ConferencePlanner.GraphQL.Attendees;

namespace ConferencePlanner.GraphQL.Attendees
[MutationType]
public static class AttendeeMutations
{
[ExtendObjectType(Name = "Mutation")]
public class AttendeeMutations
public static async Task<Attendee> RegisterAttendeeAsync(
RegisterAttendeeInput input,
ApplicationDbContext dbContext,
CancellationToken cancellationToken)
{
[UseApplicationDbContext]
public async Task<RegisterAttendeePayload> RegisterAttendeeAsync(
RegisterAttendeeInput input,
[ScopedService] ApplicationDbContext context,
CancellationToken cancellationToken)
var attendee = new Attendee
{
var attendee = new Attendee
{
FirstName = input.FirstName,
LastName = input.LastName,
UserName = input.UserName,
EmailAddress = input.EmailAddress
};
FirstName = input.FirstName,
LastName = input.LastName,
Username = input.Username,
EmailAddress = input.EmailAddress
};

context.Attendees.Add(attendee);
dbContext.Attendees.Add(attendee);

await context.SaveChangesAsync(cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);

return new RegisterAttendeePayload(attendee);
}
return attendee;
}

[UseApplicationDbContext]
public async Task<CheckInAttendeePayload> CheckInAttendeeAsync(
CheckInAttendeeInput input,
[ScopedService] ApplicationDbContext context,
[Service] ITopicEventSender eventSender,
CancellationToken cancellationToken)
{
Attendee attendee = await context.Attendees.FirstOrDefaultAsync(
t => t.Id == input.AttendeeId, cancellationToken);
public static async Task<Attendee> CheckInAttendeeAsync(
CheckInAttendeeInput input,
ApplicationDbContext dbContext,
ITopicEventSender eventSender,
CancellationToken cancellationToken)
{
var attendee = await dbContext.Attendees.FirstOrDefaultAsync(
a => a.Id == input.AttendeeId,
cancellationToken);

if (attendee is null)
{
return new CheckInAttendeePayload(
new UserError("Attendee not found.", "ATTENDEE_NOT_FOUND"));
}
if (attendee is null)
{
throw new AttendeeNotFoundException();
}

attendee.SessionsAttendees.Add(
new SessionAttendee
{
SessionId = input.SessionId
});
attendee.SessionsAttendees.Add(new SessionAttendee { SessionId = input.SessionId });

await context.SaveChangesAsync(cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);

await eventSender.SendAsync(
"OnAttendeeCheckedIn_" + input.SessionId,
input.AttendeeId,
cancellationToken);
await eventSender.SendAsync(
$"OnAttendeeCheckedIn_{input.SessionId}",
input.AttendeeId,
cancellationToken);

return new CheckInAttendeePayload(attendee, input.SessionId);
}
return attendee;
}
}
}
21 changes: 0 additions & 21 deletions code/session-7/GraphQL/Attendees/AttendeePayloadBase.cs

This file was deleted.

50 changes: 23 additions & 27 deletions code/session-7/GraphQL/Attendees/AttendeeQueries.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ConferencePlanner.GraphQL.Data;
using ConferencePlanner.GraphQL.DataLoader;
using HotChocolate;
using HotChocolate.Types;
using HotChocolate.Types.Relay;

namespace ConferencePlanner.GraphQL.Attendees
namespace ConferencePlanner.GraphQL.Attendees;

[QueryType]
public static class AttendeeQueries
{
[ExtendObjectType(Name = "Query")]
public class AttendeeQueries
[UsePaging]
public static IQueryable<Attendee> GetAttendees(ApplicationDbContext dbContext)
{
[UseApplicationDbContext]
[UsePaging]
public IQueryable<Attendee> GetAttendees(
[ScopedService] ApplicationDbContext context) =>
context.Attendees;
return dbContext.Attendees.OrderBy(a => a.Username);
}

public Task<Attendee> GetAttendeeByIdAsync(
[ID(nameof(Attendee))] int id,
AttendeeByIdDataLoader attendeeById,
CancellationToken cancellationToken) =>
attendeeById.LoadAsync(id, cancellationToken);
[NodeResolver]
public static async Task<Attendee> GetAttendeeByIdAsync(
int id,
AttendeeByIdDataLoader attendeeById,
CancellationToken cancellationToken)
{
return await attendeeById.LoadAsync(id, cancellationToken);
}

public async Task<IEnumerable<Attendee>> GetAttendeesByIdAsync(
[ID(nameof(Attendee))] int[] ids,
AttendeeByIdDataLoader attendeeById,
CancellationToken cancellationToken) =>
await attendeeById.LoadAsync(ids, cancellationToken);
public static async Task<IEnumerable<Attendee>> GetAttendeesByIdAsync(
[ID<Attendee>] int[] ids,
AttendeeByIdDataLoader attendeeById,
CancellationToken cancellationToken)
{
return await attendeeById.LoadAsync(ids, cancellationToken);
}
}
}
Loading

0 comments on commit 64f0813

Please sign in to comment.