Skip to content

jaurund/learning-project-for-RESTAPI-OAuth-SQL-Container

Repository files navigation

Netflix REST API

This is my learning project for exploring REST APIs, SQL Server, and C#, and hosting it all in containers.


Run with Docker Desktop (recommended)

Prerequisites:

  • Docker Desktop installed and running
  • Project root contains Dockerfile and docker-compose.yaml

Commands (run in project root):

# Build images
docker compose build

# Start containers in background
docker compose up -d

# See running services and ports
docker compose ps

# Tail logs (choose a service: api | db | frontend)
docker compose logs -f api

Default URLs (check with docker compose ps if you changed ports):

Stop and clean up:

# Stop and remove containers, network (keeps DB volume)
docker compose down

# Optional: remove persisted DB data volume to start fresh
docker volume rm restapi_sql_data

Notes:

  • If Docker warns that version in docker-compose.yaml is obsolete, remove the version: line.
  • API listens on port 8080 inside the container; docker-compose publishes it to your host (5000 by default in this project).
  • Connection string is injected via environment variables in docker-compose.

Local run (without Docker)

dotnet restore src/RestApi.csproj
dotnet build src/RestApi.csproj
dotnet run --project src/RestApi.csproj

Open http://localhost:5000/swagger.


Setting up the Database (manual/local)

See the Database section below if you prefer running SQL Server outside Docker.


GitHub OAuth Setup

  1. Register an OAuth app on GitHub.
  2. Copy secrets into the secrets/ folder (not committed).
  3. Ensure your callback URL matches your environment/ports.

Endpoints

  • GET /MyTable
  • GET /MyTable/title?title=...
  • GET /MyTable/director?director=...

Deploying to a Server (Hetzner) with Docker Compose

On the server:

# Install Docker/Compose (Ubuntu)
sudo apt update
sudo apt install -y docker.io docker-compose
sudo systemctl enable --now docker

# Copy project to server, then in project root:
sudo docker compose up -d
sudo docker compose ps

What does it do?

Connects to a SQL Server database and exposes the data as a JSON API. The current code uses the provided .csv and .sql script with the database hosted locally by an SQL manager, but the code can easily be changed to run with other databases. You can query titles, directors, cast, descriptions and so on through the frontend — if it's in the table and has a controller, it's accessible.


How do I use it?

git clone [URL]
dotnet restore
dotnet run

Open your browser at http://localhost:5000/swagger for interactive docs.


Setting up the Database

  1. Create the database:

    • Open SSMS (or your preferred SQL client).
    • Right-click "Databases" > "New Database..." and name it csvNetflixDB.
    • Click OK to create the database.
  2. Create the table:

    • Select the new csvNetflixDB database.
    • Open the provided CreateTable.sql script in the Database/SQLscripts/ folder.
    • Run the script to create the NetflixDataset table.
  3. Import the CSV data:

    • In SSMS, right-click the csvNetflixDB database and choose "Tasks" > "Import Data".
    • Select the Netflix Dataset.csv file as the source from the Database/ folder in this project.
    • Map columns as needed and complete the wizard.

    Alternatively, use a BULK INSERT command:

    BULK INSERT dbo.NetflixDataset
    FROM 'C:\Path\To\Netflix Dataset.csv'
    WITH (
        FIRSTROW = 2,
        FIELDTERMINATOR = ',',
        ROWTERMINATOR = '\n',
        TEXTQUALIFIER = '"'
    );

Note: If your SQL Server instance name or authentication differs, update the connection string in appsettings.json accordingly.


Using the Frontend

Start the backend API by running:

dotnet restore src/RestApi.csproj
dotnet build src/RestApi.csproj
dotnet run --project src/RestApi.csproj

in the integrated terminal.

Then, open frontend/index.html in your browser.

Use the search forms to query the database by title, director, or cast.
Results will be displayed on the page in JSON format.

Make sure the backend is running before using the frontend.


GitHub OAuth Setup

  1. Register your own GitHub OAuth app at https://github.com/settings/developers.
  2. Rename the TEMPLATE secrets/-folder to secrets/. The program is configured with the path of that folder.
  3. Fill in your own Client ID and Client Secret in secrets.txt.
  4. Make sure secret.txt is not committed to git. The .gitignore is part of the repository, so by default it will not be committed.
  5. Either make sure that Live Server or the extension you are using listens to port 5504, or change to a port of choice in the code.

Endpoints

  • GET /MyTable — All data, all at once.
  • GET /MyTable/title?title=Stranger Things — Find a show by title.
  • GET /MyTable/director?director=John Doe — Search by director.
  • More endpoints? Add them in src/Controllers/MyTableController.cs.

Requirements

  • .NET 9 SDK
  • The database itself (.csv and .sql-script to import it is included in the repository)
  • A program to host the SQL DataBase locally, like Microsoft SQL Server Management Studio (SSMS) or PostgreSQL
  • I use VScode as the IDE to build the project. If you are inexperienced, I recommend you using the same if you want to clone the repo.

NuGet Packages

These are automatically restored from the project file, but for reference:

  • Microsoft.AspNetCore.OpenApi
  • Microsoft.EntityFrameworkCore.SqlServer
  • Swashbuckle.AspNetCore

Recommended VS Code Extensions

  • C# Dev Kit (for C# language support and debugging)
  • C# (by Microsoft)
  • SQL Server (mssql) (for connecting to and managing SQL Server databases)
  • REST Client (for testing API endpoints directly in VS Code)
  • .NET Install Tool for Extension Authors (helps with .NET SDK management)

Contributing

Open a pull request, file an issue, or fork away.
This API is built to be extended, and not only by me. I will be very happy if I see forks with comments to help me learn and improve as a developer.

About

This is a repository with a personal project that is combining recent subjects I have been through and / or is currently working on during my time here at KodeHode.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors