using System; using System.Threading.Tasks; using Acme.BookStore.Authors; using Acme.BookStore.Books; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories;
var orwell = await _authorRepository.InsertAsync( await _authorManager.CreateAsync( "George Orwell", new DateTime(1903, 06, 25), "Orwell produced literary criticism and poetry, fiction and polemical journalism; and is best known for the allegorical novella Animal Farm (1945) and the dystopian novel Nineteen Eighty-Four (1949)." ) );
var douglas = await _authorRepository.InsertAsync( await _authorManager.CreateAsync( "Douglas Adams", new DateTime(1952, 03, 11), "Douglas Adams was an English author, screenwriter, essayist, humorist, satirist and dramatist. Adams was an advocate for environmentalism and conservation, a lover of fast cars, technological innovation and the Apple Macintosh, and a self-proclaimed 'radical atheist'." ) );
await _bookRepository.InsertAsync( new Book { AuthorId = orwell.Id, // 关联作者 Name = "1984", Type = BookType.Dystopia, PublishDate = new DateTime(1949, 6, 8), Price = 19.84f }, autoSave: true );
await _bookRepository.InsertAsync( new Book { AuthorId = douglas.Id, // 关联作者 Name = "The Hitchhiker's Guide to the Galaxy", Type = BookType.ScienceFiction, PublishDate = new DateTime(1995, 9, 27), Price = 42.0f }, autoSave: true ); } }
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using Acme.BookStore.Authors; using Acme.BookStore.Permissions; using Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories;
namespaceAcme.BookStore.Books;
[Authorize(BookStorePermissions.Books.Default)] publicclassBookAppService : CrudAppService< Book, //TheBookentity BookDto, //Usedtoshowbooks Guid, //Primarykeyofthebookentity PagedAndSortedResultRequestDto, //Usedforpaging/sorting CreateUpdateBookDto>, //Used to create/update a book IBookAppService//implement the IBookAppService { privatereadonly IAuthorRepository _authorRepository;
// 关联作者 var query = from book in queryable join author inawait _authorRepository.GetQueryableAsync() on book.AuthorId equals author.Id where book.Id == id selectnew { book, author }; var queryResult = await AsyncExecuter.FirstOrDefaultAsync(query); if (queryResult == null) { thrownew EntityNotFoundException(typeof(Book), id); }
// 关联作者 var query = from book in queryable join author inawait _authorRepository.GetQueryableAsync() on book.AuthorId equals author.Id selectnew {book, author};
using System; using System.Linq; using System.Threading.Tasks; using Acme.BookStore.Authors; using Shouldly; using Volo.Abp.Application.Dtos; using Volo.Abp.Modularity; using Volo.Abp.Validation; using Xunit;
namespace Acme.BookStore.Books;
public abstract class BookAppService_Tests<TStartupModule> : BookStoreApplicationTestBase<TStartupModule> where TStartupModule : IAbpModule { private readonly IBookAppService _bookAppService; private readonly IAuthorAppService _authorAppService;
[Fact] public async Task Should_Create_A_Valid_Book() { var authors = await _authorAppService.GetListAsync(new GetAuthorListDto()); var firstAuthor = authors.Items.First();
//Act var result = await _bookAppService.CreateAsync( new CreateUpdateBookDto { AuthorId = firstAuthor.Id, Name = "New test book 42", Price = 10, PublishDate = System.DateTime.Now, Type = BookType.ScienceFiction } );
//Assert result.Id.ShouldNotBe(Guid.Empty); result.Name.ShouldBe("New test book 42"); }
[Fact] public async Task Should_Not_Create_A_Book_Without_Name() { var exception = await Assert.ThrowsAsync<AbpValidationException>(async () => { await _bookAppService.CreateAsync( new CreateUpdateBookDto { Name = "", Price = 10, PublishDate = DateTime.Now, Type = BookType.ScienceFiction } ); });
exception.ValidationErrors .ShouldContain(err => err.MemberNames.Any(m => m == "Name")); } }