-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from icefoganalytics/main
Updates from IceFog
- Loading branch information
Showing
64 changed files
with
7,455 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Controllers | ||
|
||
These files map api routes to services. | ||
See https://guides.rubyonrails.org/routing.html#crud-verbs-and-actions | ||
|
||
e.g. | ||
|
||
```typescript | ||
router.route("/api/forms").post(FormsController.create) | ||
``` | ||
|
||
maps the `/api/forms` POST endpoint to the `FormsController#create` instance method. | ||
|
||
Controllers are advantageous because they provide a suite of helper methods to access various request methods. .e.g. `currentUser`, or `params`. They also provide a location to perform policy checks. | ||
|
||
Controllers should implement the BaseController, and provide instance methods. | ||
The `BaseController` provides the magic that lets those methods map to an appropriate route. | ||
|
||
## Namespacing | ||
|
||
If you need an action that generates estimates for a given form, a POST route `/api/forms/:formId/estimates/generate` is the best way to avoid future conflicts and refactors. To implement this you need to "namespace/modularize" the controller. Generally speaking, it is more flexible to keep all routes as CRUD actions, and nest controllers as needed, than it is to add custom routes to a given controller. | ||
|
||
e.g. `Forms.Estimates.GenerateController.create` is preferred to `FormsController#generateEstimates` because once you start using non-CRUD actions, your controllers will quickly expand beyond human readability and comprehension. Opting to use PascalCase for namespaces as that is the best way to avoid conflicts with local variables. | ||
|
||
This is how you would create a namespaced controller: | ||
|
||
```bash | ||
api/ | ||
|-- src/ | ||
| |-- controllers/ | ||
| |-- forms/ | ||
| |-- estimates/ | ||
| |-- generate-controller.ts | ||
| |-- index.ts | ||
``` | ||
|
||
```typescript | ||
// api/src/controllers/forms/estimates/generate-controller.ts | ||
import BaseController from "@/base-controller" | ||
|
||
export class GenerateController extends BaseController { | ||
async static create() { | ||
// Logic for generating estimates here... | ||
} | ||
} | ||
``` | ||
|
||
```typescript | ||
// api/src/controllers/forms/estimates/index.ts | ||
export * from "./generate-controller" | ||
|
||
export default undefined | ||
``` | ||
|
||
```typescript | ||
// api/src/controllers/forms/index.ts | ||
import * as Estimates from "./estimates" | ||
|
||
export { Estimates } | ||
``` | ||
|
||
```typescript | ||
// api/src/controllers/index.ts | ||
import * as Forms from "./forms" | ||
|
||
export { Forms } | ||
``` | ||
|
||
```typescript | ||
// api/src/routes/index.ts | ||
import { Router } from "express" | ||
|
||
import { Forms } from "@/controllers" | ||
|
||
const router = Router() | ||
|
||
router.route("/api/forms/:formId/estimates/generate").post(Forms.Estimates.GenerateController.create) | ||
``` |
Oops, something went wrong.