Skip to content

zorrokid/movies-collection

Repository files navigation

Client

Web client for this project is here: https://github.com/zorrokid/movies-collection-web-client

Build and run ín docker

Build image with tag "movie-api":

$ docker build -f MovieAPI/Dockerfile -t movie-api .

To run in interactive terminal, run container with name "movie-api-container":

$ docker run -it -p 5001:80 --name movie-api-container movie-api

Framework and library dependencies

MovieAPI

This is the main entry point of this backend project providing REST API for this application.

appsettings

To set environment specific appsettings.json export environment variable first, e.g. to use appsettings.Development.json:

$ export ASPNETCORE_ENVIRONMENT=Development

Database

Currently the database is PostgreSQL database.

Connection

Connection string is defined in MovieAPI-project's appsettings.json file.

Creating database

Log in as postgres user:

$ sudo su postgres

Create db user:

$ createuser --interactive --pwprompt
Enter name of role to add: movies
Enter password for new role: xxxxxxx
Enter it again: xxxxxxx
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

Create db with previously created user as owner:

$ createdb --owner=movies moviedb

To login to db:

$ psql -Umovies -dmoviedb

Building project

To build the project run in the root folder

dotnet build

Running migrations

From the project root-folder run:

dotnet ef database update --startup-project ./MovieAPI/ --project ./Infrastructure/ --context IdentityContext

dotnet ef database update --startup-project ./MovieAPI/ --project ./Infrastructure/ --context ApplicationContext

Or use helper script

$ bash migate-db.sh

Adding migrations

Adding migrations for identity db

From the project root-folder run:

dotnet ef migrations add <migration name> --startup-project ./MovieAPI/ --project ./Infrastructure/ --context IdentityContext --output-dir=Migrations/Identity

Adding migrations for app db

From the repository root folder run:

dotnet ef migrations add <migration name> --startup-project ./MovieAPI/ --project ./Infrastructure/ --context ApplicationContext --output-dir=Migrations/Application

Removing migrations

To remove last migration (from repository root folder)

dotnet ef migrations remove --context ApplicationContext --project ./Infrastructure --startup-project ./MovieAPI/ 

Reverting migartions from DB

Reverting to certain migration (from the repository root folder):

dotnet ef database update 20210531183555_Initial --startup-project ./MovieAPI/ --project ./Infrastructure/  --context ApplicationContext

Reverting all migrations (from the repository root folder)

dotnet ef database update 0 --startup-project ./MovieAPI/ --project ./Infrastructure/  --context ApplicationContext

Or use helper script

bash clear-db.sh

Integrations

Import from CSV

Currently only a custom CSV-format is supported. Use CsvImport-project to import collection from CSV.

Run build

dotnet build

Run CsvImport to get help about command line options.

Example:

$ bin/Debug/net5.0/CsvImport -p data/Collections.csv -c /home/mikko/dev/dotnet/movies/MovieAPI/

To use development appsettings, set environment before running CsvImport:

$ export ASPNETCORE_ENVIRONMENT=Development

API

Launching API

To launch API go to MoviesAPI-project folder and run

dotnet run

Swagger is started in following address: https://localhost:5001/swagger/index.html

Adding project

Create project folder inside root folder of repository.

In project folder run

dotnet new <project template name>

e.g. to create a test project

dotnet new mstest

To add project to solution in root folder run:

dotnet sln add <Project name>/<Project name>.csproj 

Referencing project in another project

$ dotnet add CsvImport/CsvImport.csproj reference Application/Application.csproj

Tests

To run tests, in root folder run

dotnet test

Creating local development certificate for HTTPS

Links:

Create root certificate

$ mkcert -install
Created a new local CA 💥
Sudo password:         
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox and/or Chrome/Chromium trust store (requires browser restart)! 🦊

Create certificate for localhost

$ mkcert -CAROOT
/home/mikko/.local/share/mkcert

~/.local/share/mkcert$ mkcert localhost

Created a new certificate valid for the following names 📜
- "localhost"

The certificate is at "./localhost.pem" and the key at "./localhost-key.pem" ✅

It will expire on 12 September 2023 🗓

Set the webpack devserver config for https

devServer: {
    contentBase: './dist',
    hot: true,
    https: {
        key: fs.readFileSync('/home/mikko/.local/share/mkcert/localhost-key.pem'),
        cert: fs.readFileSync('/home/mikko/.local/share/mkcert/localhost.pem'),
        ca: fs.readFileSync('/home/mikko/.local/share/mkcert/rootCA.pem'),
      },
},

Uninstall certificate

~/.local/share/mkcert$ mkcert -uninstall Sudo password:
The local CA is now uninstalled from the system trust store(s)! 👋

Problems

mikko@mikko-Strix-15-GL503GE:~/dev/dotnet/movies/MovieAPI$ dotnet dev-certs https -t Trusting the HTTPS development certificate was requested. Trusting the certificate on Linux distributions automatically is not supported. For instructions on how to manually trust the certificate on your Linux distribution, go to https://aka.ms/dev-certs-trust A valid HTTPS certificate is already present.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages