shoc-backend/Api.SeaHavenIndustries/Controllers/EmployeeController.cs

221 lines
7.1 KiB
C#
Raw Normal View History

2026-01-21 15:41:19 -06:00
using Api.SeaHavenIndustries.DTOs;
using Data.SeaHavenIndustries;
2026-05-14 11:00:12 -05:00
using FluentValidation;
2026-01-21 15:41:19 -06:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2026-05-06 10:49:33 -05:00
using SeaHaven.Services.Interfaces;
using SeaHaven.Services.DTOs;
2026-05-14 11:00:12 -05:00
using System.Security.Claims;
2026-01-21 15:41:19 -06:00
[ApiController]
[Route("api/Employee")]
[Route("api/employees")]
2026-01-21 15:41:19 -06:00
public class EmployeeController : Controller
{
2026-05-06 10:49:33 -05:00
private readonly IEmployeeService _employeeService;
2026-01-21 15:41:19 -06:00
2026-05-06 10:49:33 -05:00
public EmployeeController(IEmployeeService employeeService)
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
_employeeService = employeeService;
2026-01-21 15:41:19 -06:00
}
// -----------------------------------
2026-05-14 11:00:12 -05:00
// POST: api/Employee/AddEmployees or /Create
2026-01-21 15:41:19 -06:00
// -----------------------------------
[HttpPost("AddEmployees")]
2026-05-14 11:00:12 -05:00
[HttpPost("Create")]
2026-01-21 15:41:19 -06:00
public async Task<IActionResult> AddEmployees(AddEmployee_DTO dto)
{
2026-05-06 10:49:33 -05:00
try
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
var createDto = new CreateEmployeeDTO
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
FirstName = dto.FirstName,
LastName = dto.LastName,
JobTitleId = dto.JobTitleId,
DepartmentId = null, // Map from PermissionGroupId if needed
Email = dto.Email,
Phone = dto.Phone
};
2026-05-14 11:00:12 -05:00
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
await _employeeService.CreateEmployeeAsync(createDto, userId);
return Ok(new { Message = "Employee added successfully" });
2026-01-21 15:41:19 -06:00
}
2026-05-14 11:00:12 -05:00
catch (ValidationException vex)
{
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
return BadRequest(new Response { Status = "Validation Error", Message = errors });
}
2026-05-06 10:49:33 -05:00
catch (Exception ex)
2026-01-21 15:41:19 -06:00
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2026-01-21 15:41:19 -06:00
}
}
// -----------------------------------
2026-05-14 11:00:12 -05:00
// POST: api/Employee/EditEmployees or /Update
2026-01-21 15:41:19 -06:00
// -----------------------------------
[HttpPost("EditEmployees")]
2026-05-14 11:00:12 -05:00
[HttpPost("Update")]
2026-01-21 15:41:19 -06:00
public async Task<IActionResult> EditEmployees(EditEmployee_DTO dto)
{
2026-05-06 10:49:33 -05:00
try
{
var updateDto = new UpdateEmployeeDTO
{
FirstName = dto.FirstName,
LastName = dto.LastName,
JobTitleId = dto.JobTitleId
};
2026-01-21 15:41:19 -06:00
2026-05-14 11:00:12 -05:00
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
await _employeeService.UpdateEmployeeAsync(dto.Id, updateDto, userId);
2026-01-21 15:41:19 -06:00
2026-05-06 10:49:33 -05:00
return Ok(new { Message = "Employee updated successfully" });
}
2026-05-14 11:00:12 -05:00
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" });
}
2026-05-06 10:49:33 -05:00
catch (Exception ex)
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2026-05-06 10:49:33 -05:00
}
2026-01-21 15:41:19 -06:00
}
// -----------------------------------
// GET: api/Employee/GetEmployeeList
// -----------------------------------
[HttpGet("GetEmployeeList")]
2026-05-06 10:49:33 -05:00
public async Task<IActionResult> GetEmployeeList(string? search = "", int page = 1, int pageSize = 12)
2026-01-21 15:41:19 -06:00
{
2026-05-06 10:49:33 -05:00
var pagedResult = await _employeeService.GetEmployeesPagedAsync(page, pageSize, search);
2026-03-30 06:49:40 -05:00
2026-05-06 10:49:33 -05:00
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);
2026-03-30 06:49:40 -05:00
var viewModel = new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
2026-05-06 10:49:33 -05:00
TotalCount = pagedResult.TotalCount,
2026-03-30 06:49:40 -05:00
TotalPages = totalPages
};
2026-01-21 15:41:19 -06:00
2026-03-30 06:49:40 -05:00
return Ok(viewModel);
2026-01-21 15:41:19 -06:00
}
// -----------------------------------
2026-05-14 11:00:12 -05:00
// 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
2026-01-21 15:41:19 -06:00
// -----------------------------------
[HttpGet("DeleteEmployees")]
2026-05-14 11:00:12 -05:00
[HttpPost("Delete")]
2026-01-21 15:41:19 -06:00
public async Task<IActionResult> DeleteEmployees(int id)
{
try
{
2026-05-14 11:00:12 -05:00
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
2026-05-06 10:49:33 -05:00
await _employeeService.DeleteEmployeeAsync(id, userId);
2026-01-21 15:41:19 -06:00
return Ok(new DataResponse
{
Message = "Employee deleted successfully.",
Status = "200"
});
}
2026-05-06 10:49:33 -05:00
catch (InvalidOperationException ex)
{
return BadRequest(new Response
{
Status = "Error",
Message = ex.Message
});
}
2026-05-14 11:00:12 -05:00
catch (KeyNotFoundException)
{
return NotFound(new Response { Status = "Error", Message = "Employee not found" });
}
2026-01-21 15:41:19 -06:00
catch (Exception ex)
{
2026-05-14 11:00:12 -05:00
return StatusCode(500, new Response { Status = "Error", Message = ex.Message });
2026-01-21 15:41:19 -06:00
}
}
2026-05-14 11:00:12 -05:00
// -----------------------------------
// 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);
}
2026-01-21 15:41:19 -06:00
}