Skip to content

Commit

Permalink
Rename AddsRoutes back to Controller
Browse files Browse the repository at this point in the history
This renames the `AddRoutes` interface back to `Controller` and (hopefully) updates all the comments and variable/function names to reflect that change.
  • Loading branch information
NicMcPhee committed Jan 7, 2024
1 parent a4365c9 commit e2c576d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* routes without starting the server (except that the inability to compare
* lambdas in fact makes this very hard to test).
*
* Any "controller" class that provides routes for the Javalin server
* Any controller class that provides routes for the Javalin server
* must implement this interface since the `Server` class
* is just handled an array of `Controller` objects in its constructor. This
* is just handed an array of `Controller` objects in its constructor. This
* allows us to add routes to the server without having to modify the `Server`,
* and without having the server know about any specific controller implementations.
*
Expand All @@ -23,7 +23,7 @@
* you implement also implement this interface, providing their own `addRoutes()`
* method.
*/
public interface AddsRoutes {
public interface Controller {
/**
* Add routes to the server.
*
Expand Down
27 changes: 14 additions & 13 deletions server/src/main/java/umm3601/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static void main(String[] args) {
// Get the database
MongoDatabase database = mongoClient.getDatabase(databaseName);

// The implementations of `AddsRoutes` used for the server. These will presumably
// be one or more controllers, each of which implements the `AddsRoutes` interface.
// You'll add your own controllers in `getRouteAdders` as you create them.
final AddsRoutes[] routeAdders = Main.getRouteAdders(database);
// The implementations of `Controller` used for the server. These will presumably
// be one or more controllers, each of which implements the `Controller` interface.
// You'll add your own controllers in `getControllers` as you create them.
final Controller[] controllers = Main.getControllers(database);

// Construct the server
Server server = new Server(mongoClient, routeAdders);
Server server = new Server(mongoClient, controllers);

// Start the server
server.startServer();
Expand All @@ -43,21 +43,22 @@ static String getEnvOrDefault(String envName, String defaultValue) {
}

/**
* Get the implementations of `AddsRoutes` used for the server.
* Get the implementations of `Controller` used for the server.
*
* These will presumably be one or more controllers, each of which
* implements the `AddsRoutes` interface. You'll add your own controllers
* in `getRouteAdders` as you create them.
* implements the `Controller` interface. You'll add your own controllers
* in to the array returned by this method as you create them.
*
* @param database The MongoDB database object used by the controllers
* to access the database.
* @return An array of implementations of `AddRoutes` for the server.
* @return An array of implementations of `Controller` for the server.
*/
static AddsRoutes[] getRouteAdders(MongoDatabase database) {
AddsRoutes[] controllers = new AddsRoutes[] {
static Controller[] getControllers(MongoDatabase database) {
Controller[] controllers = new Controller[] {
// You would add additional controllers here, as you create them,
// although you need to make sure that your new controllers implement
// the `AddsRoutes` interface.
// although you need to make sure that each of your new controllers implements
// the `Controller` interface.
//
// You can also remove this UserController once you don't need it.
new UserController(database)
};
Expand Down
18 changes: 9 additions & 9 deletions server/src/main/java/umm3601/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ public class Server {
// The `mongoClient` field is used to access the MongoDB
private final MongoClient mongoClient;

// The `routeAdders` field is an array of all the `AddsRoutes` implementations
// The `controllers` field is an array of all the `Controller` implementations
// for the server. This is used to add routes to the server.
private AddsRoutes[] routeAdders;
private Controller[] controllers;

/**
* Construct a `Server` object that we'll use (via `startServer()`) to configure
* and start the server.
*
* @param mongoClient The MongoDB client object used to access to the database
* @param routeAdders The implementations of `AddRoutes` used for this server
* @param controllers The implementations of `Controller` used for this server
*/
public Server(MongoClient mongoClient, AddsRoutes[] routeAdders) {
public Server(MongoClient mongoClient, Controller[] controllers) {
this.mongoClient = mongoClient;
// This is what is known as a "defensive copy". We make a copy of
// the array so that if the caller modifies the array after passing
// it in, we don't have to worry about it. If we didn't do this,
// the caller could modify the array after passing it in, and then
// we'd be using the modified array without realizing it.
this.routeAdders = Arrays.copyOf(routeAdders, routeAdders.length);
this.controllers = Arrays.copyOf(controllers, controllers.length);
}

/**
Expand Down Expand Up @@ -168,10 +168,10 @@ private void configureShutdowns(Javalin server) {
* @param server The Javalin server instance
*/
private void setupRoutes(Javalin server) {
// Add the routes for all the implements of `AddRoutes` in the
// `routeAdders` array.
for (AddsRoutes routeAdder : routeAdders) {
routeAdder.addRoutes(server);
// Add the routes for all the implementations of `Controller` in the
// `controllers` array.
for (Controller controller : controllers) {
controller.addRoutes(server);
}
}
}
4 changes: 2 additions & 2 deletions server/src/main/java/umm3601/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import io.javalin.http.HttpStatus;
import io.javalin.http.NotFoundResponse;

import umm3601.AddsRoutes;
import umm3601.Controller;

/**
* Controller that manages requests for info about users.
*/
public class UserController implements AddsRoutes {
public class UserController implements Controller {

private static final String API_USERS = "/api/users";
private static final String API_USER_BY_ID = "/api/users/{id}";
Expand Down

0 comments on commit e2c576d

Please sign in to comment.