diff --git a/src/InternshipSystem.Api/Controllers/CompaniesController.cs b/src/InternshipSystem.Api/Controllers/CompaniesController.cs index 2d21d60..ed374c6 100644 --- a/src/InternshipSystem.Api/Controllers/CompaniesController.cs +++ b/src/InternshipSystem.Api/Controllers/CompaniesController.cs @@ -82,7 +82,7 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task<ActionResult> UpdateCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken) + public async Task<ActionResult> UpsertCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken) { var validator = new CompanyForm.Validator(); var validationResult = await validator.ValidateAsync(companyForm, cancellationToken); @@ -94,30 +94,24 @@ namespace InternshipSystem.Api.Controllers if (companyForm.Id.HasValue) { - var companyToUpdate = await Context.Companies.FindAsync(companyForm.Id); + var companyToUpdate = await Context.Companies.FindAsync(companyForm.Id.Value); - if (companyToUpdate != null) + if (companyToUpdate == null) { - companyToUpdate.Name = IsNullOrEmpty(companyForm.Name) ? companyToUpdate.Name : companyForm.Name; - companyToUpdate.Nip = IsNullOrEmpty(companyForm.Nip) ? companyToUpdate.Nip : companyForm.Nip; - } - else - { - return NotFound($"Company with id: {companyForm.Id} does not exist"); + return NotFound(); } + + companyToUpdate.Name = IsNullOrEmpty(companyForm.Name) ? companyToUpdate.Name : companyForm.Name; + companyToUpdate.Nip = IsNullOrEmpty(companyForm.Nip) ? companyToUpdate.Nip : companyForm.Nip; } else { - var newCompany = new Company - { - Name = companyForm.Name, - Nip = companyForm.Nip, - }; + var newCompany = Company.CreateCompany(companyForm.Nip, companyForm.Name); await Context.Companies.AddAsync(newCompany, cancellationToken); } await Context.SaveChangesAsync(cancellationToken); - return Ok($"Company updated successfully"); + return Ok(); } /// <summary> @@ -139,17 +133,16 @@ namespace InternshipSystem.Api.Controllers { var companyToDelete = await Context.Companies .Include(c => c.Branches) - .FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken); + .FirstOrDefaultAsync(c => c.Id == companyId, cancellationToken); if (companyToDelete == null) { - return NotFound($"Company with id: {companyId} does not exist"); + return NotFound(); } - - Context.Companies.Attach(companyToDelete); + Context.Companies.Remove(companyToDelete); await Context.SaveChangesAsync(cancellationToken); - return Ok($"Company with id: {companyId} deleted successfully"); + return Ok(); } /// <summary> @@ -162,7 +155,7 @@ namespace InternshipSystem.Api.Controllers /// <response code="401">This action is only available for authorized internship admin</response> /// <response code="404">Company or branch office not found</response> /// <returns></returns> - [HttpPut("branchOffice/{companyId}")] + [HttpPut("{companyId}/branchOffices")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] @@ -180,20 +173,20 @@ namespace InternshipSystem.Api.Controllers var company = await Context.Companies .Include(c => c.Branches) - .FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken); + .FirstOrDefaultAsync(c => c.Id == companyId, cancellationToken); if (company == null) { - return NotFound($"Company with id: {companyId} does not exist"); + return NotFound(); } if (branchOfficeForm.Id.HasValue) { - var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeForm.Id.Value)); + var branchOffice = company.Branches.First(b => b.Id == branchOfficeForm.Id); if (branchOffice == null) { - return NotFound($"Branch office with id: {branchOfficeForm.Id} does not exist"); + return NotFound(); } branchOffice.Address.Country = IsNullOrEmpty(branchOfficeForm.Country) ? branchOffice.Address.Country : branchOfficeForm.Country; @@ -219,7 +212,7 @@ namespace InternshipSystem.Api.Controllers } await Context.SaveChangesAsync(cancellationToken); - return Ok($"Branch office updated successfully"); + return Ok(); } /// <summary> @@ -230,30 +223,29 @@ namespace InternshipSystem.Api.Controllers /// <response code="400">Branch office id is empty</response> /// <response code="401">This action is only available for authorized internship admin</response> /// <response code="404">Company or branch office not found</response> - [HttpDelete("branchOffice/{branchOfficeId}")] + [HttpDelete("{companyId}/branchOffice/{branchOfficeId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task<ActionResult> DeleteBranch(long branchOfficeId, CancellationToken cancellationToken) + public async Task<ActionResult> DeleteBranch(long companyId, long branchOfficeId, CancellationToken cancellationToken) { var company = await Context.Companies .Include(c => c.Branches) - .Where(c => c.Branches.Any(b => b.Id.Equals(branchOfficeId))) - .FirstOrDefaultAsync(cancellationToken: cancellationToken); + .Where(c => c.Id == companyId) + .FirstOrDefaultAsync(cancellationToken); if (company == null) { - return NotFound($"Branch office with id: {branchOfficeId} does not exist"); + return NotFound(); } - var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeId)); - company.Branches.Remove(branchOffice); - + var branchOffice = company.Branches.RemoveAll(b => b.Id == branchOfficeId); + await Context.SaveChangesAsync(cancellationToken); - return Ok($"Branch office with id: {branchOfficeId} deleted successfully"); + return Ok(); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/EditionController.cs b/src/InternshipSystem.Api/Controllers/EditionController.cs index d558cd6..0708c9b 100644 --- a/src/InternshipSystem.Api/Controllers/EditionController.cs +++ b/src/InternshipSystem.Api/Controllers/EditionController.cs @@ -57,23 +57,23 @@ namespace InternshipSystem.Api.Controllers } /// <summary> - /// Get edition's configuration + /// Get current edition's configuration /// </summary> /// <response code="200">Parameters of edition registered for by student</response> /// <response code="401">This action is only available for authorized student registered for this edition edition</response> /// <response code="404">Specified edition doesn't exist</response> /// <returns></returns> - [HttpGet("{id}")] + [HttpGet("current")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] - [Authorize] - public async Task<ActionResult<EditionConfigurationResult>> GetEditionsConfiguration(Guid id, CancellationToken token) + [Authorize(Policy = Policies.RegisteredOnly)] + public async Task<ActionResult<EditionConfigurationResult>> GetEditionsConfiguration([FromServices] User user, CancellationToken token) { var edition = await Context.Editions .Include(e => e.AvailableSubjects) .Include(e => e.Course) - .Where(e => e.Id.Equals(id)) + .Where(e => e.Id == user.EditionId) .ProjectTo<EditionConfigurationResult>(Mapper.ConfigurationProvider) .FirstOrDefaultAsync(token); diff --git a/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs b/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs index d427da7..e14df07 100644 --- a/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs +++ b/src/InternshipSystem.Api/Controllers/InternshipTypesController.cs @@ -28,7 +28,7 @@ namespace InternshipSystem.Api.Controllers /// Get internship types available for current edition /// </summary> /// <returns>List of internship types for current edition</returns> - [HttpGet("current")] + [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] diff --git a/src/InternshipSystem.Api/Controllers/StaticPagesController.cs b/src/InternshipSystem.Api/Controllers/StaticPagesController.cs index e9b602f..94b9b2f 100644 --- a/src/InternshipSystem.Api/Controllers/StaticPagesController.cs +++ b/src/InternshipSystem.Api/Controllers/StaticPagesController.cs @@ -23,7 +23,7 @@ namespace InternshipSystem.Api.Controllers private InternshipDbContext Context { get; } /// <summary> - /// Get all static pages + /// Get all static pages, only available for coordinator /// </summary> /// <returns>List of static pages with titles and content</returns> /// <response code="200">Static pages list returned successfully</response> @@ -34,7 +34,7 @@ namespace InternshipSystem.Api.Controllers .ToListAsync(cancellationToken); /// <summary> - /// Get static page + /// Get static page, only available for coordinator /// </summary> /// <param name="accessName">Name of page</param> /// <returns>Static page title and content</returns> @@ -59,7 +59,7 @@ namespace InternshipSystem.Api.Controllers } /// <summary> - /// Add or update static page + /// Add or update static page, only available for coordinator /// </summary> /// <response code="200">Static page updated successfully</response> /// <response code="400">Static page form is not valid</response> @@ -134,7 +134,7 @@ namespace InternshipSystem.Api.Controllers } /// <summary> - /// Delete static page + /// Delete static page, only available for coordinator /// </summary> /// <response code="200">Static page deleted successfully</response> /// <response code="401">This action is only available for authorized internship admin</response> diff --git a/src/InternshipSystem.Api/Controllers/StudentsController.cs b/src/InternshipSystem.Api/Controllers/StudentsController.cs index 5897dbb..f07d2b8 100644 --- a/src/InternshipSystem.Api/Controllers/StudentsController.cs +++ b/src/InternshipSystem.Api/Controllers/StudentsController.cs @@ -61,11 +61,11 @@ namespace InternshipSystem.Api.Controllers await _context.SaveChangesAsync(cancellationToken); - return Ok($"Student updated successfully"); + return Ok(); } /// <summary> - /// Get student personal data + /// Get student personal data, only available for coordinator /// </summary> /// <returns>Student personal data</returns> /// <response code="200">Student data returned successfully</response> @@ -80,7 +80,7 @@ namespace InternshipSystem.Api.Controllers await _context.Students.FindAsync(studentPersonNumber); /// <summary> - /// Search students personal data + /// Search students personal data, only available for coordinator /// </summary> /// <returns>List of students personal data</returns> /// <response code="200">List of student data</response> @@ -89,7 +89,7 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [Authorize] - public async Task<ActionResult<IReadOnlyCollection<Student>>> GetStudents([FromQuery] StudentSearchQuery searchQuery, CancellationToken cancellationToken) => + public async Task<ActionResult<IReadOnlyCollection<Student>>> SearchStudents([FromQuery] StudentSearchQuery searchQuery, CancellationToken cancellationToken) => await _context.Students .Where(s => !searchQuery.AlbumNumber.HasValue || s.AlbumNumber.Equals(searchQuery.AlbumNumber)) .Where(s => string.IsNullOrEmpty(searchQuery.FirstName) || s.FirstName.ToLower().Contains(searchQuery.FirstName.ToLower())) @@ -100,18 +100,18 @@ namespace InternshipSystem.Api.Controllers .ToListAsync(cancellationToken); /// <summary> - /// Updates student personal data + /// Updates student personal data, only available for coordinator /// </summary> /// <response code="200">Student data updated successfully</response> /// <response code="401">This action is only available for authorized internship admin</response> /// <response code="404">Student with given id do not exist</response> - [HttpPut] + [HttpPut("{studentId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task<ActionResult> UpdateStudentData([FromBody] StudentForm studentNewData, CancellationToken cancellationToken) + public async Task<ActionResult> UpdateStudentData(long studentId, [FromBody] StudentForm studentNewData, CancellationToken cancellationToken) { var validator = new StudentForm.Validator(); var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken); @@ -121,11 +121,11 @@ namespace InternshipSystem.Api.Controllers return BadRequest(validationResult.ToString()); } - var currentStudent = await _context.Students.FindAsync(studentNewData.Id); + var currentStudent = await _context.Students.FindAsync(studentId); if (currentStudent == null) { - return NotFound($"Student with id: {studentNewData.Id} does not exist"); + return NotFound(); } currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber; @@ -137,7 +137,7 @@ namespace InternshipSystem.Api.Controllers await _context.SaveChangesAsync(cancellationToken); - return Ok($"Student updated successfully"); + return Ok(); } } } \ No newline at end of file diff --git a/src/InternshipSystem.Api/Controllers/GutCasClient.cs b/src/InternshipSystem.Api/GutCasClient.cs similarity index 100% rename from src/InternshipSystem.Api/Controllers/GutCasClient.cs rename to src/InternshipSystem.Api/GutCasClient.cs diff --git a/src/InternshipSystem.Api/Queries/StudentForm.cs b/src/InternshipSystem.Api/Queries/StudentForm.cs index f4ceafe..667bcf9 100644 --- a/src/InternshipSystem.Api/Queries/StudentForm.cs +++ b/src/InternshipSystem.Api/Queries/StudentForm.cs @@ -4,7 +4,6 @@ namespace InternshipSystem.Api.Queries { public class StudentForm { - public long Id { get; set; } public int? AlbumNumber { get; set; } public string FirstName { get; set; } public string LastName { get; set; } @@ -16,7 +15,6 @@ namespace InternshipSystem.Api.Queries { public Validator() { - RuleFor(c => c.Id).NotNull(); } } }