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

@ -23,6 +23,16 @@ namespace Server.DB
try
{
using (Context ctx = new Context())
{
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);
@ -34,9 +44,10 @@ namespace Server.DB
ctx.FoodPlaces.Add(foodplace);
ctx.SaveChanges();
}
return true;
}
}
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e);

View File

@ -25,6 +25,14 @@ namespace Server.DB
try
{
using (Context ctx = new Context())
{
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);
@ -49,9 +57,10 @@ namespace Server.DB
ctx.PatientAtFoodPlaces.Add(patientAtFoodPlace);
ctx.SaveChanges();
}
return true;
}
}
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e);