The Build a Starchart Web API using ASP.NET Core Application is designed to allow users to submit and retrieve data about celestial objects. This will cover using EntityFramework to retrieve, add, update, and remove data from an in memory database and making it accessible via a web service.
If you want to use Visual Studio (highly recommended) follow the following steps:
- If you already have Visual Studio installed make sure you have .Net Core installed by running the "Visual Studio Installer" and making sure ".NET Core cross-platform development" is checked
- If you need to install visual studio download it at https://www.microsoft.com/net/download/ (If you're using Windows you'll want to check "ASP.NET" and ".NET Core cross-platform development" on the workloads screen during installation.)
- Open the .sln file in visual studio
- To run the application simply press the Start Debug button (green arrow) or press F5
- If you're using Visual Studio on Windows, to run tests open the Test menu, click Run, then click on Run all tests (results will show up in the Test Explorer)
- If you're using Visual Studio on macOS, to run tests, select the StarChartTests Project, then go to the Run menu, then click on Run Unit Tests (results will show up in the Unit Tests panel)
(Note: All tests should fail at this point, this is by design. As you progress through the projects more and more tests will pass. All tests should pass upon completion of the project.)
If you would rather use something other than Visual Studio
- Install the .Net Core SDK from https://www.microsoft.com/net/download/core once that installation completes you're ready to roll!
- To run the application go into the StarChart project folder and type
dotnet run - To run the tests go into the StarChartTests project folder and type
dotnet test
- Configuring MVC and EntityFramework
- Create a web service that provides access to data
- Actions to retrieve data from the database
- Action to Submit data to the database
- Actions to Update existing data
- Action to remove data from the database
Note: this isn't the only way to accomplish this, however; this is what the project's tests are expecting. Implementing this in a different way will likely result in being marked as incomplete / incorrect.
- Adding Middleware/Configuration to
Startup.cs- In the
ConfigureServicesmethod call theAddMvcmethod onservicesto add support for MVC middleware. - In the
ConfigureServicesmethod callAddDbContext<ApplicationDbContext>onserviceswith the argumentoptions => options.UseInMemoryDatabase("StarChart")to pointEntityFrameworkto the application'sDbContext. (Note: You will need to add ausingdirectives forStarChart.DataandMicrosoft.EntityFrameworkCore) - In the
Configuremethod add a call toUseMvcmethod onapp.
- In the
- Create
CelestialObjectModel- Create a new public class
CelestialObjectin theModelsdirectory - Create a new public property of type
intnamedId. - Create a new public property of type
stringnamedName. This property should have theRequiredattribute. (Note: you will need to add ausingdirective forSystems.ComponentModel.DataAnnotations) - Create a new public property of type
int?namedOrbitedObjectId. - Create a new public property of type
List<CelestialObject>namedSatellites. This property should have theNotMappedattribute. (Note: you will need to addusingdirectives forSystem.Collections.GenericandSystem.ComponentModels.DataAnnotations.Schema) - Create a new public property of type
TimeSpannamedOrbitalPeriod. - In the
ApplicationDbContextclass, located in theDatafolder, create a new public property of typeDbSet<CelestialObject>namedCelestialObjects. (Note: you will need to add ausingdirective forStarChart.Models)
- Create a new public class
- Create
CelestialObjectControllerclass- Create a new class
CelestialObjectControllerin theControllersfolder that inherits theControllerBaseclass. If any actions are automatically generated they should be removed. (Note: you will need to add ausingdirective forMicrosoft.AspNetCore.Mvc) - Add a
Routeattribute with a value of""andApiControllerattribute to theCelestialObjectController. - Create a new private readonly field of type
ApplicationDbContextnamed_context. (Note: you will need to add ausingdirective forStarChart.Data) - Create a constructor that accepts a parameter of type
ApplicationDbContextand sets the_contextfield using the provided parameter.
- Create a new class
- Create all
CelestialObjectController's Get actions- Create a new method
GetById- This method should have a return type of
IActionResult - This method should accept a parameter of type
intnamedid. - This method should have an
HttpGetattribute with an value of"{id:int}"and theNameproperty set to"GetById". - This method should return
NotFoundthere is noCelestialObjectwith anIdproperty that matches the parameter. - This method should also set the
Satellitesproperty to anyCelestialObjectswho'sOrbitedObjectIdis the currentCelestialObject'sId. - This method should return an
Okwith a value of theCelestialObjectwho'sIdproperty matches theidparameter.
- This method should have a return type of
- Create the
GetByNamemethod- This method should have a return type of
IActionResult - This method should accept a parameter of type
stringnamedname. - This method should have an
HttpGetattribute with a value of"{name}". - This method should return
NotFoundthere is noCelestialObjectwith anNameproperty that matches thenameparameter. - This method should also set the
Satellitesproperty for eachCelestialObjectwho'sOrbitedObjectIdis the currentCelestialObject'sId. - This method should return an
Okwith a value of the list ofCelestialObjectwho'sNameproperty matches thenameparameter.
- This method should have a return type of
- Create the
GetAllmethod- This method should have a return type of
IActionResult. - This method should also set the
Satellitesproperty for each of theCelestialObjects returned. - This method should have an
HttpGetattribute. - This method should return
Okwith a value of allCelestialObjectss.
- This method should have a return type of
- Create a new method
- Create
CelestialObjectControllers's Post, Put, Patch, and Delete actions- Create the
Createmethod- This method should have a return type of
IActionResult. - This method should accept a parameter of type
[FromBody]CelestialObject. (Note: You will need to add ausingdirective forStarChart.Models) - This method should have an
HttpPostattribute. - This method should add the provided
CelestialObjectto theCelestialObjectsDbSetthenSaveChanges. - This method should return a
CreatedAtRoutewith the arguments"GetById"- A new
objectwith anidof theCelestialObject'sId(note: use thenew { }format) - The newly created
CelestialObject.
- This method should have a return type of
- Create the
Updatemethod- This method should have a return type of
IActionResult. - This method should accept a parameter of type
intnamedidand a parameter of typeCelestialObject. - This method should have the
HttpPutattribute with a value of"{id}". - This method should locate the
CelestialObjectwith anIdthat matches the providedintparameter.- If no match is found return
NotFound. - If a match is found set it's
Name,OrbitalPeriod, andOrbitedObjectIdproperties based on the providedCelestialObjectparameter. CallUpdateon theCelestialObjectsDbSetwith an argument of the updatedCelestialObject, and then callSaveChanges.
- If no match is found return
- This method should return
NoContent.
- This method should have a return type of
- Create the
RenameObjectmethod- This method should have a return type of
IActionResult. - This method should accept a parameter of type
intnamedidand a parameter of typestringnamedname. - This method should have the
HttpPatchattribute with an argument of"{id}/{name}". - This method should locate the
CelestialObjectwith anIdthat matches the providedintparameter.- If no match is found return
NotFound. - If a match is found set it's
Nameproperty to the providednameparameter. Then callUpdateon theCelestialObjectsDbSetwith an argument of the updatedCelestialObject, and then callSaveChanges.
- If no match is found return
- This method should return
NoContent.
- This method should have a return type of
- Create the
Deletemethod- This method should have a return type of
IActionResult - This method should accept a parameter of type
intnamedid. - This method should have the
HttpDeleteattribute with an argument of"{id}". - This method should get a
Listof allCelestialObjects who either have anIdorOrbitedObjectIdthat matches the provided parameter.- If there are no matches it should return
NotFound. - If there are matching
CelestialObjects callRemoveRangeon theCelestialObjectsDbSetwith an argument of the list of matchingCelestialObjects. Then callSaveChanges.
- If there are no matches it should return
- This method should return
NoContent.
- This method should have a return type of
- Create the
You've completed the tasks of this project, if you want to continue working on this project some next steps to consider would be adding authentication to help secure the Web API, refactoring to allow binaries (two objects that orbit each other), implement caching, etc.
Otherwise now is a good time to continue on the ASP.NET Core path to expand your understanding of the ASP.NET Core framework or take a look at the Microsoft Azure for Developers path as Azure is a common choice for hosting, scaling, and expanding the functionality of ASP.NET Core applications.