-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add application cycle controller (#18)
- Loading branch information
Showing
5 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
src/dev/HESIMS/HESIMS.Web/Controllers/ApplicationCyclesController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
namespace HESIMS.Web.Controllers; | ||
|
||
/// <summary> | ||
/// Application cycles controller. | ||
/// </summary> | ||
[Route("api/application-cycles")] | ||
[ApiController] | ||
public class ApplicationCyclesController : BaseController | ||
{ | ||
private readonly IApplicationCycleService applicationCycleService; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ApplicationCyclesController"/> class. | ||
/// </summary> | ||
/// <param name="applicationCycleService">Application cycle service.</param> | ||
public ApplicationCyclesController(IApplicationCycleService applicationCycleService) | ||
{ | ||
this.applicationCycleService = applicationCycleService; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> GetApplicationCyclesAsync() | ||
{ | ||
var applicationCycles = await applicationCycleService.GetApplicationCyclesAsync(); | ||
if (applicationCycles != null && applicationCycles.Count() > 0) | ||
{ | ||
return Ok(applicationCycles.Select(applicationCycle => new ApplicationCycleViewModel | ||
{ | ||
ApplicationCycleId = applicationCycle.Id, | ||
ScholarshipId = applicationCycle.ScholarshipId, | ||
AcademicYear = applicationCycle.AcademicYear | ||
})); | ||
} | ||
|
||
return Ok(applicationCycles); | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<IActionResult> GetApplicationCycleByIdAsync(Guid id) | ||
{ | ||
var applicationCycle = await applicationCycleService.GetApplicationCycleByIdAsync(id); | ||
if (applicationCycle == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return Ok(new ApplicationCycleViewModel | ||
{ | ||
ApplicationCycleId = applicationCycle.Id, | ||
ScholarshipId = applicationCycle.ScholarshipId, | ||
AcademicYear = applicationCycle.AcademicYear | ||
}); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> AddApplicationCycleAsync([FromBody] ApplicationCycleViewModel applicationCycle) | ||
{ | ||
var validationResult = applicationCycle.Validate(); | ||
if (!validationResult.IsValid) | ||
{ | ||
return BadRequest(validationResult.ErrorMessage); | ||
} | ||
|
||
var scholarshipId = GetValueFromNullableGuid(applicationCycle.ScholarshipId); | ||
var newApplicationCycle = new ApplicationCycle | ||
{ | ||
Id = Guid.NewGuid(), | ||
ScholarshipId = scholarshipId, | ||
AcademicYear = applicationCycle.AcademicYear | ||
}; | ||
|
||
await applicationCycleService.AddApplicationCycleAsync(newApplicationCycle); | ||
|
||
return Ok(new ApplicationCycleViewModel | ||
{ | ||
ApplicationCycleId = newApplicationCycle.Id, | ||
ScholarshipId = newApplicationCycle.ScholarshipId, | ||
AcademicYear = newApplicationCycle.AcademicYear | ||
}); | ||
} | ||
|
||
[HttpPut("{id}")] | ||
public async Task<IActionResult> UpdateApplicationCycleAsync(Guid id, [FromBody] ApplicationCycleViewModel applicationCycle) | ||
{ | ||
var validationResult = applicationCycle.Validate(); | ||
if (!validationResult.IsValid) | ||
{ | ||
return BadRequest(validationResult.ErrorMessage); | ||
} | ||
|
||
var scholarshipId = GetValueFromNullableGuid(applicationCycle.ScholarshipId); | ||
var updatedApplicationCycle = new ApplicationCycle | ||
{ | ||
Id = id, | ||
ScholarshipId = scholarshipId, | ||
AcademicYear = applicationCycle.AcademicYear | ||
}; | ||
|
||
await applicationCycleService.UpdateApplicationCycleAsync(updatedApplicationCycle); | ||
|
||
return Ok(new ApplicationCycleViewModel | ||
{ | ||
ApplicationCycleId = updatedApplicationCycle.Id, | ||
ScholarshipId = updatedApplicationCycle.ScholarshipId, | ||
AcademicYear = updatedApplicationCycle.AcademicYear | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace HESIMS.Web.Controllers; | ||
|
||
public class BaseController : Controller | ||
{ | ||
protected Guid GetValueFromNullableGuid(Guid? nullableGuid) | ||
{ | ||
var guidValue = nullableGuid ?? Guid.Empty; | ||
if (guidValue == Guid.Empty) | ||
{ | ||
throw new ArgumentException("Guid value is empty"); | ||
} | ||
|
||
return guidValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/dev/HESIMS/HESIMS.Web/ViewModels/ApplicationCycleViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace HESIMS.Web.ViewModels; | ||
|
||
/// <summary> | ||
/// Represents the view model for the application cycle. | ||
/// </summary> | ||
public record ApplicationCycleViewModel | ||
{ | ||
/// <summary> | ||
/// Application cycle Id. | ||
/// </summary> | ||
public Guid? ApplicationCycleId { get; set; } | ||
|
||
/// <summary> | ||
/// Application cycle scholarship Id. | ||
/// </summary> | ||
public Guid? ScholarshipId { get; set; } | ||
|
||
/// <summary> | ||
/// Application cycle academic year. | ||
/// </summary> | ||
public string? AcademicYear { get; set; } | ||
|
||
/// <summary> | ||
/// Validates the application cycle view model. | ||
/// </summary> | ||
/// <param name="applicationCycleId">Application cycle Id.</param> | ||
/// <param name="scholarshipId">Scholarship Id.</param> | ||
public ValidationResult Validate(Guid? routeId = null, bool validateId = false) | ||
{ | ||
var validationResult = new ValidationResult | ||
{ | ||
IsValid = true | ||
}; | ||
|
||
if (string.IsNullOrWhiteSpace(AcademicYear)) | ||
{ | ||
validationResult.IsValid = false; | ||
validationResult.ErrorMessage = "Academic year is required."; | ||
} | ||
|
||
var isEmptyOrDefaultGuidScholarshipId = !ScholarshipId.HasValue && ScholarshipId == Guid.Empty; | ||
if (isEmptyOrDefaultGuidScholarshipId) | ||
{ | ||
validationResult.IsValid = false; | ||
validationResult.ErrorMessage = "Scholarship ID is required."; | ||
} | ||
|
||
var isEmptyOrDefaultGuidApplicationCycleId = !ApplicationCycleId.HasValue && ApplicationCycleId == Guid.Empty; | ||
if (validateId && isEmptyOrDefaultGuidApplicationCycleId) | ||
{ | ||
validationResult.IsValid = false; | ||
validationResult.ErrorMessage = "Application cycle ID is required."; | ||
} | ||
|
||
if (validateId && ApplicationCycleId != routeId) | ||
{ | ||
validationResult.IsValid = false; | ||
validationResult.ErrorMessage = "Application cycle ID mismatch."; | ||
} | ||
|
||
return validationResult; | ||
} | ||
} |