Skip to content

Commit b6e1fd9

Browse files
committed
improve example requests and add step-by-step
1 parent 0783f39 commit b6e1fd9

19 files changed

+281
-42
lines changed

JsonApiDotnetCore.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExa
4545
EndProject
4646
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExampleTests", "test\ResourceEntitySeparationExampleTests\ResourceEntitySeparationExampleTests.csproj", "{6DFA30D7-1679-4333-9779-6FB678E48EF5}"
4747
EndProject
48-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}"
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{DF9BFD82-D937-4907-B0B4-64670417115F}"
4949
EndProject
5050
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscoveryTests", "test\DiscoveryTests\DiscoveryTests.csproj", "{09C0C8D8-B721-4955-8889-55CB149C3B5C}"
5151
EndProject

docs/generate.sh

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,33 @@ function cleanup() {
88
cleanup
99
dotnet run -p ../src/Examples/GettingStarted/GettingStarted.csproj &
1010
app_pid=$!
11+
echo "Started app with PID $app_pid"
1112

13+
rm -rf ./request-examples/*.json
14+
rm -rf ./request-examples/*.temp
1215

1316
{ # try
14-
echo "Started app with PID $app_pid"
15-
16-
sleep 5
17+
sleep 10
1718

1819
echo "sleep over"
1920

2021
for path in ./request-examples/*.sh; do
21-
op_name=$(basename "$path" .sh | sed 's/.*-//')
22-
echo $op_name
23-
bash $path | jq . > "./request-examples/$op_name-Response.json"
22+
op_name=$(basename "$path" .sh)
23+
file="./request-examples/$op_name-Response.json"
24+
temp_file="./request-examples/$op_name-Response.temp"
25+
26+
# 1. execute bash script
27+
# 2. redirect stdout to a temp file, this will be the JSON output
28+
# 3. redirect stderr to JSON file, this will be the curl verbose output
29+
# we grab the last line, trim the prefix, add some newlines and the push
30+
# it to the top of the JSON file
31+
bash $path \
32+
1> >(jq . > $temp_file) \
33+
2> >(grep "HTTP" | tail -n 1 | cut -c 3- | awk '{ printf "%s\n\n",$0 }' > "./request-examples/$op_name-Response.json")
34+
35+
# append the actual JSON to the file
36+
cat $temp_file >> $file
37+
rm $temp_file
2438
done
2539
}
2640

docs/getting-started/install.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Installation
2+
3+
Click [here](https://www.nuget.org/packages/JsonApiDotnetCore/) for the latest NuGet version.
4+
5+
```
6+
dotnet add package JsonApiDotnetCore
7+
```
8+
9+
```powershell
10+
Install-Package JsonApiDotnetCore
11+
```
12+
13+
```xml
14+
<ItemGroup>
15+
<!-- Be sure to check NuGet for the latest version # -->
16+
<PackageReference Include="JsonApiDotNetCore" Version="3.0.0" />
17+
</ItemGroup>
18+
```
19+
20+
## Pre-Release Packages
21+
22+
Occasionally we will release experimental features as pre-release packages on our
23+
MyGet feed. You can download these by adding [the pacakge feed](https://www.myget.org/feed/Details/research-institute) to your nuget configuration.
24+
25+
These releases are deployed from the `develop` branch directly.
26+
27+
```xml
28+
<?xml version="1.0" encoding="utf-8"?>
29+
<configuration>
30+
<packageSources>
31+
<add key="JADNC Pre-Release" value="https://www.myget.org/F/research-institute/api/v3/index.json" />
32+
</packageSources>
33+
</configuration>
34+
```

docs/getting-started/step-by-step.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Step-By-Step Guide to a Running API
2+
3+
The most basic use case leverages Entity Framework.
4+
The shortest path to a running API looks like:
5+
6+
- Create a new web app
7+
- Install
8+
- Define models
9+
- Define the DbContext
10+
- Define controllers
11+
- Add Middleware and Services
12+
- Seed the database
13+
- Run Migrations
14+
- Start the app
15+
16+
This page will walk you through the **simplest** use case. More detailed examples can be found in the detailed usage subsections.
17+
18+
### Create A New Web App
19+
20+
```
21+
mkdir MyApp
22+
cd MyApp
23+
dotnet new webapi
24+
```
25+
26+
### Install
27+
28+
```
29+
dotnet add package JsonApiDotnetCore
30+
31+
- or -
32+
33+
Install-Package JsonApiDotnetCore
34+
```
35+
36+
### Define Models
37+
38+
Define your domain models such that they implement `IIdentifiable<TId>`.
39+
The easiest way to do this is to inherit `Identifiable`
40+
41+
```c#
42+
public class Person : Identifiable
43+
{
44+
[Attr("name")]
45+
public string Name { get; set; }
46+
}
47+
```
48+
49+
### Define DbContext
50+
51+
Nothing special here, just an ordinary `DbContext`
52+
53+
```
54+
public class AppDbContext : DbContext
55+
{
56+
public AppDbContext(DbContextOptions<AppDbContext> options)
57+
: base(options) { }
58+
59+
public DbSet<Person> People { get; set; }
60+
}
61+
```
62+
63+
### Define Controllers
64+
65+
You need to create controllers that inherit from `JsonApiController<TEntity>` or `JsonApiController<TEntity, TId>`
66+
where `TEntity` is the model that inherits from `Identifiable<TId>`
67+
68+
```c#
69+
public class PeopleController : JsonApiController<Person>
70+
{
71+
public PeopleController(
72+
IJsonApiContext jsonApiContext,
73+
IResourceService<Person> resourceService,
74+
ILoggerFactory loggerFactory)
75+
: base(jsonApiContext, resourceService, loggerFactory)
76+
{ }
77+
}
78+
```
79+
80+
### Middleware and Services
81+
82+
Finally, add the services by adding the following to your Startup.ConfigureServices:
83+
84+
```c#
85+
public IServiceProvider ConfigureServices(IServiceCollection services)
86+
{
87+
// add the db context like you normally would
88+
services.AddDbContext<AppDbContext>(options =>
89+
{ // use whatever provider you want, this is just an example
90+
options.UseNpgsql(GetDbConnectionString());
91+
}, ServiceLifetime.Transient);
92+
93+
// add jsonapi dotnet core
94+
services.AddJsonApi<AppDbContext>();
95+
// ...
96+
}
97+
```
98+
99+
Add the middleware to the Startup.Configure method. Note that under the hood,
100+
this will call `app.UseMvc()` so there is no need to add that as well.
101+
102+
```c#
103+
public void Configure(IApplicationBuilder app)
104+
{
105+
app.UseJsonApi();
106+
}
107+
```
108+
109+
### Seeding the Database
110+
111+
One way to seed the database is in your Configure method:
112+
113+
```c#
114+
public void Configure(
115+
IApplicationBuilder app,
116+
AppDbContext context)
117+
{
118+
context.Database.EnsureCreated();
119+
if(context.People.Any() == false)
120+
{
121+
context.People.Add(new Person {
122+
Name = "John Doe"
123+
});
124+
context.SaveChanges();
125+
}
126+
// ...
127+
app.UseJsonApi();
128+
}
129+
```
130+
131+
### Run Migrations
132+
133+
```
134+
dotnet ef migrations add AddPeople
135+
dotnet ef database update
136+
```
137+
138+
### Start the Ap
139+
140+
```
141+
dotnet run
142+
curl http://localhost:5000/people
143+
```
144+

docs/getting-started/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
- name: Introduction
22
href: intro.md
3+
4+
- name: Installation
5+
href: install.md
6+
7+
- name: Step By Step
8+
href: step-by-step.md

docs/index.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,9 @@ Eliminate CRUD boilerplate and provide the following features across all your re
1616
- Sorting
1717
- Pagination
1818
- Sparse field selection
19-
- And more...
20-
21-
As an example, with just the following model and controller definitions, you can service all of the following requests:
22-
23-
```http
24-
GET /articles HTTP/1.1
25-
Accept: application/vnd.api+json
26-
```
27-
28-
[!code-csharp[Article](../src/Examples/GettingStarted/Models/Article.cs)]
29-
30-
[!code-csharp[ArticlesController](../src/Examples/GettingStarted/Controllers/ArticlesController.cs)]
19+
- Checkout the [example requests](request-examples) to see the kind of features you will get out of the box
3120

3221
### 2. Extensibility
3322

3423
This library relies heavily on an open-generic-based dependency injection model which allows for easy per-resource customization.
3524

36-

docs/request-examples/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.json
1+
*.json
2+
*.temp

docs/request-examples/000-CREATE_Article.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
curl -vs http://localhost:5001/api/people \
2+
-H "Accept: application/vnd.api+json" \
3+
-H "Content-Type: application/vnd.api+json" \
4+
-d '{
5+
"data": {
6+
"type": "people",
7+
"attributes": {
8+
"name": "Alice"
9+
}
10+
}
11+
}'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
curl -vs http://localhost:5001/api/articles \
2+
-H "Accept: application/vnd.api+json" \
3+
-H "Content-Type: application/vnd.api+json" \
4+
-d '{
5+
"data": {
6+
"type": "articles",
7+
"attributes": {
8+
"title": "test"
9+
},
10+
"relationships": {
11+
"author": {
12+
"data": {
13+
"type": "people",
14+
"id": "1"
15+
}
16+
}
17+
}
18+
}
19+
}'

docs/request-examples/001-GET_Articles.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/request-examples/002-GET_Article.sh

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl -vs http://localhost:5001/api/articles \
2+
-H "Accept: application/vnd.api+json"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl -vs http://localhost:5001/api/articles/1 \
2+
-H "Accept: application/vnd.api+json"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
curl -vs http://localhost:5001/api/articles?include=author \
2+
-H "Accept: application/vnd.api+json"

docs/request-examples/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Request Examples
2+
3+
To update these requests:
4+
5+
1. Add a bash (.sh) file prefixed by a number that is used to determine the order the scripts are executed. The bash script should execute a request and output the response. Example:
6+
```
7+
curl -vs http://localhost:5001/api/articles \
8+
-H "Accept: application/vnd.api+json"
9+
```
10+
11+
2. Add the example to `index.md`. Example:
12+
```
13+
## Get Article with Author
14+
15+
[!code-sh[GET Request](004-GET_Articles_With_Authors.sh)]
16+
[!code-json[GET Response](004-GET_Articles_With_Authors-Response.json)]
17+
```
18+
19+
3. Run `./generate.sh`
20+
4. Verify the results by running `docfx --serve`

docs/request-examples/index.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22

33
These requests have been generated against the "GettingStarted" application and are updated on every deployment.
44

5+
## Create Person
6+
7+
[!code-sh[CREATE](000-CREATE_Person.sh)]
8+
[!code-json[CREATE](000-CREATE_Person-Response.json)]
9+
510
## Create Article
611

7-
[!code-sh[CREATE](000-CREATE_Article.sh)]
8-
[!code-json[CREATE](CREATE_Article-Response.json)]
12+
[!code-sh[CREATE](001-CREATE_Article.sh)]
13+
[!code-json[CREATE](001-CREATE_Article-Response.json)]
914

1015

1116
## Get All Articles
1217

13-
[!code-sh[GET Request](001-GET_Articles.sh)]
14-
[!code-json[GET Response](GET_Articles-Response.json)]
18+
[!code-sh[GET Request](002-GET_Articles.sh)]
19+
[!code-json[GET Response](002-GET_Articles-Response.json)]
1520

1621
## Get Article By Id
1722

18-
[!code-sh[GET Request](002-GET_Article.sh)]
19-
[!code-json[GET Response](GET_Article-Response.json)]
23+
[!code-sh[GET Request](003-GET_Article.sh)]
24+
[!code-json[GET Response](003-GET_Article-Response.json)]
25+
26+
## Get Article with Author
27+
28+
[!code-sh[GET Request](004-GET_Articles_With_Authors.sh)]
29+
[!code-json[GET Response](004-GET_Articles_With_Authors-Response.json)]

docs/request-examples/toc.yml

Whitespace-only changes.

src/Examples/GettingStarted/Models/Article.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using JsonApiDotNetCore.Models;
2+
23
namespace GettingStarted.Models
34
{
45
public class Article : Identifiable

0 commit comments

Comments
 (0)