public void TestNewItem() { using (var db = new AppDataBase()) { db.Items.Add(new DocItem() { FileName = "filename", LocalCopy = false, Name = "name", Uri = "file://filename", HashCode = 0 }); db.SaveChanges(); } }
private void ChangeRateImpl(DocItemView obj) { if (obj != null) { using(var db = new AppDataBase()) { var item = (from i in db.Items where i.DocItemId == obj.Id select i).Single(); item.Rating = obj.Rating; db.SaveChanges(); } } }
public static void AddItem(NewItem param, ReportProgress progressReport) { // 0% ReportProgress(progressReport, 0); string[] sc = {";", " ", ","}; string[] tags = param.Tags.Split(sc, StringSplitOptions.RemoveEmptyEntries); // check uri bool isUriLocal = IsUriLocal(param.Uri); /* * Local URI * [keep local] store to database * [not keep] only link * * Web file * [keep local] store to database * [not keep] only link */ var item = new DocItem() { Uri = param.Uri, Name = param.Name, Rating = 3 }; // Download file // 90% if (isUriLocal) { // local filesystem item.FileName = Path.GetFileName(param.Uri); item.HashCode = HashCalc.GetHash(param.Uri); } else { var download = new WebDownload(param.Uri, (int p, string d) => { int percentage = p * 90; percentage /= 100; ReportProgress(progressReport, percentage); }); item.FileName = download.FileName; item.HashCode = HashCalc.GetHash(download.Content); } ReportProgress(progressReport, 90); // add DB record // 100% using (var db = new AppDataBase()) using(var transaction = db.Database.BeginTransaction()) { // try to select existing tags //var registeredTags = db.Tags.Select(x => x.Name).Intersect(tags, StringComparer.InvariantCultureIgnoreCase).ToArray(); var registeredTags = from i in db.Tags where tags.Contains(i.Name.ToLower()) select i; // save new tags var newTags = tags.Except(registeredTags.Select(x => x.Name), StringComparer.InvariantCultureIgnoreCase); item.Tags = new List<Tag>(); // add link to existing tags foreach(var i in registeredTags) { item.Tags.Add(i); } // add new tags and link with document foreach(var i in newTags) { var newTagRecord = db.Tags.Add(new Tag() { Name = i }); item.Tags.Add(newTagRecord); } // save tags db.SaveChanges(); // add item db.Items.Add(item); // save item db.SaveChanges(); transaction.Commit(); } }
public void Add(IEnumerable<DocItem> items, string commonTags, ReportProgress progressReport) { string[] sc = { ";", " ", "," }; string[] tags = commonTags.Split(sc, StringSplitOptions.RemoveEmptyEntries); using(var db = new AppDataBase()) { var registeredTags = from i in db.Tags where tags.Contains(i.Name.ToLower()) select i; List<Tag> allTags = new List<Tag>(registeredTags); if (progressReport != null) { progressReport(-1, Properties.Resources.AddTags); } using (var transaction = db.Database.BeginTransaction()) { var newTags = tags.Except(registeredTags.Select(x => x.Name), StringComparer.InvariantCultureIgnoreCase); // add new tags and link with document foreach (var i in newTags) { var newTagRecord = db.Tags.Add(new Tag() { Name = i }); allTags.Add(newTagRecord); } db.SaveChanges(); transaction.Commit(); } using (var transaction = db.Database.BeginTransaction()) { int totalItems = items.Count(); int c = 0; foreach (var i in items) { c++; i.Tags = allTags; db.Items.Add(i); if (progressReport != null) { int percentage = c * 100; percentage /= totalItems; progressReport(percentage, string.Format(Properties.Resources.FormatAddFile, i.FileName)); } } progressReport(-1, Properties.Resources.MsgCommitingChanges); db.SaveChanges(); transaction.Commit(); } } }