Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("AuthorToBookId,AuthorId,BookId")] AuthorToBook authorToBook)
        {
            if (id != authorToBook.AuthorToBookId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(authorToBook);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorToBookExists(authorToBook.AuthorToBookId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "name", authorToBook.AuthorId);
            ViewData["BookId"]   = new SelectList(_context.Books, "BookId", "name", authorToBook.BookId);
            return(View(authorToBook));
        }
        public IList<AuthorsAndCategoriesDto> GetAuthorsWithSpecifyCategory(string category)
        {
            /*
             select distinct a.FirstName, a.LastName --, b.CategoryName 
                from [Author] a
                inner join [BookToAuthor] ba on a.Id = ba.AuthorId 
                inner join [Book] b on ba.BookId = b.Id
                where b.CategoryName = Any (select CategoryName from [Book] where CategoryName = 'Drama')

            */
            AuthorToBook authorToBookAlies = null;
            Author authorAlies = null;
            Book bookAlies = null;
            BookCategory bookCategoryAlies = null;
            AuthorsAndCategoriesDto dtoAlies = null;

            var subquery = QueryOver.Of(() => bookAlies)
                .JoinAlias(() => bookAlies.Category, () => bookCategoryAlies)
                .JoinQueryOver(() => bookAlies.Authors, () => authorToBookAlies)
                .Where(() => bookCategoryAlies.CategoryName == category)
                .Select(x => authorToBookAlies.Author.Id);

            var result = _session.QueryOver(() => authorAlies)
                .SelectList(list => list
                    .Select(Projections.Distinct(Projections.Property(() => authorAlies.FirstName))).WithAlias(() => dtoAlies.FirstName)
                    .Select(() => authorAlies.LastName).WithAlias(() => dtoAlies.LastName)
                )

                .WithSubquery.WhereProperty(() => authorAlies.Id).In(subquery)
                //.WithSubquery.WhereProperty()
                .TransformUsing(Transformers.AliasToBean<AuthorsAndCategoriesDto>())
                .List<AuthorsAndCategoriesDto>();
            return result;
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("AuthorToBookId,AuthorId,BookId")] AuthorToBook authorToBook)
        {
            if (ModelState.IsValid)
            {
                _context.Add(authorToBook);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "name", authorToBook.AuthorId);
            ViewData["BookId"]   = new SelectList(_context.Books, "BookId", "name", authorToBook.BookId);
            return(View(authorToBook));
        }