Create a REST API using ASP.NET Core by following these steps:
In your solution, use the following components:
Microsoft.EntityFrameworkCore.InMemory- Swagger UI (
Swashbuckle.Core)
Add an Employees table to the project with the following columns:
id(Int32) - primary keyname(String[20])surname(String[20])dateOfBirth(DateTime)employedFrom(DateTime)employedTo(DateTime) - default value is 31.12.2099 if not filled
Implement validations for:
nameandsurname- maximum string length is 20 charactersemployedFromdate must be beforeemployedTodate
Create CRUD operations (create a new record, read, edit, delete a record):
GET Employees/{id}POST Employees/CreatePUT Employees/UpdateDELETE Employees/DeleteGET Employees- returns a list of employees sorted by the parameterssortByandsortType. By default, the list is sorted by the employee's ID. Implement sorting options by date of birth or surname as well. Example request:Employees?sortBy=dateOfBirth&sortType=descending
Document the code (functions, parameters, variables).
The solution was implemented according to initial task information provided.
On top of the requested operations, you can use database seeding feature. Purpose of this feature is make testing of this solution easier.
POST Employees/SeedDatabase
This will create 20 employee records with random data in the database. This endpoint will display an error if any records are present in database.
There are multiple possible approaches to implement validation. As the task request specified to do validation in model, I've used DataValidation attributes.
The basic data validation attributes do not support date comparison. Thus, I added the DateMustBeEarlierAttribute.cs class that adds support of date comparison in model definition. Usage:
[DateMustBeEarlier("EmployedTo", ErrorMessage = "EmployedFrom must be earlier than EmployedTo")]
On top of the request to limit max number of characters for name and surname in database model, I added min length for Name and Surname.
You can find documentation, example requests, possible responses when you run the solution on /swagger URL.
You can find detailed comments in each class, for each method and most of variables. Code is structured to
- Controllers
- Models
- Validations
In root directory, you can find:
- ApplicationDbContext.cs
- SeedDatabase.cs
and other files mostly generated by Visual Studio.
For any inquiries about the solution please write to info@jandrozd.eu.