-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmployeesController.cs
More file actions
147 lines (131 loc) · 4.08 KB
/
Copy pathEmployeesController.cs
File metadata and controls
147 lines (131 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using DatabaseProject.Models;
using DatabaseProject.Repositories.Interfaces;
namespace DatabaseProject.Controllers;
public class EmployeesController : Controller
{
private readonly IEmployeeRepository _repository;
public EmployeesController(IEmployeeRepository repository)
{
_repository = repository;
}
// GET: Employees
public async Task<IActionResult> Index(string searchTerm, string filterType)
{
var employees = await _repository.GetAllAsync();
if (!string.IsNullOrEmpty(searchTerm))
{
employees = employees.Where(e =>
(e.FirstName != null && e.FirstName.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)) ||
(e.LastName != null && e.LastName.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)) ||
(e.Email != null && e.Email.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
);
}
if (!string.IsNullOrEmpty(filterType))
{
employees = employees.Where(e => e.EmployeeType?.ToString() == filterType);
}
ViewBag.SearchTerm = searchTerm;
ViewBag.FilterType = filterType;
return View(employees.ToList());
}
// GET: Employees/Details/5
public async Task<IActionResult> Details(int id)
{
var employee = await _repository.GetByIdAsync(id);
if (employee == null)
{
return NotFound();
}
return View(employee);
}
// GET: Employees/Create
public IActionResult Create()
{
return View();
}
// POST: Employees/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Employee employee)
{
if (ModelState.IsValid)
{
try
{
await _repository.CreateAsync(employee);
TempData["Success"] = "Employee created successfully using stored procedures!";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
TempData["Error"] = $"Error creating employee: {ex.Message}";
return View(employee);
}
}
return View(employee);
}
// GET: Employees/Edit/5
public async Task<IActionResult> Edit(int id)
{
var employee = await _repository.GetByIdAsync(id);
if (employee == null)
{
return NotFound();
}
return View(employee);
}
// POST: Employees/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, Employee employee)
{
if (id != employee.WorkerID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
await _repository.UpdateAsync(employee);
TempData["Success"] = "Employee updated successfully!";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
TempData["Error"] = $"Error updating employee: {ex.Message}";
return View(employee);
}
}
return View(employee);
}
// GET: Employees/Delete/5
public async Task<IActionResult> Delete(int id)
{
var employee = await _repository.GetByIdAsync(id);
if (employee == null)
{
return NotFound();
}
return View(employee);
}
// POST: Employees/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
try
{
await _repository.DeleteAsync(id);
TempData["Success"] = "Employee deleted successfully!";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
TempData["Error"] = $"Error deleting employee: {ex.Message}";
return RedirectToAction(nameof(Delete), new { id });
}
}
}