A C# collaborative project to build an API with C#/ .Net Core, as well as an MVC client side with full CRUD functionality to interact with an API. This API allows users to create a profile to rate, list and review their favorite children's books. Our goal is to create a simple MVC web application for the client side to allow users to GET, POST, PUT, and DELETE books.
These instructions are specifically for MySql Workbench, but should work similarly for or any generic SQL database manager.
-
Navigate to the BookwormAPI.Solution repository or open your terminal
-
Clone this project using the GitHub button or the command:
$ git clone https://github.com/adrienne_matosich/BookwormAPI.Solution.git
-
Navigate to the
BookwormAPI.Solution
directory in your editor of choice, or use Visual Studio Code -
Within the project, navigate to the Factory directory, and Summary
dotnet restore
, thendotnet build
. -
After the build is complete, run the command
dotnet ef database update
. This will create aadrienne_matosich
database in MySql Workbench. Open or refresh MySql Workbench and confirm that the new database has been created. -
. Summary
dotnet run
into the terminal. At this time, the API is usable in an API development tool such as Postman.
As a user, I want to GET and POST children's books. As a user, I want to GET a list of books by category or genre. As a user, I want to PUT and DELETE books.
POST: /api/books - GET: /api/books/{id} - PUT: /api/books/{id} - DELETE: /api/books/{id}
Parameter | Summary | Default | Required | Description |
---|---|---|---|---|
genre | string | none | false | Return matches by genre |
category | string | non-fiction, graphic novel... | false | Return matches by category |
http://localhost:5000/api/books?AgeRange=Oregon&Summary=AgeRange
{ "BookId": 4 "Title": "Bridal Veil Falls", "AgeRange": "Oregon", "Summary": "AgeRange", }
Swagger, also known as OpenAPI, solves the problem of generating useful documentation and help pages for Web APIs. It provides benefits such as interactive documentation, client SDK generation, and API discoverability.
Swashbuckle is a Swagger generator that builds SwaggerDocument objects directly from your routes, controllers, and models. It's typically combined with the Swagger endpoint middleware to automatically expose Swagger JSON.
Install dotnet add <BooksApi>.csproj package Swashbuckle.AspNetCore -v 6.2.3
to begin the process of setting up Swagger.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TodoContext>(opt =>
opt.UseInMemoryDatabase("TodoList"));
services.AddControllers();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
}
In the Startup.Configure
method, enable the middleware for serving the generated JSON document and the Swagger UI:
public void Configure(IApplicationBuilder app)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Launch the Swagger app, and navigate to http://localhost:<yourport>/swagger/v1/swagger.json
. The generated document describing the endpoints appears as shown in Swagger specification (swagger.json).
The Swagger UI can be found at http://localhost:<yourport>/swagger
. Explore the API via Swagger UI and incorporate it in other programs.
Update your userTitle and password in the appsettings.json file. By default these are set to: user:root and an [empty password].
-
In your project's root directory, create an .gitignore file.
-
Add the following to your .gitignore file (this protects your sensitive data). DO NOT PROCEED UNTIL YOU COMPLETE THIS STEP!
- appsettings.json
- bin/
- obj/
-
Commit your .gitignore file.
- CLient Side API
- Authentication with Identity
- Search functionality
WIP progress for the client side API.
If there are any issues or questions, please reach out to me through my GitHub account.
- Visual Studio Code
- Markdown
- C#
- .NET5.0
- ASP.NET Core MVC
- Entity FrameWork
- Postman
- SQL/MySqlWorkbench
- Swagger
- Swashbuckle
This project uses the following license: MIT
Copyright(c) 2022 Adrienne Matosich Rosario Ruvlacaba Ryan Broadsword Sean Julius