Giter VIP home page Giter VIP logo

parklookupapi.solution's Introduction


ParkLookup Api

___________________________

by Joey Palchak

   

Initiated October 27th, 2023.

Project Docs · Report Bug · Request Feature


Table of Contents


🌐 About the Project

📖 Description

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.

🦠 Known Bugs

  • If any bugs are discovered, please contact the author.

🛠 Technology Used


🏁 Getting Started

📋 Prerequisites

Install .NET Core

  • 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.

Install dotnet-script

In Terminal for macOS or PowerShell for Windows, enter the following command to install the REPL dotnet-script:

$ dotnet tool install -g dotnet-script

Install dotnet-ef

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

Install MySQL Workbench

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.

Install Postman

(Optional) Download and install Postman.

Code Editor

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.

  1. Code Editor Download:
  2. Click the download most applicable to your OS and system.
  3. Wait for download to complete, then install -- Windows will run the setup exe and macOS will drag and drop into applications.
  4. Optionally, create a GitHub Account

⚙️ Setup and Use

Cloning

  1. Navigate to the ParkLookup Api repository here.
  2. Click 'Clone or download' to reveal the HTTPS url ending with .git and the 'Download ZIP' option.
  3. Open up your system Terminal or GitBash, navigate to your desktop with the command: cd Desktop, or whichever location suits you best.
  4. Clone the repository to your desktop: $ git clone https://github.com/jfpalchak/ParkLookupApi.Solution.git
  5. Run the command cd ParkLookupApi.Solution/ParkLookupApi to enter into the project directory.
  6. 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.

Download

  1. Navigate to the ParkLookup Api repository here.
  2. Click 'Clone or download' to reveal the HTTPS url ending with .git and the 'Download ZIP' option.
  3. Click 'Download ZIP' and extract.
  4. Open by double clicking on any of the files to open in a text editor.

AppSettings

  1. Create a new file in the ParkLookupApi project directory named appsettings.json
  2. 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]"
  }
}
  1. 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).
  2. In order to properly implement JSON Web Tokens for API authorization, replace [YOUR-SECRET-HERE] with your own personalized string.
    1. 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.

Database

  1. Navigate to ParkLookupApi.Solution/ParkLookupApi directory using the MacOS Terminal or Windows Powershell (e.g. cd Desktop/ParkLookupApi.Solution/ParkLookupApi).
  2. Run the command dotnet ef database update to generate the database through Entity Framework Core.
  3. (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 command dotnet ef database update to update the database.

Launch the API

  1. Navigate to ParkLookupApi.Solution/ParkLookupApi directory using the MacOS Terminal or Windows Powershell (e.g. cd Desktop/ParkLookupApi.Solution/ParkLookupApi).
  2. Run the command dotnet watch run to have access to the API in Postman or browser.

🛰️ API Documentation

Explore the API endpoints in Postman or a browser. However, take note: you will not be able to utilize authentication in a browser.

Using Swagger Documentation

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

Registering an Account and Using the JSON Web Token

In order to be authorized to use the POST, PUT, and DELETE functionality of the API, please authenticate yourself through Postman:

Registration

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!"
}

Example Query

https://localhost:5000/api/accounts/register

Sample JSON Response

{
    "status": "Success",
    "message": "User has been successfully created."
}

Here's an example of what this should look like in Postman:

Register endpoint 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:

Password-Req error in Postman

Sign In

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!"
}

Example Query

https://localhost:5000/api/accounts/signin

Sample JSON Response

{
    "status": "Success",
    "message": "[email protected] signed in.",
    "token": "xxxx.xxxx.xxxx"
}

Here's an example of what this should look like in Postman:

SignIn endpoint in Postman

Using the JSON Web Token

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:

Using a JSON Web Token in Postman

Until the Token expires, you should now have access to all endpoints requiring user authorization!

Note on Pagination

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.

Example Query

https://localhost:5000/api/parks?pageNumber=1&pageSize=2

To use the defaults, do not include pageNumber or pageSize.

Notes on Adding Search Parameters

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.


API Endpoints

Base URL: https://localhost:5000

HTTP Request Structure

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

Example Query

https://localhost:5000/api/parks/1

Sample JSON Response

{
    "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"
}

..........................................................................................

Parks Controller

Access information on available State and/or National Parks.

..........................................................................................

GET /api/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.

Path Parameters

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.

Example Query

https://localhost:5001/api/parks?category=State&location=Oregon

Sample Successful JSON Response

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
}

..........................................................................................

POST /api/parks

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:

Path Parameters

No parameters.

Example Query

https://localhost:5001/api/parks

Sample JSON Request Body

{
  "name": "Park Name",
  "location": "State",
  "description": "Park Description",
  "category": "State Park"
}

Sample Successful JSON Response

Status: 201 Created

{
  "parkId": 8,
  "name": "Park Name",
  "location": "State",
  "description": "Park Description",
  "category": "State Park"
}

..........................................................................................

GET /api/parks/{id}

Any user may access this GET endpoint of the API. This endpoint returns a single Park entry that matches the given Park ID.

Path Parameters

Parameter Type Default Required Description
id int none true Specify the desired Park according to the given Park ID.

Example Query

https://localhost:5001/api/parks/3

Sample Successful JSON Response

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"
}

..........................................................................................

PUT /api/parks/{id}

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:

Path Parameters

Parameter Type Default Required Description
id int none true Specify the desired Park according to the given Park ID.

Example Query

https://localhost:5001/api/parks/8

Sample JSON Request Body

{
  "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.

Sample Successful JSON Response

Status: 204 No Content

..........................................................................................

DELETE /api/parks/{id}

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:

Path Parameters

Parameter Type Default Required Description
id int none true Specify the desired Park according to the given Park ID.

Example Query

https://localhost:5001/api/parks/8

Sample Successful JSON Response

Status: 204 No Content

..........................................................................................

GET /api/parks/random

Any user may access the Random endpoint of the API. This endpoint returns a single random Park entry from the database.

Path Parameters

No parameters.

Example Query

https://localhost:5001/api/parks/random

Sample Successful JSON Response

Status: 200 OK

{
  "parkId": 6,
  "name": "A Park",
  "location": "State",
  "description": "This is a State Park!",
  "category": "State Park"
}

..........................................................................................

GET /api/parks/search

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.

Path Parameters

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.

Example Query

https://localhost:5001/api/parks/search?searchString=State

Sample Successful JSON Response

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
}

✉️ Contact and Support

If you have any feedback or concerns, please contact one of the contributors.

Report Bug · Request Feature


⚖️ License

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

parklookupapi.solution's People

Contributors

jfpalchak avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.