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 GradeBookTests 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
ConfigureServices
method callAddMvc
to add support for MVC middleware. - In the
Configure
method remove theapp.Run
entirely and replace it with a call toUseMvc
onapp
. - In the
ConfigureServices
method callAddDbContext<ApplicationDbContext>
onservices
with the argumentoptions => options.UseInMemoryDatabase("StarChart")
to pointEntityFramework
to the application'sDbContext
. (Note: You will need to add ausing
directive forStartChart.Data
)
- In the
- Create
CelestialObject
Model- Create a new class
CelestialObject
in theModels
directory - Create a new property of type
int
namedId
. - Create a new property of type
string
namedName
. This property should have theRequired
attribute. (Note: you will need to add ausing
directive forSystems.ComponentModel.DataAnnotations
) - Create a new property of type
CelestialObject
namedOrbitedObject
. - Create a new property of type
List<CelestialObject>
namedSatellites
. This property should have theNotMapped
attribute. (Note: you will need to add using directives forSystem.Collections.Generic
andSystem.ComponentModels.DataAnnotations.Schema
) - Create a new property of type
TimeSpan
namedOrbitalPeriod
.
- Create a new class
- Create
CelestialObjectController
class- Create a new class
CelestialObjectController
that inherits theControllerBase
class (Note: you will need to add ausing
directive forMicrosoft.AspNetCore.Mvc
) - Add a
route
attribute with a value ofstring.empty
andApiController
attribute to theCelestialObjectController
. - Create a new private field of type
ApplicationDbContext
named_context
. - Create a constructor that accepts a parameter of type
ApplicationDbContext
and sets the_context
field using the provided parameter.
- Create a new class
- Create
GetById
method inCelestialObjectController
- Create a new method
GetById
with a return type ofIActionResult
that accepts a parameter of typeint
namedid
. This method should have anHttpGet
attribute with a value of"{id}"
. This method should return anOk
with a value of theCelestialObject
who'sId
property matches theid
parameter. -
GetById
should returnNotFound
there is noCelestialObject
with anId
property that matches the parameter. -
GetById
should also set theSatellites
property to anyCelestialObjects
who'sOrbitedObject
is the currentCelestialObject
.
- Create a new method
- Create the
GetByName
method- Create a new method
GetByName
with a return type ofIActionResult
that accepts a parameter of typestring
namedname
. This method should have anHttpGet
attribute with a value of"{name}"
. This method should return anOk
with a value of theCelestialObject
who'sName
property matches thename
parameter. -
GetByName
should returnNotFound
there is noCelestialObject
with anName
property that matches thename
parameter. -
GetByName
should also set theSatellites
property to anyCelestialObjects
who'sOrbitedObject
is the currentCelestialObject
.
- Create a new method
- Create the
GetAll
method- Create a new method
GetAll
with a return type ofIActionResult
and no parameters. This method should have anHttpGet
attribute. This method should returnOk
with a value of allCelestialObjects
s. -
GetAll
should also set theSatellites
property for eachCelestialObject
returned (as we've done in the last twoGet
methods).
- Create a new method
- Create the
Create
method- Create a new method
Create
with a return type ofIActionResult
that accepts a parameter of type[FromBody]CelestialObject
. This method should have anHttpPost
attribute. This method should return aCreatedAtRoute
with the arguments"GetById"
, a newobject
with anid
of theCelestialObject
'sId
, and the newly createdCelestialObject
. -
Create
should check if theModelState
is valid, if not it should return aBadRequest
. - If the
ModelState
is valid,Create
should add the providedCelestialObject
to theCelestialObjects
DbSet
thenSaveChanges
.
- Create a new method
- Create the
Update
method- Create a new method
Update
with a return type ofIActionResult
that accepts a parameter of typeint
namedid
and a parameter of typeCelestialObject
. This method should have theHttpPut
attribute with a value of"{id}"
. This method should returnNoContent
. -
Update
should locate theCelestialObject
with anId
that matches the providedint
parameter. If no match is found returnNotFound
. - If a match is found set it's
Name
,OrbitalPeriod
,OrbitedObject
, andSatellites
properties based on the providedCelestialObject
parameter. CallUpdate
on theCelestialObjects
DbSet
with an argument of the updatedCelestialObject
, and then callSaveChanges
.
- Create a new method
- Create the
UpdateName
method- Create a new method
UpdateName
with a return type ofIActionResult
that accepts a parameter of typeint
namedid
and a parameter of typestring
. This method should have theHttpPatch
attribute with an argument of"{id}"
. This method should returnNoContent
. -
UpdateName
should locate theCelestialObject
with anId
that matches the providedint
parameter. If no match is found returnNotFound
. - If a match is found set it's
Name
property to the providedstring
parameter. Then callUpdate
on theCelestialObjects
DbSet
with an argument of the updatedCelestialObject
, and then callSaveChanges
.
- Create a new method
- Create the
Delete
method- Create a new method
Delete
with a return type ofIActionResult
that accepts a parameter of typeint
namedid
. This method should have theHttpDelete
attribute with an argument of"{id}"
. This method should returnNoContent
. -
Delete
should get aList
of allCelestialObject
s who either have anId
orOrbitedObject
with anId
that matches the provided parameter. If there are no matches it should returnNotFound
. - If there are matching
CelestialObject
s callRemoveRange
on theCelestialObjects
DbSet
with an argument of the list of matchingCelestialObject
s. Then callSaveChanges
.
- Create a new method
You've completed the tasks of this project, if you want to continue working on this project there will be additional projects added to the ASP.NET Core path that continue where this project left off adding more advanced views and models, as well as providing and consuming data as a web service.
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.