DEV Community

Jéssica Nathany
Jéssica Nathany

Posted on • Updated on

How to create a simple Investiment API NET Core 6 for tranning Part.1

Image description

I create this project to practice a little the code and include some concepts that I study. So was a sample idea, create a sample Investiment API that to return the all investiment the portfolio the customers.

I used the Visual Studio with language C# with .NET 6, but I will create a other article with coding in VSCode, now at the moment the environment this is.

So some rules the our API business investiment consunting, should have endpoints bellow:

  1. List all investiments
  2. Get investiments Fixed Income
  3. Get invetiments Variable Rent
  4. Get invetiments Funds
  5. Investing Fixed Income is a POST
  6. Investing Funds Property is a POST
  7. GET Rescue invetiments

Another rules the user cannot rescue the investiment in less than a month for all and The user cannot investing bonds that. The user cannot investing that does not fit your profile, and for the calculation rules:

  • If the date due date the investiment is than more that six month, so descount twent percent (20%) the yours yield

  • If the date due date the investiment is than more that twelve month, so descount ten percent (10%) the yours yield

  • If the date due date the investiment is equal due date all, so descount five percent (5%) the yours yield.

All ready, let´s the code.

Creating the project

First of all select the directory in your machine and create a solution blank with the name api-fanfareInvest or the name want do you want, image bellow.

Image description

After you have created, inside the solution blank, click right button -> new project and select ASP.NET Core API. In this case, we don't create a minimal API, ´follow the image.

Image description

This a structure basic the our Simple API. Now you can create the pasts: ConfigDependency, Controllers, Data, Model, Response and Service. I will explain later.

Image description

Creating the Models

For our API we need to create some Model class We need this class: (DirectTreasury, FixedIncome, Fund, VariableIncome and InvestimentPortfolio). The last class we will get all investments in the portfolio

The objective here is a create model class for get some investimet, for example I want to get all investiment direct treasury, or get fixed income, get variable income etc. I will mock data this investiment using Json Mock. But at moment, just let´s to create the classes model.

Let´s to create a class DirectTreasury in paste Model.

Image description

Image description

Let's repeat the process by creating the other classes

FixedIncome

Image description

Fund

Image description

InvestmentPortfolio

Image description

VariableIncome

Image description

Creating the Json Mocks

We are using these tools JSON Mocky this link https://designer.mocky.io/design. JSON Mocky is a nice tool because you can create JSON mocks to be consumed in APIs for testing. Bascally the tools generate a response Mock with a URL.

I created the JSON, so I used this website to validate my JSON https://jsoneditoronline.org. The first response we are to create is a Direct Treasury, as shown in the image below.

Creating JSON the response Direct Treasury

Image description

Copy the json and paste it in the field HTTP Response Body the website JSON Mocky, and after click in button Generate in my HTTP Response.

Image description

Image description

I have already prepared the response of the objects, just copy and save in a notepad. The procedure I did was the same, I created a JSON for each class, generated and formatted it in the online JSON Editor and pasted it in the body response of the JSON Mocky site.

Direct Tresuary Response: https://run.mocky.io/v3/52ad684e-30a4-4a45-9c88-3b18169c6137

Fixed Income Response: https://run.mocky.io/v3/ba42dc71-9176-46d0-8ede-a21faeb5b3ba

Fund Response: https://run.mocky.io/v3/7ac36199-db5c-4d21-bc8a-74fe91fa43b1

Variable Response: https://run.mocky.io/v3/4fcc8f18-8ab7-4a36-abc1-f466566e9785

In the next part of this article, I will continue to create the class Repository, Services, Response, and some configuration of the project.

Part2: https://dev.to/jessicanathany/how-to-create-a-simple-investiment-api-net-core-6-for-tranning-part2-1gb1

Discussion (3)

Collapse
mohamadkhaki profile image
محمد خاکى

It was a good training...tnx

Collapse
jessicanathany profile image
Jéssica Nathany Author • Edited on

I am writing the second part of this article.
I hope help you

Collapse
mohamadkhaki profile image
محمد خاکى

Thank you very much🙏