Skip to content

Commit 7a6ea8c

Browse files
authored
Merge pull request #51 from Minimal-APIs/quickstart
Updated the tutorial
2 parents 43bba79 + 3d0644e commit 7a6ea8c

File tree

9 files changed

+292
-298
lines changed

9 files changed

+292
-298
lines changed

src/.vuepress/config.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ module.exports = {
4646
text: "Quickstart",
4747
link: "/quickstart/",
4848
},
49+
{
50+
text: "Guide",
51+
link: "/guide/",
52+
},
4953
{
5054
text: "Tutorial",
5155
link: "/tutorial/",
@@ -63,7 +67,7 @@ module.exports = {
6367
sidebar: {
6468
"/tutorial/": [
6569
{
66-
title: "Guide",
70+
title: "Tutorial",
6771
collapsable: false,
6872
children: ["", "first-steps", "crud", "databases"],
6973
},
@@ -75,6 +79,13 @@ module.exports = {
7579
children: ["", "quickstart"],
7680
},
7781
],
82+
"/guide/": [
83+
{
84+
title: "Guide",
85+
collapsable: true,
86+
children: ["", "routing" , "static_assets" , "database" , "validation" , "error_handling" , "middleware" , "authentication" , "deployment", "dependency_injection","openapi", ],
87+
},
88+
],
7889
},
7990
},
8091

src/index.md

+24
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,27 @@ Minimal routes by design. Create meaningful low ceremony URLs that execute your
3737
```csharp
3838
app.MapGet("/", () => "Hello World!");
3939
```
40+
### Feels familiar
41+
42+
Minimal code patterns have been popular in JavaScript and Python web frameworks for a while. Whether you are familiar with Express or Flask, we think you can easily do things you enjoy about those frameworks with minimal APIs as well.
43+
44+
**For example**
45+
46+
Routes in Express
47+
```JS
48+
// GET method route
49+
app.get('/', function (req, res) {
50+
res.send('GET request to the homepage')
51+
})
52+
53+
// POST method route
54+
app.post('/', function (req, res) {
55+
res.send('POST request to the homepage')
56+
})
57+
```
58+
Routes in minimal APIs
59+
```csharp
60+
app.MapGet("/", () => "GET request to the homepage");
61+
app.MapPost("/", () => "POST request to the homepage");
62+
```
63+

src/quickstart/grpc.md

-3
This file was deleted.

src/quickstart/mlnet.md

-3
This file was deleted.

src/quickstart/quickstart.md

+94-111
Large diffs are not rendered by default.

src/tutorial/README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
# Tutorial
1+
# Tutorials
22

3-
## Tutorial scenario
4-
![WIP](https://user-images.githubusercontent.com/2546640/130136817-6389ec08-89dd-4ab2-83cd-3ab9644fb0c0.gif)
3+
Minimal APIs tutorials are here to teach you how to build your very first C# application.
4+
5+
### Tutorial One: CRUD and minimal APIs.
6+
7+
This tutorial will teach you how to build the backend to a Todo App using minimal APIs. By the end of this tutorial, you will know how:
8+
- Create a minimal API
9+
- Build interactive API documentation with Swagger UI
10+
- Build CRUD application
11+
- Store Todo items into a database.
12+
13+
### Tutorial Two: Connect your minimal API to Web UI
14+
15+
In this tutorial, you will learn how to connect to a frontend framework of your choice.

src/tutorial/crud.md

+55-74
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Let's build
1+
# Let's build
22
## Create Read Update Delete
33

4-
In checklist one, you built simple API where you hardcoded the results to HTTP method.
4+
In [checklist one ](/tutorial/first-steps), you built simple API where you hard coded the results to HTTP method.
55

66
For example: `app.MapGet("/todo", () => new { Item = "Water plants", Complete = "false" });` has been hard coded to return the JSON results below.
77

@@ -12,21 +12,18 @@ For example: `app.MapGet("/todo", () => new { Item = "Water plants", Complete =
1212
}
1313
```
1414

15-
In checklist 2, you are going to step it up a notch and create something dynamic. Instead of returning a static item that is hardcoded to your route, you are going to be creating to-do list you can update, create new items, mark an item as complete, and delete an item.
16-
17-
### Learn checklist Two ✔️
15+
In checklist 3 we are going to step it up a notch and create something dynamic. Instead of returning a static item that is hardcoded to our route, we are going to be creating to-do list we can update, create new items, mark an item as complete and delete an item.
1816

1917
Our to-do APIs is going to:
20-
2118
- Create a new item.➕
2219
- Return a specific item on a list. :leftwards_arrow_with_hook:
2320
- Update an existing item. :arrows_counterclockwise:
2421
- Delete an item. ➖
2522

26-
Create a class that models the data you want to collect, that is, Data model. The code for your `TodoItem` will go after `app.Run();`
2723

28-
**Create a data model**
24+
Create a class that models the data we want to collect aka Data model. The code for your `TodoItem` will go after `app.Run();`
2925

26+
**Create a data model**
3027
```cs
3128
class TodoItem
3229
{
@@ -36,43 +33,34 @@ class TodoItem
3633

3734
}
3835
```
36+
Now, that we have defined that data we want to collect we need to save it.
3937

40-
Now that you've defined the data you want to collect, you need to save it.
38+
**Store an item**
4139

42-
**Store an item**
40+
To store the items in the to do list we are going need to install the Entity Frameworkcore InMemomry package below.
4341

44-
To store the items in the to do list, install the Entity Framework Core InMemomry package as shown below.
42+
> *What is Entity Framework?* is a code library that enables the transfer of data stored in relational database tables(E.g. SQLite and MySQL, SQL server etc) into objects that are more commonly used in application code.
4543
46-
*What is Entity Framework (EF)?* EF is a code library that enables the transfer of data stored in relational database tables (such as, SQLite, MySQL, and SQL Server) into objects that are more commonly used in application code.
4744

4845
**Using .NET CLI**
4946

50-
In your terminal window:
51-
47+
In your terminal window
5248
```console
5349
TodoApi>dotnet add package Microsoft.EntityFrameworkCore.InMemory --version 5.0.7
5450
```
55-
56-
**In Visual Studio**
57-
58-
In Visual Studio, you can use the Package Manager Console or Manager Nuget Package GUI.
59-
60-
```console
61-
PM>Install-Package Microsoft.EntityFrameworkCore.InMemory -Version 5.0.7
62-
```
63-
51+
6452
Add `using Microsoft.EntityFrameworkCore;` to the top of your `Program.cs` file.
6553

66-
Now that you have Entity Framework, you can wire up your code to the data you want to save and query. To do this, create a `TodoDb` class. This class is going to do the following:
54+
Now, that we have EntityFramework we can now wire up our code to the data we want save and query it. To do this we are going to create a `TodoDb` class. `TodoDb` class is going to do the following:
6755

68-
- Expose the `Todos` property from the list of `TodoItem` in the database.
69-
- `UseInMemoryDatabase` wires the in-memory database storage. This will store data as long the app is running.
56+
- Expose our `Todos` property from our list of `TodoItem` in the database.
57+
- `UseInMemoryDatabase` wires the in memory database storage.This will store data as long the app is running.
7058

71-
To set up your in-memory database, add the following code snippets to your code.
59+
To setup your in memory database add the following code snippets to our code.
7260

73-
**Snippet 1** : Below the `TodoItem`, create a `TodoDb` class:
61+
**Snippet 1** : Below the `TodoItem` create a `TodoDb` class
7462

75-
```cs
63+
```cs
7664
class TodoDb : DbContext
7765
{
7866
public TodoDb(DbContextOptions options) : base(options) { }
@@ -84,54 +72,50 @@ class TodoDb : DbContext
8472
}
8573
}
8674
```
75+
`DbContext` represents a connection/session which is used to query and save instances of entities in a database.
8776

88-
`DbContext` represents a connection/session, which is used to query and save instances of entities in a database.
89-
90-
**Snippet 2** : Before `AddSwaggerGen` services that you configured in the [first tutorial](first-steps.md#interactive-api-docs), add the following code snippet.
77+
**Snippet 2** : Before `AddSwaggerGen` services we configured in the [first tutorial](https://github.com/LadyNaggaga/minimal-apis-blog/blob/b5e97d3168b0948d8926afbd6dbc883cb32ba21a/Tutorials/Firststeps.md#interactive-api-docs) add the code snippet below.
9178

9279
``` cs
9380
builder.Services.AddDbContext<TodoDb>(options => options.UseInMemoryDatabase("items"));
9481
```
82+
**Return a list of items**
9583

96-
**Return a list of items**
97-
98-
To read from a list of items in the todo list, replace the "/todo" route with the "/todos" route below.
84+
To read from a list of items in the todo list replace the "/todo" route with the "/todos" route below.
9985

10086
``` cs
10187
app.MapGet("/todos", async (TodoDb db) => await db.Todos.ToListAsync());
10288
```
10389

104-
Go back your browser and navigate to `https://localhost:5001/swagger`. Click on the **GET**`/todos` button and you should see that the list is empty under `Response body`.
90+
Go back your browser and navigate to `https://localhost:5001/swagger` click on the **GET**`/todos` button and you will see the list is empty under `Response body`.
10591

10692
![swagger-get-todos](https://user-images.githubusercontent.com/2546640/125181126-af45d000-e1cf-11eb-82a8-4691bdb9deb9.gif)
10793

108-
## Create new items
109-
94+
## Create new items
95+
11096
Let's `POST` new tasks to the todos list. Below `app.MapGet` you create earlier.
11197

112-
```cs
98+
```cs
11399
app.MapPost("/todos", async (TodoDb db, TodoItem todo) =>
114100
{
115101
await db.Todos.AddAsync(todo);
116102
await db.SaveChangesAsync();
117103
return Results.Created($"/todo/{todo.Id}", todo);
118104
});
119105
```
106+
Go back to `Swagger` and now you should see **`POST`**`/todos`. To add new items to the todo list
120107

121-
Go back to `Swagger` and now you should see **`POST`**`/todos`. To add new items to the todo list
122-
123-
- Click on **`POST`** `/todos`
108+
- Click on **`POST`** `/todos`
124109
- Click on `Try it out`
125110
- Update `id`, `item`, and `isComplete`
126111
- Click `Execute`
127112

128113
![swagger-post-todos](https://user-images.githubusercontent.com/2546640/125181715-b079fb80-e1d5-11eb-96e1-befc11ae8a0a.gif)
129114

130-
**Read the items in the list**
131-
132-
To read the items in the list:
115+
**Read the items in the list**
133116

134-
- Click on **`GET`**`/todos`
117+
To read the items in the list
118+
- click on **`GET`**`/todos`
135119
- Click on `Try it out`
136120
- Click `Execute
137121

@@ -151,27 +135,26 @@ The `Response body` will include the items just added.
151135
}
152136
]
153137
```
154-
155-
To `GET` an item by `id`, add the following code under the `app.MapPost` route that you created earlier.
138+
To `GET` an item by `id` add the code below `app.MapPost` route created earlier
156139

157140
```cs
158141
app.MapGet("/todos/{id}", async (TodoDb db, int id) => await db.Todos.FindAsync(id));
159-
```
160142

161-
To check this out, you can go to `https://localhost:5001/todos/1` or use the swagger UI.
162-
![swagger-get-todos-item](https://user-images.githubusercontent.com/2546640/125182403-bd99e900-e1db-11eb-83bb-72eb89b4386f.gif)
143+
```
144+
To check this out you can either go to `https://localhost:5001/todos/1` or use the swagger UI.
145+
![swagger-get-todos-item](https://user-images.githubusercontent.com/2546640/125182403-bd99e900-e1db-11eb-83bb-72eb89b4386f.gif)
163146

164-
## Update an item
147+
## Update an item
165148

166-
To update an existing item add the following code under the `GET /todos/{id}` route you created earlier.
149+
To update an existing item add the code below `GET /todos/{id}` route we created above.
167150

168-
```cs
151+
```cs
169152
app.MapPut("/todos/{id}", async ( TodoDb db, TodoItem updateTodo ,int id) =>
170153
{
171154
var todo = await db.Todos.FindAsync(id);
172-
155+
173156
if (todo is null) return NotFound();
174-
157+
175158
todo.Item = updateTodo.Item;
176159
todo.IsComplete = updateTodo.IsComplete;
177160

@@ -180,29 +163,27 @@ app.MapPut("/todos/{id}", async ( TodoDb db, TodoItem updateTodo ,int id) =>
180163
return Results.NoContent();
181164
});
182165
```
183-
184-
- Click on **`PUT`**`/todos/{id}`.
185-
- Click on `Try it out`.
186-
- In the **`id`** text box, enter 2.
166+
- click on **`PUT`**`/todos/{id}`
167+
- Click on `Try it out`
168+
- In the **`id`** text box enter 2
187169
- Update `Request body` paste the JSON below and update `isComplete` to `true`.
188-
189170
``` json
190171
{
191172
"id": 2,
192173
"item": "Water plants",
193174
"isComplete": false
194175
}
195-
```
196176

177+
```
197178
- Click `Execute`
198179

199-
To test this, scroll back to **`GET`**`/todos/{id}` and now Water Plants is marked as complete.
180+
To test this out scroll back to **`GET`**`/todos/{id}` and now Water Plants is marked as complete.
200181

201182
![swagger-put-todos-item](https://user-images.githubusercontent.com/2546640/125183698-bdebb180-e1e6-11eb-80fd-c78c1ff01ea4.gif)
202183

203-
## Delete an item
184+
## Delete an item.
204185

205-
To delete an existing item, add the following code under **`PUT`**`/todos/{id}` that you created earlier.
186+
To delete an existing item add the code below **`PUT`**`/todos/{id}` we created above.
206187

207188
```cs
208189
app.MapDelete("/todos/{id}", async (TodoDb db, int id) =>
@@ -219,16 +200,16 @@ app.MapDelete("/todos/{id}", async (TodoDb db, int id) =>
219200

220201
});
221202
```
222-
223203
Now, try deleting an item.
224204
![swagger-delete-todos-item](https://user-images.githubusercontent.com/2546640/125184240-2daf6b80-e1ea-11eb-86db-6109bc04f700.gif)
225205

226-
### Learn checklist one ✔️
227206

228-
- Add model class and database context
229-
- CRUD methods
230-
- `GET`:read an item.
231-
- `POST`: create an item.
232-
- `PUT`: update an item.
233-
- `DELETE`: delete an item.
234-
- Configured routing and returned values
207+
### Learn checklist three ✔️
208+
209+
- Add model class and database context
210+
- CRUD methods
211+
- `GET`:read an item.
212+
- `POST`: create an item.
213+
- `PUT`: update an item.
214+
- `DELETE`: delete an item.
215+
- Configured routing and returned values

0 commit comments

Comments
 (0)