diff --git a/Areas/OTcalculate/Models/CalendarModel.cs b/Areas/OTcalculate/Models/CalendarModel.cs index 06fd26e..dc47176 100644 --- a/Areas/OTcalculate/Models/CalendarModel.cs +++ b/Areas/OTcalculate/Models/CalendarModel.cs @@ -14,5 +14,6 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Models [ForeignKey("StateId")] public virtual StateModel? States { get; set; } + } } \ No newline at end of file diff --git a/Areas/OTcalculate/Models/StateModel.cs b/Areas/OTcalculate/Models/StateModel.cs index 0ac3d49..61598ea 100644 --- a/Areas/OTcalculate/Models/StateModel.cs +++ b/Areas/OTcalculate/Models/StateModel.cs @@ -8,6 +8,14 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Models { [Key] public int StateId { get; set; } + + [Required] public required string StateName { get; set; } + + public int? WeekendId { get; set; } + + [ForeignKey("WeekendId")] + public virtual WeekendModel? Weekends { get; set; } } + } \ No newline at end of file diff --git a/Areas/OTcalculate/Models/WeekendModel.cs b/Areas/OTcalculate/Models/WeekendModel.cs new file mode 100644 index 0000000..e2e43a6 --- /dev/null +++ b/Areas/OTcalculate/Models/WeekendModel.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using PSTW_CentralSystem.Models; + +namespace PSTW_CentralSystem.Areas.OTcalculate.Models +{ + public class WeekendModel + { + [Key] + public int WeekendId { get; set; } + public required string Day { get; set; } + } +} diff --git a/Areas/OTcalculate/Views/HrDashboard/Calendar.cshtml b/Areas/OTcalculate/Views/HrDashboard/Calendar.cshtml index bf561a0..968a204 100644 --- a/Areas/OTcalculate/Views/HrDashboard/Calendar.cshtml +++ b/Areas/OTcalculate/Views/HrDashboard/Calendar.cshtml @@ -111,7 +111,47 @@ @* Update Weekend *@
-

Weekend Update Section

+ +
+
+
+ + +
+
+
+
+ +
+ + +
+ + +
+ + +
+
+ +
+
+
Weekend Days per State
+
+
+
+ {{ day }} +
    +
  • {{ state }}
  • +
+
+
+
@@ -132,13 +172,19 @@ selectedStates: [], holidayName: '', selectedState: null, - holidayList: [] + holidayList: [], + selectedDay: "", + weekendList: [], + stateWeekends: [] }; }, mounted() { this.fetchStates(); this.fetchHolidays(); + this.fetchWeekends(); + this.fetchStateWeekends(); + }, computed: { @@ -146,6 +192,16 @@ return this.selectedState ? this.holidayList.filter(h => h.stateId === this.selectedState) : []; + }, + groupedWeekends() { + const grouped = {}; + this.stateWeekends.forEach(weekend => { + if (!grouped[weekend.day]) { + grouped[weekend.day] = []; + } + grouped[weekend.day].push(weekend.stateName); + }); + return grouped; } }, @@ -222,11 +278,84 @@ } }, + async fetchWeekends() { + try { + const response = await fetch("/OvertimeAPI/GetWeekendDay"); + if (!response.ok) throw new Error("Failed to fetch weekends"); + + this.dayList = await response.json(); + } catch (error) { + console.error("Error fetching weekends:", error); + alert("Failed to load weekend list."); + } + }, + async fetchStateWeekends() { + try { + const response = await fetch("/OvertimeAPI/GetStateWeekends"); + if (!response.ok) throw new Error("Failed to fetch state weekends"); + + this.stateWeekends = await response.json(); + console.log("Fetched state weekends:", this.stateWeekends); + } catch (error) { + console.error("Error fetching state weekends:", error); + alert("Failed to load state weekend data."); + } + }, + async updateWeekend() { + try { + if (this.selectedStates.length === 0 || !this.selectedDay) { + alert("Please select at least one state and a day."); + return; + } + + const selectedDayObject = this.dayList.find(day => day.day === this.selectedDay); + if (!selectedDayObject) { + alert("Selected day not found in the list."); + return; + } + + const payload = this.selectedStates.map(stateId => { + const state = this.stateList.find(s => s.stateId === stateId); + return { + StateId: stateId, + StateName: state.stateName, + WeekendId: selectedDayObject.weekendId + }; + }); + + console.log("Payload being sent to API:", payload); + + const response = await fetch("/OvertimeAPI/UpdateWeekend", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(payload) + }); + + if (response.ok) { + alert("Weekend updated successfully!"); + this.clearWeekend(); + await this.fetchStateWeekends(); + } else { + alert("Failed to update weekend."); + } + } catch (error) { + console.error("Error updating weekend:", error); + alert("An error occurred while updating weekend."); + } + }, + clearForm() { this.selectedDate = ''; this.selectedStates = []; this.holidayName = ''; + this.selectedDay = ''; + }, + + clearWeekend() { + this.selectedDay = ""; + this.selectedStates = []; } + } }); diff --git a/Controllers/API/OvertimeAPI.cs b/Controllers/API/OvertimeAPI.cs index bdcf9a3..cac0933 100644 --- a/Controllers/API/OvertimeAPI.cs +++ b/Controllers/API/OvertimeAPI.cs @@ -197,5 +197,115 @@ namespace PSTW_CentralSystem.Controllers.API } #endregion + #region Weekend + [HttpGet("GetWeekendDay")] + public async Task 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 UpdateWeekend([FromBody] List 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 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 + } } \ No newline at end of file diff --git a/DBContext/CentralSystemContext.cs b/DBContext/CentralSystemContext.cs index 7a036be..c53b8de 100644 --- a/DBContext/CentralSystemContext.cs +++ b/DBContext/CentralSystemContext.cs @@ -102,5 +102,6 @@ namespace PSTW_CentralSystem.DBContext public DbSet Rates { get; set; } public DbSet Holidays { get; set; } public DbSet States { get; set; } + public DbSet Weekends { get; set; } } }