From de4a8e54a621c93524fb31ff7b0f7ca658e3828d Mon Sep 17 00:00:00 2001 From: MaxchilKH Date: Sun, 4 Oct 2020 10:41:27 +0200 Subject: [PATCH] clean up api bit --- .../Controllers/CompaniesController.cs | 62 ++++++++----------- .../Controllers/EditionController.cs | 10 +-- .../Controllers/InternshipTypesController.cs | 2 +- .../Controllers/StaticPagesController.cs | 8 +-- .../Controllers/StudentsController.cs | 20 +++--- .../{Controllers => }/GutCasClient.cs | 0 .../Queries/StudentForm.cs | 2 - 7 files changed, 47 insertions(+), 57 deletions(-) rename src/InternshipSystem.Api/{Controllers => }/GutCasClient.cs (100%) 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 UpdateCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken) + public async Task 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(); } /// @@ -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(); } /// @@ -162,7 +155,7 @@ namespace InternshipSystem.Api.Controllers /// This action is only available for authorized internship admin /// Company or branch office not found /// - [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(); } /// @@ -230,30 +223,29 @@ namespace InternshipSystem.Api.Controllers /// Branch office id is empty /// This action is only available for authorized internship admin /// Company or branch office not found - [HttpDelete("branchOffice/{branchOfficeId}")] + [HttpDelete("{companyId}/branchOffice/{branchOfficeId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task DeleteBranch(long branchOfficeId, CancellationToken cancellationToken) + public async Task 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 } /// - /// Get edition's configuration + /// Get current edition's configuration /// /// Parameters of edition registered for by student /// This action is only available for authorized student registered for this edition edition /// Specified edition doesn't exist /// - [HttpGet("{id}")] + [HttpGet("current")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] - [Authorize] - public async Task> GetEditionsConfiguration(Guid id, CancellationToken token) + [Authorize(Policy = Policies.RegisteredOnly)] + public async Task> 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(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 /// /// List of internship types for current edition - [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; } /// - /// Get all static pages + /// Get all static pages, only available for coordinator /// /// List of static pages with titles and content /// Static pages list returned successfully @@ -34,7 +34,7 @@ namespace InternshipSystem.Api.Controllers .ToListAsync(cancellationToken); /// - /// Get static page + /// Get static page, only available for coordinator /// /// Name of page /// Static page title and content @@ -59,7 +59,7 @@ namespace InternshipSystem.Api.Controllers } /// - /// Add or update static page + /// Add or update static page, only available for coordinator /// /// Static page updated successfully /// Static page form is not valid @@ -134,7 +134,7 @@ namespace InternshipSystem.Api.Controllers } /// - /// Delete static page + /// Delete static page, only available for coordinator /// /// Static page deleted successfully /// This action is only available for authorized internship admin 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(); } /// - /// Get student personal data + /// Get student personal data, only available for coordinator /// /// Student personal data /// Student data returned successfully @@ -80,7 +80,7 @@ namespace InternshipSystem.Api.Controllers await _context.Students.FindAsync(studentPersonNumber); /// - /// Search students personal data + /// Search students personal data, only available for coordinator /// /// List of students personal data /// List of student data @@ -89,7 +89,7 @@ namespace InternshipSystem.Api.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [Authorize] - public async Task>> GetStudents([FromQuery] StudentSearchQuery searchQuery, CancellationToken cancellationToken) => + public async Task>> 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); /// - /// Updates student personal data + /// Updates student personal data, only available for coordinator /// /// Student data updated successfully /// This action is only available for authorized internship admin /// Student with given id do not exist - [HttpPut] + [HttpPut("{studentId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status404NotFound)] [Authorize] - public async Task UpdateStudentData([FromBody] StudentForm studentNewData, CancellationToken cancellationToken) + public async Task 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(); } } }