oop_AM-6/Server/Server/DB/FoodPlaceDB.cs

97 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Server.Models;
namespace Server.DB
{
public class FoodPlaceDB
{
public ICollection<FoodPlace> GetAllFoodPlaces()
{
using (Context ctx = new Context())
{
ctx.Configuration.ProxyCreationEnabled = false;
return ctx.FoodPlaces
.Include("City")
.Include("PatientAtFoodplaces")
.ToList();
}
}
public bool CreateFoodPlace(FoodPlace foodplace)
{
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);
ctx.Cities.Attach(city);
ctx.Countries.Attach(country);
foodplace.City = city;
ctx.FoodPlaces.Add(foodplace);
ctx.SaveChanges();
return true;
}
}
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e);
return false;
}
}
public bool UpdateFoodPlace(FoodPlace foodplace)
{
try
{
using (Context ctx = new Context())
{
ctx.FoodPlaces.Attach(foodplace);
ctx.Entry(foodplace).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();
}
return true;
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e);
return false;
}
}
public bool DeleteFoodPlace(FoodPlace foodplace)
{
try
{
using (Context ctx = new Context())
{
ctx.FoodPlaces.Attach(foodplace);
ctx.FoodPlaces.Remove(foodplace);
ctx.SaveChanges();
}
return true;
}
catch (Exception e)
{
System.Diagnostics.Trace.WriteLine(e);
return false;
}
}
}
}