-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin work on incremental adoption tutorial
- Loading branch information
Showing
7 changed files
with
158 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
title: Test | ||
excerpt: Learn how to incrementally adopt Effect into your application | ||
section: Incremental adoption | ||
workspace: express | ||
--- | ||
|
||
Second page |
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,27 @@ | ||
--- | ||
title: Welcome | ||
excerpt: Learn how to incrementally adopt Effect into your application | ||
section: Incremental adoption | ||
workspace: express | ||
--- | ||
|
||
### Incrementally Adopting Effect | ||
|
||
Effect was designed with incremental adoption in mind. With Effect, you can continue to use your existing code and incorporate Effect as much (or as little) as you prefer where it makes the most sense within your application. In this way, you can immediately begin to benefit from Effect without needing to make drastic changes to your codebase. | ||
|
||
### What We'll Be Doing | ||
|
||
In this tutorial we will walk through strategies for incrementally adopting Effect into your existing applications. Our goal will be to taking an existing REST API built with [Express](https://expressjs.com) and gradually incorporate Effect. | ||
|
||
In the editor to the right, you will see the following files in the `src` directory: | ||
|
||
- `main.ts`: contains the logic for starting our HTTP server and serving requests | ||
- `repo.ts`: contains the logic for creating, reading, updating, and deleting (CRUD) todos | ||
|
||
### How To Use This Tutorial | ||
|
||
To the right is an editor and a console window. As you make changes to the code in the editor, the program will re-run and show the output in the console. | ||
|
||
Each section will present an exercise designed to illustrate a feature. Later exercises build on the knowledge gained in earlier ones, so it's recommended that you go from start to finish. If necessary, you can navigate via the menu above. | ||
|
||
If you want to view the recommended solution to an exercise, click the "Solve" button. |
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,37 @@ | ||
import Express from "express" | ||
import { TodoRepository } from "./repo" | ||
|
||
const app = Express() | ||
|
||
app.use(Express.json() as Express.NextFunction) | ||
|
||
const repo = new TodoRepository() | ||
|
||
// Creat Todo | ||
app.post("/todos", (req, res) => { | ||
res.json(repo.create(req.body.text)) | ||
}) | ||
|
||
// Read Todo | ||
app.get("/todos/:id", (req, res) => { | ||
res.json(repo.get(Number.parseInt(req.params.id))) | ||
}) | ||
|
||
// Read Todos | ||
app.get("/todos", (_, res) => { | ||
res.json(repo.getAll()) | ||
}) | ||
|
||
// Update Todo | ||
app.patch("/todos/:id", (req, res) => { | ||
res.json(repo.update(Number.parseInt(req.params.id), req.body)) | ||
}) | ||
|
||
// Delete Todo | ||
app.delete("/todos/:id", (req, res) => { | ||
res.json(repo.delete(Number.parseInt(req.params.id))) | ||
}) | ||
|
||
app.listen(3000, () => { | ||
console.log("Server listing on port 3000...") | ||
}) |
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,37 @@ | ||
import Express from "express" | ||
import { TodoRepository } from "./repo" | ||
|
||
const app = Express() | ||
|
||
app.use(Express.json() as Express.NextFunction) | ||
|
||
const repo = new TodoRepository() | ||
|
||
// Creat Todo | ||
app.post("/todos", (req, res) => { | ||
res.json(repo.create(req.body.text)) | ||
}) | ||
|
||
// Read Todo | ||
app.get("/todos/:id", (req, res) => { | ||
res.json(repo.get(Number.parseInt(req.params.id))) | ||
}) | ||
|
||
// Read Todos | ||
app.get("/todos", (_, res) => { | ||
res.json(repo.getAll()) | ||
}) | ||
|
||
// Update Todo | ||
app.patch("/todos/:id", (req, res) => { | ||
res.json(repo.update(Number.parseInt(req.params.id), req.body)) | ||
}) | ||
|
||
// Delete Todo | ||
app.delete("/todos/:id", (req, res) => { | ||
res.json(repo.delete(Number.parseInt(req.params.id))) | ||
}) | ||
|
||
app.listen(3000, () => { | ||
console.log("Server listing on port 3000...") | ||
}) |
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,45 @@ | ||
export interface Todo { | ||
readonly id: number | ||
readonly text: string | ||
readonly completed: boolean | ||
} | ||
|
||
export class TodoRepository { | ||
readonly todos: Array<Todo> = [ | ||
{ id: 1, text: "Finish homework", completed: false }, | ||
{ id: 2, text: "Buy groceries", completed: false }, | ||
{ id: 3, text: "Write report", completed: false }, | ||
{ id: 4, text: "Clean house", completed: false }, | ||
{ id: 5, text: "Pay bills", completed: false } | ||
] | ||
|
||
get(id: number): Todo | undefined { | ||
return this.todos.find((todo) => todo.id === id) | ||
} | ||
|
||
getAll(): ReadonlyArray<Todo> { | ||
return this.todos | ||
} | ||
|
||
create(text: string): Todo { | ||
const maxId = this.todos.reduce((max, todo) => todo.id > max ? todo.id : max, 0) | ||
const newTodo = { id: maxId + 1, text, completed: false } | ||
this.todos.push(newTodo) | ||
return newTodo | ||
} | ||
|
||
update(id: number, props: Partial<Omit<Todo, "id">>): Todo | undefined { | ||
const todo = this.todos.find((todo) => todo.id === id) | ||
if (todo) { | ||
Object.assign(todo, props) | ||
return todo | ||
} | ||
return undefined | ||
} | ||
|
||
delete(id: number): boolean { | ||
const initialLength = this.todos.length | ||
this.todos.filter((todo) => todo.id !== id) | ||
return this.todos.length < initialLength | ||
} | ||
} |