___________________________
Initiated October 27th, 2023.
Project Docs · Report Bug · Request Feature
The ParkLookup API, true to its name, helps users look through lists of both State and National parks across the United States. This API utilizes RESTful principles, as well as Json Web Tokens (JWT) for authentication & authorization to keep the API Read-Only by default.
Authenticated users have access to POST
, PUT
, and DELETE
functionality throughout the API.
Any and all users have access to all available GET
functionality throughout the API.
Any endpoint in the API that returns a list of Parks utilizes Pagination.
- If any bugs are discovered, please contact the author.
- Visual Studio Code
- C#
- ASP.NET Core MVC
- MySQL 8.0.34
- Entity Framework Core 6.0.0
- Entity Framework Core CLI Tools 6.0.0
- Entity Framework Core Identity 6.0.0
- Swagger - NSwag 13.3.0
- Postman
- On macOS with Apple Chip:
- Click here to download the .NET Core SDK from Microsoft Corp for macOS.
- On macOs with Intel Chip:
- Click here to download the .NET Core SDK from Microsoft Corp for macOS.
- On Windows:
- Click here to download the 64-bit .NET Core SDK from Microsoft Corp for Windows.
In Terminal for macOS or PowerShell for Windows, enter the following command to install the REPL dotnet-script:
$ dotnet tool install -g dotnet-script
For Entity Framework Core, we'll use a tool called dotnet-ef to reference the project's migrations and update our database accordingly. To install this tool globally, run the following command in your terminal:
$ dotnet tool install --global dotnet-ef --version 6.0.0
Optionally, you can run the following command to verify that EF Core CLI tools are correctly installed:
$ dotnet ef
This project assumes you have MySQL Server and MySQL Workbench installed on your system. If you do not have these tools installed, follow along with the installation steps for the the necessary tools introduced in the series of lessons found here on LearnHowToProgram.
Or, Download and install the appropriate version of MySQL Workbench.
(Optional) Download and install Postman.
To view or edit the code, you will need a code editor or text editor. A popular open-source choice for a code editor is VisualStudio Code.
- Code Editor Download:
- Click the download most applicable to your OS and system.
- Wait for download to complete, then install -- Windows will run the setup exe and macOS will drag and drop into applications.
- Optionally, create a GitHub Account
- Navigate to the ParkLookup Api repository here.
- Click 'Clone or download' to reveal the HTTPS url ending with .git and the 'Download ZIP' option.
- Open up your system Terminal or GitBash, navigate to your desktop with the command:
cd Desktop
, or whichever location suits you best. - Clone the repository to your desktop:
$ git clone https://github.com/jfpalchak/ParkLookupApi.Solution.git
- Run the command
cd ParkLookupApi.Solution/ParkLookupApi
to enter into the project directory. - View or Edit:
- Code Editor - Run the command
code .
to open the project in VisualStudio Code for review and editing. - Text Editor - Open by double clicking on any of the files to open in a text editor.
- Code Editor - Run the command
- Navigate to the ParkLookup Api repository here.
- Click 'Clone or download' to reveal the HTTPS url ending with .git and the 'Download ZIP' option.
- Click 'Download ZIP' and extract.
- Open by double clicking on any of the files to open in a text editor.
- Create a new file in the ParkLookupApi project directory named
appsettings.json
- Add in the following code snippet to the new
appsettings.json
file:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Port=3306;database=park_lookup_api;uid=[YOUR-USERNAME-HERE];pwd=[YOUR-PASSWORD-HERE];"
},
"JWT": {
"ValidAudience": "example-audience",
"ValidIssuer": "example-issuer",
"Secret": "[YOUR-SECRET-HERE]"
}
}
- Change the server, port, and user id as necessary. Replace
[YOUR-USERNAME-HERE]
and[YOUR-PASSWORD-HERE]
with your personal MySQL username and password (set at installation of MySQL). - In order to properly implement JSON Web Tokens for API authorization, replace
[YOUR-SECRET-HERE]
with your own personalized string.- NOTE: The
Secret
is a special string that will be used to encode our JWTs, to make them unique to our application. Depending on what type of algorithm being used, the Secret string will need to be a certain length. In this case, it needs to be at least 16 characters long.
- NOTE: The
- Navigate to ParkLookupApi.Solution/ParkLookupApi directory using the MacOS Terminal or Windows Powershell (e.g.
cd Desktop/ParkLookupApi.Solution/ParkLookupApi
). - Run the command
dotnet ef database update
to generate the database through Entity Framework Core. - (Optional) To update the database with any changes to the code, run the command
dotnet ef migrations add <MigrationsName>
which will use Entity Framework Core's code-first principle to generate a database update. 4) After, run the previous commanddotnet ef database update
to update the database.
- Navigate to ParkLookupApi.Solution/ParkLookupApi directory using the MacOS Terminal or Windows Powershell (e.g.
cd Desktop/ParkLookupApi.Solution/ParkLookupApi
). - Run the command
dotnet watch run
to have access to the API in Postman or browser.
Explore the API endpoints in Postman or a browser. However, take note: you will not be able to utilize authentication in a browser.
To explore the ParkLookup Api with NSwag, launch the project using dotnet run
with the Terminal or Powershell, and input the following URL into your browser: http://localhost:5000/swagger
In order to be authorized to use the POST
, PUT
, and DELETE
functionality of the API, please authenticate yourself through Postman:
Again, we'll be using Postman for this example. Let's setup a POST
request to the accounts/register
endpoint. Select the 'Body' tab, choose the 'raw' radio button, and select 'JSON' from the dropdown selection.
In the Body of the Post request, use the following format:
{
"email": "[email protected]",
"userName": "testUser",
"password": "Password123!"
}
https://localhost:5000/api/accounts/register
{
"status": "Success",
"message": "User has been successfully created."
}
Here's an example of what this should look like in Postman:
Note that the password must contain at least six characters, one non-alphanumeric character, at least one digit lowercase letter, at least one uppercase letter and at least two unique characters. An invalid password will generate the following response from the API:
Now that we've registered an account with our API, we'll need to authenticate our account and generate a JSON Web Token. We'll be using Postman again for this example.
Let's setup another POST
request to the accounts/signin
endpoint. Select the 'Body' tab, choose the 'raw' radio button, and select 'JSON' from the dropdown selection.
In the Body of the Post request, use the following format:
{
"email": "[email protected]",
"password": "Password123!"
}
https://localhost:5000/api/accounts/signin
{
"status": "Success",
"message": "[email protected] signed in.",
"token": "xxxx.xxxx.xxxx"
}
Here's an example of what this should look like in Postman:
Now let's copy that token from the response, and add it as an authorization header to our next request. Copy the token from the body, and click on the Authorization tab in Postman. On the 'Type', make sure that is set to 'Bearer Token', and then paste in the token in the field on the right.
Here's an example of what that should look like in Postman:
Until the Token expires, you should now have access to all endpoints requiring user authorization!
For certain endpoints, the ParkLookup Api returns a default of 10 results per page at a time, which is also the maximum number of results possible.
To modify this, use the query parameters pageSize
and pageNumber
to alter the response results displayed. The pageSize
parameter will specify how many results will be displayed, and the pageNumber
parameter will specify which element in the response the limit should start counting.
https://localhost:5000/api/parks?pageNumber=1&pageSize=2
To use the defaults, do not include pageNumber
or pageSize
.
When adding more than one search parameter to an endpoint query, be sure to include an &
between search parameters, as shown in the example query on pagination just above.
Base URL: https://localhost:5000
Parks | |
---|---|
GET | /api/parks |
POST | /api/parks |
GET | /api/parks/{id} |
PUT | /api/parks/{id} |
DELETE | /api/parks/{id} |
GET | /api/parks/random |
GET | /api/parks/search |
Accounts | |
---|---|
POST | /api/accounts/register |
POST | /api/accounts/signin |
https://localhost:5000/api/parks/1
{
"parkId": 1,
"name": "Acadia National Park",
"location": "Maine",
"description": "Today, Acadia preserves about 40,000 acres of Atlantic coast shoreline, mixed hardwood and spruce/fir forest, mountains, and lakes, as well as several offshore islands.",
"category": "National Park"
}
..........................................................................................
Access information on available State and/or National Parks.
..........................................................................................
Any user may access this GET
endpoint of the API. This endpoint returns a paginated list of available Parks in the database.
NOTE: By default, this endpoint returns a list of 10 Parks per page, starting from page 1. To continue searching through the available parks, make sure to change the pageNumber
parameter to search through each consecutive page available.
The total number of pages available, totalPages
, as well as reference to the user's position in the available page index as noted by hasPreviousPage
and hasNextPage
, will be marked in the body of the initial JSON Response, as shown below.
Parameter | Type | Default | Required | Description |
---|---|---|---|---|
category | string | none | false | Return matches by category. Must be either 'State' or 'National'. |
location | string | none | false | Return any Park found in the specified location. |
pageNumber | int | 1 | false | Specifies which element in the response the pageSize limit should start counting from; default is the initial index of available elements. |
pageSize | int | 10 | false | Returns the specified number of elements per response; default is 10 elements. |
https://localhost:5001/api/parks?category=State&location=Oregon
Status: 200 OK
{
"pageNumber": 1,
"pageSize": 10,
"totalPages": 1,
"hasPreviousPage": false,
"hasNextPage": false,
"data": [
{
"parkId": 3,
"name": "Cape Kiwanda",
"location": "Oregon",
"description": "This sandstone headland just north of Pacific City offers one of the best viewpoints on the coast for witnessing the ocean's power. The landmark is one of three along the Three Capes Scenic Route (along with Cape Meares and Cape Lookout).",
"category": "State Park"
}
],
"succeeded": true,
"errors": null,
"message": null
}
..........................................................................................
Authenticated users, while including their Token in the authorization header of the request, may POST
new Park entries to the database when using the following format:
No parameters.
https://localhost:5001/api/parks
{
"name": "Park Name",
"location": "State",
"description": "Park Description",
"category": "State Park"
}
Status: 201 Created
{
"parkId": 8,
"name": "Park Name",
"location": "State",
"description": "Park Description",
"category": "State Park"
}
..........................................................................................
Any user may access this GET
endpoint of the API. This endpoint returns a single Park entry that matches the given Park ID.
Parameter | Type | Default | Required | Description |
---|---|---|---|---|
id | int | none | true | Specify the desired Park according to the given Park ID. |
https://localhost:5001/api/parks/3
Status: 200 OK
{
"parkId": 3,
"name": "Cape Kiwanda",
"location": "Oregon",
"description": "This sandstone headland just north of Pacific City offers one of the best viewpoints on the coast for witnessing the ocean's power. The landmark is one of three along the Three Capes Scenic Route (along with Cape Meares and Cape Lookout).",
"category": "State Park"
}
..........................................................................................
Authenticated users, while including their Token in the authorization header of the request, may PUT
updates for Park entries in the database when using the following format:
Parameter | Type | Default | Required | Description |
---|---|---|---|---|
id | int | none | true | Specify the desired Park according to the given Park ID. |
https://localhost:5001/api/parks/8
{
"parkId": 8,
"name": "NEW Park Name",
"location": "State",
"description": "Park Description",
"category": "State Park"
}
NOTE: When sending a
PUT
request, the Park's ID is required in the body of the request.
Status: 204 No Content
..........................................................................................
Authenticated users, while including their Token in the authorization header of the request, may DELETE
specific Park entries in the database when using the following format:
Parameter | Type | Default | Required | Description |
---|---|---|---|---|
id | int | none | true | Specify the desired Park according to the given Park ID. |
https://localhost:5001/api/parks/8
Status: 204 No Content
..........................................................................................
Any user may access the Random
endpoint of the API. This endpoint returns a single random Park entry from the database.
No parameters.
https://localhost:5001/api/parks/random
Status: 200 OK
{
"parkId": 6,
"name": "A Park",
"location": "State",
"description": "This is a State Park!",
"category": "State Park"
}
..........................................................................................
Any user may access the Search
endpoint of the API. This endpoint returns a paginated list of Park results that match the content of the user's search parameter.
NOTE: By default, this endpoint returns a list of 10 Parks per page, starting from page 1. To continue searching through the query results, make sure to change the pageNumber
parameter to search through each consecutive page available.
The total number of pages available, totalPages
, as well as reference to the user's position in the available page index as noted by hasPreviousPage
and hasNextPage
, will be marked in the body of the initial JSON Response, as shown below.
Parameter | Type | Default | Required | Description |
---|---|---|---|---|
searchString | string | none | false | Returns a list of Park results that contains the content of the searchString in either their Name, Location, or Category. |
pageNumber | int | 1 | false | Specifies which element in the response the pageSize limit should start counting from; default is the initial index of available elements. |
pageSize | int | 10 | false | Returns the specified number of elements per response; default is 10 elements. |
https://localhost:5001/api/parks/search?searchString=State
Status: 200 OK
{
"pageNumber": 1,
"pageSize": 10,
"totalPages": 1,
"hasPreviousPage": false,
"hasNextPage": false,
"data": [
{
"parkId": 3,
"name": "Cape Kiwanda",
"location": "Oregon",
"description": "This sandstone headland just north of Pacific City offers one of the best viewpoints on the coast for witnessing the ocean's power. The landmark is one of three along the Three Capes Scenic Route (along with Cape Meares and Cape Lookout).",
"category": "State Park"
},
{
"parkId": 6,
"name": "A Park",
"location": "Somewhere",
"description": "This is a park, too!",
"category": "State Park"
}
],
"succeeded": true,
"errors": null,
"message": null
}
If you have any feedback or concerns, please contact one of the contributors.
This project is licensed under the MIT License. Copyright (C) 2023 Joey Palchak. All Rights Reserved.
MIT License
Copyright (c) 2023 Joey Palchak.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Return to Top