Models/User.cs
namespace MyApi.Models
{
public class User
{
public int Id {get; set;}
public string Name {get; set;}
}
}
{
"id": 1
"name": "Sonja"
}
(Basically just JSON data that ASP.NET can show automatically)
Controllers/UserController.cs
using Microsoft.AspNetCore.Mvc;
using MyApi.Models;
namespace MyApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetUser(int id)
// ...
}
}
using Microsoft.AspNetCore.Mvc;
using MyApi.Models;
namespace MyApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
var person = new Person
{
Id = 1,
Name = "Sonja"
};
return Ok(person);
}
}
}
GET
methodCourseAPI
. Delete WeatherForecastController.cs
and WeatherForecast.cs
.CoursesController
by right-clicking the Controllers
folder and selecting Add > Controller... > API Controller > EmptyModels
. Inside, add a new class file ( Add > Class... ) named Course.cs
int Id
, string Name
and int Credits
GET
endpointsGET
requests with URIs api/courses
and api/courses/{id}
which return all courses and a single course with a corresponding ID, respectivelyRepositories
for our needs, and make it static so that it can be accessed everywhere
// Repositories/ContactRepository.cs
public class MockRepo
{
// Replace this later on with a database context
private List<Contact> Contacts { get; set; }
// Constructor to initialize contact list
private MockRepo()
{
Contacts = new List<Contact>();
}
// Replace this later on with dependency injection
public static MockRepo Instance { get; } =
new MockRepo();
public List<Contact> GetContacts() => Contacts;
// Update database here later
// Other methods to read/write from database
// ...
}
// Controllers/ContactsController.cs
// ...
[HttpGet]
public List<Contact> Get()
{
return MockRepo.Instance.GetContacts();
}
// ...
Continue working on CourseAPI.
Repositories
Create a service interface and a class implementing it:
A rule of thumb: Create a separate repository for all tightly coupled models, not necessarily every model
public interface IContactRepository
{
Contact GetContact(int id);
List<Contact> GetContacts();
void AddContact(Contact contact);
void UpdateContact(int id, Contact contact);
void DeleteContact(int id);
// UpdateDataBase() later on...
}
public class ContactRepository : IContactRepository
{
// Replace this with database context in a real life application
private static List<Contact> Contacts = new List<Contact>
{
new Contact{Id=0, Name="Johannes Kantola", Email="johkant@example.com"},
new Contact{Id=1, Name="Rene Orosz", Email="rene_king@example.com"}
};
public void AddContact(Contact contact) => Contacts.Add(contact);
public void DeleteContact(int id) =>
Contacts = Contacts.Where(c => c.Id != id).ToList();
public Contact GetContact(int id) => Contacts.FirstOrDefault(c => c.Id == id);
public List<Contact> GetContacts() => Contacts;
public void UpdateContact(int id, Contact newContact) =>
Contacts = Contacts.Select(c => c.Id != id ? c : newContact).ToList();
}
Program.cs
// ...
builder.Services.AddSingleton<IContactRepository, ContactRepository>();
builder.Services.AddControllers().AddNewtonsoftJson();
// ...
public class ContactsController : ControllerBase
{
private readonly IContactRepository _contactRepository;
public ContactsController(IContactRepository contactRepository)
{
_contactRepository = contactRepository;
}
}
MockRepo.Instance.GetContacts()
anymore![HttpGet("{id}")]
public IActionResult GetContactById(int id)
{
Contact contact = _contactRepository.GetContact(id);
if (contact == null)
{
return NotFound();
}
return Ok(contact);
}
Continue working on CourseAPI.
ICourseRepository
with methods for getting a single course or all courses. Rename your mock repository to CourseRepository
and make sure the interface is fully implemented.CourseRepository
as a service, and refactor the controller to use methods for ICourseRepository
At this point, the flow of your API should be in line with this chart: