Leatherback Quickstart - Oxidda/Leatherback GitHub Wiki

Prerequisites:

  • Have dotnet 3.1+ installed
  • Have an coding editor like VSCode or VS2019
  • Have MSSQL installed

Full example

The full example is found here.

Quickstart

  1. Make a new WebApi project for this quickstart, this can be done by opening op the terminal and running dotnet new webapi –name Leatherback.Example

    This creates a new dotnet webapi named Leatherback.Example

  2. In the terminal navigate to the newly created project and run dotnet add package Leatherback --version 0.1.0-dev-1592915303

    This will install Leatherback package with the specified version from nuget.

  3. When the installation is done, open up the WebApi in your favorite code editor.

  4. Remove the Controller folder and its contents from the project.

  5. Remove the Weatherforecast.cs from the project.

  6. Create a new folder Models in the project

  7. Add a new class named “Person” to the project.

  8. Add the following properties to person:

public DateTime DateOfBirth {get;set;}
public EmploymentStatus EmplomentStatus {get;set;}
  1. Implement the interface ILeatherbackEntity on person.
  2. Create EmploymentStatus Enum with:
None = 0
Employed = 1
Unemployed = 2
  1. Open up Person.cs again and add the attribute LeatherbackController, set the route to something like "api/person"

The final result of these entities should look something like this:

using Leatherback.Core.Entity;

namespace Leatherback.Example.Models
{
   [LeatherbackController("api/person")]
    public class Person : ILeatherbackEntity
    {
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
        public EmploymentStatus EmploymentStatus { get; set; }
        public Guid Id { get; set; }
    }
}

namespace Leatherback.Example.Models
{
    public enum EmploymentStatus
    {
        None = 0,
        Employed = 1,
        Unemployed = 2
    }
}
  1. Create a new class named LeatherbackExampleDbContext, Inherit LeatherbackDbContext . And add a Property with a DbSet. The DbContext should look something like:
using Leatherback.Example.Models;
using Microsoft.EntityFrameworkCore;

namespace Leatherback.Example.DbContext
{
    public class LeatherbackExampleDbContext : DataAccess.LeatherbackDbContext
    {
        public LeatherbackExampleDbContext(DbContextOptions options) :base(options){ }

        public DbSet<Person> Persons { get; set; }
    }
}
  1. Now we want Leatherback to know what DbContext to use and a connectionstring so add the following line to register Leatherback (Also this registers all the rest that leatherback needs ). services.UseLeatherback<LeatherbackExampleDbContext>("Server=.;Database=LeatherbackExample;Trusted_Connection=True;");

As you can see, it requires a connectionstring. Make sure you make a database on your MSSQL instance and create the database you want your tables in, this example has "LeatherbackExample" .

  1. One more thing to do, we need to create migrations. Since we are using EF here all we need to do is create migrations the EF way (Read more here https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet ) * Make sure the EF toolings are installed, by running: dotnet tool install --global dotnet-ef

  2. Afterwards make sure the Design package for ef is installed, by running : dotnet add package Microsoft.EntityFrameworkCore.Design

  3. So we want to use the migration in EF, this means first create one, by running the following command: dotnet ef migrations add Initial.

  4. Next update the database with the newly created migration by running: dotnet ef database update .

  5. Next simply type dotnet run in your terminal and the api should run.

It usually opens up by default to port 5000, but look in the terminal just in case. Now open up your browser and open http://localhost:5000/api/person/getall It should return an empty array since you didnt add anything yet.

All other endpoints can be found on the endpoints page ,it also includes an Add so some data can actually be added. Or just do a SQL statement yourself and add something and see if it returns data (Which it should)

⚠️ **GitHub.com Fallback** ⚠️