1

I have created an employee class and EmployeeWebSerivce,

I need to create a controller class which exposes three HTTP endpoints: api/employees (GET)

Returning a list of all employees. Given a boolean parameter ‘hasOvertime’, the endpoint should be able to filter the list of Employees using the EmployeeService. E.g.: api/employees?hasOvertime=true.

api/employees (POST)

Taking an Employee object in the request body and storing it in the list of employees.

api/payments (GET)

Returning the sum of all monthly payments. Similar to the other GET method, you should be able to filter by overtime, given a boolean parameter.

I don't know how to call methods from EmployeeWebService class.

 public class Employee
{
    public String Name { get; set; }
    public double HourlyWage { get; set; }
    public double HoursPerMonth { get; set; }
    public double GetMonthlyPay()
    {

        double overTimeHours= HoursPerMonth - 150;
        double pay;

        if(overTimeHours <= 0)
        {
            pay = HourlyWage * HoursPerMonth;
        }
        else
        {
            pay = (150 * HourlyWage) + (overTimeHours * HourlyWage * 1.5);
        }
        return pay;


    }

EmployeeWebService

public class EmployeeWebService
{

    private List<Employee> FilterEmployeeBasedOnOverTime(List<Employee> employee, bool hasOvertime = false)
    {
        return employee.Where(e => hasOvertime ? e.HoursPerMonth > 150 : e.HoursPerMonth <= 150).ToList();


    }
    private double GetTotalMonthlyExpense(List<Employee> employees)
    {
        return employees.Sum(e => e.GetMonthlyPay());
    }
SJcoder
  • 11
  • 4
  • Are you using .Net Core? You could instantiate the EmployeeWebService in the controller but that would make testing a nightmare. So i would use Dependency Injection. In .Net Core this is built in but if you are using ASP Net 5 you could use Autofac – Daniaal Mar 08 '20 at 15:22
  • I am using .net Core. can you help me a little that how I should use Dependency Injection? Thanks – SJcoder Mar 08 '20 at 17:01
  • I'll post an answer below. – Daniaal Mar 10 '20 at 13:07

2 Answers2

0

You can instantiate the controller class like this:

var controller = new EmployeeWebService();

Then you can call the methods like this:

var result = controller.FilterEmployeeBasedOnOverTime(someEmployee, false);

The problem you will run in to though is that your methods are private and therefore you can't call them from a different class. You can read more on access modifiers here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers

Karly
  • 1
-1

Welcome to the Community SJcoder. So because you are using .NET Core you can use Dependency Injection. If you are unfamiliar with the concept see this link here. Also i changed your "GetMonthlyTotalExpense" from double to decimal. See why here

First you will want an interface of IEmployeeWebService. This will look like the below:

 public interface IEmployeeWebService
 {
    List<Employee> FilterEmployeeBasedOnOverTime(List<Employee> employee, bool hasOvertime = false);
    decimal GetTotalMonthlyExpense(List<Employee> employees);
 }

Then you have to implement this interface. So the implementing class will look like the below:

  public class EmployeeWebService : IEmployeeWebService
  {
    public List<Employee> FilterEmployeeBasedOnOverTime(List<Employee> employee, bool hasOvertime = false)
    {
        return employee.Where(e => hasOvertime ? e.HoursPerMonth > 150 : e.HoursPerMonth <= 150).ToList();

    }

    public decimal GetTotalMonthlyExpense(List<Employee> employees)
    {
        return employees.Sum(e => e.GetMonthlyPay());
    }
  }

Then you have to tell the DI Container what class the interface is going to resolve to. You may be wondering what "AddScoped<>()" does. See this link, it helped me understand. Your startup class will look like the below:

     public void ConfigureServices(IServiceCollection services)
     {
        services.AddScoped<IEmployeeWebService, EmployeeWebService>();
     }

And now in you controller you can use the service and its methods like so:

    public HomeController(IEmployeeWebService employeeWebService)
    {
        _employeeWebService = employeeWebService;
    }

    public IActionResult Index()
    {
        List<Models.Employee> filteredResults = _employeeWebService.FilterEmployeeBasedOnOverTime(employees, true);
        return View(filteredResults);
    }

I don't know how you are getting your employees but if you are getting them from the database then you can pass that as a parameter to the service. I hope this helps and any questions please comment :)

Daniaal
  • 882
  • 8
  • 16
  • Thanks Daniaal and thanks for detailed explanation. i read all the links which were really helpful. learned alot. – SJcoder Mar 14 '20 at 21:53
  • @SJcoder No problem. You got to start from somewhere. Anything else you want to know just comment and i will help you :) – Daniaal Mar 15 '20 at 18:54