Lesson 2 Lab — CategoriesController¶
Build a CategoriesController following the same pattern as the StaffController
from the guide. Refer to the guide and the StaffController you already built as
your model.
The Category entity¶
Add Models/Category.cs:
namespace TableServe.Api.Models;
public class Category
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public int SortOrder { get; set; }
}
Steps¶
- Add
Category.cstoModels/ - Add
DbSet<Category> CategoriestoTableServeDbContext - Run
Add-Migration AddCategorythenUpdate-Database - Verify the
Categoriestable exists in SQL Server Object Explorer - Run this in SSMS to seed the Categories:
SET IDENTITY_INSERT [dbo].[Categories] ON
INSERT INTO [dbo].[Categories] ([Id], [Name], [SortOrder]) VALUES (1, 'Appetizers', 1)
INSERT INTO [dbo].[Categories] ([Id], [Name], [SortOrder]) VALUES (2, 'Entrees', 2)
INSERT INTO [dbo].[Categories] ([Id], [Name], [SortOrder]) VALUES (3, 'Sides', 3)
INSERT INTO [dbo].[Categories] ([Id], [Name], [SortOrder]) VALUES (4, 'Desserts', 4)
INSERT INTO [dbo].[Categories] ([Id], [Name], [SortOrder]) VALUES (5, 'Drinks', 5)
SET IDENTITY_INSERT [dbo].[Categories] OFF
- Create
Controllers/CategoriesController.cswithGetAllandGetById - Run the project and verify your endpoints using the Categories folder in the Insomnia collection
Verify in Insomnia¶
Insomnia setup (importing the collection, setting baseUrl) is covered in
detail in this lesson's guide — section 12. Once that's done (no need to log in
first — the endpoints are open):
- Expand the Categories folder
- Run Get All Categories → expect
200 OKwith all 5 categories - Run Get Category by Id → expect
200 OKwith Appetizers - Run Get Category by Id (Not Found) → expect
404 Not Found - Check the Tests tab on each response — all tests should be green
You just repeated the same pattern on a different entity — this is exactly what you'll do five times when building the PRS backend in the capstone.
Stretch challenges¶
Optional — for when you finish early. Not needed for the capstone. [Reinforce] builds on what you just did; [Reach] goes past the guide and needs some research.
- Sort the list — [Reinforce] — return categories from
GetAllordered bySortOrder, thenName, usingOrderBy/ThenBy. - Filter by name — [Reinforce] — accept an optional
?name=query string onGetAlland return only categories whoseNamecontains it. (This is the same shape as the?status=filter you'll write on Orders in Lesson 4.) - 404 on a bad id — [Reinforce] — make
GetByIdreturnNotFound()when the id doesn't exist, ahead of Lesson 3 formally covering status codes in controllers.
Finished these and want more? See stretch-api-challenges.md for bigger challenges that span the whole API pass.