111 lines
No EOL
4 KiB
C#
111 lines
No EOL
4 KiB
C#
// ***********************************************************************
|
|
// Assembly : Ultimate.Presentation
|
|
// Author : frede
|
|
// Created : 2023 06 11 15:25
|
|
//
|
|
// Last Modified By : frede
|
|
// Last Modified On : 2023 06 11 15:25
|
|
// ***********************************************************************
|
|
// <copyright file="EmployeesController.cs" company="FCS">
|
|
// Copyright (C) 2023-2023 FCS Frede's Computer Services.
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
|
// </copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
|
|
using Microsoft.AspNetCore.JsonPatch;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Service.Contracts;
|
|
using Shared.DataTransferObjects;
|
|
|
|
namespace Ultimate.Presentation.Controllers;
|
|
|
|
[Route("api/companies/{companyId}/employees")]
|
|
public class EmployeesController : ControllerBase
|
|
{
|
|
private readonly IServiceManager _service;
|
|
|
|
public EmployeesController(IServiceManager service)
|
|
{
|
|
_service = service;
|
|
}
|
|
|
|
|
|
[HttpGet]
|
|
public IActionResult GetEmployeesForCompany(Guid companyId)
|
|
{
|
|
var employees = _service.EmployeeService.GetEmployees(companyId, trackChanges: false);
|
|
return Ok(employees);
|
|
}
|
|
|
|
|
|
[HttpGet("{employeeId:guid}", Name = "GetEmployeeForCompany")]
|
|
public IActionResult GetEmployeeForCompany(Guid companyId, Guid employeeId)
|
|
{
|
|
var employee = _service.EmployeeService.GetEmployee(companyId, employeeId, trackChanges: false);
|
|
return Ok(employee);
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public IActionResult CreateEmployeeForCompany(Guid companyId, [FromBody] EmployeeCreateDto? employee)
|
|
{
|
|
if (employee == null)
|
|
{
|
|
return BadRequest("Employee is null");
|
|
}
|
|
|
|
var result = _service.EmployeeService.CreateEmployeeForCompany(companyId, employee, trackChanges: false);
|
|
|
|
return CreatedAtRoute("GetEmployeeForCompany", new { companyId, employeeId = result.Id }, result);
|
|
}
|
|
|
|
[HttpDelete("{employeeId:guid", Name = "DeleteEmployeeForCompany")]
|
|
public IActionResult DeleteEmployeeForCompany(Guid companyId, Guid employeeId)
|
|
{
|
|
_service.EmployeeService.DeleteEmployeeForCompany(companyId, employeeId, trackChanges:false);
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpPut("{employeeId:guid", Name = "UpdateEmployeeForCompany")]
|
|
public IActionResult UpdateEmployeeForCompany(Guid companyId, Guid employeeId,
|
|
[FromBody] EmployeeUpdateDto? employee)
|
|
{
|
|
if (employee is null)
|
|
{
|
|
return BadRequest("Employee data object is null");
|
|
}
|
|
|
|
_service.EmployeeService.UpdateEmployeeForCompany(companyId, employeeId, employee, companyTrackChanges:false, employeeTrackChanges:true);
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
[HttpPatch("{employeeId:guid", Name = "PatchEmployeeForCompany")]
|
|
public IActionResult PatchEmployeeForCompany(Guid companyId, Guid employeeId,
|
|
[FromBody] JsonPatchDocument<EmployeeUpdateDto>? patchDoc)
|
|
{
|
|
if (patchDoc is null)
|
|
return BadRequest("patchDoc object is null");
|
|
var (employeeToPatch, employeeEntity) = _service.EmployeeService
|
|
.GetEmployeeForPatch(companyId, employeeId, companyTrackChanges: false, employeeTrackChanges: true);
|
|
|
|
patchDoc.ApplyTo(employeeToPatch);
|
|
|
|
_service.EmployeeService.SaveChangesForPatch(employeeToPatch, employeeEntity);
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
} |