diff --git "a/\345\220\264\350\257\227\350\214\265/20240530-8.\345\260\217\345\236\213\351\241\271\347\233\256.md" "b/\345\220\264\350\257\227\350\214\265/20240530-8.\345\260\217\345\236\213\351\241\271\347\233\256\350\216\267\345\217\226\346\211\200\346\234\211\346\225\260\346\215\256.md" similarity index 100% rename from "\345\220\264\350\257\227\350\214\265/20240530-8.\345\260\217\345\236\213\351\241\271\347\233\256.md" rename to "\345\220\264\350\257\227\350\214\265/20240530-8.\345\260\217\345\236\213\351\241\271\347\233\256\350\216\267\345\217\226\346\211\200\346\234\211\346\225\260\346\215\256.md" diff --git "a/\345\220\264\350\257\227\350\214\265/20240531-9.\345\260\217\345\236\213\351\241\271\347\233\256.md" "b/\345\220\264\350\257\227\350\214\265/20240531-9.\345\260\217\345\236\213\351\241\271\347\233\256\350\216\267\345\217\226\346\214\207\345\256\232\346\225\260\346\215\256.md" similarity index 100% rename from "\345\220\264\350\257\227\350\214\265/20240531-9.\345\260\217\345\236\213\351\241\271\347\233\256.md" rename to "\345\220\264\350\257\227\350\214\265/20240531-9.\345\260\217\345\236\213\351\241\271\347\233\256\350\216\267\345\217\226\346\214\207\345\256\232\346\225\260\346\215\256.md" diff --git "a/\345\220\264\350\257\227\350\214\265/20240603-10.\345\260\217\345\236\213\351\241\271\347\233\256\346\226\260\345\242\236\346\225\260\346\215\256\344\270\216\344\277\256\346\224\271\346\225\260\346\215\256.md" "b/\345\220\264\350\257\227\350\214\265/20240603-10.\345\260\217\345\236\213\351\241\271\347\233\256\346\226\260\345\242\236\346\225\260\346\215\256\344\270\216\344\277\256\346\224\271\346\225\260\346\215\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..0797021cc6bd6717b1f7097733cdd23654c4e4fc --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240603-10.\345\260\217\345\236\213\351\241\271\347\233\256\346\226\260\345\242\236\346\225\260\346\215\256\344\270\216\344\277\256\346\224\271\346\225\260\346\215\256.md" @@ -0,0 +1,139 @@ +## 小型项目 + +在前面的基础上增加代码: + +在AuthorRepository.cs里写: + +``` +using BookStore.Api.Dto; +namespace BookStore.Api.Services; + +public class AuthorRepository : IAuthorRepository +{ + public AuthorDto Insert(AuthorCreateDto authorCreateDto) + { + // 将传入的dto转换为保存到数据库需要的实体类型 + var author=new Authors + { + Id=Guid.NewGuid(), + AuthorName=authorCreateDto.AuthorName, + Gender=authorCreateDto.Gender, + Birth=authorCreateDto.Birth, + }; + // 插入数据库 + 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(); + // let resultList=[] + var resultList=new List(); + list.ForEach(item=> + { + // 实例化一个对象这里有两种方式,一个是直接调用构造函数,形如: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(tmpResult!=null) + // { + // xResult=new AuthorDto{Id=tmp.Id,AuthorName=tmp.AuthorName,Gender=tmp.Gender}; + // } + return tmpResult; + } + + public AuthorDto? Update(Guid authorId,AuthorUpdateDto authorUpdateDto) + { + var author=BookStoreDb.Instance.Authors.FirstOrDefault(item=>item.Id==authorId); + if(author==null) + { + return null; + } + // 修改值 + author.AuthorName=authorUpdateDto.AuthorName; + author.Gender=authorUpdateDto.Gender; + author.Birth=authorUpdateDto.Birth; + + var result=new AuthorDto{Id=author.Id,AuthorName=author.AuthorName,Gender=author.Gender}; + return result; + } +} +``` + +在AuthorsController.cs里写: + +``` +using BookStore.Api.Dto; +using Microsoft.AspNetCore.Mvc; + +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) + { + // 1.拿到AuhtorCreateDto类型的实例化数据-模型绑定 + // 2.将相关数据保存到数据库 + // ① 转换AuthorCreateDto类型的数据为Authors + // ② 调用数据库上下文,将数据插入 + 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 Delete(Guid id) + { + var author=_authorRepository.Delete(id); + return Ok(author); + } +} +``` \ No newline at end of file diff --git "a/\345\220\264\350\257\227\350\214\265/20240604-11.\345\260\217\345\236\213\351\241\271\347\233\256\345\210\240\351\231\244\346\225\260\346\215\256.md" "b/\345\220\264\350\257\227\350\214\265/20240604-11.\345\260\217\345\236\213\351\241\271\347\233\256\345\210\240\351\231\244\346\225\260\346\215\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..78a0d72bafb366c7f1e083d7fe1a3569afaf9056 --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240604-11.\345\260\217\345\236\213\351\241\271\347\233\256\345\210\240\351\231\244\346\225\260\346\215\256.md" @@ -0,0 +1,54 @@ +## 小型项目 + +在AuthorRepository.cs里写: + +``` +public AuthorDto? Delete(Guid authorId) +{ + // 先找出满足id的元素,然后再移除对应元素 + var tmp=BookStoreDb.Instance.Authors.FirstOrDefault(item=>itemId==authorId); + // var tmpResult=tmp!=null?new AuthorDto{Id=tmp.Id,AuthorName=tmpAuthorName,Gender=tmp.Gender}:null; + if(tmp!=null) + { + BookStoreDb.Instance.Authors.Remove(tmp); + var dto=new AuthorDto{Id=tmp.Id,AuthorName=tmp.AuthorNameGender=tmp.Gender}; + return dto; + } + else + { + return null; + } +} +``` + +在AuthorsController.cs里写: + +``` +public IActionResult Delete(Guid id) +{ + var author=_authorRepository.Delete(id); + return Ok(author); +} +``` + +在IAuthorRepository.cs里写: + +``` +using BookStore.Api.Dto; + +namespace BookStore.Api; + +public interface IAuthorRepository +{ + // 通过Id获取指定作者的方法 + AuthorDto? GetAuthorById(Guid id); + + ICollectionGetAllAuthors(); + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + AuthorDto Insert(AuthorCreateDto authorCreateDto); + AuthorDto? Update(Guid authorId,AuthorUpdateDto authorUpdateDto); + AuthorDto? Delete(Guid authorId); +} +``` \ No newline at end of file diff --git "a/\345\220\264\350\257\227\350\214\265/20240606-12.\345\260\217\345\236\213\351\241\271\347\233\256-\345\233\276\344\271\246.md" "b/\345\220\264\350\257\227\350\214\265/20240606-12.\345\260\217\345\236\213\351\241\271\347\233\256-\345\233\276\344\271\246.md" new file mode 100644 index 0000000000000000000000000000000000000000..5211bb2e8be761781443d660d53be87575bc57cb --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240606-12.\345\260\217\345\236\213\351\241\271\347\233\256-\345\233\276\344\271\246.md" @@ -0,0 +1,257 @@ +## 小型项目 + +在Program.cs中写: + +``` +namespace BookStore.Api; + +public static class Program +{ + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + public static IHostBuilder CreateWebHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder=> + { + webBuilder.UseStartup(); + }); + } +} +``` + +在Startup.cs中写: + +``` +using BookStore.Api.Interface; +using BookStore.Api.Services; +namespace BookStore.Api; +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints=> + { + endpoints.MapControllers(); + }); + } + public void ConfigureServices(IServiceCollection services) + { + // 将swagger端点服务器和界面服务注册到容器 + services.AddEndpointsApiExplorer(); + services.AddSwaggerGen(); + + // 将控制器服务注册到容器 + services.AddControllers(); + + // 将仓储服务注册到容器 + services.AddScoped(); + services.AddScoped(); + } +} +``` + +在BookStoreDb.cs中写: + +``` +using BookStore.Api.Domain; + +namespace BookStore.Api.Db; +public class BookStoreDb +{ + public static BookStoreDb Instance { get; set;}=new BookStoreDb(); + // 常见集合接口ICollection、IQueryable、IEnumable、IList + public ICollectionAuthors { get; set;}=new List{}; + public ICollectionBooks{ get; set;}=new List{}; + public BookStoreDb() + { + Authors.Add(new Authors + { + Id=Guid.NewGuid(), + AuthorName="思航航航", + Gender=2, + Birth=DateTime.Now, + }); + Authors.Add(new Authors + { + Id=Guid.NewGuid(), + AuthorName="小咕叽叽", + Gender=1, + Birth=DateTime.Now, + }); + Authors.Add(new Authors + { + Id=Guid.NewGuid(), + AuthorName="咕叽咕叽叽", + Gender=2, + Birth=DateTime.Now, + }); + Authors.Add(new Authors + { + Id=Guid.NewGuid(), + AuthorName="司丝", + Gender=1, + Birth=DateTime.Now, + }); + Authors.Add(new Authors + { + Id=Guid.NewGuid(), + AuthorName="绾绾", + Gender=2, + Birth=DateTime.Now, + }); + Books.Add(new Books + { + Id=Guid.NewGuid(), + BookName="哈利波特1", + Pubisher="山东出版社", + Price=88, + AuthorId=Authors.Single(item=>item.AuthorName=="绾绾").Id, + }); + Books.Add(new Books + { + Id=Guid.NewGuid(), + BookName="哈利波特2", + Pubisher="广东出版社", + Price=33, + AuthorId=Authors.Single(item=>item.AuthorName=="思航航航").Id, + }); + Books.Add(new Books + { + Id=Guid.NewGuid(), + BookName="哈利波特3", + Pubisher="西北出版社", + Price=66, + AuthorId=Authors.Single(item=>item.AuthorName=="司丝").Id, + }); + Books.Add(new Books + { + Id=Guid.NewGuid(), + BookName="哈利波特4", + Pubisher="江苏出版社", + Price=55, + AuthorId=Authors.Single(item=>item.AuthorName=="思航航航").Id, + }); + Books.Add(new Books + { + Id=Guid.NewGuid(), + BookName="哈利波特5", + Pubisher="杭州出版社", + Price=99, + AuthorId=Authors.Single(item=>item.AuthorName=="咕叽咕叽叽").Id, + }); + } +} +``` + +在BookController.cs里写: + +``` +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 bookList=_bookRepository.GetAllBooks(authorId); + return Ok(bookList); + } + 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 bookId) + { + return Ok(bookId); + } + + [HttpDelete("{bookId?}")] + public IActionResult Delete(Guid bookId) + { + return Ok(bookId); + } +} +``` + +在Books.cs中写: + +``` +namespace BookStore.Api.Domain; + +public class Books +{ + public Guid Id{ get; set; } + public string BookName { get; set;}=null!; + public string? Pubisher { get; set;} + public int Price { get; set;} + public Guid AuthorId{ get; set; } +} +``` + +在BookDto.cs中写: + +``` +namespace BookStore.Api.Dto; + +public class BookDto +{ + public Guid Id{ get; set; } + public string BookName { get; set;}=null!; + public Guid AuthorId{ get; set; } + public dynamic? Author{ get; set; } +} +``` + +在BookCreateDto.cs中写: + +``` +namespace BookStore.Api.Dto; + +public class BookCreateDto +{ + public string BookName { get; set;}=null!; + public string? Pubisher { get; set;} + public int Price { get; set;} + public Guid AuthorId{ get; set; } +} +``` + +在BookUpdateDto.cs中写: + +``` +namespace BookStore.Api.Dto; + +// 专为作者更新传输而设 +public class BookUpdateDto +{ + public string BookName { get; set;}=null!; + public string? Pubisher { get; set;} + public int Price { get; set;} + public Guid AuthorId{ get; set; } +} +``` \ No newline at end of file diff --git "a/\345\220\264\350\257\227\350\214\265/20240607-13.\345\260\217\345\236\213\351\241\271\347\233\256-\345\233\276\344\271\246.md" "b/\345\220\264\350\257\227\350\214\265/20240607-13.\345\260\217\345\236\213\351\241\271\347\233\256-\345\233\276\344\271\246.md" new file mode 100644 index 0000000000000000000000000000000000000000..489c7a110b6342dcafba45d8234f8ca8f0f41c50 --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240607-13.\345\260\217\345\236\213\351\241\271\347\233\256-\345\233\276\344\271\246.md" @@ -0,0 +1,146 @@ +## 小型项目 + +在IBookRepository.cs中写: + +``` +using BookStore.Api.Dto; +namespace BookStore.Api.Interface; + +public interface IBookRepository +{ + BookDto? GetBookById(Guid authorId,Guid bookId); + + ICollectionGetAllBooks(Guid authorId); + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + BookDto? Insert(Guid authorId,BookCreateDto bookCreateDto); + BookDto? Update(Guid authorId,Guid BookId,BookUpdateDto bookUpdateDto); + BookDto? Delete(Guid authorId,Guid BookId); +} +``` + +在BookRepository.cs中写: + +``` +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) + { + throw new NotImplementedException(); + } + + public ICollection GetAllBooks(Guid authorId) + { + // 较好的做法,是确认对应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) + { + // 1.确认传入的authorId存在对应的记录 + // 如果存在,继续后面的流程,否则返回null + // 2.将输入的数据进行整理转换,插入到数据中 + // ① 将BookCreateDto类型的数据转换成Books类型的数据 + // ② 将转换到books数据插入到数据库 + // 3.为了简单,目前从未对传入数据进行任何验证,原因就是为了让整个应用尽量简单,后续一定要进行数据验证 + // ① 简单验证 数据类型,是否为空,字符长度,是否一定格式等 + // ② 复杂验证或者叫业务验证 + 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, + Pubisher=bookCreateDto.Pubisher, + Price=bookCreateDto.Price, + AuthorId=authorId + }; + BookStoreDb.Instance.Books.Add(tmp); + var result=new BookDto + { + Id=tmp.Id, + BookName=tmp.BookName, + AuthorId=tmp.AuthorId + }; + return result; + } + + public BookDto? Update(Guid authorId, Guid BookId, BookUpdateDto bookUpdateDto) + { + throw new NotImplementedException(); + } +} +``` + +在BooksStore.Api.http中写: + +``` +@url = http://localhost:5000 + +### 获取作者列表 +GET {{url}}/api/authors HTTP/1.1 + +### 获取指定id的作者 +GET {{url}}/api/authors/2adf521f-f160-4e7c-8f5a-36af005e917c HTTP/1.1 + +### 新增作者 +POST {{url}}/api/authors HTTP/1.1 +Content-Type: application/json + +{ + "authorName":"咕叽咕叽咕sssssssss叽咕aaaaaa", + "gender":2, + "birth":"2000-08-07" +} + +### 修改 +PUT {{url}}/api/authors/2adf521f-f160-4e7c-8f5a-36af005e917c HTTP/1.1 +Content-Type: application/json + +{ + "authorName":"司丝司丝司丝", + "gender":1, + "birth":"1985-05-07" +} + +### 删除 +DELETE {{url}}/api/authors/89fa5c87-2ecd-4f4d-8e23-aa270d80ba8f HTTP/1.1 + +### 获取指定作者下的图书列表 +GET {{url}}/api/authors/581381ff-126c-4cd9-8ce5-a034bfffadef/books HTTP/1.1 + +### 获取指定作者下的图书 +GET {{url}}/api/authors/581381ff-126c-4cd9-8ce5-a034bfffadef/books/7cfc33ec-3df2-4eb0-a8ea-7eedc128d461 HTTP/1.1 +``` \ No newline at end of file