mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 13:03:12 +00:00
190 lines
6.2 KiB
C#
190 lines
6.2 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
[ApiController]
|
|
[Route("api/Employee")]
|
|
public class EmployeeController : Controller
|
|
{
|
|
private readonly ApplicationDbContext _db;
|
|
|
|
public EmployeeController(ApplicationDbContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
// -----------------------------------
|
|
// POST: api/Employee/AddEmployees
|
|
// -----------------------------------
|
|
[HttpPost("AddEmployees")]
|
|
public async Task<IActionResult> AddEmployees(AddEmployee_DTO dto)
|
|
{
|
|
var employee = new Employee
|
|
{
|
|
FirstName = dto.FirstName,
|
|
LastName = dto.LastName,
|
|
JobTitleId = dto.JobTitleId,
|
|
PermissionGroupId = dto.PermissionGroupId,
|
|
Status = dto.Status,
|
|
LastLogin = dto.LastLogin,
|
|
CreatedDate = DateTime.UtcNow
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(dto.Email))
|
|
{
|
|
employee.Emails.Add(new EmployeeEmail
|
|
{
|
|
Address = dto.Email,
|
|
IsPrimary = true,
|
|
EmailTypeId = 1
|
|
});
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(dto.Phone))
|
|
{
|
|
employee.Phones.Add(new EmployeePhone
|
|
{
|
|
Number = dto.Phone,
|
|
Extension = dto.PhoneExtension,
|
|
IsPrimary = true,
|
|
PhoneTypeId = 1
|
|
});
|
|
}
|
|
|
|
_db.Employees.Add(employee);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(new { Message = "Employee added successfully" });
|
|
}
|
|
|
|
// -----------------------------------
|
|
// POST: api/Employee/EditEmployees
|
|
// -----------------------------------
|
|
[HttpPost("EditEmployees")]
|
|
public async Task<IActionResult> EditEmployees(EditEmployee_DTO dto)
|
|
{
|
|
var employee = await _db.Employees
|
|
.Include(e => e.Emails)
|
|
.Include(e => e.Phones)
|
|
.FirstOrDefaultAsync(e => e.Id == dto.Id);
|
|
|
|
if (employee == null)
|
|
return BadRequest("Employee not found");
|
|
|
|
employee.FirstName = dto.FirstName;
|
|
employee.LastName = dto.LastName;
|
|
employee.JobTitleId = dto.JobTitleId;
|
|
employee.PermissionGroupId = dto.PermissionGroupId;
|
|
employee.Status = dto.Status;
|
|
employee.LastLogin = dto.LastLogin;
|
|
employee.LastModificationTime = DateTime.UtcNow;
|
|
|
|
await _db.SaveChangesAsync();
|
|
return Ok(new { Message = "Employee updated successfully" });
|
|
}
|
|
|
|
// -----------------------------------
|
|
// GET: api/Employee/GetEmployeeList
|
|
// -----------------------------------
|
|
[HttpGet("GetEmployeeList")]
|
|
public IActionResult GetEmployeeList(string? search = "", int page = 1, int pageSize = 12)
|
|
{
|
|
search ??= "";
|
|
|
|
var totalCount = _db.Employees.Where(e => !e.IsDeleted.HasValue || e.IsDeleted == false).Count();
|
|
|
|
var data = _db.Employees
|
|
.Include(e => e.JobTitle)
|
|
.Include(e => e.Department)
|
|
.Include(e => e.Emails)
|
|
.Include(e => e.Phones)
|
|
.Where(e => !e.IsDeleted.HasValue || e.IsDeleted == false)
|
|
.Where(e =>
|
|
string.IsNullOrWhiteSpace(search) ||
|
|
(e.FirstName != null && e.FirstName.ToLower().Contains(search.ToLower())) ||
|
|
(e.LastName != null && e.LastName.ToLower().Contains(search.ToLower())) ||
|
|
e.Emails.Any(x => x.Address != null && x.Address.ToLower().Contains(search.ToLower())) ||
|
|
e.Phones.Any(x => x.Number != null && x.Number.Contains(search))
|
|
)
|
|
.Select(e => new
|
|
{
|
|
e.Id,
|
|
e.FirstName,
|
|
e.LastName,
|
|
JobTitleName = e.JobTitle != null ? e.JobTitle.Name : "",
|
|
DepartmentName = e.Department != null ? e.Department.Name : "",
|
|
e.Status,
|
|
PrimaryEmail = e.Emails.Where(x => x.IsPrimary == true).Select(x => x.Address).FirstOrDefault(),
|
|
PrimaryPhone = e.Phones.Where(x => x.IsPrimary == true).Select(x => x.Number).FirstOrDefault(),
|
|
PhoneExtension = e.Phones.Where(x => x.IsPrimary == true).Select(x => x.Extension).FirstOrDefault()
|
|
})
|
|
.OrderByDescending(e => e.Id)
|
|
.ToList()
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(e => new
|
|
{
|
|
id = e.Id,
|
|
firstName = e.FirstName,
|
|
lastName = e.LastName,
|
|
jobTitle = e.JobTitleName ?? "",
|
|
email = e.PrimaryEmail ?? "",
|
|
phone = e.PrimaryPhone != null
|
|
? e.PrimaryPhone + (e.PhoneExtension != null ? " ext. " + e.PhoneExtension : "")
|
|
: "",
|
|
department = e.DepartmentName ?? "",
|
|
status = e.Status
|
|
});
|
|
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = data,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = totalPages
|
|
};
|
|
|
|
return Ok(viewModel);
|
|
}
|
|
// -----------------------------------
|
|
// GET: api/Employee/DeleteEmployees?id=5
|
|
// -----------------------------------
|
|
[HttpGet("DeleteEmployees")]
|
|
public async Task<IActionResult> DeleteEmployees(int id)
|
|
{
|
|
try
|
|
{
|
|
var employee = await _db.Employees
|
|
.FirstOrDefaultAsync(e => e.Id == id);
|
|
|
|
if (employee == null)
|
|
return BadRequest(new Response
|
|
{
|
|
Status = "Error",
|
|
Message = "Employee not found."
|
|
});
|
|
|
|
_db.Employees.Remove(employee);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(new DataResponse
|
|
{
|
|
Message = "Employee deleted successfully.",
|
|
Status = "200"
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response
|
|
{
|
|
Status = "Error",
|
|
Message = ex.Message
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|