Este es mi método, el objeto sugetionRequest
trae 3 valores:
int capacityNeeded
string preferredNeigborHood
string [] resourcesNeeded)
Los dos últimos pueden ser null
, ¿cómo puedo manejar esto? Para que la consulta me devuelva un resultado en función de la capacidad si ambos son nulos, en función de la capacidad y los recursos si el vecindario es nulo o en función de la capacidad y el vecindario si los recursos son nulos, ¿todo se puede hacer en la misma consulta?
public IEnumerable<IOffice> GetOfficeSuggestion(SuggestionRequest suggestionRequest)
{
var resources = suggestionRequest.ResourcesNeeded;
var neigborhoodLocation = locations
.Where(x => x.Neighborhood == suggestionRequest.PreferedNeigborHood);
return offices
.Where(o => resources.All(r => o.AvailableResources.Contains(r))
&& o.MaxCapacity >= suggestionRequest.CapacityNeeded
&& neigborhoodLocation.Any(l => o.LocationName == l.Name))
.OrderBy(o => o.AvailableResources.Except(resources).Count())
.Select(o => new OfficeDto
{
Name = o.Name,
LocationName = o.LocationName,
MaxCapacity = o.MaxCapacity,
AvailableResources = o.AvailableResources
});
}
Estos datos son enviados a través de este Test XUnit
en el cual efectúan la carga de las oficinas con las cuales debo comparar y sugerir la mas cercana a los requisitos pedidos. Los mismos datos que son enviados al método del test ya tienen cual location
y office
debería devolver el método
[Theory]
[InlineData(18, "Palermo", null, "Centro 2", "1")]
[InlineData(6, "Centro", new[] { "wi-fi", "tv" }, "Centro 2", "3")]
[InlineData(2, null, null, "Centro 1", "4")]
[InlineData(2, null, new[] { "proyector", "catering" }, "Centro 2", "1")]
[InlineData(30, null, null, null, null)]
public void ShouldGiveOfficeSuggestions(int capacityNeeded, string preferedNeigborHood, string[] resourcesNeeded, string expectedLocation, string expectedOffice)
{
OfficeRental.AddLocation(new LocationSpecs("Centro 1", "Centro"));
OfficeRental.AddOffice(new OfficeSpecs("Centro 1", "1", 12, new[] { "wi-fi", "proyector", "cafe" }));
OfficeRental.AddOffice(new OfficeSpecs("Centro 1", "2", 8, new[] { "wi-fi", "tv", "cafe" }));
OfficeRental.AddOffice(new OfficeSpecs("Centro 1", "3", 8, new[] { "wi-fi" }));
OfficeRental.AddOffice(new OfficeSpecs("Centro 1", "4", 4, new[] { "tv" }));
OfficeRental.AddLocation(new LocationSpecs("Centro 2", "Centro"));
OfficeRental.AddOffice(new OfficeSpecs("Centro 2", "1", 20, new[] { "wi-fi", "proyector", "cafe", "catering" }));
OfficeRental.AddOffice(new OfficeSpecs("Centro 2", "2", 6, new[] { "wi-fi", "tv", "cafe" }));
OfficeRental.AddOffice(new OfficeSpecs("Centro 2", "3", 6, new[] { "wi-fi", "tv" }));
OfficeRental.AddLocation(new LocationSpecs("Palermo", "Palermo"));
OfficeRental.AddOffice(new OfficeSpecs("Palermo", "1", 10, new[] { "wi-fi", "tv" }));
OfficeRental.AddOffice(new OfficeSpecs("Palermo", "2", 8, new[] { "wi-fi", "tv" }));
var suggestions = OfficeRental.GetOfficeSuggestion(new SuggestionRequest(capacityNeeded, preferedNeigborHood, resourcesNeeded));
Assert.Equal(expectedLocation, suggestions.FirstOrDefault()?.LocationName);
Assert.Equal(expectedOffice, suggestions.FirstOrDefault()?.Name);
}