Skip to content

Commit f17a2cf

Browse files
committed
improve example requests and add step-by-step
1 parent 51e47b8 commit f17a2cf

24 files changed

+335
-43
lines changed

JsonApiDotnetCore.sln

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
33
VisualStudioVersion = 15.0.27130.2010
44
MinimumVisualStudioVersion = 10.0.40219.1
@@ -45,6 +45,8 @@ 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", "{DF9BFD82-D937-4907-B0B4-64670417115F}"
49+
EndProject
4850
Global
4951
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5052
Debug|Any CPU = Debug|Any CPU
@@ -187,6 +189,18 @@ Global
187189
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x64.Build.0 = Release|Any CPU
188190
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.ActiveCfg = Release|Any CPU
189191
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.Build.0 = Release|Any CPU
192+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
193+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.Build.0 = Debug|Any CPU
194+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x64.ActiveCfg = Debug|Any CPU
195+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x64.Build.0 = Debug|Any CPU
196+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x86.ActiveCfg = Debug|Any CPU
197+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x86.Build.0 = Debug|Any CPU
198+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|Any CPU.ActiveCfg = Release|Any CPU
199+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|Any CPU.Build.0 = Release|Any CPU
200+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x64.ActiveCfg = Release|Any CPU
201+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x64.Build.0 = Release|Any CPU
202+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x86.ActiveCfg = Release|Any CPU
203+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x86.Build.0 = Release|Any CPU
190204
EndGlobalSection
191205
GlobalSection(SolutionProperties) = preSolution
192206
HideSolutionNode = FALSE
@@ -205,6 +219,7 @@ Global
205219
{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
206220
{F4097194-9415-418A-AB4E-315C5D5466AF} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
207221
{6DFA30D7-1679-4333-9779-6FB678E48EF5} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
222+
{DF9BFD82-D937-4907-B0B4-64670417115F} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
208223
EndGlobalSection
209224
GlobalSection(ExtensibilityGlobals) = postSolution
210225
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}

docs/generate.sh

+20-6
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

+34
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

+144
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

+6
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

+1-13
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.json
1+
*.json
2+
*.temp

docs/request-examples/000-CREATE_Article.sh

-11
This file was deleted.
+11
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+
}'
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

-2
This file was deleted.

docs/request-examples/002-GET_Article.sh

-2
This file was deleted.
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"
+2
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"
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

+20
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`

0 commit comments

Comments
 (0)