Skip to content

Latest commit

 

History

History
146 lines (121 loc) · 2.81 KB

File metadata and controls

146 lines (121 loc) · 2.81 KB

RestApiServer

Build status NuGet Badge

General Information

RestApiServer provides simple REST API based on EF DbContext. It is fast and lightweight and also easy to configure.

Setup

  1. Create ASP.NET Core 3.x application.
  2. Create entities and DbContext.
public class Customer
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTimeOffset DateOfBirth { get; set; }
}

public class DemoDbContext : DbContext
{
    public DemoDbContext(DbContextOptions<DemoDbContext> options) : base(options){}

    public DbSet<Customer> Customers { get; set; }
}
  1. Then configure it in Startup.cs.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DemoDbContext>(options =>
    {
        options.UseInMemoryDatabase("memmory-db");
    });
  1. Install nuget package
install-package RestApiServer
  1. Configure RestApiServer
using RestApiServer;
public void ConfigureServices(IServiceCollection services)
{
    // Configure RestApiServer
    services.AddRestApiServer<DemoDbContext>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Use RestApiServer middlewere
    app.UseRestApiServer<DemoDbContext>();
}

You can find full working example here.

Usage

Run web application. REST endpoints will be awailable in this pattern: [HOST]/api/[DBSET_NAME]/[ID]

Get Collection

GET /api/customers

Response
[
    {
        "id": 1,
        "name": "John",
        "dateOfBirth": "1995-05-05"
    }
]

Get Signle

GET /api/customers/1

Response 200 (application/json)
{
    "id": 1,
    "name": "John",
    "dateOfBirth": "1995-05-05"
}

Create

POST /api/customers

Request (application/json)
{
    "name": "Jane",
    "dateOfBirth": "1995-05-05"
}
Response 200 (application/json)
{
    "id": 2,
    "name": "Jane",
    "dateOfBirth": "1995-05-05"
}

Update

PUT /api/customers/2

Request (application/json)
{
    "id": 2,
    "name": "Jane",
    "dateOfBirth": "2000-05-05"
}
Response 200 (application/json)
{
    "id": 2,
    "name": "Jane",
    "dateOfBirth": "2000-05-05"
}

Delete

DELETE /api/customers/2

Response 204

License

MIT License