Initiated February 23rd, 2024
The Park Lookup API provides endpoints for managing and accessing information about national parks and state parks across different regions. This RESTful API allows users to perform various operations such as registering and authenticating users, retrieving information about national parks and state parks, adding new parks, updating existing park details, and deleting parks.
The API supports user authentication using JWT (JSON Web Tokens) for secure access to protected endpoints. Users can register for an account, sign in using their credentials, and sign out when done with their session. Tokens last for 60 minutes.
- If any bugs are discovered, please contact the author.
- Visual Studio Code
- C#
- ASP.NET Core MVC
- MySQL 6.0.0 for Windows
- Entity Framework Core 6.0.0
- Entity Framework Core Identity 6.0.0
- Swagger - NSwag 6.5.0
- Postman
- On macOS Mojave or later
- Click here to download the .NET Core SDK from Microsoft Corp for macOS.
- On Windows 10 x64 or later
- Click here to download the 64-bit .NET Core SDK from Microsoft Corp for Windows.
Enter the following command in Terminal for macOS or PowerShell for Windows.
$ dotnet tool install -g dotnet-script
Enter the following command in Terminal for macOS or PowerShell for Windows.
$ dotnet tool install --global dotnet-ef --version 6.0.0
Download and install the appropriate version of MySQL Workbench.
(Optional) Download and install Postman.
To view or edit the code, you will need an code editor or text editor. A popular open-source choice for a code editor is VisualStudio Code.
- Code Editor Download: VisualStudio Code
- 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/tdietzel/ParkLookupAPI
- Run the command
cd ParkLookup
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 unextract.
- Open by double clicking on any of the files to open in a text editor.
- Create a new file in the ParkLookupAPI/ParkLookup directory named
appsettings.json
- Add in the following code snippet to the new appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=YourServerName;database=YourDatabaseName;uid=YourUsername;pwd=YourPassword;"
},
"JwtSettings": {
"ValidIssuer": "ParkLookup-audience",
"ValidAudience": "ParkLookup-issuer",
"SecretKey": "256_BIT_SECRET_REQUIRED"
}
}
- Replace
YourServerName
,YourDatabaseName
,YourUsername
, andYourPassword
with your actual MySQL Server instance details. - Replace the ValidIssuer with the URL where your authentication server is hosted, and ValidAudience with the URL where your API server is hosted. For instance:
"ValidIssuer": "https://localhost:7050",
"ValidAudience": "http://localhost:5208",
- Make sure the
SecretKey
is atleast 16 characters long.
- Navigate to ParkLookupAPI/ParkLookup directory using the MacOS Terminal or Windows Powershell (e.g.
cd Desktop/ParkLookupAPI/ParkLookup
). - 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. After, run the previous commanddotnet ef database update
to update the database.
- Navigate to ParkLookupAPI/ParkLookup directory using the MacOS Terminal or Windows Powershell (e.g.
cd Desktop/ParkLookupAPI/ParkLookup
). - Run the command
dotnet run
to have access to the API in Postman or browser.
Explore the API endpoints in Postman or a browser. You will not be able to utilize authentication in a browser.
To explore the Park Lookup API with NSwag, launch the project using dotnet run
with the Terminal or Powershell, and input the following URL into your browser: https://localhost:7050/swagger/index.html
In order to be authorized to use the POST, PUT, DELETE functionality of the API, please authenticate yourself through Postman.
- Open Postman and create a POST request using the URL:
https://localhost:7050/api/Accounts/register
- Add the following query to the request as raw data in the Body tab:
{
"email": "user@example.com",
"userName": "string",
"password": "string",
"confirmPassword": "string"
}
- The password must contain at least
six characters
,one non-alphanumeric character
, at leastone digit lowercase letter
, at leastone uppercase letter
and at leasttwo unique characters
.
Now that you've registered an account with the API, you'll need to authenticate your account and generate the JSON Web Token. I'll be using Postman again for this example.
Let's setup another POST request using the URL: https://localhost:7050/api/Accounts/signin
- Add the following query to the request as raw data in the Body tab:
{
"email": "user@example.com",
"password": "string"
}
- Successfully logging in will generate a token in the response.
Copy the token from the response, and add it as an authorization header to your POST, PUT or DELETE query. On the authorization 'Type', make sure that is set to 'Bearer Token', and then paste in the token in the field on the right.
CORS is a W3C standard that allows a server to relax the same-origin policy. It is not a security feature, CORS relaxes security. It allows a server to explicitly allow some cross-origin requests while rejecting others. An API is not safer by allowing CORS. For more information or to see how CORS functions, see the Microsoft documentation.
Base URL: https://localhost:7050
๐๏ธNational | |
---|---|
GET | /national/park |
POST | /national/park |
PUT | /national/park/{id} |
DELETE | /national/park/{id} |
๐๏ธState | |
---|---|
GET | /state/park |
GET | /state/{id} |
POST | /state/park |
PUT | /state/park/{id} |
DELETE | /state/park/{id} |
Accounts | |
---|---|
POST | /api/accounts/register |
POST | /api/accounts/signin |
POST | /api/accounts/logout |
Access information on available State Parks.
Any user may access this GET
endpoint of the API. This endpoint returns a list of available state parks in the database.
https://localhost:7050/state/park
Status: 200 OK
{
"stateId": 1,
"name": "Alabama",
"parks": [
{
"stateParkId": 2,
"stateId": 1,
"parkName": "Blue Springs"
},
{
"stateParkId": 1,
"stateId": 1,
"parkName": "Bladon Springs"
}
]
}
Any user may access this GET
endpoint of the API. This endpoint returns a list of available state parks in the database according to the state id entered.
https://localhost:7050/state/1
Status: 200 OK
[
{
"stateParkId": 4,
"stateId": 3,
"parkName": "Alamo Lake"
}
]
Authenticated users, when including their Token in the authorization header of the request, may POST new state park entries to the database when using the following format:
https://localhost:7050/state/park
{
"stateId": stateId,
"parkName": "string"
}
NOTE: When sending a
POST
request, there's no need to enter a stateParkId because its set up to be the auto incrementing primary key.
Status: 201 Created
{
"stateParkId": AI PK,
"stateId": stateId,
"parkName": "string"
}
Authenticated users, when including their Token in the authorization header of the request, may PUT state park entries already in the database when using the following format:
https://localhost:7050/state/park/{id}
{
"stateParkId": parkId,
"stateId": stateId,
"parkName": "string"
}
Status: 204 No Content
Authenticated users, when including their Token in the authorization header of the request, may DELETE state park entries already in the database when using the following format:
https://localhost:7050/state/park/{id}
NOTE: When sending a
DELETE
request, the Park's ID in the query is the only thing required.
Status: 204 No Content
Access information on available National Parks.
Any user may access this GET
endpoint of the API. This endpoint returns a list of available national parks in the database.
https://localhost:7050/national/park
Status: 200 OK
{
"nationalId": 1,
"name": "National Parks Conservation Association",
"parks": [
{
"nationalParkId": 1,
"nationalId": 1,
"parkName": "Death Valley"
},
{
"nationalParkId": 2,
"nationalId": 1,
"parkName": "Grand Canyon"
}
]
}
Authenticated users, when including their Token in the authorization header of the request, may POST new national park entries to the database when using the following format:
https://localhost:7050/national/park
{
"nationalId": 1,
"parkName": "string"
}
NOTE: When sending a
POST
request, there's no need to enter a nationalParkId because its set up to be the auto incrementing primary key.
Status: 201 Created
{
"nationalParkId": 3,
"nationalId": 1,
"parkName": "string"
}
Authenticated users, when including their Token in the authorization header of the request, may PUT national park entries already in the database when using the following format:
https://localhost:7050/national/park/{id}
{
"nationalParkId": parkId,
"nationalId": stateId,
"parkName": "string"
}
Status: 204 No Content
Authenticated users, when including their Token in the authorization header of the request, may DELETE national park entries already in the database when using the following format:
https://localhost:7050/national/park/{id}
NOTE: When sending a
DELETE
request, the Park's ID in the query is the only thing required.
Status: 204 No Content
Author | GitHub | |
---|---|---|
Trent Dietzel | tdietzel | dietzelbiz@outlook.com |
If you have any feedback or concerns, please contact Trent Dietzel at dietzelbiz@outlook.com.
This project is licensed under the MIT License. Copyright (C) 2024 Trent Dietzel. All Rights Reserved.
MIT License
Copyright (c) 2024 Trent Dietzel.
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.