public async Task MatchTagToBookAsync(IEnumerable <string> tags, string bookId) { foreach (var tagName in tags) { var tag = await this.tagRepository.GetByExpressionAsync(t => t.Value == tagName); if (tag == null) { var tagNew = new Tag() { TagId = Guid.NewGuid().ToString(), Value = tagName }; tag = tagNew; await this.tagUpdateService.AddAsync(tag); } var tagId = tag.TagId; var bookTagRecord = new BookTag() { BookId = bookId, TagId = tagId }; await this.bookTagUpdateService.AddAsync(bookTagRecord); } }
public static BookTagViewModel ToViewModel(this BookTag obj) { return(new BookTagViewModel() { BookID = obj.BookID, TagID = obj.TagID }); }
public ActionResult DeleteConfirmed(int id) { BookTag bookTag = db.BookTags.Find(id); db.BookTags.Remove(bookTag); db.SaveChanges(); return(RedirectToAction("Index")); }
public Book FindBookByTag(object tagObject, BookTag tag) { if (tagObject == null) { throw new ArgumentNullException(nameof(tagObject)); } string stringTagObject; Book resultBook = null; switch (tag) { case BookTag.Author: stringTagObject = tagObject as string; if (stringTagObject == null) { throw new ArgumentException("Incorrect tagObject!"); } resultBook = _books.Find(x => x.Author.Equals(stringTagObject, StringComparison.InvariantCultureIgnoreCase)); break; case BookTag.Title: stringTagObject = tagObject as string; if (stringTagObject == null) { throw new ArgumentException("Incorrect tagObject!"); } resultBook = _books.Find(x => x.Title.Equals(stringTagObject, StringComparison.InvariantCultureIgnoreCase)); break; case BookTag.PageCount: int pageCount; try { pageCount = Convert.ToInt32(tagObject); } catch (Exception) { throw new ArgumentException("Incorrect tagObject!"); } resultBook = _books.Find(x => x.PageCount == pageCount); break; case BookTag.Year: int year; try { year = Convert.ToInt32(tagObject); } catch (Exception) { throw new ArgumentException("Incorrect tagObject!"); } resultBook = _books.Find(x => x.PageCount == year); break; } return(resultBook); }
public ActionResult Edit([Bind(Include = "Id,Text")] BookTag bookTag) { if (ModelState.IsValid) { db.Entry(bookTag).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(bookTag)); }
public ActionResult Create([Bind(Include = "Id,Text")] BookTag bookTag) { if (ModelState.IsValid) { db.BookTags.Add(bookTag); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(bookTag)); }
public int Create(BookTag e) { object[] paremeter = { new SqlParameter("Tag", e.Tag == null?(object)DBNull.Value:e.Tag), new SqlParameter("IDBook", e.IDBook), }; var res = context.Database.ExecuteSqlCommand("SP_BookTag_Create @Tag,@IDBook", paremeter); return(res); }
private static Book MapTags(Book book, BookTag bookTag) { if (!bookDictionary.TryGetValue(book.Id, out var bookEntry)) { bookEntry = book; bookEntry.BookTags = new List <BookTag>(); bookDictionary.Add(bookEntry.Id, bookEntry); } bookEntry.BookTags.Add(bookTag); return(bookEntry); }
public TagBooks(BookTag bookTag) { Name = bookTag.Tag; TotalPages = bookTag.TotalPages; TotalBooks = bookTag.TotalBooksReadBy; Books = new Book[bookTag.BooksWithTag.Count]; for (int i = 0; i < bookTag.BooksWithTag.Count; i++) { Books[i] = new Book(bookTag.BooksWithTag[i]); } }
/// <summary> /// Find books by tag. /// </summary> /// <param name="tag">Book tag.</param> /// <param name="value">Value of tag.</param> /// <returns>Books.</returns> public IEnumerable <Book> FindBooksByTag(BookTag tag, object value) { IComparer <Book> comparer = Comparers[tag]; Book key = GetKeyBook(tag, value); foreach (Book book in _booksBuffer) { if (comparer.Compare(book, key) == 0) { yield return(book); } } }
// GET: BookTag/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BookTag bookTag = db.BookTags.Find(id); if (bookTag == null) { return(HttpNotFound()); } return(View(bookTag)); }
public async Task MatchTagToBookAsync(IEnumerable <string> tags, string bookId) { foreach (var tagName in tags) { var tagId = this.tagRepository.GetByExpressionAsync(t => t.Value == tagName).Result.TagId; var bookTagRecord = new BookTag() { BookId = bookId, TagId = tagId }; await this.dbContext.DbSet <BookTag>().AddAsync(bookTagRecord); } await this.dbContext.SaveAsync(); }
static Expression <Func <DbBook, object> > GetBookTagsPath(BookTag tag) { switch (tag) { default: return(b => b.TagsGeneral); case BookTag.Artist: return(b => b.TagsArtist); case BookTag.Parody: return(b => b.TagsParody); case BookTag.Character: return(b => b.TagsCharacter); case BookTag.Convention: return(b => b.TagsConvention); case BookTag.Series: return(b => b.TagsSeries); } }
public void SortByTag(BookTag tag) { switch (tag) { case BookTag.Year: _books.Sort((x, y) => x.Year.CompareTo(y.Year)); break; case BookTag.Author: _books.Sort((x, y) => string.Compare(x.Author, y.Author, StringComparison.InvariantCulture)); break; case BookTag.PageCount: _books.Sort((x, y) => x.PageCount.CompareTo(y.PageCount)); break; case BookTag.Title: _books.Sort(); break; } }
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity <BookTags>(BookTag => { BookTag.HasKey(ur => new { ur.BookId, ur.TagId }); BookTag.HasOne(u => u.Book).WithMany(r => r.BookTags).HasForeignKey(bc => bc.BookId); BookTag.HasOne(u => u.Tag).WithMany(r => r.BookTags).HasForeignKey(bc => bc.TagId); }); //modelBuilder.Entity<Book>(t => //{ // t.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); //}); //.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); }
public async Task AddTagsToBookAsync(int bookId, IEnumerable <int> tagsIds) { foreach (var id in tagsIds) { if (!await this.db.BookTags.AnyAsync(bt => bt.BookId == bookId && bt.TagId == id)) { var bookTag = new BookTag { BookId = bookId, TagId = id, }; await this.db.BookTags.AddAsync(bookTag); await this.db.SaveChangesAsync(); } } }
public async Task AddBookTagAsync(int bookId, BookTag bookTag) { // Get book var book = _context.Book .Where(b => b.Id == bookId) .Include(b => b.BookTags) .FirstOrDefault(); // Lock for modification _context.Update(book); // Append comments List <BookTag> result = book.BookTags.ToList <BookTag>(); result.Add(bookTag); book.BookTags = result; await _context.SaveChangesAsync(); }
public async Task <bool> AddTagToBookAsync(string bookId, string tagId) { // Verify that the tag exists, the book exists, and that the tag has not already been added to the book if (!await this.Context.Tags.AnyAsync(t => t.Id == tagId) || !await this.Context.Books.AnyAsync(b => b.Id == bookId) || await this.Context.BookTags.AnyAsync(bt => bt.BookId == bookId && bt.TagId == tagId)) { return(false); } var bookTag = new BookTag { TagId = tagId, BookId = bookId }; await this.Context.BookTags.AddAsync(bookTag); await this.Context.SaveChangesAsync(); return(true); }
/// <summary> /// Get key book. /// </summary> /// <param name="tag">Book tag.</param> /// <param name="value">Book tag value.</param> /// <returns>Book key.</returns> private Book GetKeyBook(BookTag tag, object value) { switch (tag) { case BookTag.ISBN: return(new Book { ISBN = (string)value }); case BookTag.Author: return(new Book { Author = (string)value }); case BookTag.Title: return(new Book { Title = (string)value }); case BookTag.Publisher: return(new Book { Publisher = (string)value }); case BookTag.PublishingYear: return(new Book { PublishingYear = (int)value }); case BookTag.PagesCount: return(new Book { PagesCount = (int)value }); default: return(new Book()); } }
private void UpdateBookTags() { BookTags.Clear(); Dictionary <string, BookTag> bookTagSet = new Dictionary <string, BookTag>(); foreach (BookRead book in BooksRead) { if (book.Tags == null || book.Tags.Count == 0) { continue; } foreach (string tag in book.Tags) { string trimmedTag = tag.Trim(); if (bookTagSet.ContainsKey(trimmedTag)) { bookTagSet[trimmedTag].BooksWithTag.Add(book); } else { BookTag bookTag = new BookTag { Tag = trimmedTag }; bookTag.BooksWithTag.Add(book); bookTagSet.Add(trimmedTag, bookTag); } } } foreach (BookTag bookTag in bookTagSet.Values.ToList()) { BookTags.Add(bookTag); } }
/// <summary> /// Sort books by tag. /// </summary> /// <param name="tag">Book tag.</param> public void SortBooksByTag(BookTag tag) { _booksBuffer.Sort(Comparers[tag]); Logger.Log(0, "Sort books by " + tag.ToString()); }
public async Task EditBookCommentAsync(BookTag bookTag) { _context.Entry(bookTag).State = EntityState.Modified; await _context.SaveChangesAsync(); }
public async Task DeleteBookCommentAsync(BookTag bookTag) { _context.BookTags.Remove(bookTag); await _context.SaveChangesAsync(); }
public async Task <int> CreateBookAsync(string title, int authorId, string summary, DateTime publishedOn, int pages, string pictureUrl, int editionLanguageId, string amazonLink, IEnumerable <int> genresIds, IEnumerable <int> tagsIds, IEnumerable <int> awardsIds) { var book = new Book { Title = title, AuthorId = authorId, Summary = summary, PublishedOn = publishedOn, Pages = pages, PictureURL = pictureUrl, EditionLanguageId = editionLanguageId, AmazonLink = amazonLink, CreatedOn = DateTime.UtcNow, }; await this.db.Books.AddAsync(book); foreach (var id in genresIds) { if (!await this.db.BookGenres.AnyAsync(bg => bg.BookId == book.Id && bg.GenreId == id)) { var bookGenre = new BookGenre { BookId = book.Id, GenreId = id, }; book.Genres.Add(bookGenre); await this.db.SaveChangesAsync(); } } foreach (var id in tagsIds) { if (!await this.db.BookTags.AnyAsync(bt => bt.BookId == book.Id && bt.TagId == id)) { var bookTag = new BookTag { BookId = book.Id, TagId = id, }; book.Tags.Add(bookTag); await this.db.SaveChangesAsync(); } } foreach (var id in awardsIds) { if (!await this.db.BookAwards.AnyAsync(ba => ba.BookId == book.Id && ba.AwardId == id)) { var bookAward = new BookAward { BookId = book.Id, AwardId = id, }; book.Awards.Add(bookAward); await this.db.SaveChangesAsync(); } } await this.db.SaveChangesAsync(); return(book.Id); }
public BookQuery WithTag(BookTag tag, TextQuery q) => Set(x => x.Tags[tag] = q);
public async Task <bool> CreateAsync(BookTag tag) { await _tags.InsertOneAsync(tag); return(true); }
public Book FindBookByTag(object tagObject, BookTag tag) { if (tagObject==null) throw new ArgumentNullException(nameof(tagObject)); string stringTagObject; Book resultBook = null; switch (tag) { case BookTag.Author: stringTagObject = tagObject as string; if (stringTagObject == null) throw new ArgumentException("Incorrect tagObject!"); resultBook = _books.Find(x => x.Author.Equals(stringTagObject, StringComparison.InvariantCultureIgnoreCase)); break; case BookTag.Title: stringTagObject = tagObject as string; if (stringTagObject==null) throw new ArgumentException("Incorrect tagObject!"); resultBook=_books.Find(x => x.Title.Equals(stringTagObject, StringComparison.InvariantCultureIgnoreCase)); break; case BookTag.PageCount: int pageCount; try { pageCount = Convert.ToInt32(tagObject); } catch (Exception) { throw new ArgumentException("Incorrect tagObject!"); } resultBook = _books.Find(x => x.PageCount == pageCount); break; case BookTag.Year: int year; try { year = Convert.ToInt32(tagObject); } catch (Exception) { throw new ArgumentException("Incorrect tagObject!"); } resultBook = _books.Find(x => x.PageCount == year); break; } return resultBook; }
public async Task <BookTag> CreateAsync(BookTag entity) { entity.Id = await _bookTagRepository.InsertAndGetIdAsync(entity); return(entity); }
public async Task <IActionResult> CreatePOST() { try { if (ModelState.IsValid) { var book = BooksVM.Book; if (SameBookExist(book)) { ModelState.AddModelError("SameBook", "Sách đã có trong hệ thống"); ViewData["AuthorId"] = new SelectList(_context.Authors, "Id", "Signed", BooksVM.Book.AuthorId); ViewData["PublisherId"] = new SelectList(_context.Publishers, "Id", "PublisherName", BooksVM.Book.PublisherId); return(View(BooksVM)); } _context.Add(BooksVM.Book); await _context.SaveChangesAsync(); if (BooksVM.BareTag != null) { List <string> tags = HandingBareTag(BooksVM.BareTag); foreach (var tag in tags) { int id; Tag bTag = new Tag() { Tagname = tag }; var getTag = await _context.Tag.Where(u => u.Tagname == bTag.Tagname).FirstOrDefaultAsync(); if (getTag == null) { _context.Tag.Add(bTag); await _context.SaveChangesAsync(); id = bTag.Id; } else { id = getTag.Id; } BookTag bookTag = new BookTag() { BookId = BooksVM.Book.Id, TagId = id }; _context.BookTags.Add(bookTag); await _context.SaveChangesAsync(); } } var bookFromDb = _context.Books.Find(BooksVM.Book.Id); var webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count != 0) { var uploads = Path.Combine(webRootPath, SD.BookImageFolder); var extension = Path.GetExtension(files[0].FileName); using (var fileStream = new FileStream(Path.Combine(uploads, BooksVM.Book.Id + extension), FileMode.Create)) { files[0].CopyTo(fileStream); } bookFromDb.BookImage = @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + extension; } else { var uploads = Path.Combine(webRootPath, SD.BookDefaultImage); System.IO.File.Copy(uploads, webRootPath + @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + ".png"); bookFromDb.BookImage = @"\" + SD.BookImageFolder + @"\" + BooksVM.Book.Id + ".png"; } if (BooksVM.GenresViewModels != null) { foreach (var genresView in BooksVM.GenresViewModels) { if (genresView.Selected) { BookGenres bookGenres = new BookGenres() { BookId = bookFromDb.Id, GenresId = genresView.Genres.Id }; _context.BookGenres.Add(bookGenres); } } } bookFromDb.Approved = false; await _context.SaveChangesAsync(); _iSingleton.LogException("Thêm sách Id: " + bookFromDb.Id + " " + bookFromDb.BookName); return(RedirectToAction(nameof(Index))); } ViewData["AuthorId"] = new SelectList(_context.Authors, "Id", "Id", BooksVM.Book.AuthorId); ViewData["PublisherId"] = new SelectList(_context.Publishers, "Id", "Id", BooksVM.Book.PublisherId); return(View(BooksVM.Book)); } catch (Exception e) { _iSingleton.LogException(e.Message); return(RedirectToAction("Error", "Log")); } }
public async Task CreateOrUpdate(BookDTO dto) { var entity = this.mapper.Map <Book>(dto); ThrowIf.Null(entity, nameof(entity)); this.Uow.BookRepository.InsertOrUpdate(entity); var tagEntities = new List <Tag>(); if (dto.Tags.IsNotNullOrEmpty()) { var tags = dto.Tags.Split(new char[] { ',' }); foreach (var tag in tags) { var tagEntity = this.Uow.TagRepository.Get(entity => entity.Name == tag && entity.TagType == TagType.Public).FirstOrDefault(); if (tagEntity == null) { tagEntity = new Tag() { Id = Guid.NewGuid(), TagType = TagType.Public, Name = tag }; this.Uow.TagRepository.Insert(tagEntity); tagEntities.Add(tagEntity); } } } var associatedTags = this.Uow.BookTagRepository.Get(entity => entity.SourceId == dto.Id) .Select(x => x.TargetId).ToList(); var tagsToBeDeleted = associatedTags.Where(x => tagEntities.Any(tag => tag.Id == x) == false); var tagsNeedToBeAdded = tagEntities.Where(x => associatedTags.Contains(x.Id) == false); var bookSEO = this.Uow.BookSEORepository.Get(x => x.BookId == entity.Id).FirstOrDefault(); if (tagsToBeDeleted.Any()) { foreach (var tagId in tagsToBeDeleted) { var bookTag = this.Uow.BookTagRepository.Get(x => x.TargetId == tagId).SingleOrDefault(); this.Uow.BookTagRepository.Delete(bookTag); } } if (tagsNeedToBeAdded.Any()) { foreach (var tagId in tagsNeedToBeAdded) { var association = new BookTag { Id = Guid.NewGuid() }; association.SourceId = entity.Id; association.TargetId = tagId.Id; this.Uow.BookTagRepository.Insert(association); } } if (dto.IsPublished) { if (bookSEO == null) { this.Uow.BookSEORepository.Insert(new BookSEO { BookId = entity.Id, Description = dto.Summary, Image = dto.Cover, Keywords = dto.Tags, Locale = dto.ContentLanguage, PageName = dto.PageName, Title = dto.Title, }); } else { bookSEO.Description = dto.Summary; bookSEO.Image = dto.Cover; bookSEO.Keywords = dto.Tags; bookSEO.Locale = dto.ContentLanguage; bookSEO.PageName = dto.PageName; bookSEO.Title = dto.Title; this.Uow.BookSEORepository.Update(bookSEO); } } else { if (bookSEO != null) { this.Uow.BookSEORepository.Delete(bookSEO.Id); } } this.Uow.Save(); }
public async Task UpdateAsync(BookTag entity) { await _bookTagRepository.UpdateAsync(entity); }