private SalesPersonAndGroup GetChosenSalesPerson(SalesPersonRequest salesPersonRequest) { SalesPersonAndGroup chosenperson = null; Random rand = new Random(); // Linq will get excuted only when accessed. No need to List <SalesPersonAndGroup> anySalesPerson = SalesPeopleData.SalesPeopleInfo; List <SalesPersonAndGroup> personsSpecialisingInCar = SalesPeopleData.SalesPeopleInfo.FindAll(x => x.Groups.Any(x => x == salesPersonRequest.CarType)); List <SalesPersonAndGroup> personsSpecialisingInLangAndCar = SalesPeopleData.SalesPeopleInfo.FindAll(x => x.Groups.Any(x => x == salesPersonRequest.Language) && x.Groups.Any(x => x == salesPersonRequest.CarType)); if (personsSpecialisingInLangAndCar.Count >= 1) { chosenperson = GetSaleSPersonAtRandom(rand, personsSpecialisingInLangAndCar); } else if (personsSpecialisingInCar.Count >= 1) { chosenperson = GetSaleSPersonAtRandom(rand, personsSpecialisingInCar); } else if (anySalesPerson.Count > 1) { chosenperson = GetSaleSPersonAtRandom(rand, anySalesPerson); } else { throw new Exception("Possibly no sales person DATA INPUT"); } return(chosenperson); }
public AssignedSalesPerson AssignTheMostSuitableSalesPerson(SalesPersonRequest salesPersonRequest) { ValidateRequest(salesPersonRequest); SalesPersonAndGroup chosenperson = GetChosenSalesPerson(salesPersonRequest); // we want to send sepaciality descriptions not just code return(ParseToAssignedSalesPerson(chosenperson));; }
private void ValidateRequest(SalesPersonRequest salesPersonRequest) { // we need to validate the input since api can be called from any client // like postman, fiddler if (!customerLanguageService.IsValidLanguage(salesPersonRequest.Language)) { throw new Exception("Invalid langauge selection"); } if (!carTypesService.IsValidCarType(salesPersonRequest.CarType)) { throw new Exception("Invalid car type selection"); } }
public AssignedSalesPerson Post([FromBody] SalesPersonRequest salesPersonRequest) { try { AssignedSalesPerson assignedSalesPerson = salesPersonAssignmentService .AssignTheMostSuitableSalesPerson(salesPersonRequest); return(assignedSalesPerson); } catch (Exception ex) { Response.StatusCode = 500; HttpContext.Response.HttpContext.Features.Get <IHttpResponseFeature>().ReasonPhrase = ex.Message; return(null); } }