using Api.SeaHavenIndustries.DTOs; using Data.SeaHavenIndustries; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; [ApiController] [Route("api/FollowUp")] public class FollowUpController : ControllerBase { private readonly ApplicationDbContext _db; public FollowUpController(ApplicationDbContext db) { _db = db; } [HttpGet("GetFollowUpList")] public IActionResult GetFollowUps( string? search = "", bool completed = false, int page = 1, int pageSize = 12) { var status = completed ? "Completed" : "Open"; var query = _db.FollowUps .Include(x => x.Employee) .Include(x => x.Account) .Include(x => x.Location) .Include(x => x.WorkOrder) .Where(x => x.Status == status); if (!string.IsNullOrWhiteSpace(search)) { search = search.ToLower(); query = query.Where(x => (x.Reason ?? "").ToLower().Contains(search) || (x.Account != null && (x.Account.Name ?? "").ToLower().Contains(search)) || (x.Employee != null && ( ((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? "")) .ToLower() .Contains(search) ) ) ); } var totalCount = query.Count(); var data = query .OrderByDescending(x => x.Id) .Skip((page - 1) * pageSize) .Take(pageSize) .Select(x => new { x.Id, x.Reason, x.ScheduleDate, Account = x.Account != null ? x.Account.Name : "", AssignedTo = x.Employee != null ? (x.Employee.FirstName + " " + x.Employee.LastName) : "", LocationName = x.Location != null ? x.Location.Name : "", WorkOrderNumber = x.WorkOrder != null ? x.WorkOrder.WorkerOrderNumber : "", x.Status }) .ToList(); return Ok(new Pagination_DTO { Data = data, PageNumber = page, PageSize = pageSize, TotalCount = totalCount, TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize) }); } [HttpPost("Create")] public async Task Create(FollowUp_DTO dto) { var model = new FollowUps { Reason = dto.Reason, ScheduleDate = dto.ScheduleDate, ScheduleStartTime = dto.ScheduleStartTime, EmployeeId = dto.EmployeeId, WorkOrderId = dto.WorkOrderId, AccountId = dto.AccountId, LocationId = dto.LocationId, Status = "Open", CreatedDate = DateTime.UtcNow }; _db.FollowUps.Add(model); await _db.SaveChangesAsync(); return Ok(new DataResponse { Status = "200", Message = "Follow Up created" }); } [HttpPost("Update")] public async Task Update(FollowUp_DTO dto) { var model = await _db.FollowUps.FindAsync(dto.Id); if (model == null) return NotFound(); model.Reason = dto.Reason; model.ScheduleDate = dto.ScheduleDate; model.ScheduleStartTime = dto.ScheduleStartTime; model.EmployeeId = dto.EmployeeId; model.WorkOrderId = dto.WorkOrderId; model.AccountId = dto.AccountId; model.LocationId = dto.LocationId; model.LastModificationTime = DateTime.UtcNow; await _db.SaveChangesAsync(); return Ok(new DataResponse { Status = "200", Message = "Follow Up updated" }); } [HttpPost("Complete")] public async Task Complete(int id) { var model = await _db.FollowUps.FindAsync(id); if (model == null) return NotFound(); model.Status = "Completed"; model.LastModificationTime = DateTime.UtcNow; await _db.SaveChangesAsync(); return Ok(); } [HttpPost("Cancel")] public async Task Cancel(int id) { var model = await _db.FollowUps.FindAsync(id); if (model == null) return NotFound(); model.Status = "Cancelled"; model.LastModificationTime = DateTime.UtcNow; await _db.SaveChangesAsync(); return Ok(); } }