public static void Activity_BookRemoved(SDCContext db, UserProfile profile, Book book, string shelfName) { string template = "<p>Removed <strong>%booktitle% </strong> from %shelfname% <span class='text-muted'>on %when%</span></p>"; var content = template .Replace("%booktitle%", book.Title) .Replace("%shelfname%", shelfName) .Replace("%when%", DateTime.Now.ToString(G.DATE)); Activity activity = new Activity() { Profile = profile, Content = content, Type = ActivityType.RemoveBook }; db.Activities.Add(activity); db.SaveChanges(); }
public static void Activity_BookUpdated(SDCContext db, UserProfile profile, Book book, string bookurl, string shelfurl) { string template = "<p>Updated <a href = '%bookurl%'> <strong>%booktitle%</strong> </a> in <a href = '%shelfurl%'> <strong>%shelfname%</strong> </a> <span class='text-muted'>on %when%</span></p>"; var content = template .Replace("%bookurl%", bookurl) .Replace("%booktitle%", book.Title) .Replace("%shelfurl%", shelfurl) .Replace("%shelfname%", book.Shelf.Name) .Replace("%when%", DateTime.Now.ToString(G.DATE)); Activity activity = new Activity() { Profile = profile, Content = content, Type = ActivityType.UpdateBook }; db.Activities.Add(activity); db.SaveChanges(); }
private void LoadBooks(SDCContext db, UserProfile profile, Shelf shelf) { int perShelf = _rnd.Next(10, 50); int startindex = Progress; for(int i = startindex; i < startindex + perShelf && Progress < _max; i++) { string isbn, title, author, publisher, imgurl; int year; try { isbn = _booksSplit[i, 0]; title = _booksSplit[i, 1]; author = _booksSplit[i, 2]; year = Int32.Parse(_booksSplit[i, 3]); publisher = _booksSplit[i, 4]; imgurl = _booksSplit[i, 5]; }catch(Exception) { continue; } DateTime d1 = DateTime.Now; Author a = null; if (!_authorsSet.ContainsKey(author)) { a = new Author() { Name = author, AddedDate = DateTime.Now, AddedBy = profile, LastModifiedBy = profile, IsVerified = true }; db.Authors.Add(a); _authorsSet.Add(author, a); } else { a = _authorsSet[author]; } Debug.WriteLine("Author lookup in " + DateTime.Now.Subtract(d1).TotalMilliseconds); DateTime d2 = DateTime.Now; Publisher p = null; if (!_publishersSet.ContainsKey(publisher)) { p = new Publisher() { Name = publisher, AddedBy = profile, IsVerified = true }; db.Publishers.Add(p); _publishersSet.Add(publisher, p); } else { p = _publishersSet[publisher]; } Debug.WriteLine("Author lookup in " + DateTime.Now.Subtract(d2).TotalMilliseconds); Book book = new Book() { Authors = new List<Author>(new Author[] { a }), Title = title, AddedDate = DateTime.Now, Language = _lang, Shelf = shelf, ISBN = isbn, Publisher = p, Year = year, Description = _booksSplit[i, 6], Pages = _rnd.Next(1, 500), Price = _rnd.Next(10, 100) }; //3 random genres bongo bong! var bookGenres = _allGenres.OrderBy(x => Guid.NewGuid().ToString()).Take(3).ToArray(); book.Genres.Add(bookGenres[0]); db.Books.Add(book); //book pictures BookPicture bp = new BookPicture() { Book = book, Key = null, Url = imgurl, IsMain = true, Title = "img title" }; //book.Pictures = new List<BookPicture>(); book.Pictures.Add(bp); Progress++; if (Cancel) break; } }
public static void MapComplexProperties(SDCContext db, Book book, BookViewModel bookViewModel, UserProfile profile) { #region Authors entities var auth_to_remove = book.Authors.Where(a => !bookViewModel.Authors.Any(a2 => a2.Id == a.Id)).ToList(); var auth_to_add = bookViewModel.Authors.Where(a => !book.Authors.Any(a2 => a2.Id == a.Id)).ToList(); auth_to_remove.ForEach(a => book.Authors.Remove(a)); auth_to_add.ForEach(a => book.Authors.Add(a)); foreach (Author a in book.Authors) { if (a.Id == 0) { a.AddedDate = DateTime.Now; a.AddedBy = profile; db.Entry<Author>(a).State = EntityState.Added; } else { db.Attach(a); } } #endregion #region Genres entities var genres_to_remove = book.Genres.Where(g => !bookViewModel.Genres.Any(g2 => g2.Id == g.Id)).ToList(); var genres_to_add = bookViewModel.Genres.Where(g => !book.Genres.Any(g2 => g2.Id == g.Id)).ToList(); genres_to_remove.ForEach(g => book.Genres.Remove(g)); genres_to_add.ForEach(g => book.Genres.Add(g)); foreach (var g in book.Genres) { db.Attach(g); } #endregion #region Publisher entity if (bookViewModel.Publisher != null) { db.Attach(bookViewModel.Publisher); book.Publisher = bookViewModel.Publisher; } else book.Publisher = null; #endregion #region Language var lang = bookViewModel.Language; db.AttachCodeEntity(ref lang); book.Language = lang; #endregion }