public BookViewModel(Book book, EbooksContext db)
        {
            BookId = book.BookId;
            Title = book.Title;
            Author = book.Author?.AuthorId;
            Authors = new SelectList(new Author[] { new Author() }.Concat(db.Authors.Where(a => a.Books.Any(b => b.UserId == book.UserId))).ToArray(), "AuthorId", "Name", Author);
            Publisher = book.Publisher?.PublisherId;
            Publishers = new SelectList(new Publisher[] { new Publisher() }.Concat(db.Publishers.Where(p => p.Books.Any(b => b.UserId == book.UserId))).ToArray(), "PublisherId", "Name", Publisher);
            Description = book.Description;
            SelectedTags = book.Tags.Select(t => t.Item).ToArray();

            // Because of the bug in ListBoxFor both the tag objects and a tag list are required to
            // populate the tagger control.
            Tags = book.Tags.ToList();
            TagList = new MultiSelectList(db.Tags.Where(t => t.Books.Any(b => b.UserId == book.UserId)).ToArray(), "Item", "Item", SelectedTags);

            // The JSON data required by the tagger control.
            var tagsInfo = new JObject();
            foreach (var tag in db.Tags) {
                var item = new JObject();
                item["id"] = tag.Item;
                item["selected"] = book.Tags.Contains(tag);
                item["suggestable"] = true;
                item["suggestion"] = tag.Item;
                item["key"] = tag.Item;
                tagsInfo.Add(tag.Item, item);
            }
            TagsInfo = tagsInfo.ToString();

            Idents = db.Idents.Where(i => i.BookIdents.Any(bi => bi.Book.UserId == book.UserId)).Select(i => i.Name).ToArray();
            BookIdents = book.BookIdents.ToDictionary(bi => bi.Ident.Name, bi => bi.Identifier);
        }
Beispiel #2
0
        /// <summary>
        /// Loads a new book into the database for the given user, reusing records wherever possible.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ep"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// <remarks>Does not save the changes.</remarks>
        public static Book Load(EbooksContext db, Epub ep, int userId)
        {
            var book = new Book {
                UserId = userId,
                Title = ep.Title,
                Author = Author.Get(db, ep),
                Publisher = Publisher.Get(db, ep),
                Cover = Cover.Get(db, ep),
                EpubFile = EpubFile.Get(db, ep),
                Series = Series.Get(db, ep),
                SeriesNbr = ep.SeriesNbr,
                Description = ep.Description
            };
            book.SetTags(db, ep.Tags);
            book.SetIdentifiers(db, ep.Identifiers);

            db.Books.Add(book);

            return book;
        }
 public GutenbergViewModel(IEnumerable<LanguageName> languages)
 {
     Books = new Book[0];
     Filter = null;
     setLanguages(languages);
 }