In this session, we'll add the rest of our models and controllers that expose them. We'll also refactor our application, moving our data transfer objects (DTOs) to a shared project so they can be used by our front-end application later.
We'll start by creating the new shared project to hold our data transfer objects.
- If using Visual Studio, right-click on the Solution and select Add > New Project....
- Select Class Library from the project type, name the project ConferenceDTO and press OK.
- Delete the generated
Class1.cs
file from this new project. - Right-click the 'Dependencies' node under the BackEnd project, select Add > Project Reference and put a checkmark near ConferenceDTO.
-
Open a command prompt and navigate to the root ConferencePlanner directory.
-
Run the following command:
dotnet new classlib -o ConferenceDTO
-
Next we'll need to add a reference to the ConferenceDTO project from the BackEnd project. From the command line, navigate to the BackEnd project directory and execute the following command:
dotnet add reference ../ConferenceDTO
-
Add the ConferenceDTO project to the solution:
dotnet sln add ConferenceDTO/ConferenceDTO.csproj
-
Copy the
Speaker.cs
class from the BackEnd application into the root of the new ConferenceDTO project, and change the namespace fromBackend.Models
toConferenceDTO
. -
Go back to the BackEnd application and modify the code in
Speaker.cs
as shown:namespace BackEnd.Models; public class Speaker : ConferenceDTO.Speaker { }
-
Run the application and view the Speakers data using the Swagger UI to verify everything still works.
We've got several more models to add, and unfortunately it's a little mechanical. You can copy the following classes manually, or open the completed solution which is shown at the end.
-
Create an
Attendee.cs
class in the ConferenceDTO project with the following code:using System.ComponentModel.DataAnnotations; namespace ConferenceDTO; public class Attendee { public int Id { get; set; } [Required] [StringLength(200)] public virtual string? FirstName { get; set; } [Required] [StringLength(200)] public virtual string? LastName { get; set; } [Required] [StringLength(200)] public string? UserName { get; set; } [StringLength(256)] public virtual string? EmailAddress { get; set; } }
-
Create a
Session.cs
class in the ConferenceDTO project with the following code:using System.ComponentModel.DataAnnotations; namespace ConferenceDTO; public class Session { public int Id { get; set; } [Required] [StringLength(200)] public string? Title { get; set; } [StringLength(4000)] public virtual string? Abstract { get; set; } public virtual DateTimeOffset? StartTime { get; set; } public virtual DateTimeOffset? EndTime { get; set; } // Bonus points to those who can figure out why this is written this way public TimeSpan Duration => EndTime?.Subtract(StartTime ?? EndTime ?? DateTimeOffset.MinValue) ?? TimeSpan.Zero; public int? TrackId { get; set; } }
-
Create a new
Track.cs
class in the ConferenceDTO project with the following code:using System.ComponentModel.DataAnnotations; namespace ConferenceDTO; public class Track { public int Id { get; set; } [Required] [StringLength(200)] public string? Name { get; set; } }
We're not going to create our EF models directly from the ConferenceDTO
classes. Instead, we'll create some composite classes such as SessionSpeaker
, since these will map more closely to what our application will be working with.
We're also going to take this opportunity to rename the Models
directory in the BackEnd project to Data
since it no longer just contains models.
-
Right-click the Models directory and select Rename, changing the name to Data.
-
Change the namespace for both
ApplicationDbContext
andSpeaker
toBackend.Data
.Note As you add the following classes, you will see syntax error indications (the dreaded "red squiggly lines") for a bit, because you're adding several classes that reference eachother. Don't worry, it'll be fixed when this section is complete.
-
Add a
SessionSpeaker.cs
class to the BackEnd project's Data directory with the following code:namespace BackEnd.Data; public class SessionSpeaker { public int SessionId { get; set; } public Session Session { get; set; } = null!; public int SpeakerId { get; set; } public Speaker Speaker { get; set; } = null!; }
-
Add an
SessionAttendee.cs
class to the BackEnd project's Data directory with the following code:namespace BackEnd.Data; public class SessionAttendee { public int SessionId { get; set; } public Session Session { get; set; } = null!; public int AttendeeId { get; set; } public Attendee Attendee { get; set; } = null!; }
-
Add an
Attendee.cs
class to the BackEnd project'sData
directory with the following code:namespace BackEnd.Data; public class Attendee : ConferenceDTO.Attendee { public virtual ICollection<SessionAttendee> SessionsAttendees { get; set; } = null!; }
-
Add a
Session.cs
class to the BackEnd project's Data directory with the following code:using System; using System.Collections; using System.Collections.Generic; namespace BackEnd.Data { public class Session : ConferenceDTO.Session { public virtual ICollection<SessionSpeaker> SessionSpeakers { get; set; } = null!; public virtual ICollection<SessionAttendee> SessionAttendees { get; set; } = null!; public Track Track { get; set; } } }
-
Add a
Track.cs
class to the BackEnd project's Data directory with the following code:namespace BackEnd.Data; public class Track : ConferenceDTO.Track { public virtual ICollection<Session> Sessions { get; set; } = null!; }
-
Modify the
Speaker.cs
class we wrote in the BackEnd project previously to make the following two changes: update to the namespace to match our directory rename (if not already done), and add a reference to theSessionSpeaker
composite class:namespace BackEnd.Data; public class Speaker : ConferenceDTO.Speaker { public virtual ICollection<SessionSpeaker> SessionSpeakers { get; set; } = new List<SessionSpeaker>(); }
Okay, now we need to update our ApplicationDbContext
so Entity Framework knows about our new models.
-
Update
ApplicationDbContext.cs
to use the following code:using Microsoft.EntityFrameworkCore; namespace BackEnd.Data; public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Attendee>() .HasIndex(a => a.UserName) .IsUnique(); // Many-to-many: Session <-> Attendee modelBuilder.Entity<SessionAttendee>() .HasKey(ca => new { ca.SessionId, ca.AttendeeId }); // Many-to-many: Speaker <-> Session modelBuilder.Entity<SessionSpeaker>() .HasKey(ss => new { ss.SessionId, ss.SpeakerId }); } public DbSet<Session> Sessions => Set<Session>(); public DbSet<Track> Tracks => Set<Track>(); public DbSet<Speaker> Speakers => Set<Speaker>(); public DbSet<Attendee> Attendees => Set<Attendee>(); }
-
Fix errors due to the rename from
BackEnd.Models
toBackEnd.Data
. You can either do this using a find / replace (replacing "BackEnd.Models" with "BackEnd.Data") or you can do a build and fix errors. -
Ensure that the application builds now.
-
Run the following commands in the Visual Studio Developer Powershell or command prompt in the
BackEnd
project directory:dotnet ef migrations add Refactor dotnet ef database update
-
Now take a deep breath and run the application and navigate to
/swagger
. Hopefully, you should see the Swagger UI. There are no new endpoints, but you will see some new types listed in the Schemas section (e.g.Session
,Track
,Speaker
, etc.).
Now that we've added our new models, we can update our Speakers
API endpoints to include richer information, which will simplify building our UI by removing the need to make a lot of separate requests. When we display the list of speakers, we're going to want to include the sessions they are presenting. We can do this by updating our EF query to use the many-to-many navigation properties we've added to our models.
We could just modify the first route in our SpeakerEndpoints
class as shown below.
Warning Don't do this, it's just for illustration. We'll implement a better solution in a minute.
routes.MapGet("/api/Speaker", async (ApplicationDbContext db) =>
{
var speakers = await db.Speakers.AsNoTracking()
.Include(s => s.SessionSpeakers)
.ThenInclude(ss => ss.Session)
.ToListAsync();
return speakers;
})
.WithTags("Speaker")
.WithName("GetAllSpeakers")
.Produces<List<Speaker>>(StatusCodes.Status200OK);
While the above will work, this is directly returning our model class. A better practice is to return an output model class.
-
Create a
SpeakerResponse.cs
class in theConferenceDTO
project with the following code:namespace ConferenceDTO; public class SpeakerResponse : Speaker { public ICollection<Session> Sessions { get; set; } = new List<Session>(); }
-
Now we'll add a utility method to map between these classes. In the BackEnd project, create an Infrastructure directory. Add a class named
EntityExtensions.cs
with the following mapping code:namespace BackEnd.Data; public static class EntityExtensions { public static ConferenceDTO.SpeakerResponse MapSpeakerResponse(this Speaker speaker) => new ConferenceDTO.SpeakerResponse { Id = speaker.Id, Name = speaker.Name, Bio = speaker.Bio, WebSite = speaker.WebSite, Sessions = speaker.SessionSpeakers? .Select(ss => new ConferenceDTO.Session { Id = ss.SessionId, Title = ss.Session.Title }) .ToList() ?? new() }; }
Now we can update the GetSpeakers()
method of the SpeakersEndpoints so that it returns our response model.
-
Add a using statement to
SpeakersEndpoints.cs
:using BackEnd.Infrastructure;
-
Update the
/api/Speaker
endpoint method so that it reads as follows:routes.MapGet("/api/Speaker", async (ApplicationDbContext db) => { var speakers = await db.Speakers.AsNoTracking() .Include(s => s.SessionSpeakers) .ThenInclude(ss => ss.Session) .Select(s => s.MapSpeakerResponse()) .ToListAsync(); return speakers; }) .WithTags("Speaker") .WithName("GetAllSpeakers") .Produces<List<ConferenceDTO.Speaker>>(StatusCodes.Status200OK);
-
Update the route for
/api/Speaker/{id}
to use our mapped response models as follows:routes.MapGet("/api/Speaker/{id}", async (int Id, ApplicationDbContext db) => { return await db.Speakers.AsNoTracking() .Include(s => s.SessionSpeakers) .ThenInclude(ss => ss.Session) .SingleOrDefaultAsync(s => s.Id == Id) is Speaker model ? Results.Ok(model.MapSpeakerResponse()) : Results.NotFound(); }) .WithTags("Speaker") .WithName("GetSpeakerById") .Produces<ConferenceDTO.Speaker>(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound);
-
Remove the other routes (
MapPut
,MapPost
,MapDelete
), on theSpeakersEndpoints
. We will be loading our speakers from a JSON file into our database, so we don't need to support these actions. This is a good concept to keep in mind when you're working with a REST API: only expose the actions that you want to allow.
Let's do one more cleanup task on the SpeakerEndpoints
before moving on. As you've probably guessed, we're going to have some other endpoints to interact with our other models, like Attendees and Sessions. Before we go further down that path, let's create a directory to move our Endpoints
classes out of the root of the project.
- Create an Endpoints directory in the BackEnd project.
- Move the
SpeakerEndpoints
class into the Endpoints directory and update the namespace toBackEnd.Endpoints
. Fix the references inProgram.cs
for this refactor.
Okay, we've been through the the refactoring process with the Speaker
model classes. The process with the other models is very similar, so we'll move through that quickly.
Note You've got a few turbo-boost options if you'd like to just skim through the rest of this unit without writing / copying the code.
- You can just use the code from the the save point folder when you start the next unit.
- Alternatively, you can copy the following files from the second save point into your project:
ConferenceDTO.AttendeeResponse
ConferenceDTO.SessionResponse
BackEnd.Endpoints.SessionEndpoints
BackEnd.Endpoints.AttendeeEndpoints
BackEnd.Data.EntityExtensions
BackEnd.Program
Next, we'll add response classes for Attendees
and Sessions
, then update the EntityExtensions
class to map them.
-
Add an
AttendeeResponse
class to theConferenceDTO
project with the following code:namespace ConferenceDTO; public class AttendeeResponse : Attendee { public ICollection<Session> Sessions { get; set; } = new List<Session>(); }
-
Add an
SessionResponse
class to theConferenceDTO
project with the following code:namespace ConferenceDTO; public class SessionResponse : Session { public Track Track { get; set; } = null!; public List<Speaker> Speakers { get; set; } = new List<Speaker>(); }
-
Update the
EntityExtensions
class with theMapSessionResponse
andMapAttendeeResponse
methods as shown below:namespace BackEnd.Data; public static class EntityExtensions { public static ConferenceDTO.SessionResponse MapSessionResponse(this Session session) => new ConferenceDTO.SessionResponse { Id = session.Id, Title = session.Title, StartTime = session.StartTime, EndTime = session.EndTime, Speakers = session.SessionSpeakers? .Select(ss => new ConferenceDTO.Speaker { Id = ss.SpeakerId, Name = ss.Speaker.Name }) .ToList() ?? new(), TrackId = session.TrackId, Track = new ConferenceDTO.Track { Id = session?.TrackId ?? 0, Name = session?.Track?.Name }, Abstract = session?.Abstract }; public static ConferenceDTO.SpeakerResponse MapSpeakerResponse(this Speaker speaker) => new ConferenceDTO.SpeakerResponse { Id = speaker.Id, Name = speaker.Name, Bio = speaker.Bio, WebSite = speaker.WebSite, Sessions = speaker.SessionSpeakers? .Select(ss => new ConferenceDTO.Session { Id = ss.SessionId, Title = ss.Session.Title }) .ToList() ?? new() }; public static ConferenceDTO.AttendeeResponse MapAttendeeResponse(this Attendee attendee) => new ConferenceDTO.AttendeeResponse { Id = attendee.Id, FirstName = attendee.FirstName, LastName = attendee.LastName, UserName = attendee.UserName, Sessions = attendee.SessionsAttendees? .Select(sa => new ConferenceDTO.Session { Id = sa.SessionId, Title = sa.Session.Title, StartTime = sa.Session.StartTime, EndTime = sa.Session.EndTime }) .ToList() ?? new() }; }
-
Add a new
AttendeeEndpoints
class to the Endpoints directory with the following code:using Microsoft.EntityFrameworkCore; using BackEnd.Data; using ConferenceDTO; namespace BackEnd.Endpoints; public static class AttendeeEndpoints { public static void MapAttendeeEndpoints(this IEndpointRouteBuilder routes) { routes.MapGet("/api/Attendee/{username}", async (string username, ApplicationDbContext db) => { var attendee = await db.Attendees.Include(a => a.SessionsAttendees) .ThenInclude(sa => sa.Session) .SingleOrDefaultAsync(a => a.UserName == username); return await db.Attendees.Include(a => a.SessionsAttendees) .ThenInclude(sa => sa.Session) .SingleOrDefaultAsync(a => a.UserName == username) is Data.Attendee model ? Results.Ok(model.MapAttendeeResponse()) : Results.NotFound(); }) .WithTags("Attendee") .WithName("GetAttendee") .Produces<AttendeeResponse>(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound); routes.MapPost("/api/Attendee/", async (ConferenceDTO.Attendee input, ApplicationDbContext db) => { // Check if the attendee already exists var existingAttendee = await db.Attendees .Where(a => a.UserName == input.UserName) .FirstOrDefaultAsync(); if (existingAttendee == null) { var attendee = new Data.Attendee { FirstName = input.FirstName, LastName = input.LastName, UserName = input.UserName, EmailAddress = input.EmailAddress }; db.Attendees.Add(attendee); await db.SaveChangesAsync(); var result = attendee.MapAttendeeResponse(); return Results.Created($"/api/Attendee/{attendee.UserName}", result); } else { return Results.Conflict(); } }) .WithTags("Attendee") .WithName("CreateAttendee") .Produces<AttendeeResponse>(StatusCodes.Status201Created) .Produces(StatusCodes.Status409Conflict); routes.MapPost("/api/Attendee/{username}/Session/{sessionId}", async (string username, int sessionId, ApplicationDbContext db) => { var attendee = await db.Attendees.Include(a => a.SessionsAttendees) .ThenInclude(sa => sa.Session) .SingleOrDefaultAsync(a => a.UserName == username); if (attendee == null) { return Results.NotFound(new { Attendee = username }); } var session = await db.Sessions.FindAsync(sessionId); if (session == null) { return Results.NotFound(new { Session = sessionId }); } attendee.SessionsAttendees.Add(new SessionAttendee { AttendeeId = attendee.Id, SessionId = sessionId }); await db.SaveChangesAsync(); var result = attendee.MapAttendeeResponse(); return Results.Created($"/api/Attendee/{result.UserName}", result); }) .WithTags("Attendee") .WithName("AddAttendeeSession") .Produces<AttendeeResponse>(StatusCodes.Status201Created) .Produces(StatusCodes.Status404NotFound); routes.MapDelete("/api/Attendee/{username}/Session/{sessionId}", async (string username, int sessionId, ApplicationDbContext db) => { var attendee = await db.Attendees.Include(a => a.SessionsAttendees) .SingleOrDefaultAsync(a => a.UserName == username); if (attendee is Data.Attendee) { var session = await db.Sessions.FindAsync(sessionId); if (session is Data.Session) { var sessionAttendee = attendee.SessionsAttendees .FirstOrDefault(sa => sa.SessionId == sessionId); if(sessionAttendee is SessionAttendee) attendee.SessionsAttendees.Remove(sessionAttendee); await db.SaveChangesAsync(); return Results.Ok(); } } return Results.NotFound(); }) .WithTags("Attendee") .WithName("RemoveSessionFromAttendee") .Produces(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound); } }
-
Add a new
SessionEndpoints
class to the Endpoints directory with the following code:using Microsoft.EntityFrameworkCore; using BackEnd.Data; using ConferenceDTO; namespace BackEnd.Endpoints; public static class SessionEndpoints { public static void MapSessionEndpoints(this IEndpointRouteBuilder routes) { routes.MapGet("/api/Session/", async (ApplicationDbContext db) => { return await db.Sessions.AsNoTracking() .Include(s => s.Track) .Include(s => s.SessionSpeakers) .ThenInclude(ss => ss.Speaker) .Select(m => m.MapSessionResponse()) .ToListAsync() is List<SessionResponse> model ? Results.Ok(model) : Results.NotFound(); }) .WithTags("Session") .WithName("GetAllSessions") .Produces<List<SessionResponse>>(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound); routes.MapGet("/api/Session/{id}", async (int id, ApplicationDbContext db) => { return await db.Sessions.AsNoTracking() .Include(s => s.Track) .Include(s => s.SessionSpeakers) .ThenInclude(ss => ss.Speaker) .SingleOrDefaultAsync(s => s.Id == id) is Data.Session model ? Results.Ok(model.MapSessionResponse()) : Results.NotFound(); }) .WithTags("Session") .WithName("Getsession") .Produces<SessionResponse>(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound); routes.MapPost("/api/Session/", async (ConferenceDTO.Session input, ApplicationDbContext db) => { var session = new Data.Session { Title = input.Title, StartTime = input.StartTime, EndTime = input.EndTime, Abstract = input.Abstract, TrackId = input.TrackId }; db.Sessions.Add(session); await db.SaveChangesAsync(); return Results.Created($"/api/Session/{session.Id}", session.MapSessionResponse()); }) .WithTags("Session") .WithName("CreateSession") .Produces<SessionResponse>(StatusCodes.Status201Created) .Produces(StatusCodes.Status409Conflict); routes.MapPut("/api/Session/{id}", async (int id, ConferenceDTO.Session input, ApplicationDbContext db) => { var session = await db.Sessions.FindAsync(id); if (session is null) { return Results.NotFound(); } session.Id = input.Id; session.Title = input.Title; session.Abstract = input.Abstract; session.StartTime = input.StartTime; session.EndTime = input.EndTime; session.TrackId = input.TrackId; await db.SaveChangesAsync(); return Results.NoContent(); }) .WithTags("Session") .WithName("UpdateSession") .Produces(StatusCodes.Status404NotFound) .Produces(StatusCodes.Status204NoContent); routes.MapDelete("/api/Sessions/{id}/", async (int id, ApplicationDbContext db) => { if (await db.Sessions.FindAsync(id) is Data.Session session) { db.Sessions.Remove(session); await db.SaveChangesAsync(); return Results.Ok(); } return Results.NotFound(); }) .WithTags("Session") .WithName("DeleteSession") .Produces(StatusCodes.Status200OK) .Produces(StatusCodes.Status404NotFound); routes.MapPost("api/Sessions/upload", async (HttpRequest req, ApplicationDbContext db) => { if (db.Sessions.Any()) { return Results.Conflict("Sessions already uploaded"); } var loader = new TechoramaDataLoader(); await loader.LoadDataAsync(req.Body, db); await db.SaveChangesAsync(); return Results.NoContent(); }) .WithTags("Session") .Accepts<IFormFile>("text/plain"); } }
We need to update our Program.cs
to include these new endpoints. While we're at it, let's remove the WeatherForecast sample endpoint and its related classes.
-
Below the line that adds the
SpeakerEndpoints
, add the following:app.MapAttendeeEndpoints(); app.MapSessionEndpoints();
-
Delete this code:
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; app.MapGet("/weatherforecast", () => { var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast ( DateTime.Now.AddDays(index), Random.Shared.Next(-20, 55), summaries[Random.Shared.Next(summaries.Length)] )) .ToArray(); return forecast; }) .WithName("GetWeatherForecast");
-
Delete the
WeatherForecast
record at the bottom ofProgram.cs
.
-
Copy the
DataLoader.cs
class from here into theData
directory of theBackEnd
project. -
Copy the
SessionizeLoader.cs
,DevIntersectionLoader.cs
, andTechoramaLoader.cs
classes from here into the current project's/src/BackEnd/Data/
directory.Note: We have data loaders from a few of the conference series where this workshop has been presented most; you can update this to plug in your own conference file format.
-
Add a new
api/Sessions/upload
endpoint to theSessionEndpoints
class:routes.MapPost("api/Sessions/upload", async (HttpRequest req, ApplicationDbContext db) => { if (db.Sessions.Any()) { return Results.Conflict("Sessions already uploaded"); } var loader = new TechoramaDataLoader(); await loader.LoadDataAsync(req.Body, db); await db.SaveChangesAsync(); return Results.NoContent(); }) .WithTags("Session") .Accepts<IFormFile>("text/plain");
-
Run the application to see the updated data via Swagger UI.
-
Use the Swagger UI to upload Techorama_2022.json to the
/api/Sessions/upload
API.
Next: Session #3 - Front-end | Previous: Session #1 - Setup, basic EF model