clean up api bit

This commit is contained in:
MaxchilKH 2020-10-04 10:41:27 +02:00
parent 1b80f058dc
commit de4a8e54a6
7 changed files with 47 additions and 57 deletions

View File

@ -82,7 +82,7 @@ namespace InternshipSystem.Api.Controllers
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize] [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 validator = new CompanyForm.Validator();
var validationResult = await validator.ValidateAsync(companyForm, cancellationToken); var validationResult = await validator.ValidateAsync(companyForm, cancellationToken);
@ -94,30 +94,24 @@ namespace InternshipSystem.Api.Controllers
if (companyForm.Id.HasValue) 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; return NotFound();
companyToUpdate.Nip = IsNullOrEmpty(companyForm.Nip) ? companyToUpdate.Nip : companyForm.Nip;
}
else
{
return NotFound($"Company with id: {companyForm.Id} does not exist");
} }
companyToUpdate.Name = IsNullOrEmpty(companyForm.Name) ? companyToUpdate.Name : companyForm.Name;
companyToUpdate.Nip = IsNullOrEmpty(companyForm.Nip) ? companyToUpdate.Nip : companyForm.Nip;
} }
else else
{ {
var newCompany = new Company var newCompany = Company.CreateCompany(companyForm.Nip, companyForm.Name);
{
Name = companyForm.Name,
Nip = companyForm.Nip,
};
await Context.Companies.AddAsync(newCompany, cancellationToken); await Context.Companies.AddAsync(newCompany, cancellationToken);
} }
await Context.SaveChangesAsync(cancellationToken); await Context.SaveChangesAsync(cancellationToken);
return Ok($"Company updated successfully"); return Ok();
} }
/// <summary> /// <summary>
@ -139,17 +133,16 @@ namespace InternshipSystem.Api.Controllers
{ {
var companyToDelete = await Context.Companies var companyToDelete = await Context.Companies
.Include(c => c.Branches) .Include(c => c.Branches)
.FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken); .FirstOrDefaultAsync(c => c.Id == companyId, cancellationToken);
if (companyToDelete == null) if (companyToDelete == null)
{ {
return NotFound($"Company with id: {companyId} does not exist"); return NotFound();
} }
Context.Companies.Attach(companyToDelete);
Context.Companies.Remove(companyToDelete); Context.Companies.Remove(companyToDelete);
await Context.SaveChangesAsync(cancellationToken); await Context.SaveChangesAsync(cancellationToken);
return Ok($"Company with id: {companyId} deleted successfully"); return Ok();
} }
/// <summary> /// <summary>
@ -162,7 +155,7 @@ namespace InternshipSystem.Api.Controllers
/// <response code="401">This action is only available for authorized internship admin</response> /// <response code="401">This action is only available for authorized internship admin</response>
/// <response code="404">Company or branch office not found</response> /// <response code="404">Company or branch office not found</response>
/// <returns></returns> /// <returns></returns>
[HttpPut("branchOffice/{companyId}")] [HttpPut("{companyId}/branchOffices")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
@ -180,20 +173,20 @@ namespace InternshipSystem.Api.Controllers
var company = await Context.Companies var company = await Context.Companies
.Include(c => c.Branches) .Include(c => c.Branches)
.FirstOrDefaultAsync(c => c.Id.Equals(companyId), cancellationToken: cancellationToken); .FirstOrDefaultAsync(c => c.Id == companyId, cancellationToken);
if (company == null) if (company == null)
{ {
return NotFound($"Company with id: {companyId} does not exist"); return NotFound();
} }
if (branchOfficeForm.Id.HasValue) 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) 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; branchOffice.Address.Country = IsNullOrEmpty(branchOfficeForm.Country) ? branchOffice.Address.Country : branchOfficeForm.Country;
@ -219,7 +212,7 @@ namespace InternshipSystem.Api.Controllers
} }
await Context.SaveChangesAsync(cancellationToken); await Context.SaveChangesAsync(cancellationToken);
return Ok($"Branch office updated successfully"); return Ok();
} }
/// <summary> /// <summary>
@ -230,30 +223,29 @@ namespace InternshipSystem.Api.Controllers
/// <response code="400">Branch office id is empty</response> /// <response code="400">Branch office id is empty</response>
/// <response code="401">This action is only available for authorized internship admin</response> /// <response code="401">This action is only available for authorized internship admin</response>
/// <response code="404">Company or branch office not found</response> /// <response code="404">Company or branch office not found</response>
[HttpDelete("branchOffice/{branchOfficeId}")] [HttpDelete("{companyId}/branchOffice/{branchOfficeId}")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize] [Authorize]
public async Task<ActionResult> DeleteBranch(long branchOfficeId, CancellationToken cancellationToken) public async Task<ActionResult> DeleteBranch(long companyId, long branchOfficeId, CancellationToken cancellationToken)
{ {
var company = var company =
await Context.Companies await Context.Companies
.Include(c => c.Branches) .Include(c => c.Branches)
.Where(c => c.Branches.Any(b => b.Id.Equals(branchOfficeId))) .Where(c => c.Id == companyId)
.FirstOrDefaultAsync(cancellationToken: cancellationToken); .FirstOrDefaultAsync(cancellationToken);
if (company == null) 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)); var branchOffice = company.Branches.RemoveAll(b => b.Id == branchOfficeId);
company.Branches.Remove(branchOffice);
await Context.SaveChangesAsync(cancellationToken); await Context.SaveChangesAsync(cancellationToken);
return Ok($"Branch office with id: {branchOfficeId} deleted successfully"); return Ok();
} }
} }
} }

View File

@ -57,23 +57,23 @@ namespace InternshipSystem.Api.Controllers
} }
/// <summary> /// <summary>
/// Get edition's configuration /// Get current edition's configuration
/// </summary> /// </summary>
/// <response code="200">Parameters of edition registered for by student</response> /// <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="401">This action is only available for authorized student registered for this edition edition</response>
/// <response code="404">Specified edition doesn't exist</response> /// <response code="404">Specified edition doesn't exist</response>
/// <returns></returns> /// <returns></returns>
[HttpGet("{id}")] [HttpGet("current")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize] [Authorize(Policy = Policies.RegisteredOnly)]
public async Task<ActionResult<EditionConfigurationResult>> GetEditionsConfiguration(Guid id, CancellationToken token) public async Task<ActionResult<EditionConfigurationResult>> GetEditionsConfiguration([FromServices] User user, CancellationToken token)
{ {
var edition = var edition =
await Context.Editions await Context.Editions
.Include(e => e.AvailableSubjects) .Include(e => e.AvailableSubjects)
.Include(e => e.Course) .Include(e => e.Course)
.Where(e => e.Id.Equals(id)) .Where(e => e.Id == user.EditionId)
.ProjectTo<EditionConfigurationResult>(Mapper.ConfigurationProvider) .ProjectTo<EditionConfigurationResult>(Mapper.ConfigurationProvider)
.FirstOrDefaultAsync(token); .FirstOrDefaultAsync(token);

View File

@ -28,7 +28,7 @@ namespace InternshipSystem.Api.Controllers
/// Get internship types available for current edition /// Get internship types available for current edition
/// </summary> /// </summary>
/// <returns>List of internship types for current edition</returns> /// <returns>List of internship types for current edition</returns>
[HttpGet("current")] [HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]

View File

@ -23,7 +23,7 @@ namespace InternshipSystem.Api.Controllers
private InternshipDbContext Context { get; } private InternshipDbContext Context { get; }
/// <summary> /// <summary>
/// Get all static pages /// Get all static pages, only available for coordinator
/// </summary> /// </summary>
/// <returns>List of static pages with titles and content</returns> /// <returns>List of static pages with titles and content</returns>
/// <response code="200">Static pages list returned successfully</response> /// <response code="200">Static pages list returned successfully</response>
@ -34,7 +34,7 @@ namespace InternshipSystem.Api.Controllers
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
/// <summary> /// <summary>
/// Get static page /// Get static page, only available for coordinator
/// </summary> /// </summary>
/// <param name="accessName">Name of page</param> /// <param name="accessName">Name of page</param>
/// <returns>Static page title and content</returns> /// <returns>Static page title and content</returns>
@ -59,7 +59,7 @@ namespace InternshipSystem.Api.Controllers
} }
/// <summary> /// <summary>
/// Add or update static page /// Add or update static page, only available for coordinator
/// </summary> /// </summary>
/// <response code="200">Static page updated successfully</response> /// <response code="200">Static page updated successfully</response>
/// <response code="400">Static page form is not valid</response> /// <response code="400">Static page form is not valid</response>
@ -134,7 +134,7 @@ namespace InternshipSystem.Api.Controllers
} }
/// <summary> /// <summary>
/// Delete static page /// Delete static page, only available for coordinator
/// </summary> /// </summary>
/// <response code="200">Static page deleted successfully</response> /// <response code="200">Static page deleted successfully</response>
/// <response code="401">This action is only available for authorized internship admin</response> /// <response code="401">This action is only available for authorized internship admin</response>

View File

@ -61,11 +61,11 @@ namespace InternshipSystem.Api.Controllers
await _context.SaveChangesAsync(cancellationToken); await _context.SaveChangesAsync(cancellationToken);
return Ok($"Student updated successfully"); return Ok();
} }
/// <summary> /// <summary>
/// Get student personal data /// Get student personal data, only available for coordinator
/// </summary> /// </summary>
/// <returns>Student personal data</returns> /// <returns>Student personal data</returns>
/// <response code="200">Student data returned successfully</response> /// <response code="200">Student data returned successfully</response>
@ -80,7 +80,7 @@ namespace InternshipSystem.Api.Controllers
await _context.Students.FindAsync(studentPersonNumber); await _context.Students.FindAsync(studentPersonNumber);
/// <summary> /// <summary>
/// Search students personal data /// Search students personal data, only available for coordinator
/// </summary> /// </summary>
/// <returns>List of students personal data</returns> /// <returns>List of students personal data</returns>
/// <response code="200">List of student data</response> /// <response code="200">List of student data</response>
@ -89,7 +89,7 @@ namespace InternshipSystem.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[Authorize] [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 await _context.Students
.Where(s => !searchQuery.AlbumNumber.HasValue || s.AlbumNumber.Equals(searchQuery.AlbumNumber)) .Where(s => !searchQuery.AlbumNumber.HasValue || s.AlbumNumber.Equals(searchQuery.AlbumNumber))
.Where(s => string.IsNullOrEmpty(searchQuery.FirstName) || s.FirstName.ToLower().Contains(searchQuery.FirstName.ToLower())) .Where(s => string.IsNullOrEmpty(searchQuery.FirstName) || s.FirstName.ToLower().Contains(searchQuery.FirstName.ToLower()))
@ -100,18 +100,18 @@ namespace InternshipSystem.Api.Controllers
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
/// <summary> /// <summary>
/// Updates student personal data /// Updates student personal data, only available for coordinator
/// </summary> /// </summary>
/// <response code="200">Student data updated successfully</response> /// <response code="200">Student data updated successfully</response>
/// <response code="401">This action is only available for authorized internship admin</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> /// <response code="404">Student with given id do not exist</response>
[HttpPut] [HttpPut("{studentId}")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
[Authorize] [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 validator = new StudentForm.Validator();
var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken); var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken);
@ -121,11 +121,11 @@ namespace InternshipSystem.Api.Controllers
return BadRequest(validationResult.ToString()); return BadRequest(validationResult.ToString());
} }
var currentStudent = await _context.Students.FindAsync(studentNewData.Id); var currentStudent = await _context.Students.FindAsync(studentId);
if (currentStudent == null) if (currentStudent == null)
{ {
return NotFound($"Student with id: {studentNewData.Id} does not exist"); return NotFound();
} }
currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber; currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber;
@ -137,7 +137,7 @@ namespace InternshipSystem.Api.Controllers
await _context.SaveChangesAsync(cancellationToken); await _context.SaveChangesAsync(cancellationToken);
return Ok($"Student updated successfully"); return Ok();
} }
} }
} }

View File

@ -4,7 +4,6 @@ namespace InternshipSystem.Api.Queries
{ {
public class StudentForm public class StudentForm
{ {
public long Id { get; set; }
public int? AlbumNumber { get; set; } public int? AlbumNumber { get; set; }
public string FirstName { get; set; } public string FirstName { get; set; }
public string LastName { get; set; } public string LastName { get; set; }
@ -16,7 +15,6 @@ namespace InternshipSystem.Api.Queries
{ {
public Validator() public Validator()
{ {
RuleFor(c => c.Id).NotNull();
} }
} }
} }