public string ChangeCarToMain(int carId, int userId)
{
if (carId <= 0 || userId <= 0)
{
return "Error";
}
// We need cars that are currently being used by current driver.
var usedCar = uOW.CarRepo.Get(x => x.isMain == true & x.UserId == userId).FirstOrDefault(); // find a car with status set as true, which are used by our driver
if (usedCar != null) // if such car exists
{
if (usedCar.Id == carId) // and if this car id match input parameter
{
return MakeACarMain(usedCar); // change this car`s id
}
else // if it`s not, if its other car, change its status to false and change status of car that we need
{
uOW.CarRepo.SetStateModified(usedCar);
usedCar.isMain = false;
uOW.Save();
var message = MakeACarMain(carId, userId);
return message;
}
}
else // if there are no cars with status as true
{
var message = MakeACarMain(carId, userId);
return message;
}
}