Skip to content

swiftsoftwaregroup/swift-api-rest-cs

Repository files navigation

swift-api-rest-cs

REST Web API using C# and ASP.NET Core

Setup

Run

Configure project:

source configure.sh

Run:

./watch.sh

Browse the docs and test the API via the Swagger UI:

open http://localhost:5000/docs

swagger-ui

Browse the docs using Redoc. This is an alternative to the Swagger UI:

open http://127.0.0.1:5000/redoc

redoc-ui

Test

Run unit tests:

dotnet test

Database Migrations

If you want to use migrations

Add first migration and initialize the database:

dotnet ef --project swift-api migrations add InitialCreate

Create database:

dotnet ef --project swift-api database update 

Point .env to the new database:

echo 'DATABASE_URL=sqlite:///./swift-api/books.db' > .env

Run the application:

dotnet run

Run in Podman / Docker

In order to do this you will need Podman. See Setup Podman on macOS for details.

Rebuild container image and start container:

./podman.sh

Delete container and image:

./podman-delete.sh

How to create a new project

Activate dotnet:

export DOTNET_ROOT=$HOME/.dotnet
export PATH="$HOME/.dotnet:$PATH"

Create .net solution

dotnet new sln --name swift-api

Create C# project

# add web project
dotnet new web --name swift-api --framework net8.0
dotnet sln add swift-api

# add packages
dotnet add swift-api package DotNetEnv
dotnet add swift-api package Microsoft.EntityFrameworkCore
dotnet add swift-api package Microsoft.EntityFrameworkCore.Design
dotnet add swift-api package Microsoft.EntityFrameworkCore.Sqlite

dotnet add swift-api package Microsoft.AspNetCore.OpenApi
dotnet add swift-api package Swashbuckle.AspNetCore

# add test project
dotnet new xunit --name swift-api-tests
dotnet sln add swift-api-tests

dotnet add swift-api-tests package Microsoft.EntityFrameworkCore.InMemory
dotnet add swift-api-tests package Microsoft.AspNetCore.Mvc.Testing

# add reference to the main project
dotnet add swift-api-tests reference swift-api

# add tools
dotnet tool install --global dotnet-ef

About

REST Web API using C# and ASP.NET Core

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published