-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (122 loc) · 4.76 KB
/
Copy pathProgram.cs
File metadata and controls
131 lines (122 loc) · 4.76 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
using BusinessLayer;
using DomainModels;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RepositoryPatternDemoApp
{
class Program
{
static void Main(string[] args)
{
#region OrdinaryRepository
Console.WriteLine("Select from the list of actions what do you want to do:" +
" \n1.See all employees" +
" \n2.See all employees from selected company" +
" \n3.Show employee by id" +
" \n4.Edit employee" +
" \n5.Delete employee");
string userInput = Console.ReadLine();
switch (userInput)
{
case "1":
ShowAllEmployees();
break;
case "2":
Console.WriteLine("Please enter company id:");
ShowAllCompanies();
int companyId = Int32.Parse(Console.ReadLine());
Console.WriteLine("Employees that works in the selected company:");
GetAllEmployeesByCompanyId(companyId);
break;
case "3":
Console.WriteLine("Please enter an id:");
int id = Int32.Parse(Console.ReadLine());
GetEmployeeById(id);
break;
case "4":
Console.WriteLine($"Which employee do you want to edit (Enter id)");
ShowAllEmployees();
int input = Int32.Parse(Console.ReadLine());
GetEmployeeById(input);
Console.WriteLine("First name: ");
string fName = Console.ReadLine();
Console.WriteLine("Lastname: ");
string lName = Console.ReadLine();
Employee emp = new Employee();
emp.FirstName = fName;
emp.LastName = lName;
Edit(emp, input);
ShowAllEmployees();
break;
case "5":
Console.WriteLine($"Which employee do you want to delete (Enter id)");
ShowAllEmployees();
int userToDelete = Int32.Parse(Console.ReadLine());
Delete(userToDelete);
ShowAllEmployees();
break;
default:
break;
}
Console.ReadLine();
#endregion
}
public static IEmployeeService GetEmployeeServiceInstance()
{
return new EmployeeService();
}
public static ICompanyService GetCompanyServiceInstance()
{
return new CompanyService();
}
public static void Edit(Employee model, int id)
{
var _employeeService = GetEmployeeServiceInstance();
Employee employee = _employeeService.GetEmployeeById(id);
employee.Id = id;
employee.FirstName = model.FirstName;
employee.LastName = model.LastName;
_employeeService.EditEmployee(employee);
}
private static void GetEmployeeById(int id)
{
var _employeeService = GetEmployeeServiceInstance();
Employee employee = _employeeService.GetEmployeeById(id);
Console.WriteLine($"{employee.Id}. {employee.FirstName} {employee.LastName} \n");
}
private static void Delete(int id)
{
var _employeeService = GetEmployeeServiceInstance();
_employeeService.DeleteEmployee(id);
}
private static void ShowAllEmployees()
{
var _employeeService = GetEmployeeServiceInstance();
List<Employee> employees = _employeeService.GetAllEmployees().ToList();
foreach (var emp in employees)
{
Console.WriteLine($"{emp.Id}. {emp.FirstName} {emp.LastName} \n");
}
}
private static void GetAllEmployeesByCompanyId(int id)
{
var _employeeService = GetEmployeeServiceInstance();
List<Employee> employees = _employeeService.GetEmployeesByCompanyId(id).ToList();
foreach (var emp in employees)
{
Console.WriteLine($"{emp.Id}. {emp.FirstName} {emp.LastName} \n");
}
}
private static void ShowAllCompanies()
{
var _companyService = GetCompanyServiceInstance();
List<Company> companies = _companyService.GetAllCompanies().ToList();
foreach (var company in companies)
{
Console.WriteLine($"{company.Id}. {company.Name} \n");
}
}
}
}