PSTW_CentralizeSystem/Controllers/API/OvertimeAPI.cs
2025-03-26 16:41:41 +08:00

368 lines
12 KiB
C#

using Azure.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Mono.TextTemplating;
using Newtonsoft.Json;
using PSTW_CentralSystem.Areas.OTcalculate.Models;
using PSTW_CentralSystem.Controllers.API;
using PSTW_CentralSystem.Controllers.API.Inventory;
using PSTW_CentralSystem.DBContext;
using PSTW_CentralSystem.Models;
using System.ComponentModel.Design;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using static System.Collections.Specialized.BitVector32;
namespace PSTW_CentralSystem.Controllers.API
{
[ApiController]
[Route("[controller]")]
public class OvertimeAPI : Controller
{
private readonly ILogger<OvertimeAPI> _logger;
private readonly CentralSystemContext _centralDbContext;
private readonly UserManager<UserModel> _userManager;
public OvertimeAPI(ILogger<OvertimeAPI> logger, CentralSystemContext centralDbContext, UserManager<UserModel> userManager)
{
_logger = logger;
_centralDbContext = centralDbContext;
_userManager = userManager;
}
#region Settings
[HttpGet("GetUpdateDates")]
public IActionResult GetUpdateDates()
{
try
{
var latestRateUpdate = _centralDbContext.Rates.OrderByDescending(r => r.LastUpdated).FirstOrDefault()?.LastUpdated;
var latestCalendarUpdate = _centralDbContext.Holidays.OrderByDescending(c => c.LastUpdated).FirstOrDefault()?.LastUpdated;
var updateDates = new
{
rateUpdateDate = latestRateUpdate.HasValue ? latestRateUpdate.Value.ToString("dd MMMM yyyy") : null,
calendarUpdateDate = latestCalendarUpdate.HasValue ? latestCalendarUpdate.Value.ToString("dd MMMM yyyy") : null
};
return Json(updateDates);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
#endregion
#region Rate
[HttpPost("UpdateRates")]
public async Task<IActionResult> UpdateRate([FromBody] List<RateModel> rates)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
foreach (var rate in rates)
{
var existingRate = await _centralDbContext.Rates
.FirstOrDefaultAsync(r => r.UserId == rate.UserId);
if (existingRate != null)
{
existingRate.RateValue = rate.RateValue;
existingRate.LastUpdated = DateTime.Now;
_centralDbContext.Rates.Update(existingRate);
}
else
{
_centralDbContext.Rates.Add(new RateModel
{
UserId = rate.UserId,
RateValue = rate.RateValue,
LastUpdated = DateTime.Now
});
}
}
await _centralDbContext.SaveChangesAsync();
var updatedRates = await _centralDbContext.Rates
.Include(r => r.Users)
.Select(r => new
{
r.RateId,
r.RateValue,
r.UserId,
FullName = r.Users.FullName,
DepartmentName = r.Users.Department.DepartmentName
})
.ToListAsync();
return Json(updatedRates);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpPost("GetUserRates")]
public async Task<IActionResult> GetUserRates()
{
try
{
var userRates = await _centralDbContext.Rates
.Include(rates => rates.Users)
.ThenInclude(user => user.Department)
.Select(rates => new
{
rates.RateId,
rates.RateValue,
rates.UserId,
rates.Users.FullName,
rates.Users.Department.DepartmentName
})
.ToListAsync();
return Json(userRates);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
#endregion
#region Calendar
[HttpGet("GetStatesName")]
public async Task<IActionResult> GetStatesName()
{
try
{
var states = await _centralDbContext.States
.Select(s => new
{
s.StateId,
s.StateName
})
.ToListAsync();
return Json(states);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpPost("UpdateHoliday")]
public async Task<IActionResult> UpdateHoliday([FromBody] List<CalendarModel> holidays)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
foreach (var calendar in holidays)
{
var existingCalendar = await _centralDbContext.Holidays
.FirstOrDefaultAsync(h => h.StateId == calendar.StateId && h.HolidayDate == calendar.HolidayDate);
if (existingCalendar != null)
{
existingCalendar.HolidayName = calendar.HolidayName;
existingCalendar.LastUpdated = DateTime.Now;
_centralDbContext.Holidays.Update(existingCalendar);
}
else
{
_centralDbContext.Holidays.Add(new CalendarModel
{
HolidayName = calendar.HolidayName,
HolidayDate = calendar.HolidayDate,
StateId = calendar.StateId,
LastUpdated = DateTime.Now
});
}
}
await _centralDbContext.SaveChangesAsync();
var updatedHoliday = await _centralDbContext.Holidays
.Include(h => h.States)
.Select(h => new
{
h.HolidayId,
h.HolidayName,
h.HolidayDate,
h.StateId,
StateName = h.States.StateName
})
.ToListAsync();
return Json(updatedHoliday);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("GetAllHolidays")]
public IActionResult GetAllHolidays()
{
var holidays = _centralDbContext.Holidays.ToList();
return Ok(holidays);
}
[HttpDelete("DeleteHoliday/{id}")]
public async Task<IActionResult> DeleteHoliday(int id)
{
try
{
var holiday = await _centralDbContext.Holidays.FindAsync(id);
if (holiday == null)
{
return NotFound("Holiday not found.");
}
_centralDbContext.Holidays.Remove(holiday);
await _centralDbContext.SaveChangesAsync();
return Ok(new { message = "Holiday deleted successfully." });
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
#endregion
#region Weekend
[HttpGet("GetWeekendDay")]
public async Task<IActionResult> GetWeekendDay()
{
try
{
var weekends = await _centralDbContext.Weekends
.Select(w => new
{
w.WeekendId,
w.Day
})
.ToListAsync();
return Json(weekends);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpPost("UpdateWeekend")]
public async Task<IActionResult> UpdateWeekend([FromBody] List<StateModel> states)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
foreach (var state in states)
{
var existingState = await _centralDbContext.States
.FirstOrDefaultAsync(s => s.StateId == state.StateId);
if (existingState != null)
{
// Corrected: Updating WeekendId
existingState.WeekendId = state.WeekendId;
_centralDbContext.States.Update(existingState);
}
else
{
// Ensure new states are added correctly
_centralDbContext.States.Add(new StateModel
{
StateId = state.StateId,
StateName = state.StateName,
WeekendId = state.WeekendId
});
}
}
await _centralDbContext.SaveChangesAsync();
var updatedWeekend = await _centralDbContext.States
.Include(w => w.Weekends)
.Select(w => new
{
w.StateId,
w.StateName,
w.WeekendId,
Day = w.Weekends.Day
})
.ToListAsync();
return Json(updatedWeekend);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("GetAllWeekends")]
public IActionResult GetAllWeekends()
{
var weekends = _centralDbContext.Weekends.ToList();
return Ok(weekends);
}
[HttpGet("GetStateWeekends")]
public async Task<IActionResult> GetStateWeekends()
{
try
{
var stateWeekends = await _centralDbContext.States
.Include(s => s.Weekends)
.Where(s => s.WeekendId != null)
.Select(s => new
{
s.StateId,
s.StateName,
Day = s.Weekends.Day
})
.ToListAsync();
return Json(stateWeekends);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
#endregion
#region OtRegister
#endregion
}
}