Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Convert Attendee Sessions from controller to endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jongalloway committed Apr 10, 2023
1 parent 3c6d9e2 commit 0456c01
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
32 changes: 20 additions & 12 deletions docs/5. Add personal agenda.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,29 @@ One problem with the above system is that if a new user creates an account but d

### Add the BackEnd API to get sessions by an attendee

1. Add an action called `GetSessions` to the `AttendeesController` in the `BackEnd` project:
1. Add an action called `GetSessions` to the `AttendeesEndpoints` in the `BackEnd` project:

```c#
[HttpGet("{username}/sessions")]
public async Task<ActionResult<List<SessionResponse>>> GetSessions(string username)
routes.MapGet("/api/Attendee/{username}/Sessions",
async (string username, ApplicationDbContext db) =>
{
var sessions = await _context.Sessions.AsNoTracking()
.Include(s => s.Track)
.Include(s => s.SessionSpeakers)
.ThenInclude(ss => ss.Speaker)
.Where(s => s.SessionAttendees.Any(sa => sa.Attendee.UserName == username))
.Select(m => m.MapSessionResponse())
.ToListAsync();
return sessions;
}
var sessions = await db.Sessions.AsNoTracking()
.Include(s => s.Track)
.Include(s => s.SessionSpeakers)
.ThenInclude(ss => ss.Speaker)
.Where(s => s.SessionAttendees.Any(sa => sa.Attendee.UserName == username))
.Select(m => m.MapSessionResponse())
.ToListAsync();

return sessions is IQueryable<Data.Session>
? Results.Ok(sessions)
: Results.NotFound();

})
.WithTags("Attendee")
.WithName("GetAllSessionsForAttendee")
.Produces<List<Data.Session>>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound);
```

## Add Add/Remove to personal agenda buttons to Session details page
Expand Down
27 changes: 13 additions & 14 deletions src/ConferencePlanner/BackEnd/Endpoints/AttendeeEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ is Data.Attendee model
async (string username, ApplicationDbContext db) =>
{
var sessions = await db.Sessions.AsNoTracking()
.Include(s => s.Track)
.Include(s => s.SessionSpeakers)
.ThenInclude(ss => ss.Speaker)
.Where(s => s.SessionAttendees.Any(sa => sa.Attendee.UserName == username))
.Select(m => m.MapSessionResponse())
.ToListAsync();
if (sessions is List<Data.Session>)
{
return Results.Ok(sessions);
}
return Results.NotFound();
.Include(s => s.Track)
.Include(s => s.SessionSpeakers)
.ThenInclude(ss => ss.Speaker)
.Where(s => s.SessionAttendees.Any(sa => sa.Attendee.UserName == username))
.Select(m => m.MapSessionResponse())
.ToListAsync();
return sessions is IQueryable<Data.Session>
? Results.Ok(sessions)
: Results.NotFound();
})
.WithTags("Attendee")
.WithName("GetAllSessionsForAttendee")
Expand Down Expand Up @@ -132,8 +131,8 @@ is Data.Attendee model
var sessionAttendee = attendee.SessionsAttendees
.FirstOrDefault(sa => sa.SessionId == sessionId);
if(sessionAttendee is SessionAttendee)
attendee.SessionsAttendees.Remove(sessionAttendee);
if (sessionAttendee is SessionAttendee)
attendee.SessionsAttendees.Remove(sessionAttendee);
await db.SaveChangesAsync();
return Results.Ok();
Expand Down

0 comments on commit 0456c01

Please sign in to comment.