PeriodController
Namespace: Commissiestrijd.Controllers
Assembly: backend.dll
Controller for handling period operations. This controller allows users to create, retrieve, update, and delete periods. It checks if the user is authorized before retrieving periods, and it checks if the user is an admin before creating, updating, or deleting periods. The periods are defined by a name, start date, and end date.
[Authorize]
[ApiController]
[Route("[controller]")]
public class PeriodController : Controller, IActionFilter, IAsyncActionFilter, IFilterMetadata, IDisposable
Inheritance
object
← ControllerBase
← Controller
← PeriodController
Implements
IActionFilter
,
IAsyncActionFilter
,
IFilterMetadata
,
IDisposable
Show Inherited Members (204)
Constructors
PeriodController(AppDbContext, ILogger<PeriodController>)
Constructor for the PeriodController. Initializes the controller with the provided database context and logger. This constructor is used to set up the necessary dependencies for the controller, allowing it to access period data and log information or errors during operations.
public PeriodController(AppDbContext context, ILogger<PeriodController> logger)
Parameters
Name | Type | Description |
---|---|---|
context | AppDbContext | The database context used to access the application's data. |
logger | ILogger<PeriodController> | The logger used for logging information or errors during operations. |
Methods
CreatePeriod(string, DateTime, DateTime)
Creates a new period with the specified name, start date, and end date. This method checks if the user is an admin before allowing the creation of a period. It validates that the name is not empty, that the start date is not after the end date, and that a period with the same name does not already exist.
[HttpPost("CreatePeriod")]
[SwaggerOperation(null, null, Summary = "Create Period", Description = "This endpoint allows an admin user to create a new period with a specified name, start date, and end date.")]
[SwaggerResponse(201, "Returns the created period.", null)]
[ProducesResponseType(typeof(Period), 201)]
[SwaggerResponse(400, "If the name is empty, the start date is after the end date, or a period with the same name already exists, a BadRequest response is returned with an error message.", null)]
[SwaggerResponse(401, "If the user is not an admin, an Unauthorized response is returned.", null)]
[SwaggerResponse(500, "If an error occurs while creating the period, a 500 Internal Server Error response is returned.", null)]
public Task<IActionResult> CreatePeriod(string Name, DateTime StartDate, DateTime EndDate)
Parameters
Name | Type | Description |
---|---|---|
Name | string | The name of the period to be created. |
StartDate | DateTime | The start date of the period to be created. |
EndDate | DateTime | The end date of the period to be created. |
Returns
Type | Description |
---|---|
Task | An IActionResult containing the created period or an error message if the creation fails. |
DeletePeriod(Guid)
Deletes a specific period by its ID. This method checks if the user is an admin before allowing the deletion of a period. It validates that the period exists and removes it from the database if found. If the period does not exist, it returns a NotFound response.
[HttpDelete("DeletePeriod")]
[SwaggerOperation(null, null, Summary = "Delete Period", Description = "This endpoint allows an admin user to delete a specific period by its ID.")]
[SwaggerResponse(200, "Returns a success message if the period is deleted successfully.", null)]
[ProducesResponseType(typeof(string), 200)]
[SwaggerResponse(400, "If the PeriodId is not provided, a BadRequest response is returned with an error message indicating that the PeriodId is required.", null)]
[SwaggerResponse(401, "If the user is not an admin, an Unauthorized response is returned with an error message indicating that the user does not have permission to delete committees.", null)]
[SwaggerResponse(404, "If the period does not exist, a NotFound response is returned with an error message indicating that the period was not found.", null)]
[SwaggerResponse(500, "If an error occurs while deleting the period, a 500 Internal Server Error response is returned.", null)]
public Task<IActionResult> DeletePeriod(Guid PeriodId)
Parameters
Name | Type | Description |
---|---|---|
PeriodId | Guid | The ID of the period to be deleted. |
Returns
Type | Description |
---|---|
Task | An IActionResult indicating the result of the deletion operation. |
GetPeriod(Guid)
Retrieves a specific period by its ID. This method checks if the user is authorized before allowing access to the period. It validates that the period exists and returns it if found. If the period does not exist, it returns a NotFound response.
[HttpGet("GetPeriod")]
[SwaggerOperation(null, null, Summary = "Get Period", Description = "This endpoint retrieves a specific period by its ID.")]
[SwaggerResponse(200, "Returns the period if found.", null)]
[ProducesResponseType(typeof(Period), 200)]
[SwaggerResponse(404, "If the period does not exist, a NotFound response is returned with an error message.", null)]
[SwaggerResponse(500, "If an error occurs while retrieving the period, a 500 Internal Server Error response is returned.", null)]
public IActionResult GetPeriod(Guid PeriodId)
Parameters
Name | Type | Description |
---|---|---|
PeriodId | Guid | The ID of the period to be retrieved. |
Returns
Type | Description |
---|---|
IActionResult | An IActionResult containing the period if found, or a NotFound response if the period does not exist. |
If an error occurs while retrieving the period, a 500 Internal Server Error response is returned.
GetPeriods()
Retrieves all periods from the database and sorts them into future, current, and past periods based on the current date in the Netherlands timezone. The future periods are sorted by start date and then by the duration of the period. The current periods are sorted by the duration of the period in descending order. The past periods are sorted by end date and then by the duration of the period in descending order.
[HttpGet("GetPeriods")]
[SwaggerOperation(null, null, Summary = "Get Periods", Description = "This endpoint retrieves all periods from the database and sorts them into future, current, and past periods based on the current date in the Netherlands timezone.")]
[SwaggerResponse(200, "Returns a list of sorted periods.", null)]
[ProducesResponseType(typeof(List<Period>), 200)]
[SwaggerResponse(500, "If an error occurs while retrieving the periods, a 500 Internal Server Error response is returned.", null)]
public IActionResult GetPeriods()
Returns
Type | Description |
---|---|
IActionResult | An IActionResult containing a list of sorted periods. |
UpdatePeriod(Guid, string, DateTime, DateTime)
Updates an existing period with the specified ID, name, start date, and end date This method checks if the user is an admin before allowing the update of a period. It validates that the name is not empty, that the start date is not after the end date, and that the period exists. If a period with the same name already exists (excluding the current period), it returns a BadRequest response.
[HttpPut("UpdatePeriod")]
[SwaggerOperation(null, null, Summary = "Update Period", Description = "This endpoint allows an admin user to update an existing period with a specified ID, name, start date, and end date.")]
[SwaggerResponse(200, "Returns the updated period if the update is successful.", null)]
[ProducesResponseType(typeof(Period), 200)]
[SwaggerResponse(400, "If the name is empty, the start date is after the end date, or a period with the same name already exists, a BadRequest response is returned with an error message.", null)]
[SwaggerResponse(401, "If the user is not an admin, an Unauthorized response is returned.", null)]
[SwaggerResponse(404, "If the period does not exist, a NotFound response is returned with an error message.", null)]
[SwaggerResponse(500, "If an error occurs while updating the period, a 500 Internal Server Error response is returned.", null)]
public Task<IActionResult> UpdatePeriod(Guid PeriodId, string Name, DateTime StartDate, DateTime EndDate)
Parameters
Name | Type | Description |
---|---|---|
PeriodId | Guid | The ID of the period to be updated. |
Name | string | The new name for the period. |
StartDate | DateTime | The new start date for the period. |
EndDate | DateTime | The new end date for the period. |
Returns
Type | Description |
---|---|
Task | An IActionResult containing the updated period or an error message if the update fails. |