SearchQuery changes + new student endpoints
This commit is contained in:
parent
da052c19aa
commit
c1d72eb370
@ -22,6 +22,8 @@ namespace InternshipSystem.Api
|
|||||||
CreateMap<Edition, EditionConfigurationResult>();
|
CreateMap<Edition, EditionConfigurationResult>();
|
||||||
|
|
||||||
CreateMap<EditionSubject, InternshipSubject>().IncludeMembers(es => es.Subject);
|
CreateMap<EditionSubject, InternshipSubject>().IncludeMembers(es => es.Subject);
|
||||||
|
|
||||||
|
CreateMap<CurrentStudentForm, Student>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,8 +3,10 @@ using System.Linq;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using InternshipSystem.Api.Queries;
|
using InternshipSystem.Api.Queries;
|
||||||
|
using InternshipSystem.Api.Queries.SearchQuery;
|
||||||
using InternshipSystem.Core;
|
using InternshipSystem.Core;
|
||||||
using InternshipSystem.Repository;
|
using InternshipSystem.Repository;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@ -35,9 +37,9 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public async Task<IReadOnlyCollection<Company>> SearchByNameAsync([FromQuery] SearchQuery searchQuery, CancellationToken cancellationToken) =>
|
public async Task<ActionResult<IReadOnlyCollection<Company>>> SearchByNameAsync([FromQuery] CompanySearchQuery searchQuery, CancellationToken cancellationToken) =>
|
||||||
await Context.Companies
|
await Context.Companies
|
||||||
.Where(c => c.Name.ToLower().Contains(searchQuery.Query.ToLower()))
|
.Where(c => c.Name.ToLower().Contains(searchQuery.Name.ToLower()))
|
||||||
.OrderBy(o => o.Name)
|
.OrderBy(o => o.Name)
|
||||||
.Skip(searchQuery.Page * searchQuery.PerPage)
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
||||||
.Take(searchQuery.PerPage)
|
.Take(searchQuery.PerPage)
|
||||||
@ -54,14 +56,14 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[HttpGet("{companyId}")]
|
[HttpGet("{companyId}")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
public async Task<IReadOnlyCollection<BranchOffice>> SearchBranchesByAddress([FromQuery] SearchQuery searchQuery, long companyId, CancellationToken token)
|
public async Task<ActionResult<IReadOnlyCollection<BranchOffice>>> SearchBranchesByAddress([FromQuery] BranchOfficeSearchQuery searchQuery, long companyId, CancellationToken token)
|
||||||
{
|
{
|
||||||
var company = await Context.Companies.Where(c => c.Id == companyId).FirstAsync(token);
|
var company = await Context.Companies.Where(c => c.Id == companyId).FirstAsync(token);
|
||||||
|
|
||||||
return await Context.Entry(company)
|
return await Context.Entry(company)
|
||||||
.Collection(c => c.Branches)
|
.Collection(c => c.Branches)
|
||||||
.Query()
|
.Query()
|
||||||
.Where(office => office.Address.City.Contains(searchQuery.Query.ToLower()))
|
.Where(office => office.Address.City.ToLower().Contains(searchQuery.City.ToLower()))
|
||||||
.Skip(searchQuery.Page * searchQuery.PerPage)
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
||||||
.Take(searchQuery.PerPage)
|
.Take(searchQuery.PerPage)
|
||||||
.ToListAsync(token);
|
.ToListAsync(token);
|
||||||
@ -79,6 +81,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
public async Task<ActionResult> UpdateCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken)
|
public async Task<ActionResult> UpdateCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var validator = new CompanyForm.Validator();
|
var validator = new CompanyForm.Validator();
|
||||||
@ -129,6 +132,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
public async Task<ActionResult> DeleteCompany(long companyId, CancellationToken cancellationToken)
|
public async Task<ActionResult> DeleteCompany(long companyId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var companyToDelete = await PractiseSupervisorDbContext.Companies
|
var companyToDelete = await PractiseSupervisorDbContext.Companies
|
||||||
@ -159,6 +163,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
public async Task<ActionResult> UpdateBranch([FromBody] BranchOfficeForm branchOfficeForm, long companyId, CancellationToken cancellationToken)
|
public async Task<ActionResult> UpdateBranch([FromBody] BranchOfficeForm branchOfficeForm, long companyId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var validator = new BranchOfficeForm.Validator();
|
var validator = new BranchOfficeForm.Validator();
|
||||||
@ -225,6 +230,7 @@ namespace InternshipSystem.Api.Controllers
|
|||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize]
|
||||||
public async Task<ActionResult> DeleteBranch(long branchOfficeId, CancellationToken cancellationToken)
|
public async Task<ActionResult> DeleteBranch(long branchOfficeId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var company =
|
var company =
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using InternshipSystem.Api.Queries;
|
|
||||||
using InternshipSystem.Api.Security;
|
|
||||||
using InternshipSystem.Core;
|
|
||||||
using InternshipSystem.Repository;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace InternshipSystem.Api.Controllers
|
|
||||||
{
|
|
||||||
[Route("student")]
|
|
||||||
[ApiController]
|
|
||||||
public class StudentController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly InternshipDbContext _context;
|
|
||||||
|
|
||||||
public StudentController(InternshipDbContext context)
|
|
||||||
{
|
|
||||||
_context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("current")]
|
|
||||||
[Authorize]
|
|
||||||
public async Task<Student> GetCurrentStudentData([FromServices] User user, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return await _context.Students.FindAsync(user.PersonNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPut("updateCurrent")]
|
|
||||||
[Authorize]
|
|
||||||
public async Task<ActionResult> UpdateStudentData([FromBody] StudentForm studentNewData, [FromServices] User user, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var validator = new StudentForm.Validator();
|
|
||||||
var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken);
|
|
||||||
|
|
||||||
if (!validationResult.IsValid || !user.PersonNumber.Equals(studentNewData.Id))
|
|
||||||
{
|
|
||||||
return BadRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentStudent = await _context.Students.FindAsync(user.PersonNumber);
|
|
||||||
|
|
||||||
currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber;
|
|
||||||
currentStudent.FirstName = string.IsNullOrEmpty(studentNewData.FirstName) ? currentStudent.FirstName : studentNewData.FirstName;
|
|
||||||
currentStudent.LastName = string.IsNullOrEmpty(studentNewData.LastName) ? currentStudent.LastName : studentNewData.LastName;
|
|
||||||
currentStudent.Email = string.IsNullOrEmpty(studentNewData.Email) ? currentStudent.Email : studentNewData.Email;
|
|
||||||
currentStudent.Course = string.IsNullOrEmpty(studentNewData.Course) ? currentStudent.Course : studentNewData.Course;
|
|
||||||
currentStudent.Semester = studentNewData.Semester ?? currentStudent.Semester;
|
|
||||||
|
|
||||||
await _context.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return Ok();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
99
src/InternshipSystem.Api/Controllers/StudentsController.cs
Normal file
99
src/InternshipSystem.Api/Controllers/StudentsController.cs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using InternshipSystem.Api.Queries;
|
||||||
|
using InternshipSystem.Api.Security;
|
||||||
|
using InternshipSystem.Core;
|
||||||
|
using InternshipSystem.Repository;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace InternshipSystem.Api.Controllers
|
||||||
|
{
|
||||||
|
[Route("students")]
|
||||||
|
[ApiController]
|
||||||
|
public class StudentsController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly InternshipDbContext _context;
|
||||||
|
|
||||||
|
public StudentsController(InternshipDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("current")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<Student>> GetCurrentStudentData([FromServices] User user, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return await _context.Students.FindAsync(user.PersonNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("current")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult> UpdateCurrentStudentData([FromBody] CurrentStudentForm studentNewData, [FromServices] User user, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var currentStudent = await _context.Students.FindAsync(user.PersonNumber);
|
||||||
|
|
||||||
|
currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber;
|
||||||
|
currentStudent.FirstName = string.IsNullOrEmpty(studentNewData.FirstName) ? currentStudent.FirstName : studentNewData.FirstName;
|
||||||
|
currentStudent.LastName = string.IsNullOrEmpty(studentNewData.LastName) ? currentStudent.LastName : studentNewData.LastName;
|
||||||
|
currentStudent.Email = string.IsNullOrEmpty(studentNewData.Email) ? currentStudent.Email : studentNewData.Email;
|
||||||
|
currentStudent.Course = string.IsNullOrEmpty(studentNewData.Course) ? currentStudent.Course : studentNewData.Course;
|
||||||
|
currentStudent.Semester = studentNewData.Semester ?? currentStudent.Semester;
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
return Ok($"Student updated successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{studentPersonNumber}")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<Student>> GetStudentByPersonNumber(long studentPersonNumber, CancellationToken cancellationToken) =>
|
||||||
|
await _context.Students.FindAsync(studentPersonNumber);
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<IReadOnlyCollection<Student>>> GetStudents([FromBody] 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()))
|
||||||
|
.Where(s => string.IsNullOrEmpty(searchQuery.LastName) || s.LastName.ToLower().Contains(searchQuery.LastName.ToLower()))
|
||||||
|
.OrderBy(s => s.AlbumNumber)
|
||||||
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
||||||
|
.Take(searchQuery.PerPage)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult> UpdateStudentData([FromBody] StudentForm studentNewData, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var validator = new StudentForm.Validator();
|
||||||
|
var validationResult = await validator.ValidateAsync(studentNewData, cancellationToken);
|
||||||
|
|
||||||
|
if (!validationResult.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(validationResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentStudent = await _context.Students.FindAsync(studentNewData.Id);
|
||||||
|
|
||||||
|
if (currentStudent == null)
|
||||||
|
{
|
||||||
|
return NotFound($"Student with id: {studentNewData.Id} does not exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
currentStudent.AlbumNumber = studentNewData.AlbumNumber ?? currentStudent.AlbumNumber;
|
||||||
|
currentStudent.FirstName = string.IsNullOrEmpty(studentNewData.FirstName) ? currentStudent.FirstName : studentNewData.FirstName;
|
||||||
|
currentStudent.LastName = string.IsNullOrEmpty(studentNewData.LastName) ? currentStudent.LastName : studentNewData.LastName;
|
||||||
|
currentStudent.Email = string.IsNullOrEmpty(studentNewData.Email) ? currentStudent.Email : studentNewData.Email;
|
||||||
|
currentStudent.Course = string.IsNullOrEmpty(studentNewData.Course) ? currentStudent.Course : studentNewData.Course;
|
||||||
|
currentStudent.Semester = studentNewData.Semester ?? currentStudent.Semester;
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
return Ok($"Student updated successfully");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/InternshipSystem.Api/Queries/CurrentStudentForm.cs
Normal file
12
src/InternshipSystem.Api/Queries/CurrentStudentForm.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace InternshipSystem.Api.Queries
|
||||||
|
{
|
||||||
|
public class CurrentStudentForm
|
||||||
|
{
|
||||||
|
public int? AlbumNumber { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Course { get; set; }
|
||||||
|
public int? Semester { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
using InternshipSystem.Api.Queries.SearchQuery;
|
||||||
|
|
||||||
|
namespace InternshipSystem.Api.Controllers
|
||||||
|
{
|
||||||
|
public class BranchOfficeSearchQuery : SearchQuery
|
||||||
|
{
|
||||||
|
public string City { get; set; } = "";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace InternshipSystem.Api.Queries.SearchQuery
|
||||||
|
{
|
||||||
|
public class CompanySearchQuery : SearchQuery
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Value against which collection will be queried
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,7 @@
|
|||||||
namespace InternshipSystem.Api.Queries
|
namespace InternshipSystem.Api.Queries.SearchQuery
|
||||||
{
|
{
|
||||||
public class SearchQuery
|
public class SearchQuery
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Value against which collection will be queried
|
|
||||||
/// </summary>
|
|
||||||
public string Query { get; set; } = "";
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Which part of the collections to retrieve
|
/// Which part of the collections to retrieve
|
||||||
/// </summary>
|
/// </summary>
|
@ -0,0 +1,11 @@
|
|||||||
|
using InternshipSystem.Api.Queries.SearchQuery;
|
||||||
|
|
||||||
|
namespace InternshipSystem.Api.Controllers
|
||||||
|
{
|
||||||
|
public class StudentSearchQuery : SearchQuery
|
||||||
|
{
|
||||||
|
public int? AlbumNumber { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@ namespace InternshipSystem.Api.Queries
|
|||||||
{
|
{
|
||||||
public class StudentForm
|
public class StudentForm
|
||||||
{
|
{
|
||||||
public long? Id { get; set; }
|
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; }
|
||||||
|
Loading…
Reference in New Issue
Block a user