This repository can be used to create a simple back-end for an online marketplace. Coded in C#, using Swagger and MongoDB for persistent storage.
Create a database using MongoDB within the terminal.
- Open Mongo by typing
mongo
into the terminal - Create a database called Inventory by entering
use Inventory
- Create a table called Products by entering
db.createCollection("Products")
- Optional - seed the database using the following:
db.Products.insertMany(
[
{'Name':'Lavender heart', 'Price':925},
{'Name':'Personalised cufflinks', 'Price':4500},
{'Name':'Kids T-shirt', 'Price':1995}
]
)
Install the following packages to ensure the code compiles:
- Microsoft.Api (v.1.1.4)
- Microsoft.VisualStudio.Web.CodeGeneration.Design (v.3.0.0)
- MongoDB.Driver (v.2.9.3)
- Swashbuckle.AspNetCore.SwaggerGen (v.5.0.0-rc4)
- Swashbuckle.AspNetCore.SwaggerUI (v.5.0.0-rc4)
- With Swagger, it is particularly important to be on version 5, as older versions of Swagger will throw conversion errors on compile.
Build and run and a browser window should open automatically to https://localhost:[port]/product
and show all products stored.
Visiting https://localhost:[port]/swagger/index.html
will provide you with a list of endroutes.
- Remove MongoDB and replace with MySQL / Postgres to allow for relational database mapping (also achievable through Mongoose with MongoDB, but I'm biased towards SQL).
- Product codes as autogenerated unique ids would be easier to manage in a SQL database, as MongoDB generates large ids.