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 call theAddMvc
method onservices
to add support for MVC middleware. - 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
directives forStarChart.Data
andMicrosoft.EntityFrameworkCore
) - In the
Configure
method add a call toUseMvc
method onapp
.
- In the
- Create
CelestialObject
Model- Create a new public class
CelestialObject
in theModels
directory - Create a new public property of type
int
namedId
. - Create a new public 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 public property of type
int?
namedOrbitedObjectId
. - Create a new public property of type
List<CelestialObject>
namedSatellites
. This property should have theNotMapped
attribute. (Note: you will need to addusing
directives forSystem.Collections.Generic
andSystem.ComponentModels.DataAnnotations.Schema
) - Create a new public property of type
TimeSpan
namedOrbitalPeriod
. - In the
ApplicationDbContext
class, located in theData
folder, create a new public property of typeDbSet<CelestialObject>
namedCelestialObjects
. (Note: you will need to add ausing
directive forStarChart.Models
)
- Create a new public class
- Create
CelestialObjectController
class- Create a new class
CelestialObjectController
in theControllers
folder that inherits theControllerBase
class. If any actions are automatically generated they should be removed. (Note: you will need to add ausing
directive forMicrosoft.AspNetCore.Mvc
) - Add a
Route
attribute with a value of""
andApiController
attribute to theCelestialObjectController
. - Create a new private readonly field of type
ApplicationDbContext
named_context
. (Note: you will need to add ausing
directive forStarChart.Data
) - Create a constructor that accepts a parameter of type
ApplicationDbContext
and sets the_context
field 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
int
namedid
. - This method should have an
HttpGet
attribute with an value of"{id:int}"
and theName
property set to"GetById"
. - This method should return
NotFound
there is noCelestialObject
with anId
property that matches the parameter. - This method should also set the
Satellites
property to anyCelestialObjects
who'sOrbitedObjectId
is the currentCelestialObject
'sId
. - This method should return an
Ok
with a value of theCelestialObject
who'sId
property matches theid
parameter.
- This method should have a return type of
- Create the
GetByName
method- This method should have a return type of
IActionResult
- This method should accept a parameter of type
string
namedname
. - This method should have an
HttpGet
attribute with a value of"{name}"
. - This method should return
NotFound
there is noCelestialObject
with anName
property that matches thename
parameter. - This method should also set the
Satellites
property for eachCelestialObject
who'sOrbitedObjectId
is the currentCelestialObject
'sId
. - This method should return an
Ok
with a value of the list ofCelestialObject
who'sName
property matches thename
parameter.
- This method should have a return type of
- Create the
GetAll
method- This method should have a return type of
IActionResult
. - This method should also set the
Satellites
property for each of theCelestialObject
s returned. - This method should have an
HttpGet
attribute. - This method should return
Ok
with a value of allCelestialObjects
s.
- This method should have a return type of
- Create a new method
- Create
CelestialObjectControllers
's Post, Put, Patch, and Delete actions- Create the
Create
method- 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 ausing
directive forStarChart.Models
) - This method should have an
HttpPost
attribute. - This method should add the provided
CelestialObject
to theCelestialObjects
DbSet
thenSaveChanges
. - This method should return a
CreatedAtRoute
with the arguments"GetById"
- A new
object
with anid
of theCelestialObject
'sId
(note: use thenew { }
format) - The newly created
CelestialObject
.
- This method should have a return type of
- Create the
Update
method- This method should have a return type of
IActionResult
. - This method should accept a parameter of type
int
namedid
and a parameter of typeCelestialObject
. - This method should have the
HttpPut
attribute with a value of"{id}"
. - This method should locate the
CelestialObject
with anId
that matches the providedint
parameter.- If no match is found return
NotFound
. - If a match is found set it's
Name
,OrbitalPeriod
, andOrbitedObjectId
properties based on the providedCelestialObject
parameter. CallUpdate
on theCelestialObjects
DbSet
with 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
RenameObject
method- This method should have a return type of
IActionResult
. - This method should accept a parameter of type
int
namedid
and a parameter of typestring
namedname
. - This method should have the
HttpPatch
attribute with an argument of"{id}/{name}"
. - This method should locate the
CelestialObject
with anId
that matches the providedint
parameter.- If no match is found return
NotFound
. - If a match is found set it's
Name
property to the providedname
parameter. Then callUpdate
on theCelestialObjects
DbSet
with 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
Delete
method- This method should have a return type of
IActionResult
- This method should accept a parameter of type
int
namedid
. - This method should have the
HttpDelete
attribute with an argument of"{id}"
. - This method should get a
List
of allCelestialObject
s who either have anId
orOrbitedObjectId
that matches the provided parameter.- If there are no matches it should return
NotFound
. - If there are matching
CelestialObject
s callRemoveRange
on theCelestialObjects
DbSet
with an argument of the list of matchingCelestialObject
s. 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), impliment 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.