-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeeMaster.cs
275 lines (244 loc) · 9.58 KB
/
EmployeeMaster.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EmployeeManagementSystem
{
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public DateTime DateOfJoining { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
public ICollection<Attendance> Attendances { get; set; }
public ICollection<Payroll> Payrolls { get; set; }
public ICollection<PerformanceReview> PerformanceReviews { get; set; }
public ICollection<Task> Tasks { get; set; }
public ICollection<Notification> Notifications { get; set; }
public ICollection<Report> Reports { get; set; }
}
public class Attendance
{
public int AttendanceId { get; set; }
public DateTime Date { get; set; }
public bool IsPresent { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class Payroll
{
public int PayrollId { get; set; }
public decimal Salary { get; set; }
public DateTime PaymentDate { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class PerformanceReview
{
public int PerformanceReviewId { get; set; }
public DateTime ReviewDate { get; set; }
public string Comments { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public ICollection<Employee> Employees { get; set; }
}
public class Task
{
public int TaskId { get; set; }
public string TaskName { get; set; }
public string Description { get; set; }
public DateTime DueDate { get; set; }
public bool IsCompleted { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class Notification
{
public int NotificationId { get; set; }
public string Message { get; set; }
public DateTime Date { get; set; }
public bool IsRead { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class Report
{
public int ReportId { get; set; }
public string ReportName { get; set; }
public string ReportContent { get; set; }
public DateTime GeneratedOn { get; set; }
public int EmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class EmployeeManagementContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Attendance> Attendances { get; set; }
public DbSet<Payroll> Payrolls { get; set; }
public DbSet<PerformanceReview> PerformanceReviews { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Task> Tasks { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<Report> Reports { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("YourConnectionStringHere");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.HasMany(e => e.Attendances)
.WithOne(a => a.Employee)
.HasForeignKey(a => a.EmployeeId);
modelBuilder.Entity<Employee>()
.HasMany(e => e.Payrolls)
.WithOne(p => p.Employee)
.HasForeignKey(p => p.EmployeeId);
modelBuilder.Entity<Employee>()
.HasMany(e => e.PerformanceReviews)
.WithOne(pr => pr.Employee)
.HasForeignKey(pr => pr.EmployeeId);
modelBuilder.Entity<Employee>()
.HasMany(e => e.Tasks)
.WithOne(t => t.Employee)
.HasForeignKey(t => t.EmployeeId);
modelBuilder.Entity<Employee>()
.HasMany(e => e.Notifications)
.WithOne(n => n.Employee)
.HasForeignKey(n => n.EmployeeId);
modelBuilder.Entity<Employee>()
.HasMany(e => e.Reports)
.WithOne(r => r.Employee)
.HasForeignKey(r => r.EmployeeId);
modelBuilder.Entity<Role>()
.HasMany(r => r.Employees)
.WithOne(e => e.Role)
.HasForeignKey(e => e.RoleId);
}
}
class Program
{
static void Main(string[] args)
{
using (var context = new EmployeeManagementContext())
{
// Add a new role
var role = new Role
{
RoleName = "Administrator"
};
context.Roles.Add(role);
context.SaveChanges();
// Add a new employee
var employee = new Employee
{
FirstName = "John",
LastName = "Doe",
Position = "Software Engineer",
DateOfJoining = DateTime.Now,
RoleId = role.RoleId
};
context.Employees.Add(employee);
context.SaveChanges();
// Add attendance record
var attendance = new Attendance
{
Date = DateTime.Today,
IsPresent = true,
EmployeeId = employee.EmployeeId
};
context.Attendances.Add(attendance);
context.SaveChanges();
// Add payroll record
var payroll = new Payroll
{
Salary = 5000,
PaymentDate = DateTime.Today,
EmployeeId = employee.EmployeeId
};
context.Payrolls.Add(payroll);
context.SaveChanges();
// Add performance review
var review = new PerformanceReview
{
ReviewDate = DateTime.Today,
Comments = "Excellent performance",
EmployeeId = employee.EmployeeId
};
context.PerformanceReviews.Add(review);
context.SaveChanges();
// Assign a task
var task = new Task
{
TaskName = "Complete Module 1",
Description = "Work on the first module of Project A",
DueDate = DateTime.Today.AddDays(7),
IsCompleted = false,
EmployeeId = employee.EmployeeId
};
context.Tasks.Add(task);
context.SaveChanges();
// Send a notification
var notification = new Notification
{
Message = "Your leave request has been approved",
Date = DateTime.Today,
IsRead = false,
EmployeeId = employee.EmployeeId
};
context.Notifications.Add(notification);
context.SaveChanges();
// Generate a report
var report = new Report
{
ReportName = "Monthly Attendance Report",
ReportContent = "Attendance report content",
GeneratedOn = DateTime.Today,
EmployeeId = employee.EmployeeId
};
context.Reports.Add(report);
context.SaveChanges();
// Mark the task as completed
task.IsCompleted = true;
context.Tasks.Update(task);
context.SaveChanges();
// Mark the notification as read
notification.IsRead = true;
context.Notifications.Update(notification);
context.SaveChanges();
// Display all employees with their roles
var employees = context.Employees.Include(e => e.Role);
foreach (var emp in employees)
{
Console.WriteLine($"{emp.FirstName} {emp.LastName} - {emp.Role.RoleName}");
}
// Display tasks of the employee
var tasks = context.Tasks.Where(t => t.EmployeeId == employee.EmployeeId);
foreach (var t in tasks)
{
Console.WriteLine($"Task: {t.TaskName}, Completed: {t.IsCompleted}");
}
// Display notifications of the employee
var notifications = context.Notifications.Where(n => n.EmployeeId == employee.EmployeeId);
foreach (var n in notifications)
{
Console.WriteLine($"Notification: {n.Message}, Read: {n.IsRead}");
}
// Display reports generated by the employee
var reports = context.Reports.Where(r => r.EmployeeId == employee.EmployeeId);
foreach (var r in reports)
{
Console.WriteLine($"Report: {r.ReportName}, Generated On: {r.GeneratedOn}");
}
}
}
}
}