In this episode, we're diving into how to let users create new teams in our app—from both the frontend and backend! We kick things off by making sure all our tests pass, fixing a small issue in the process, just to be confident everything is working before we make big changes.
First, we build out a super basic UI for creating teams: a form to collect a team name, the necessary routes, and a nav link so users can find it. Then, we handle the backend by adding the logic to our Team Controller. We validate the request, create a team, associate it with the current user, and make sure they're set as the team's admin.
But there's a nice opportunity here for some refactoring! Since we already have similar logic in our User Observer (when users are first created), we extract that shared functionality into a reusable action class called CreateTeam
. This helps us keep the code DRY and future-proof.
After moving that logic over, we update both the User Observer and Team Controller to use the new action. We rerun our tests to make sure everything still works—and celebrate when they pass. To wrap up, we walk through writing a simple test to check that creating a team through the form actually does what it's supposed to do: the user gets a new team, they're set as admin, and their current team switches over.
All in all, it's a solid episode about improving your app's internal structure while adding cool new features for your users!