How to create a simple Investiment API NET Core 6 for tranning Part.1
Tags: dotnet, core, webdev
Published: 2022-06-25 21:08:05

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/604lzj0oa56lc3y8mxuc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mre4twgr2v0xu9mk8792.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7p1d45gxefem8xq3mog.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsqjxnvahp8qccp5ti98.png)
 

 
###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9j6di6dnqflj2pvjn1g.png)
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y573pp4jlphw37uccali.png)

 
Let's repeat the process by creating the other classes

FixedIncome 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0fxenmmlpdc2r99netc.png)


Fund

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6oqsrq48hh3akou5v9q.png)


InvestmentPortfolio

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27tykynltx1ehs5hl9i9.png)
  
VariableIncome

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxcgzwm7gp86bw3a2wtq.png)
 

###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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xks9i7s2yiqbihbn8v0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75f2uwy3lcfvu1j60vkj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tl6bu771ip1boyra2qjw.png)

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