mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 14:13:12 +00:00
220 lines
7.1 KiB
C#
220 lines
7.1 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using FluentValidation;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.Services.Interfaces;
|
|
using SeaHaven.Services.DTOs;
|
|
using System.Security.Claims;
|
|
|
|
[ApiController]
|
|
[Route("api/Employee")]
|
|
[Route("api/employees")]
|
|
public class EmployeeController : Controller
|
|
{
|
|
private readonly IEmployeeService _employeeService;
|
|
|
|
public EmployeeController(IEmployeeService employeeService)
|
|
{
|
|
_employeeService = employeeService;
|
|
}
|
|
|
|
// -----------------------------------
|
|
// POST: api/Employee/AddEmployees or /Create
|
|
// -----------------------------------
|
|
[HttpPost("AddEmployees")]
|
|
[HttpPost("Create")]
|
|
public async Task<IActionResult> AddEmployees(AddEmployee_DTO dto)
|
|
{
|
|
try
|
|
{
|
|
var createDto = new CreateEmployeeDTO
|
|
{
|
|
FirstName = dto.FirstName,
|
|
LastName = dto.LastName,
|
|
JobTitleId = dto.JobTitleId,
|
|
DepartmentId = null, // Map from PermissionGroupId if needed
|
|
Email = dto.Email,
|
|
Phone = dto.Phone
|
|
};
|
|
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
if (userId == null)
|
|
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
|
|
|
|
await _employeeService.CreateEmployeeAsync(createDto, userId);
|
|
|
|
return Ok(new { Message = "Employee added successfully" });
|
|
}
|
|
catch (ValidationException vex)
|
|
{
|
|
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
|
|
return BadRequest(new Response { Status = "Validation Error", Message = errors });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// POST: api/Employee/EditEmployees or /Update
|
|
// -----------------------------------
|
|
[HttpPost("EditEmployees")]
|
|
[HttpPost("Update")]
|
|
public async Task<IActionResult> EditEmployees(EditEmployee_DTO dto)
|
|
{
|
|
try
|
|
{
|
|
var updateDto = new UpdateEmployeeDTO
|
|
{
|
|
FirstName = dto.FirstName,
|
|
LastName = dto.LastName,
|
|
JobTitleId = dto.JobTitleId
|
|
};
|
|
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
if (userId == null)
|
|
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
|
|
|
|
await _employeeService.UpdateEmployeeAsync(dto.Id, updateDto, userId);
|
|
|
|
return Ok(new { Message = "Employee updated successfully" });
|
|
}
|
|
catch (ValidationException vex)
|
|
{
|
|
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
|
|
return BadRequest(new Response { Status = "Validation Error", Message = errors });
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound(new Response { Status = "Error", Message = "Employee not found" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// GET: api/Employee/GetEmployeeList
|
|
// -----------------------------------
|
|
[HttpGet("GetEmployeeList")]
|
|
public async Task<IActionResult> GetEmployeeList(string? search = "", int page = 1, int pageSize = 12)
|
|
{
|
|
var pagedResult = await _employeeService.GetEmployeesPagedAsync(page, pageSize, search);
|
|
|
|
var data = pagedResult.Items.Select(e => new
|
|
{
|
|
id = e.Id,
|
|
firstName = e.FirstName,
|
|
lastName = e.LastName,
|
|
jobTitle = e.JobTitleName ?? "",
|
|
email = e.Email ?? "",
|
|
phone = e.Phone ?? "",
|
|
department = e.DepartmentName ?? "",
|
|
status = "Active" // Map from employee status if available
|
|
});
|
|
|
|
var totalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize);
|
|
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = data,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = pagedResult.TotalCount,
|
|
TotalPages = totalPages
|
|
};
|
|
|
|
return Ok(viewModel);
|
|
}
|
|
// -----------------------------------
|
|
// GET: api/Employee/GetById?id=5
|
|
// -----------------------------------
|
|
[HttpGet("GetById")]
|
|
public async Task<IActionResult> GetById(int id)
|
|
{
|
|
try
|
|
{
|
|
var employee = await _employeeService.GetEmployeeByIdAsync(id);
|
|
|
|
return Ok(new
|
|
{
|
|
id = employee.Id,
|
|
firstName = employee.FirstName,
|
|
lastName = employee.LastName,
|
|
jobTitle = employee.JobTitleName ?? "",
|
|
email = employee.Email ?? "",
|
|
phone = employee.Phone ?? "",
|
|
department = employee.DepartmentName ?? "",
|
|
status = "Active"
|
|
});
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound(new Response { Status = "Error", Message = "Employee not found" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// GET: api/Employee/DeleteEmployees or /Delete?id=5
|
|
// -----------------------------------
|
|
[HttpGet("DeleteEmployees")]
|
|
[HttpPost("Delete")]
|
|
public async Task<IActionResult> DeleteEmployees(int id)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
if (userId == null)
|
|
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
|
|
|
|
await _employeeService.DeleteEmployeeAsync(id, userId);
|
|
|
|
return Ok(new DataResponse
|
|
{
|
|
Message = "Employee deleted successfully.",
|
|
Status = "200"
|
|
});
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return BadRequest(new Response
|
|
{
|
|
Status = "Error",
|
|
Message = ex.Message
|
|
});
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound(new Response { Status = "Error", Message = "Employee not found" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
// -----------------------------------
|
|
// GET: api/Employee/Dropdown
|
|
// -----------------------------------
|
|
[HttpGet("Dropdown")]
|
|
public async Task<IActionResult> GetDropdown()
|
|
{
|
|
var pagedResult = await _employeeService.GetEmployeesPagedAsync(1, 1000, "");
|
|
|
|
var data = pagedResult.Items.Select(e => new
|
|
{
|
|
id = e.Id,
|
|
name = $"{e.FirstName} {e.LastName}".Trim()
|
|
});
|
|
|
|
return Ok(data);
|
|
}
|
|
|
|
}
|