public FrmEdit(DBAppunto appunto) { InitializeComponent(); if( appunto == null) { _selectedAppunto = new DBAppunto(); lblHeader.Content = "Nuova Nota"; } else { _selectedAppunto = appunto; lblHeader.Content = "Modifica Nota"; } IEnumerable<DBImage> images = Appunto.GetImages(); if( images != null) foreach (DBImage item in images) { ImageGallery img = new ImageGallery(item); gallery.Children.Add(img); img.Load(); } this.DataContext = Appunto; }
/// <summary> /// Aggiunge le immagini di un appunto /// </summary> internal static void AddImages(List<DBImage> listImage, DBAppunto appunto) { using (OmeopautaContext db = new OmeopautaContext()) { db.Appunti.Attach(appunto); foreach (DBImage item in listImage) { item.Appunto = appunto; if (item.ID == 0) //aggiungo le nuove immagini { moveImage(item, appunto); db.Images.Add(item); } } db.SaveChanges(); } }
private void openEdit(DBAppunto selected) { if (SelectedAppunti.Count != 1 && selected != null) return; FrmEdit frm = new FrmEdit(selected); frm.Owner = this; FormActive = false; frm.ShowDialog(); FormActive = true; RefreshData(); }
private static void UpdateTags(OmeopautaContext db, DBAppunto newAppunto, string[] _oldTags) { List<string> newTags = (newAppunto.ListTags != null && newAppunto.ListTags.Length > 0) ? newAppunto.ListTags.ToList<string>() : new List<string>(); List<string> oldTags = (_oldTags != null && _oldTags.Length > 0) ? _oldTags.ToList<string>() : new List<string>(); List<string> allTags = new List<string>(); allTags.AddRange(newTags); allTags.AddRange(oldTags); foreach (string tag in allTags) { if (string.IsNullOrWhiteSpace(tag)) continue; if (newTags.Contains(tag) && oldTags.Contains(tag)) continue; //non è cambiato DBTag dbtag = db.Tags.FirstOrDefault<DBTag>(t => t.Tag == tag); if( newTags.Contains(tag)) //aggiunge o incrementa un tag { AddTag(db, tag, newAppunto, dbtag); } else { RemoveTag(db, newAppunto, dbtag); } } }
public static string[] GetTags(OmeopautaContext db, DBAppunto appunto) { return (from DBAppuntiTag app in db.AppuntiTag where app.Appunto.ID == appunto.ID select app.Tag.Tag).ToArray(); }
private static void RemoveTagsFrom(OmeopautaContext db, DBAppunto appunto) { while(appunto.Tags.Count>0) { DBAppuntiTag item = appunto.Tags.First<DBAppuntiTag>(); DBTag tag = item.Tag; //prendo il tag da eliminare RemoveTag(db, appunto, tag); //lo cancello } }
private static void RemoveTag(OmeopautaContext db, DBAppunto appunto, DBTag dbTag) { //cancello la tupla per il n-n //DBAppuntiTag dbApp = db.AppuntiTag.FirstOrDefault<DBAppuntiTag>( // t => t.Appunto.ID == appunto.ID && t.Tag.Tag == dbTag.Tag); DBAppuntiTag dbApp = appunto.Tags.FirstOrDefault<DBAppuntiTag>( t => t.Tag.Tag == dbTag.Tag); db.AppuntiTag.Remove(dbApp); if (dbTag != null) { //decremento dbTag.ContUsed--; if (dbTag.ContUsed == 0) //cancello se non ci sono piu tag db.Tags.Remove(dbTag); } }
/// <summary> /// appunto deve essere stato precedentemente salvato per avere l'ID corretto /// </summary> private static void moveImage(DBImage item, DBAppunto appunto) { if (!Directory.Exists(FOLDER_IMAGES)) Directory.CreateDirectory(FOLDER_IMAGES); string newName = appunto.ID + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "_" + item.Name; string newPath = FOLDER_IMAGES + "/" + newName; File.Copy(item.AbsPath, newPath); item.Name = newName; }
private static void AddTag(OmeopautaContext db, string strTag, DBAppunto appunto, DBTag dbTag) { if (dbTag == null) dbTag = db.Tags.Add(new DBTag() { ContUsed = 1, Tag = strTag }); else dbTag.ContUsed++; db.AppuntiTag.Add(new DBAppuntiTag() { Appunto = appunto, Tag = dbTag }); }
internal static void InsertOrUpdate(DBAppunto appunto) { using (OmeopautaContext db = new OmeopautaContext()) { DBAppunto old = db.Appunti.FirstOrDefault<DBAppunto>(a => a.ID == appunto.ID); if ( old == null ) //aggiungo l'appunto { db.Appunti.Add(appunto); UpdateTags(db, appunto, null); } else // modifico quello esistente { string[] oldTags = null; //stacco l'appunto di test db.Entry(old).State = EntityState.Detached; oldTags = GetTags(db, old); db.Appunti.Attach(appunto); db.Entry(appunto).State = EntityState.Modified; UpdateTags(db, appunto, oldTags); } db.SaveChanges(); } }