make sure that foodplaces and relations don't appear twice in the DB

This commit is contained in:
Andreas Zweili 2018-09-02 12:24:09 +02:00
parent 9592dc9748
commit 382b9be68a
2 changed files with 49 additions and 29 deletions

View File

@ -24,18 +24,29 @@ namespace Server.DB
{
using (Context ctx = new Context())
{
var city = ctx.Cities.FirstOrDefault(c => c.CityID == foodplace.City.CityID);
var country = ctx.Countries.FirstOrDefault(c => c.CountryID == foodplace.City.Country.CountryID);
var foodplaceFromDB = ctx.FoodPlaces.SingleOrDefault(x => x.Name == foodplace.Name
&& x.Streetname == foodplace.Streetname
&& x.Streetnumber == foodplace.Streetnumber
&& x.City.CityID == foodplace.City.CityID);
if (foodplaceFromDB != null)
{
return true;
}
else
{
var city = ctx.Cities.FirstOrDefault(c => c.CityID == foodplace.City.CityID);
var country = ctx.Countries.FirstOrDefault(c => c.CountryID == foodplace.City.Country.CountryID);
ctx.Cities.Attach(city);
ctx.Countries.Attach(country);
ctx.Cities.Attach(city);
ctx.Countries.Attach(country);
foodplace.City = city;
foodplace.City = city;
ctx.FoodPlaces.Add(foodplace);
ctx.SaveChanges();
ctx.FoodPlaces.Add(foodplace);
ctx.SaveChanges();
return true;
}
}
return true;
}
catch (Exception e)
{

View File

@ -26,31 +26,40 @@ namespace Server.DB
{
using (Context ctx = new Context())
{
var patient = ctx.Persons.FirstOrDefault(p => p.PersonID == patientAtFoodPlace.Patient.PersonID);
var foodplace = ctx.FoodPlaces.FirstOrDefault(f => f.FoodPlaceID == patientAtFoodPlace.FoodPlace.FoodPlaceID);
var pcity = ctx.Cities.FirstOrDefault(c => c.CityID == patient.City.CityID);
var fcity = ctx.Cities.FirstOrDefault(c => c.CityID == patientAtFoodPlace.FoodPlace.City.CityID);
var pcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == pcity.Country.CountryID);
var fcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == fcity.Country.CountryID);
var salutation = ctx.Salutations.FirstOrDefault(s => s.SalutationID == patient.Salutation.SalutationID);
var gender = ctx.Genders.FirstOrDefault(g => g.GenderID == patient.Gender.GenderID);
var relation = ctx.PatientAtFoodPlaces.SingleOrDefault(x => x.Patient.PersonID == patientAtFoodPlace.Patient.PersonID
&& x.FoodPlace.FoodPlaceID == patientAtFoodPlace.FoodPlace.FoodPlaceID);
if (relation != null)
{
return true;
}
else
{
var patient = ctx.Persons.FirstOrDefault(p => p.PersonID == patientAtFoodPlace.Patient.PersonID);
var foodplace = ctx.FoodPlaces.FirstOrDefault(f => f.FoodPlaceID == patientAtFoodPlace.FoodPlace.FoodPlaceID);
var pcity = ctx.Cities.FirstOrDefault(c => c.CityID == patient.City.CityID);
var fcity = ctx.Cities.FirstOrDefault(c => c.CityID == patientAtFoodPlace.FoodPlace.City.CityID);
var pcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == pcity.Country.CountryID);
var fcountry = ctx.Countries.FirstOrDefault(c => c.CountryID == fcity.Country.CountryID);
var salutation = ctx.Salutations.FirstOrDefault(s => s.SalutationID == patient.Salutation.SalutationID);
var gender = ctx.Genders.FirstOrDefault(g => g.GenderID == patient.Gender.GenderID);
ctx.Cities.Attach(pcity);
ctx.Cities.Attach(fcity);
ctx.Countries.Attach(pcountry);
ctx.Countries.Attach(fcountry);
ctx.Genders.Attach(gender);
ctx.Salutations.Attach(salutation);
ctx.Persons.Attach(patient);
ctx.FoodPlaces.Attach(foodplace);
ctx.Cities.Attach(pcity);
ctx.Cities.Attach(fcity);
ctx.Countries.Attach(pcountry);
ctx.Countries.Attach(fcountry);
ctx.Genders.Attach(gender);
ctx.Salutations.Attach(salutation);
ctx.Persons.Attach(patient);
ctx.FoodPlaces.Attach(foodplace);
patientAtFoodPlace.FoodPlace = foodplace;
patientAtFoodPlace.Patient = patient;
patientAtFoodPlace.FoodPlace = foodplace;
patientAtFoodPlace.Patient = patient;
ctx.PatientAtFoodPlaces.Add(patientAtFoodPlace);
ctx.SaveChanges();
ctx.PatientAtFoodPlaces.Add(patientAtFoodPlace);
ctx.SaveChanges();
return true;
}
}
return true;
}
catch (Exception e)
{