Exemplo n.º 1
0
        public async Task PopulateBooks()
        {
            var dbCtx = new LibraryDbContext();

            var genre1 = new DbGenre()
            {
                Genre = "Education"
            };

            var hashtagAgile = new DbHashtag()
            {
                Hastag = "#agile"
            };
            var hashtagCosmos = new DbHashtag()
            {
                Hastag = "#cosmos"
            };
            var hashtagCoding = new DbHashtag()
            {
                Hastag = "#coding"
            };

            var book1 = new DbBook()
            {
                Title    = "Clean Code: A Handbook of Agile Software Craftmanship",
                Author   = "Robert C. Martin",
                Hashtags = new List <DbHashtag>()
            };

            var book2 = new DbBook()
            {
                Title    = "Cosmos",
                Author   = "Carl Sagan",
                Hashtags = new List <DbHashtag>()
            };

            dbCtx.Books.Add(book1);
            dbCtx.Books.Add(book2);
            dbCtx.Genres.Add(genre1);
            dbCtx.Hashtags.Add(hashtagAgile);
            dbCtx.Hashtags.Add(hashtagCosmos);
            dbCtx.Hashtags.Add(hashtagCoding);

            book1.Genre = genre1;
            book1.Hashtags.Add(hashtagAgile);
            book1.Hashtags.Add(hashtagCoding);

            book2.Genre = genre1;
            book1.Hashtags.Add(hashtagCosmos);

            await dbCtx.SaveChangesAsync();
        }
        public async Task <int> AddReview(Reviews review)
        {
            _dbContext.Reviews.Add(ConvertToDbReviews(review));

            var reviewedBook = _dbContext.Books.FirstOrDefault(book => book.Code == review.BookCode);

            foreach (Match match in Regex.Matches(review.Review, @"(?<!\w)#\w+"))
            {
                var hashtag    = match.Value;
                var newHashtag = new DbHashtag()
                {
                    Hastag = hashtag
                };
                _dbContext.Hashtags.Add(newHashtag);
                reviewedBook.Hashtags.Add(newHashtag);
            }

            return(await _dbContext.SaveChangesAsync());
        }