251 lines
11 KiB
C#
251 lines
11 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using InternshipSystem.Api.Queries;
|
|
using InternshipSystem.Api.Queries.SearchQuery;
|
|
using InternshipSystem.Core;
|
|
using InternshipSystem.Repository;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using static System.String;
|
|
|
|
namespace InternshipSystem.Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("companies")]
|
|
public class CompaniesController : ControllerBase
|
|
{
|
|
public CompaniesController(InternshipDbContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
|
|
private InternshipDbContext Context { get; }
|
|
|
|
/// <summary>
|
|
/// Get companies matching provided paginated query
|
|
/// </summary>
|
|
/// <param name="searchQuery">Paginated query description</param>
|
|
/// <response code="200">Successfully retrieved Companies</response>
|
|
/// <response code="400">Search query was malformed</response>
|
|
/// <returns>Part of companies collection</returns>
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<ActionResult<IReadOnlyCollection<Company>>> SearchByNameAsync([FromQuery] CompanySearchQuery searchQuery, CancellationToken cancellationToken) =>
|
|
await Context.Companies
|
|
.Where(c => c.Name.ToLower().Contains(searchQuery.Name.ToLower()))
|
|
.OrderBy(o => o.Name)
|
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
|
.Take(searchQuery.PerPage)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
/// <summary>
|
|
/// Get company branches matching provided paginated query
|
|
/// </summary>
|
|
/// <param name="searchQuery">Paginated query description</param>
|
|
/// <param name="companyId"></param>
|
|
/// <response code="200">Successfully retrieved matching offices</response>
|
|
/// <response code="400">Search query was malformed</response>
|
|
/// <returns>Part of companies collection</returns>
|
|
[HttpGet("{companyId}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
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);
|
|
|
|
return await Context.Entry(company)
|
|
.Collection(c => c.Branches)
|
|
.Query()
|
|
.Where(office => office.Address.City.ToLower().Contains(searchQuery.City.ToLower()))
|
|
.Skip(searchQuery.Page * searchQuery.PerPage)
|
|
.Take(searchQuery.PerPage)
|
|
.ToListAsync(token);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates or add new company (if not new than contains id)
|
|
/// </summary>
|
|
/// <param name="companyForm"></param>
|
|
/// <response code="200">Successfully updated company</response>
|
|
/// <response code="400">Company form was malformed</response>
|
|
/// <response code="401">This action is only available for authorized internship admin</response>
|
|
/// <response code="404">Company not found</response>
|
|
/// <returns></returns>
|
|
[HttpPut]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> UpsertCompany([FromBody] CompanyForm companyForm, CancellationToken cancellationToken)
|
|
{
|
|
var validator = new CompanyForm.Validator();
|
|
var validationResult = await validator.ValidateAsync(companyForm, cancellationToken);
|
|
|
|
if (!validationResult.IsValid)
|
|
{
|
|
return BadRequest(validationResult.ToString());
|
|
}
|
|
|
|
if (companyForm.Id.HasValue)
|
|
{
|
|
var companyToUpdate = await Context.Companies.FindAsync(companyForm.Id.Value);
|
|
|
|
if (companyToUpdate == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
companyToUpdate.Name = IsNullOrEmpty(companyForm.Name) ? companyToUpdate.Name : companyForm.Name;
|
|
companyToUpdate.Nip = IsNullOrEmpty(companyForm.Nip) ? companyToUpdate.Nip : companyForm.Nip;
|
|
}
|
|
else
|
|
{
|
|
var newCompany = Company.CreateCompany(companyForm.Nip, companyForm.Name);
|
|
await Context.Companies.AddAsync(newCompany, cancellationToken);
|
|
}
|
|
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes existing company by id
|
|
/// </summary>
|
|
/// <param name="companyId"></param>
|
|
/// <response code="200">Successfully deleted company</response>
|
|
/// <response code="400">Company id is empty</response>
|
|
/// <response code="401">This action is only available for authorized internship admin</response>
|
|
/// <response code="404">Company not found</response>
|
|
/// <returns></returns>
|
|
[HttpDelete("{companyId}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> DeleteCompany(long companyId, CancellationToken cancellationToken)
|
|
{
|
|
var companyToDelete = await Context.Companies
|
|
.Include(c => c.Branches)
|
|
.FirstOrDefaultAsync(c => c.Id == companyId, cancellationToken);
|
|
|
|
if (companyToDelete == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
Context.Companies.Remove(companyToDelete);
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates or add new branchOffice (if not new than contains id)
|
|
/// </summary>
|
|
/// <param name="branchOfficeForm"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <response code="200">Successfully updated company branch office</response>
|
|
/// <response code="400">Branch office was malformed</response>
|
|
/// <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("{companyId}/branchOffices")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> UpdateBranch([FromBody] BranchOfficeForm branchOfficeForm, long companyId, CancellationToken cancellationToken)
|
|
{
|
|
var validator = new BranchOfficeForm.Validator();
|
|
var validationResult = await validator.ValidateAsync(branchOfficeForm, cancellationToken);
|
|
|
|
if (!validationResult.IsValid)
|
|
{
|
|
return BadRequest(validationResult.ToString());
|
|
}
|
|
|
|
var company = await Context.Companies
|
|
.Include(c => c.Branches)
|
|
.FirstOrDefaultAsync(c => c.Id == companyId, cancellationToken);
|
|
|
|
if (company == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
if (branchOfficeForm.Id.HasValue)
|
|
{
|
|
var branchOffice = company.Branches.First(b => b.Id == branchOfficeForm.Id);
|
|
|
|
if (branchOffice == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
branchOffice.Address.Country = IsNullOrEmpty(branchOfficeForm.Country) ? branchOffice.Address.Country : branchOfficeForm.Country;
|
|
branchOffice.Address.City = IsNullOrEmpty(branchOfficeForm.City) ? branchOffice.Address.City : branchOfficeForm.City;
|
|
branchOffice.Address.PostalCode = IsNullOrEmpty(branchOfficeForm.PostalCode) ? branchOffice.Address.PostalCode : branchOfficeForm.PostalCode;
|
|
branchOffice.Address.Street = IsNullOrEmpty(branchOfficeForm.Street) ? branchOffice.Address.Street : branchOfficeForm.Street;
|
|
branchOffice.Address.Building = IsNullOrEmpty(branchOfficeForm.Building) ? branchOffice.Address.Building : branchOfficeForm.Building;
|
|
}
|
|
else
|
|
{
|
|
var newBranchOffice = new BranchOffice
|
|
{
|
|
Address = new BranchAddress
|
|
{
|
|
Country = branchOfficeForm.Country,
|
|
City = branchOfficeForm.City,
|
|
PostalCode = branchOfficeForm.PostalCode,
|
|
Street = branchOfficeForm.Street,
|
|
Building = branchOfficeForm.Building,
|
|
}
|
|
};
|
|
company.Branches.Add(newBranchOffice);
|
|
}
|
|
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes existing branchOffice
|
|
/// </summary>
|
|
/// <param name="branchOfficeId"></param>
|
|
/// <response code="200">Successfully deleted company branch office</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="404">Company or branch office not found</response>
|
|
[HttpDelete("{companyId}/branchOffice/{branchOfficeId}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> DeleteBranch(long companyId, long branchOfficeId, CancellationToken cancellationToken)
|
|
{
|
|
var company =
|
|
await Context.Companies
|
|
.Include(c => c.Branches)
|
|
.Where(c => c.Id == companyId)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
if (company == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var branchOffice = company.Branches.RemoveAll(b => b.Id == branchOfficeId);
|
|
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok();
|
|
}
|
|
}
|
|
} |