Skip to content

Commit bf452dd

Browse files
committed
edits
1 parent a4c4c11 commit bf452dd

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

source/fundamentals/crud/restful-api-tutorial.txt

+32-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Microsoft offers. To do this, run the following commands in your terminal:
5555
To add the {+driver-short+} to your project as a dependency, run the following command:
5656

5757
.. code-block:: bash
58+
5859
dotnet add package MongoDB.Driver
5960

6061
The preceding commands create a new web application project for .NET core named
@@ -82,7 +83,7 @@ the data model for your RESTful API.
8283
:language: csharp
8384
:dedent:
8485

85-
The precedeing code defines a class named ``MongoDBSettings`` that
86+
The preceding code defines a class named ``MongoDBSettings`` that
8687
contains information about your connection, the database name, and the
8788
collection name.
8889

@@ -178,4 +179,33 @@ the data model for your RESTful API.
178179
be ``username`` in C#, in JSON, and in MongoDB.
179180

180181
You now have a MongoDB service and document model for your collection to work
181-
with for .NET Core.
182+
with for .NET Core.
183+
184+
Build CRUD Endpoints
185+
--------------------
186+
187+
To create the CRUD endpoints for this application, you need to update two
188+
different files within the project. In this section, you can learn how to define
189+
the endpoint within a controller and update the corresponding work within the
190+
service.
191+
192+
.. procedure:: Build endpoints to interact with MongoDB
193+
:style: connected
194+
195+
.. step:: Create a controller
196+
197+
In your project, create a folder named ``Controllers``. In the
198+
``Controllers`` folder, create a new file named ``PlaylistController.cs``
199+
and add the following code:
200+
201+
.. literalinclude:: /includes/fundamentals/code-examples/restful-api-tutorial/PlaylistControllerSetup.cs
202+
:language: csharp
203+
:dedent:
204+
205+
The ``PlaylistController`` class contains a constructor method that gains
206+
access to your singleton service class. Then, there is a series of
207+
endpoints for this controller.
208+
209+
.. step:: Add data through the POST endpoing
210+
211+
Navigate to ``Services/MongoDBService.cs`` and add the following code:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using Microsoft.AspNetCore.Mvc;
3+
using MongoExample.Services;
4+
using MongoExample.Models;
5+
6+
namespace MongoExample.Controllers;
7+
8+
[Controller]
9+
[Route("api/[controller]")]
10+
public class PlaylistController: Controller {
11+
12+
private readonly MongoDBService _mongoDBService;
13+
14+
public PlaylistController(MongoDBService mongoDBService) {
15+
_mongoDBService = mongoDBService;
16+
}
17+
18+
[HttpGet]
19+
public async Task<List<Playlist>> Get() {}
20+
21+
[HttpPost]
22+
public async Task<IActionResult> Post([FromBody] Playlist playlist) {}
23+
24+
[HttpPut("{id}")]
25+
public async Task<IActionResult> AddToPlaylist(string id, [FromBody] string movieId) {}
26+
27+
[HttpDelete("{id}")]
28+
public async Task<IActionResult> Delete(string id) {}
29+
30+
}

0 commit comments

Comments
 (0)