Skip to content

Commit

Permalink
AUTO MAPPING OBJECTS
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicTheCat committed Mar 18, 2019
1 parent d916a76 commit 8fcd219
Show file tree
Hide file tree
Showing 73 changed files with 2,420 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace NonStopMarket.App.Core
{
using System;
using System.Linq;
using System.Reflection;

using NonStopMarket.App.Core.Contracts;

public class CommandInterpreter : ICommandInterpreter
{
private const string Suffix = "command";

private readonly IServiceProvider serviceProvider;

public CommandInterpreter(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}

public IExecutable InterpretCommand(string[] data)
{
var inputCommand = data[0];

var type = Assembly
.GetCallingAssembly()
.GetTypes()
.Where(x => x.Name.ToLower() == inputCommand.ToLower() + Suffix)
.SingleOrDefault();

var commandConstructor = type
.GetConstructors()
.First();

var constructorParams = commandConstructor
.GetParameters()
.Select(x => x.ParameterType)
.ToArray();

var injectedParams = constructorParams
.Select(serviceProvider.GetService)
.ToArray();

var command = (IExecutable)commandConstructor.Invoke(injectedParams);

return command;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
{
using Contracts;
using Dtos.EmployeeDtos;

public class AddEmployeeCommand : IExecutable
{
public AddEmployeeCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
EmployeeDto employee = new EmployeeDto()
{
FirstName = data[0],
LastName = data[1],
Salary = decimal.Parse(data[2])
};

this.UnitOfWork.Employees.AddEmployee(employee);
this.UnitOfWork.Complete();

return $"User with name {data[0]} {data[1]} has been added!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
{
using NonStopMarket.App.Core.Contracts;

public class EmployeeInfoCommand : IExecutable
{
public EmployeeInfoCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var id = int.Parse(data[0]);

var employee = this.UnitOfWork.Employees.EmployeeInfo(id);

return employee.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
{
using Contracts;

public class EmployeePersonalInfoCommand : IExecutable
{
public EmployeePersonalInfoCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var id = int.Parse(data[0]);

var employee = this.UnitOfWork.Employees.EmployeePersonalInfo(id);

return employee.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
{
using NonStopMarket.App.Core.Contracts;
using System.Linq;

public class SetAddressCommand : IExecutable
{
public SetAddressCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var id = int.Parse(data[0]);
var address = string.Join(" ", data.Skip(1));

this.UnitOfWork.Employees.SetAddress(id, address);
this.UnitOfWork.Complete();

return $"Address for user with id {id} has been changed!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace NonStopMarket.App.Core.Commands.EmployeeCommands
{
using System;

using Contracts;

public class SetBirthdayCommand : IExecutable
{
public SetBirthdayCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public int MyProperty { get; set; }

public string Execute(string[] data)
{
var id = int.Parse(data[0]);
var birthday = DateTime.Parse(data[1]);

this.UnitOfWork.Employees.SetBirthday(id, birthday);
this.UnitOfWork.Complete();

return $"Birthdate for user with id {id} has been changed!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace NonStopMarket.App.Core.Commands.ManagerCommands
{
using Contracts;

class ManagerInfoCommand : IExecutable
{
public ManagerInfoCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var id = int.Parse(data[0]);

var manager = this.UnitOfWork.Employees.ManagerInfo(id);

return manager.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace NonStopMarket.App.Core.Commands.ManagerCommands
{
using Contracts;

public class SetManagerCommand : IExecutable
{
public SetManagerCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var employeeId = int.Parse(data[0]);
var managerId = int.Parse(data[1]);

this.UnitOfWork.Employees.SetManager(employeeId, managerId);
this.UnitOfWork.Complete();

return $"Employee with id {employeeId} has manager with id {managerId}!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace NonStopMarket.App.Core.Commands.OrderCommands
{
using System;

using Contracts;
using NonStopMarket.Models;
using NonStopMarket.Models.Enums;

public class AddOrderCommand : IExecutable
{
public AddOrderCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var empId = int.Parse(data[0]);
var commandToEnum = (PaymentMethod)Enum.Parse(typeof(PaymentMethod), data[1], true);

var order = new Order()
{
PaymentMethod = commandToEnum,
Date = DateTime.Now,
EmployeeId = empId
};

this.UnitOfWork.Orders.Add(order);
this.UnitOfWork.Complete();

return "Order was added!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace NonStopMarket.App.Core.Commands.OrderCommands
{
using System.Text;

using NonStopMarket.App.Core.Contracts;

public class GetOrdersByIncomeCommand : IExecutable
{
public GetOrdersByIncomeCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var orders = this.UnitOfWork.Orders.GetOrdersByIncome();

StringBuilder sb = new StringBuilder();

foreach (var order in orders)
{
sb.AppendLine($"ID: {order.OrderId}, Income: ${order.Income}");

foreach (var product in order.Products)
{
sb.AppendLine($"--- ProductName: {product.Name} Price: ${product.Price}");
}
}

return sb.ToString().TrimEnd();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace NonStopMarket.App.Core.Commands.ProductCommands
{
using Contracts;
using NonStopMarket.Models;

public class AddProductCommand : IExecutable
{
public AddProductCommand(IUnitOfWork unitOfwork)
{
this.UnitOfWork = unitOfwork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var name = data[0];
var totalQ = int.Parse(data[1]);
var qInStock = int.Parse(data[2]);
var price = decimal.Parse(data[3]);

var product = new Product()
{
Name = name,
TotalQuantity = totalQ,
QuantityInstock = qInStock,
Price = price
};

this.UnitOfWork.Products.Add(product);
this.UnitOfWork.Complete();

return $"Product {name} was delivered in the market!";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace NonStopMarket.App.Core.Commands.ProductCommands
{
using NonStopMarket.App.Core.Contracts;

public class FindBestSellerCommand : IExecutable
{
public FindBestSellerCommand(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}

public IUnitOfWork UnitOfWork { get; private set; }

public string Execute(string[] data)
{
var bestSeller = this.UnitOfWork.Products.FindBestSeller();

return $"ID: {bestSeller.ProductId}, Name: {bestSeller.Name}, SoldAmount: {bestSeller.SoldAmount}";
}
}
}
Loading

0 comments on commit 8fcd219

Please sign in to comment.