mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 08:23:12 +00:00
154 lines
4.7 KiB
C#
154 lines
4.7 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 = "")
|
|
{
|
|
search ??= "";
|
|
|
|
var data = _db.Employees
|
|
.Include(e => e.JobTitle)
|
|
.Include(e => e.Emails)
|
|
.Include(e => e.Phones)
|
|
.Where(e => !e.IsDeleted.HasValue || e.IsDeleted == false)
|
|
.Select(e => new EmployeeListItem_DTO
|
|
{
|
|
Id = e.Id,
|
|
Name = e.FirstName + " " + e.LastName,
|
|
JobTitle = e.JobTitle != null ? e.JobTitle.Name : "",
|
|
Role = "",
|
|
Email = e.Emails.Where(x => x.IsPrimary == true).Select(x => x.Address).FirstOrDefault(),
|
|
Phone = e.Phones.Where(x => x.IsPrimary == true)
|
|
.Select(x => x.Number + (x.Extension != null ? " ext. " + x.Extension : ""))
|
|
.FirstOrDefault(),
|
|
Status = e.Status,
|
|
LastLogin = e.LastLogin
|
|
})
|
|
.ToList();
|
|
|
|
return Ok(data);
|
|
}
|
|
// -----------------------------------
|
|
// 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
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|