public static OsobaEvent CreateOrUpdate(OsobaEvent osobaEvent, string user) { using (Lib.Data.DbEntities db = new Data.DbEntities()) { // create if (osobaEvent.pk == 0 && osobaEvent.OsobaId > 0) { osobaEvent.Organizace = ParseTools.NormalizaceStranaShortName(osobaEvent.Organizace); osobaEvent.Created = DateTime.Now; // check if event exists so we are not creating duplicities... var oe = db.OsobaEvent .Where(ev => ev.OsobaId == osobaEvent.OsobaId && ev.Title == osobaEvent.Title && ev.DatumOd == osobaEvent.DatumOd && ev.Type == osobaEvent.Type && ev.Organizace == osobaEvent.Organizace) .FirstOrDefault(); if (oe != null) { return(oe); } db.OsobaEvent.Add(osobaEvent); db.SaveChanges(); Audit.Add(Audit.Operations.Update, user, osobaEvent, null); return(osobaEvent); } // update if (osobaEvent.pk > 0) { var eventToUpdate = db.OsobaEvent .Where(ev => ev.pk == osobaEvent.pk ).FirstOrDefault(); var eventOriginal = eventToUpdate.ShallowCopy(); if (eventToUpdate != null) { eventToUpdate.DatumOd = osobaEvent.DatumOd; eventToUpdate.DatumDo = osobaEvent.DatumDo; eventToUpdate.Organizace = ParseTools.NormalizaceStranaShortName(osobaEvent.Organizace); eventToUpdate.AddInfoNum = osobaEvent.AddInfoNum; eventToUpdate.AddInfo = osobaEvent.AddInfo; eventToUpdate.Title = osobaEvent.Title; eventToUpdate.Type = osobaEvent.Type; eventToUpdate.Zdroj = osobaEvent.Zdroj; eventToUpdate.Created = DateTime.Now; db.SaveChanges(); Audit.Add(Audit.Operations.Update, user, eventToUpdate, eventOriginal); return(eventToUpdate); } } } return(osobaEvent); }
public FirmaEvent AddOrUpdateEvent(FirmaEvent ev, string user, bool rewrite = false, bool checkDuplicates = true) { if (ev == null) { return(null); } //if (ev.Type == (int)OsobaEvent.Types.Sponzor) //{ // return AddSponsoring(ev.AddInfo, ev.DatumOd.Value.Year, ev.AddInfoNum.Value, ev.Zdroj, user); //} //check duplicates using (DbEntities db = new Data.DbEntities()) { FirmaEvent exists = null; if (ev.pk > 0 && checkDuplicates) { exists = db.FirmaEvent .AsNoTracking() .FirstOrDefault(m => m.pk == ev.pk ); } else if (checkDuplicates) { exists = db.FirmaEvent .AsNoTracking() .FirstOrDefault(m => m.ICO == this.ICO && m.Type == ev.Type && m.AddInfo == ev.AddInfo && m.AddInfoNum == ev.AddInfoNum && m.DatumOd == ev.DatumOd && m.DatumDo == ev.DatumDo && m.Zdroj == ev.Zdroj ); if (exists != null) { ev.pk = exists.pk; } } if (exists != null && rewrite) { db.FirmaEvent.Remove(exists); Audit.Add <FirmaEvent>(Audit.Operations.Delete, user, exists, null); db.SaveChanges(); exists = null; } if (exists == null) { ev.ICO = this.ICO; db.FirmaEvent.Add(ev); Audit.Add(Audit.Operations.Create, user, ev, null); db.SaveChanges(); return(ev); } else { ev.ICO = this.ICO; ev.pk = exists.pk; db.FirmaEvent.Attach(ev); db.Entry(ev).State = System.Data.Entity.EntityState.Modified; Audit.Add(Audit.Operations.Create, user, ev, exists); db.SaveChanges(); return(ev); } } }
public static FirmaEvent CreateOrUpdate(FirmaEvent firmaEvent, string user) { using (Lib.Data.DbEntities db = new Data.DbEntities()) { // create if (firmaEvent.pk == 0 && firmaEvent.ICO.Length > 0) { //firmaEvent.Organizace = ParseTools.NormalizaceStranaShortName(firmaEvent.Organizace); firmaEvent.Created = DateTime.Now; // check if event exists so we are not creating duplicities... var oe = db.FirmaEvent .Where(ev => ev.ICO == firmaEvent.ICO && ev.AddInfo == firmaEvent.AddInfo && ev.DatumOd == firmaEvent.DatumOd && ev.Type == firmaEvent.Type) //&& ev.Organizace == firmaEvent.Organizace) .FirstOrDefault(); if (oe != null) { return(oe); } db.FirmaEvent.Add(firmaEvent); db.SaveChanges(); //Firmy.Get(firmaEvent.ICO).FlushCache(); Audit.Add(Audit.Operations.Update, user, firmaEvent, null); return(firmaEvent); } // update if (firmaEvent.pk > 0) { var eventToUpdate = db.FirmaEvent .Where(ev => ev.pk == firmaEvent.pk ).FirstOrDefault(); var eventOriginal = eventToUpdate.ShallowCopy(); if (eventToUpdate != null) { eventToUpdate.DatumOd = firmaEvent.DatumOd; eventToUpdate.DatumDo = firmaEvent.DatumDo; //eventToUpdate.Organizace = ParseTools.NormalizaceStranaShortName(firmaEvent.Organizace); eventToUpdate.AddInfoNum = firmaEvent.AddInfoNum; eventToUpdate.AddInfo = firmaEvent.AddInfo; eventToUpdate.Title = firmaEvent.Title; eventToUpdate.Type = firmaEvent.Type; eventToUpdate.Zdroj = firmaEvent.Zdroj; //eventToUpdate.Status = firmaEvent.Status; eventToUpdate.Created = DateTime.Now; db.SaveChanges(); //Osoby.GetById.Get(firmaEvent.OsobaId).FlushCache(); Audit.Add(Audit.Operations.Update, user, eventToUpdate, eventOriginal); return(eventToUpdate); } } } return(firmaEvent); }