Skip to content

Commit 8fcd219

Browse files
committed
AUTO MAPPING OBJECTS
1 parent d916a76 commit 8fcd219

File tree

73 files changed

+2420
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2420
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace NonStopMarket.App.Core
2+
{
3+
using System;
4+
using System.Linq;
5+
using System.Reflection;
6+
7+
using NonStopMarket.App.Core.Contracts;
8+
9+
public class CommandInterpreter : ICommandInterpreter
10+
{
11+
private const string Suffix = "command";
12+
13+
private readonly IServiceProvider serviceProvider;
14+
15+
public CommandInterpreter(IServiceProvider serviceProvider)
16+
{
17+
this.serviceProvider = serviceProvider;
18+
}
19+
20+
public IExecutable InterpretCommand(string[] data)
21+
{
22+
var inputCommand = data[0];
23+
24+
var type = Assembly
25+
.GetCallingAssembly()
26+
.GetTypes()
27+
.Where(x => x.Name.ToLower() == inputCommand.ToLower() + Suffix)
28+
.SingleOrDefault();
29+
30+
var commandConstructor = type
31+
.GetConstructors()
32+
.First();
33+
34+
var constructorParams = commandConstructor
35+
.GetParameters()
36+
.Select(x => x.ParameterType)
37+
.ToArray();
38+
39+
var injectedParams = constructorParams
40+
.Select(serviceProvider.GetService)
41+
.ToArray();
42+
43+
var command = (IExecutable)commandConstructor.Invoke(injectedParams);
44+
45+
return command;
46+
}
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
2+
{
3+
using Contracts;
4+
using Dtos.EmployeeDtos;
5+
6+
public class AddEmployeeCommand : IExecutable
7+
{
8+
public AddEmployeeCommand(IUnitOfWork unitOfWork)
9+
{
10+
this.UnitOfWork = unitOfWork;
11+
}
12+
13+
public IUnitOfWork UnitOfWork { get; private set; }
14+
15+
public string Execute(string[] data)
16+
{
17+
EmployeeDto employee = new EmployeeDto()
18+
{
19+
FirstName = data[0],
20+
LastName = data[1],
21+
Salary = decimal.Parse(data[2])
22+
};
23+
24+
this.UnitOfWork.Employees.AddEmployee(employee);
25+
this.UnitOfWork.Complete();
26+
27+
return $"User with name {data[0]} {data[1]} has been added!";
28+
}
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
2+
{
3+
using NonStopMarket.App.Core.Contracts;
4+
5+
public class EmployeeInfoCommand : IExecutable
6+
{
7+
public EmployeeInfoCommand(IUnitOfWork unitOfWork)
8+
{
9+
this.UnitOfWork = unitOfWork;
10+
}
11+
12+
public IUnitOfWork UnitOfWork { get; private set; }
13+
14+
public string Execute(string[] data)
15+
{
16+
var id = int.Parse(data[0]);
17+
18+
var employee = this.UnitOfWork.Employees.EmployeeInfo(id);
19+
20+
return employee.ToString();
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
2+
{
3+
using Contracts;
4+
5+
public class EmployeePersonalInfoCommand : IExecutable
6+
{
7+
public EmployeePersonalInfoCommand(IUnitOfWork unitOfWork)
8+
{
9+
this.UnitOfWork = unitOfWork;
10+
}
11+
12+
public IUnitOfWork UnitOfWork { get; private set; }
13+
14+
public string Execute(string[] data)
15+
{
16+
var id = int.Parse(data[0]);
17+
18+
var employee = this.UnitOfWork.Employees.EmployeePersonalInfo(id);
19+
20+
return employee.ToString();
21+
}
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
2+
{
3+
using NonStopMarket.App.Core.Contracts;
4+
using System.Linq;
5+
6+
public class SetAddressCommand : IExecutable
7+
{
8+
public SetAddressCommand(IUnitOfWork unitOfWork)
9+
{
10+
this.UnitOfWork = unitOfWork;
11+
}
12+
13+
public IUnitOfWork UnitOfWork { get; private set; }
14+
15+
public string Execute(string[] data)
16+
{
17+
var id = int.Parse(data[0]);
18+
var address = string.Join(" ", data.Skip(1));
19+
20+
this.UnitOfWork.Employees.SetAddress(id, address);
21+
this.UnitOfWork.Complete();
22+
23+
return $"Address for user with id {id} has been changed!";
24+
}
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
2+
{
3+
using System;
4+
5+
using Contracts;
6+
7+
public class SetBirthdayCommand : IExecutable
8+
{
9+
public SetBirthdayCommand(IUnitOfWork unitOfWork)
10+
{
11+
this.UnitOfWork = unitOfWork;
12+
}
13+
14+
public IUnitOfWork UnitOfWork { get; private set; }
15+
16+
public int MyProperty { get; set; }
17+
18+
public string Execute(string[] data)
19+
{
20+
var id = int.Parse(data[0]);
21+
var birthday = DateTime.Parse(data[1]);
22+
23+
this.UnitOfWork.Employees.SetBirthday(id, birthday);
24+
this.UnitOfWork.Complete();
25+
26+
return $"Birthdate for user with id {id} has been changed!";
27+
}
28+
}
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace NonStopMarket.App.Core.Commands.ManagerCommands
2+
{
3+
using Contracts;
4+
5+
class ManagerInfoCommand : IExecutable
6+
{
7+
public ManagerInfoCommand(IUnitOfWork unitOfWork)
8+
{
9+
this.UnitOfWork = unitOfWork;
10+
}
11+
12+
public IUnitOfWork UnitOfWork { get; private set; }
13+
14+
public string Execute(string[] data)
15+
{
16+
var id = int.Parse(data[0]);
17+
18+
var manager = this.UnitOfWork.Employees.ManagerInfo(id);
19+
20+
return manager.ToString();
21+
}
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace NonStopMarket.App.Core.Commands.ManagerCommands
2+
{
3+
using Contracts;
4+
5+
public class SetManagerCommand : IExecutable
6+
{
7+
public SetManagerCommand(IUnitOfWork unitOfWork)
8+
{
9+
this.UnitOfWork = unitOfWork;
10+
}
11+
12+
public IUnitOfWork UnitOfWork { get; private set; }
13+
14+
public string Execute(string[] data)
15+
{
16+
var employeeId = int.Parse(data[0]);
17+
var managerId = int.Parse(data[1]);
18+
19+
this.UnitOfWork.Employees.SetManager(employeeId, managerId);
20+
this.UnitOfWork.Complete();
21+
22+
return $"Employee with id {employeeId} has manager with id {managerId}!";
23+
}
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace NonStopMarket.App.Core.Commands.OrderCommands
2+
{
3+
using System;
4+
5+
using Contracts;
6+
using NonStopMarket.Models;
7+
using NonStopMarket.Models.Enums;
8+
9+
public class AddOrderCommand : IExecutable
10+
{
11+
public AddOrderCommand(IUnitOfWork unitOfWork)
12+
{
13+
this.UnitOfWork = unitOfWork;
14+
}
15+
16+
public IUnitOfWork UnitOfWork { get; private set; }
17+
18+
public string Execute(string[] data)
19+
{
20+
var empId = int.Parse(data[0]);
21+
var commandToEnum = (PaymentMethod)Enum.Parse(typeof(PaymentMethod), data[1], true);
22+
23+
var order = new Order()
24+
{
25+
PaymentMethod = commandToEnum,
26+
Date = DateTime.Now,
27+
EmployeeId = empId
28+
};
29+
30+
this.UnitOfWork.Orders.Add(order);
31+
this.UnitOfWork.Complete();
32+
33+
return "Order was added!";
34+
}
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace NonStopMarket.App.Core.Commands.OrderCommands
2+
{
3+
using System.Text;
4+
5+
using NonStopMarket.App.Core.Contracts;
6+
7+
public class GetOrdersByIncomeCommand : IExecutable
8+
{
9+
public GetOrdersByIncomeCommand(IUnitOfWork unitOfWork)
10+
{
11+
this.UnitOfWork = unitOfWork;
12+
}
13+
14+
public IUnitOfWork UnitOfWork { get; private set; }
15+
16+
public string Execute(string[] data)
17+
{
18+
var orders = this.UnitOfWork.Orders.GetOrdersByIncome();
19+
20+
StringBuilder sb = new StringBuilder();
21+
22+
foreach (var order in orders)
23+
{
24+
sb.AppendLine($"ID: {order.OrderId}, Income: ${order.Income}");
25+
26+
foreach (var product in order.Products)
27+
{
28+
sb.AppendLine($"--- ProductName: {product.Name} Price: ${product.Price}");
29+
}
30+
}
31+
32+
return sb.ToString().TrimEnd();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)