diff --git "a/\346\236\227\346\230\245\347\277\224/20240531.\344\273\223\345\202\250\346\250\241\345\274\217.md" "b/\346\236\227\346\230\245\347\277\224/20240531_\344\273\223\345\202\250\346\250\241\345\274\217.md" similarity index 100% rename from "\346\236\227\346\230\245\347\277\224/20240531.\344\273\223\345\202\250\346\250\241\345\274\217.md" rename to "\346\236\227\346\230\245\347\277\224/20240531_\344\273\223\345\202\250\346\250\241\345\274\217.md" diff --git "a/\346\236\227\346\230\245\347\277\224/20240603_\345\242\236\345\212\240\343\200\201\347\274\226\350\276\221.md" "b/\346\236\227\346\230\245\347\277\224/20240603_\345\242\236\345\212\240\343\200\201\347\274\226\350\276\221.md" new file mode 100644 index 0000000000000000000000000000000000000000..cf32648d7f7bdad72d4f3a898a193fdfccc695d5 --- /dev/null +++ "b/\346\236\227\346\230\245\347\277\224/20240603_\345\242\236\345\212\240\343\200\201\347\274\226\350\276\221.md" @@ -0,0 +1,144 @@ +- AuthorController +```csharp +[HttpGet("{id?}")] +public IActionResult Get(Guid id) +{ + var list = _authorRepository.GetAllAuthors(id); // 使用注入的接口获取所有作者信息 + return Ok(list); // 返回获取到的作者信息 +} + +[HttpPost] +public ActionResult Post(AuthorCreateDto AuthorCreateDto) +{ + var list = _authorRepository.Create(AuthorCreateDto); // 使用注入的接口创建作者 + return Ok(list); // 返回创建的作者信息 +} + +[HttpPut("{id}")] +public ActionResult Put(Guid id, AuthorEditDto authorEditDto) +{ + var list = _authorRepository.Edit(id, authorEditDto); // 使用注入的接口编辑作者信息 + return Ok(list); // 返回编辑后的作者信息 +} +``` + +- AuthorStoreDb +```csharp +public static AuthorStoreDb Index { get; set; } = new AuthorStoreDb(); // 初始化AuthorStoreDb对象 + +public ICollection Authors { get; set; } = new List(); // 初始化作者集合 + +public ICollection Books { get; set; } = new List(); // 初始化书籍集合 + +public AuthorStoreDb() +{ + // 添加作者信息到内存数据库 + Authors.Add(new Authors { Id = Guid.NewGuid(), AuthorName = "aaaa", Gender = 1 }); + Authors.Add(new Authors { Id = Guid.NewGuid(), AuthorName = "bbbb", Gender = 0 }); + Authors.Add(new Authors { Id = Guid.NewGuid(), AuthorName = "cccc", Gender = 1 }); + + // 添加书籍信息到内存数据库 + Books.Add(new Books { Id = Guid.NewGuid(), BookName = "aaa", AuthorId = Guid.NewGuid() }); + Books.Add(new Books { Id = Guid.NewGuid(), BookName = "bbb", AuthorId = Guid.NewGuid() }); + Books.Add(new Books { Id = Guid.NewGuid(), BookName = "cccc", AuthorId = Guid.NewGuid() }); +} +``` + +- Startup +```csharp +public void ConfigureServices(IServiceCollection service) +{ + service.AddControllers(); // 注册控制器 + + service.AddScoped(); // 注册仓储服务到容器 +} +``` + +- IAuthorRepository +```csharp +using Admin8.Domain; +using Admin8.Dto; + +namespace Admin8.Interface; + +public interface IAuthorRepository +{ + // 获取所有作者信息 + ICollection? GetAllAuthors(Guid id); + + // 创建作者 + AuthorDto Create(AuthorCreateDto authorCreateDto); + + // 编辑作者信息 + AuthorDto? Edit(Guid id, AuthorEditDto authorEditDto); + + // 删除作者 + AuthorDto? Delete(Guid id); +} +``` + +- AuthorRepository +```csharp +public ICollection? GetAllAuthors(Guid guid) +{ + if (guid.ToString() == "00000000-0000-0000-0000-000000000000") + { + var list = AuthorStoreDb.Index.Authors.ToList(); // 获取所有作者信息 + var result = new List(); + list.ForEach(item => + { + var temp = new AuthorDto { Id = item.Id, AuthorName = item.AuthorName, Gender = item.Gender }; + result.Add(temp); + }); + return result; // 返回作者信息列表 + } + else + { + var list = AuthorStoreDb.Index.Authors.FirstOrDefault(item => item.Id == guid); + if (list == null) + { + return null; + } + var result = new List(); + var obj = new AuthorDto { Id = list.Id, AuthorName = list.AuthorName, Gender = list.Gender }; + result.Add(obj); + return result; // 返回指定作者信息 + } +} + +public AuthorDto Create(AuthorCreateDto authorCreateDto) +{ + var obj = new Authors + { + Id = Guid.NewGuid(), + AuthorName = authorCreateDto.AuthorName, + Gender = authorCreateDto.Gender, + Password = authorCreateDto.Password + }; + AuthorStoreDb.Index.Authors.Add(obj); + var list = new AuthorDto { Id = obj.Id, AuthorName = obj.AuthorName, Gender = obj.Gender }; + return list; // 返回创建的作者信息 +} + +public AuthorDto? Edit(Guid guid, AuthorEditDto authorEditDto) +{ + var list = AuthorStoreDb.Index.Authors.FirstOrDefault(item => item.Id == guid); + if (list == null) + { + return null; + } + + list.AuthorName = authorEditDto.AuthorName; + list.Gender = authorEditDto.Gender; + list.Password = authorEditDto.Password; + + var result = new AuthorDto + { + Id = guid, + AuthorName = list.AuthorName, + Gender = list.Gender + }; + + return result; // 返回编辑后的作者信息 +} +``` \ No newline at end of file diff --git "a/\346\236\227\346\230\245\347\277\224/20240604_\345\210\240\351\231\244.md" "b/\346\236\227\346\230\245\347\277\224/20240604_\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..bfd1297789eaf77c81754863c86575fb1d7f81dd --- /dev/null +++ "b/\346\236\227\346\230\245\347\277\224/20240604_\345\210\240\351\231\244.md" @@ -0,0 +1,55 @@ +- AuthorController +```csharp +[HttpDelete("{id}")] +public IActionResult Del(Guid id) +{ + var list = _authorRepository.Delete(id); // 使用注入的接口删除指定作者信息 + return Ok(list); // 返回删除的作者信息 +} +``` + +- IAuthorRepository +```csharp +using Admin8.Domain; +using Admin8.Dto; + +namespace Admin8.Interface; + +public interface IAuthorRepository +{ + // 获取所有作者信息 + ICollection? GetAllAuthors(Guid id); + + // 创建作者 + AuthorDto Create(AuthorCreateDto authorCreateDto); + + // 编辑作者信息 + AuthorDto? Edit(Guid id, AuthorEditDto authorEditDto); + + // 删除作者 + AuthorDto? Delete(Guid id); +} +``` + +- AuthorRepository +```csharp +public AuthorDto? Delete(Guid id) +{ + var obj = AuthorStoreDb.Index.Authors.FirstOrDefault(item => item.Id == id); // 在内存数据库中查找要删除的作者信息 + if (obj == null) + { + return null; // 如果作者信息不存在,返回空 + } + + AuthorStoreDb.Index.Authors.Remove(obj); // 从内存数据库中删除作者信息 + + var result = new AuthorDto + { + Id = obj.Id, + AuthorName = obj.AuthorName, + Gender = obj.Gender + }; + + return result; // 返回被删除的作者信息 +} +``` \ No newline at end of file diff --git "a/\346\236\227\346\230\245\347\277\224/20240606_\345\256\236\347\216\260\344\270\244\345\274\240\350\241\250\347\232\204crudmd" "b/\346\236\227\346\230\245\347\277\224/20240606_\345\256\236\347\216\260\344\270\244\345\274\240\350\241\250\347\232\204crudmd" new file mode 100644 index 0000000000000000000000000000000000000000..b7cff18347eb8d14bac33c91b2b9df86ccf9ef8c --- /dev/null +++ "b/\346\236\227\346\230\245\347\277\224/20240606_\345\256\236\347\216\260\344\270\244\345\274\240\350\241\250\347\232\204crudmd" @@ -0,0 +1,138 @@ +- BookController +```csharp +using BookStore.Api.Dto; +using BookStore.Api.Interface; +using Microsoft.AspNetCore.Mvc; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/authors/{authorId}/[controller]/")] +public class BooksController : ControllerBase +{ + private readonly IBookRepository _bookRepository; + + public BooksController(IBookRepository bookRepository) + { + _bookRepository = bookRepository; + } + + [HttpGet("{bookId?}")] + public IActionResult Get(Guid authorId, Guid bookId) + { + var result = bookId == Guid.Empty ? _bookRepository.GetAllBooks(authorId) : _bookRepository.GetBookById(authorId, bookId); + return Ok(result); + } + + [HttpPost] + public IActionResult Post(Guid authorId, BookCreateDto bookCreateDto) + { + var result = _bookRepository.Insert(authorId, bookCreateDto); + return Ok(result); + } + + [HttpPut("{bookId}")] + public IActionResult Put(Guid authorId, Guid bookId, BookUpdateDto bookUpdateDto) + { + var result = _bookRepository.Update(authorId, bookId, bookUpdateDto); + return Ok(result); + } + + [HttpDelete("{bookId}")] + public IActionResult Delete(Guid authorId, Guid bookId) + { + var result = _bookRepository.Delete(authorId, bookId); + return Ok(result); + } +} +``` + +- BookRepository +```csharp +using BookStore.Api.Db; +using BookStore.Api.Domain; +using BookStore.Api.Dto; +using BookStore.Api.Interface; + +namespace BookStore.Api.Services; + +public class BookRepository : IBookRepository +{ + public BookDto? Delete(Guid authorId, Guid bookId) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + var book = author != null ? BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == bookId) : null; + + if (author == null || book == null) + return null; + + BookStoreDb.Instance.Books.Remove(book); + return new BookDto { Id = bookId, AuthorId = authorId, BookName = book.BookName }; + } + + public ICollection GetAllBooks(Guid authorId) + { + var list = BookStoreDb.Instance.Books.Where(item => item.AuthorId == authorId) + .Select(item => new BookDto { Id = item.Id, AuthorId = item.AuthorId, BookName = item.BookName }).ToList(); + return list; + } + + public BookDto? GetBookById(Guid authorId, Guid bookId) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + var book = author != null ? BookStoreDb.Instance.Books.FirstOrDefault(item => item.AuthorId == authorId && item.Id == bookId) : null; + + if (author == null || book == null) + return null; + + return new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + } + + public BookDto? Insert(Guid authorId, BookCreateDto bookCreateDto) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + return null; + + var newBook = new Books + { + Id = Guid.NewGuid(), + BookName = bookCreateDto.BookName, + Publisher = bookCreateDto.Publisher, + Price = bookCreateDto.Price, + AuthorId = authorId + }; + + BookStoreDb.Instance.Books.Add(newBook); + + return new BookDto { Id = newBook.Id, BookName = newBook.BookName, AuthorId = newBook.AuthorId, Author = author }; + } + + public BookDto? Update(Guid authorId, Guid bookId, BookUpdateDto bookUpdateDto) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + var book = author != null ? BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == bookId) : null; + + if (author == null || book == null) + return null; + + book.BookName = bookUpdateDto.BookName; + book.Publisher = bookUpdateDto.Publisher; + book.Price = bookUpdateDto.Price; + + return new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + } +} +``` + +- Startup +```csharp +public void ConfigureServices(IServiceCollection services) +{ + services.AddEndpointsApiExplorer(); + services.AddSwaggerGen(); + services.AddControllers(); + services.AddScoped(); + services.AddScoped(); // 添加书籍仓储服务到容器 +} +``` \ No newline at end of file diff --git "a/\346\236\227\346\230\245\347\277\224/20240607_EF Core.md" "b/\346\236\227\346\230\245\347\277\224/20240607_EF Core.md" new file mode 100644 index 0000000000000000000000000000000000000000..e27713fd511005495e39c2d81cd5c7ec851cf432 --- /dev/null +++ "b/\346\236\227\346\230\245\347\277\224/20240607_EF Core.md" @@ -0,0 +1,58 @@ +#### IRepositoryBase 接口 +```csharp +public interface IRepositoryBase +{ + T GetById(Guid id); + List GetAll(); + T Insert(T entity); + T Update(T entity); + void Delete(T entity); + void Delete(Guid id); +} +``` + +#### BookStoreDbContext 类 +```csharp +using BookStore.Api.Domain; +using Microsoft.EntityFrameworkCore; + +namespace BookStore.Api.Db; + +public class BookStoreDbContext : DbContext +{ + public BookStoreDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet Authors { get; set; } + public DbSet Books { get; set; } +} +``` + +#### Startup 类 +```csharp +public void ConfigureServices(IServiceCollection services) +{ + // 添加Swagger服务 + services.AddSwaggerGen(); + + // 添加控制器服务 + services.AddControllers(); + + // 添加仓储服务 + services.AddScoped(); + services.AddScoped(); + + // 注册数据库上下文到容器 + services.AddDbContext(options => + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")) + ); +} +``` + +#### 更新迁移命令 +- 更新 EF 工具包版本:`dotnet tool update -g dotnet-ef` +- 生成迁移文件:`dotnet ef migrations add InitialCreate` +- 应用迁移:`dotnet ef database update` + +通过这些优化,你的 EF Core 数据库部分代码变得更加简洁和易于理解。 \ No newline at end of file