252 lines
11 KiB
C#
252 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="404">Company not found</response>
|
|
/// <returns></returns>
|
|
[HttpPut]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> UpdateCompany([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);
|
|
|
|
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");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var newCompany = new Company
|
|
{
|
|
Name = companyForm.Name,
|
|
Nip = companyForm.Nip,
|
|
};
|
|
await Context.Companies.AddAsync(newCompany, cancellationToken);
|
|
}
|
|
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok($"Company updated successfully");
|
|
}
|
|
|
|
/// <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="404">Company not found</response>
|
|
/// <returns></returns>
|
|
[HttpDelete("{companyId}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[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.Equals(companyId), cancellationToken: cancellationToken);
|
|
|
|
if (companyToDelete == null)
|
|
{
|
|
return NotFound($"Company with id: {companyId} does not exist");
|
|
}
|
|
|
|
Context.Companies.Attach(companyToDelete);
|
|
Context.Companies.Remove(companyToDelete);
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok($"Company with id: {companyId} deleted successfully");
|
|
}
|
|
|
|
/// <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="404">Company or branch office not found</response>
|
|
/// <returns></returns>
|
|
[HttpPut("branchOffice/{companyId}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[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.Equals(companyId), cancellationToken: cancellationToken);
|
|
|
|
if (company == null)
|
|
{
|
|
return NotFound($"Company with id: {companyId} does not exist");
|
|
}
|
|
|
|
if (branchOfficeForm.Id.HasValue)
|
|
{
|
|
var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeForm.Id.Value));
|
|
|
|
if (branchOffice == null)
|
|
{
|
|
return NotFound($"Branch office with id: {branchOfficeForm.Id} does not exist");
|
|
}
|
|
|
|
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($"Branch office updated successfully");
|
|
}
|
|
|
|
/// <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="404">Company or branch office not found</response>
|
|
/// <returns></returns>
|
|
[HttpDelete("branchOffice/{branchOfficeId}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[Authorize]
|
|
public async Task<ActionResult> DeleteBranch(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);
|
|
|
|
if (company == null)
|
|
{
|
|
return NotFound($"Branch office with id: {branchOfficeId} does not exist");
|
|
}
|
|
|
|
var branchOffice = company.Branches.Find(b => b.Id.Equals(branchOfficeId));
|
|
company.Branches.Remove(branchOffice);
|
|
|
|
await Context.SaveChangesAsync(cancellationToken);
|
|
return Ok($"Branch office with id: {branchOfficeId} deleted successfully");
|
|
}
|
|
}
|
|
} |