diff --git "a/\350\265\265\346\254\243/0603.\350\267\257\347\224\261\346\237\245\350\257\242.md" "b/\350\265\265\346\254\243/0603.\350\267\257\347\224\261\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..23c16280eb32f3e2679a431dc8c5244fbd198222 --- /dev/null +++ "b/\350\265\265\346\254\243/0603.\350\267\257\347\224\261\346\237\245\350\257\242.md" @@ -0,0 +1,102 @@ +## 路由查询· +### 接口 +```C# + using BookStore.Api.Dto; + +namespace BookStore.Api.Interface; + +public interface IAuthorRepository +{ + // 通过Id获取指定作者的方法 + AuthorDto? GetAuthorById(Guid id); + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + ICollection GetAllAuthors(); + + + AuthorDto Insert(AuthorCreateDto authorCreateDto); + +} + ``` +### 实现 +```C# + public AuthorDto Insert(AuthorCreateDto authorCreateDto) + { + // 将传入的dto转换为保存到数据库需要的实体类型 + var author = new Authors + { + Id = Guid.NewGuid(), + AuthorName = authorCreateDto.AuthorName, + Gender = authorCreateDto.Gender, + Birthday = authorCreateDto.Birthday, + }; + // 插入数据库 + BookStoreDb.Instance.Authors.Add(author); + // 将内存数据库获取的数据转换为AuthorDto的实例 + var authorDto = new AuthorDto { Id = author.Id, AuthorName = author.AuthorName, Gender = author.Gender }; + return authorDto; + } + + public ICollection GetAllAuthors() + { + // 这个实现应该从持久化的数据源中获得:数据库、文件、各种异构数据、从各种api中获取的数据 + var list = BookStoreDb.Instance.Authors.ToList(); + var resultList = new List();// let resultList=[] + list.ForEach(item => + { + // 实例化一个对象这里有2种方式,一个是直接调用构造函数,形如:new AuthorDto() + // 另一个是直接填充其属性,形如:new AuthorDto{} + var tmp = new AuthorDto { Id = item.Id, AuthorName = item.AuthorName, Gender = item.Gender }; + resultList.Add(tmp); + }); + return resultList; + } + + public AuthorDto? GetAuthorById(Guid id) + { + var tmp = BookStoreDb.Instance.Authors.SingleOrDefault(item => item.Id == id); + var tmpResult = tmp != null ? new AuthorDto { Id = tmp.Id, AuthorName = tmp.AuthorName, Gender = tmp.Gender } : null; + // dynamic xResult=null; + // if(tmp!=null){ + // xResult=new AuthorDto { Id = tmp.Id, AuthorName = tmp.AuthorName, Gender = tmp.Gender }; + // } + return tmpResult; + } + ``` +### action +```C# + [HttpGet("{id?}")] + public IActionResult Get(Guid id) + { + if (id.ToString() == "00000000-0000-0000-0000-000000000000") + { + return Ok(_authorRepository.GetAllAuthors()); + } + else + { + var item = _authorRepository.GetAuthorById(id); + return Ok(item); + } + } + + /// + /// 新增作者 + /// + /// 接受的参数 + /// + [HttpPost] + public IActionResult Post(AuthorCreateDto authorCreateDto) + { + /* + 1.拿到AuthorCreateDto类型的实例化数据-模型绑定 + 2.将相关数据保存到数据库 + 1.转换AuthorCreateDto类型的数据为Authors + 2.调用数据库上下文,将数据插入 + + */ + var result = _authorRepository.Insert(authorCreateDto); + return Ok(result); + } + +``` \ No newline at end of file diff --git "a/\350\265\265\346\254\243/0604.\345\242\236\346\224\271.md" "b/\350\265\265\346\254\243/0604.\345\242\236\346\224\271.md" new file mode 100644 index 0000000000000000000000000000000000000000..682e84dfd1f08a9d5e147b6926355d432c5a42aa --- /dev/null +++ "b/\350\265\265\346\254\243/0604.\345\242\236\346\224\271.md" @@ -0,0 +1,79 @@ +IAuthorRepository.cs: +```c# +using BookStore.Api.Dto; + +namespace BookStore.Api.Interface; + +public interface IAuthorRepository +{ + // 通过Id获取指定作者的方法 + AuthorDto? GetAuthorById(Guid id); + + ICollection GetAllAuthors(); + + + AuthorDto Insert(AuthorCreateDto authorCreateDto); + AuthorDto? Update(Guid authorId,AuthorUpdateDto authorUpdateDto); + AuthorDto? Delete(Guid authorId); + + +} +``` + +AuthorController.cs: +```c# + +```using Microsoft.AspNetCore.Mvc; +using BookStore.Api.Interface; +using BookStore.Api.Dto; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/[controller]")] +public class AuthorsController : ControllerBase +{ + private readonly IAuthorRepository _authorRepository; + + public AuthorsController(IAuthorRepository authorRepository) + { + _authorRepository = authorRepository; + } + + [HttpGet("{id?}")] + public IActionResult Get(Guid id) + { + if (id.ToString() == "00000000-0000-0000-0000-000000000000") + { + return Ok(_authorRepository.GetAllAuthors()); + } + else + { + var item = _authorRepository.GetAuthorById(id); + return Ok(item); + } + } + + + [HttpPost] + public IActionResult Post(AuthorCreateDto authorCreateDto) + { + + var result = _authorRepository.Insert(authorCreateDto); + return Ok(result); + } + + [HttpPut("{id}")] + public IActionResult Put(Guid id, AuthorUpdateDto authorUpdateDto) + { + var result = _authorRepository.Update(id, authorUpdateDto); + return Ok(result); + } + + [HttpDelete("{id}")] + public IActionResult Del(Guid id) + { + var author = _authorRepository.Delete(id); + return Ok(author); + } +} diff --git "a/\350\265\265\346\254\243/0606.\345\233\276\344\271\246\345\242\236\346\224\271\346\237\245.md" "b/\350\265\265\346\254\243/0606.\345\233\276\344\271\246\345\242\236\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..3bd7fffea26356e4478be09318a47203773f2291 --- /dev/null +++ "b/\350\265\265\346\254\243/0606.\345\233\276\344\271\246\345\242\236\346\224\271\346\237\245.md" @@ -0,0 +1,179 @@ +## BooksController.cs: +```c# +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) + { + if (bookId.ToString() == "00000000-0000-0000-0000-000000000000") + { + var list = _bookRepository.GetAllBooks(authorId); + return Ok(list); + } + + var result = _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); + } +} +``` + +## IBookRepository.cs: +```c# +using BookStore.Api.Dto; + +namespace BookStore.Api.Interface; + +public interface IBookRepository +{ + // 通过Id获取指定作者的方法 + BookDto? GetBookById(Guid authorId,Guid bookId); + + ICollection GetAllBooks(Guid authorId); + + + BookDto? Insert(Guid authorId,BookCreateDto bookCreateDto); + BookDto? Update(Guid authorId,Guid BookId, BookUpdateDto bookUpdateDto); + BookDto? Delete(Guid authorId,Guid BookId); +} +``` + +## BookRepository.cs: +```c# +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); + if (author == null) + { + return null; + } + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == bookId); + if (book == null) + { + return null; + } + BookStoreDb.Instance.Books.Remove(book); + var result = new BookDto { Id = bookId, AuthorId = authorId, BookName = book.BookName }; + return result; + } + + 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); + if (author == null) + { + return null; + } + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.AuthorId == authorId && item.Id == bookId); + + if (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 tmp = new Books + { + Id = Guid.NewGuid(), + BookName = bookCreateDto.BookName, + Publisher = bookCreateDto.Publisher, + Price = bookCreateDto.Price, + AuthorId = authorId + }; + + BookStoreDb.Instance.Books.Add(tmp); + + var result = new BookDto { Id = tmp.Id, BookName = tmp.BookName, AuthorId = tmp.AuthorId, Author = author }; + + return result; + } + + public BookDto? Update(Guid authorId, Guid BookId, BookUpdateDto bookUpdateDto) + { + + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + + var book = BookStoreDb.Instance.Books.FirstOrDefault(item => item.Id == BookId); + if (book == null) + { + return null; + } + + book.BookName = bookUpdateDto.BookName; + book.Publisher = bookUpdateDto.Publisher; + book.Price = bookUpdateDto.Price; + book.BookName = bookUpdateDto.BookName; + + var result = new BookDto { Id = book.Id, BookName = book.BookName, AuthorId = book.AuthorId }; + return result; + } +} +``` \ No newline at end of file diff --git "a/\350\265\265\346\254\243/0607.Entity Framework Core.md" "b/\350\265\265\346\254\243/0607.Entity Framework Core.md" new file mode 100644 index 0000000000000000000000000000000000000000..1a37873065fa6871b88a169d9c935d3ab51c4960 --- /dev/null +++ "b/\350\265\265\346\254\243/0607.Entity Framework Core.md" @@ -0,0 +1,32 @@ +## 介绍:是一个基于.NETCore的轻量级ORM框架 + +特点:跨平台、可扩展 + +- 类 => 表 +- 属性 => 列 +- 实例 => 行 +- 其他类的引用 => 外键 + +```终端 +dotnet add package Microsoft.EntityFrameworkCore + +dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +dotnet tool install -g dotnet-ef + +dotnet ef migrations add firstii +// firstii是迁移的名称,此命令基于模型类生成初次数据库迁移脚本。 + +dotnet ef database update +// 将最新的数据库迁移应用到数据库中,实际执行DDL操作以更新数据库结构。 + +dotnet ef migrations remove +// 删除最后一次生成的迁移文件,不会改变数据库结构,仅作用于代码层面。 +``` + +appsettings.json: +```c# +"ConnectionStrings": { + "Mssql": "Server=.;Database=BookStore;uid=sa;pwd=123456;TrustServerCertificate = true;" +} +```