diff --git "a/\345\220\264\350\257\227\350\214\265/20240527-6.\345\205\263\344\272\216\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\345\220\264\350\257\227\350\214\265/20240527-6.\345\205\263\344\272\216\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..19581b6b31379d51c7472c3ce106422ffe9c6507 --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240527-6.\345\205\263\344\272\216\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,130 @@ +## 关于增删改查 + +在Program.cs中写: + +``` +namespace mm.Api; + +public static class Program +{ + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + public static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(builder=> + { + builder.UseStartup(); + }); + } +} +``` + +在BlogsController.cs中写: + +``` +using Microsoft.AspNetCore.Mvc; + +namespace mm.Api; + +[ApiController] +[Route("[controller]")] +public class BlogsController:ControllerBase +{ + // /blogs/:id + [Route("{id?}")] + public IActionResult Details(int id) + { + return Ok(id); + } + + [HttpPost] + public ActionResult Post(BlogDto blogDto) + { + return blogDto; + } + + [HttpPut("{id}")] + public ActionResult Put(int id, BlogDto blogDto) + { + return new{id,blogDto}; + } + + public IActionResult Delete(int id) + { + return Ok(id); + } +} +``` + +在BlogDto.cs中写: + +``` +namespace mm.Api; + +public class BlogDto +{ + public string Title { get; set;}=null!; + public string Author{ get; set;}=null!; + public string? Flag{ get; set;} +} +``` + +在Startup.cs中写: + +``` +namespace mm.Api; + +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints =>endpoints.MapControllers()); + } + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } +} +``` + +在mm.Api.http中写: + +``` +@url = http://localhost:8080 + +### +GET {{url}}/blogs +Accept: application/json + +### +GET {{url}}/blogs/20 HTTP/1.1 + +### +GET {{url}}/users HTTP/1.1 + +### +POST {{url}}/blogs HTTP/1.1 +Content-Type: application/json + +{ + "title":"思航航", + "author":"咕叽咕叽", + "flag":"小咕叽" +} + +### +PUT {{url}}/blogs/9 HTTP/1.1 +Content-Type: application/json + +{ + "title":"思航航", + "author":"咕叽咕叽", + "flag":"小咕叽" +} + +### +DELETE {{url}}/blogs/99 HTTP/1.1 +``` \ No newline at end of file diff --git "a/\345\220\264\350\257\227\350\214\265/20240528-7.\350\267\257\347\224\261.md" "b/\345\220\264\350\257\227\350\214\265/20240528-7.\350\267\257\347\224\261.md" new file mode 100644 index 0000000000000000000000000000000000000000..f6698c6806327cd2d0d33cfe1b6659636b030d6a --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240528-7.\350\267\257\347\224\261.md" @@ -0,0 +1,140 @@ +## 路由 + +在Program.cs中写: + +``` +namespace mm.Api; + +public static class Program +{ + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + public static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(static builder => + { + builder.UseStartup(); + }); + } +} +``` + +在Startup.cs中写: + +``` +using Microsoft.AspNetCore.Mvc; +using mm.Api.Filters; + +namespace mm.Api; + +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints =>endpoints.MapControllers()); + } + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(Options=>{Options.Filters.Add();}); + // services.AddScoped(); + services.Configure(options =>{options.Filters.Add();}); + } +} +``` + +在BlogsController.cs中写: + +``` +using Microsoft.AspNetCore.Mvc; + +namespace mm.Api; + +[ApiController] +[Route("[controller]")] +public class BlogsController:ControllerBase +{ + // /blogs/:id + [Route("{id?}")] + public IActionResult Details(int id) + { + return Ok(id); + } + + [HttpPost] + public ActionResult Post(BlogDto blogDto) + { + return blogDto; + } + + [HttpPut("{id}")] + public ActionResult Put(int id, BlogDto blogDto) + { + return new{id,blogDto}; + } + + public IActionResult Delete(int id) + { + return Ok(new{Code=1000,Msg="请求成功aa",Data="135"}); + } +} +``` + +在ApiResult.cs中写: + +``` +namespace mm.Api; + +public class ApiResult +{ + public int Code{ get; set; } + public string? Msg{ get; set; } + public object? Data{ get; set; } +} +``` + +在BlogDto.cs中写: + +``` +namespace mm.Api; + +public class BlogDto +{ + public string Title { get; set;}=null!; + public string Author{ get; set;}=null!; + public string? Flag{ get; set;} +} +``` + +在ApiResultFilter.cs中写: + +``` +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace mm.Api.Filters; + +public class ApiResultFilter : IAsyncResultFilter +{ + public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + // 判断返回结果是否是内容,如果是,我们就给context.Result赋一个新的实例对象ApiResult + if (context.Result is ObjectResult objectResult) + { + context.Result = new ObjectResult(new ApiResult + { + Code = 1000, + Msg = "请求成功", + Data = objectResult.Value + }); + } + else + { + context.Result = new ObjectResult(new ApiResult { Code = 1001 }); + } + await next(); + } +} +``` \ No newline at end of file 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.md" new file mode 100644 index 0000000000000000000000000000000000000000..c2871dccd6c5d2bb4c1e75c0543722d8df2abde2 --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240530-8.\345\260\217\345\236\213\351\241\271\347\233\256.md" @@ -0,0 +1,192 @@ +## 小型项目 + +在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中写: + +``` +namespace BookStore.Api; + +public class Startup +{ + public void Configure(IApplicationBuilder app) + { + app.UseRouting(); + app.UseEndpoints(endpoints=> + { + endpoints.MapControllers(); + }); + } + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } +} +``` + +在AuthorsController.cs中写: + +``` +using BookStore.Api.Db; +using BookStore.Api.Domain; +using Microsoft.AspNetCore.Mvc; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/[controller]")] +public class AuthorsController : ControllerBase +{ + [HttpGet("{id?}")] + public IActionResult Get(int id) + { + var db=new BookStoreDb(); + db.Authors.Add(new Authors{AuthorName="思航航",Gender=1,Birth=Convert.ToDateTime("2000-8-8")}); + var list=db.Authors.ToList(); + return Ok(list); + } + [HttpPost("")] + public IActionResult Post() + { + return Ok(); + } + [HttpPut("{id?}")] + public IActionResult Put(int id) + { + return Ok(id); + } + [HttpDelete("{id?}")] + public IActionResult Delete(int id) + { + return Ok(id); + } +} +``` + +在BooksController.cs中写: + +``` +using Microsoft.AspNetCore.Mvc; + +namespace BookStore.Api.Controller; + +[ApiController] +[Route("api/authors/{authorId}/[controller]")] +public class BooksController : ControllerBase +{ + [HttpGet("{bookId?}")] + public IActionResult Get(int authorId,int bookId) + { + return Ok(new{authorId, bookId}); + } + [HttpPost("")] + public IActionResult Post(int authorId) + { + return Ok(authorId); + } + [HttpPut("{bookId?}")] + public IActionResult Put(int bookId) + { + return Ok(bookId); + } + [HttpDelete("{bookId?}")] + public IActionResult Delete(int bookId) + { + return Ok(bookId); + } +} +``` + +在Authors.cs中写: + +``` +namespace BookStore.Api.Domain; + +public class Authors +{ + public Guid Id{ get; set; } + public string? AuthorName { get; set;} + public int Gender { get; set;} + public DateTime Birth { get; set;} +} +``` + +在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;} +} +``` + +在BooksStore.cs中写: + +``` +using BookStore.Api.Domain; + +namespace BookStore.Api.Db; +public class BookStoreDb +{ + public static BookStoreDb Instance { get; set;}=new BookStoreDb(); + + public ICollectionAuthors { get; set;}=new List{}; + public ICollectionBooks{ get; set;}=new List{}; + +} +``` + +在BooksStore.Api.http中写: + +``` +@url = http://localhost:5000 + +### 获取作者列表、获取指定id的作者 +GET {{url}}/api/authors/92229 HTTP/1.1 + +### 新增作者 +POST {{url}}/api/authors HTTP/1.1 +Content-Type: application/json + +{ + +} + +### 修改 +PUT {{url}}/api/authors/33 HTTP/1.1 +Content-Type: application/json + +{ + +} + +### 删除 +DELETE {{url}}/api/authors/66 HTTP/1.1 + +### +GET {{url}}/api/authors/53/books/90 HTTP/1.1 +``` \ No newline at end of file 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.md" new file mode 100644 index 0000000000000000000000000000000000000000..3a6a08d782f7fb828c48b25dcf0a448cafc0d155 --- /dev/null +++ "b/\345\220\264\350\257\227\350\214\265/20240531-9.\345\260\217\345\236\213\351\241\271\347\233\256.md" @@ -0,0 +1,209 @@ +## 小型项目 + +在前面的基础上增加代码: + +在IAuthorRepository.cs里写: + +``` +using BookStore.Api.Domain; + +namespace BookStore.Api; + +public interface IAuthorRepository +{ + // 通过Id获取指定作者的方法 + Authors? GetAuthorById(Guid id); + + // 获取所有作者的方法 + // 函数三要素:函数名称、函数参数、函数返回值 + ICollectionGetAllAuthors(); +} +``` + +在AuthorRepository.cs里写: + +``` +using BookStore.Api.Db; +using BookStore.Api.Domain; + +namespace BookStore.Api.Services; + +public class AuthorRepository : IAuthorRepository +{ + public ICollection GetAllAuthors() + { + // 这个实现应该从持久化的数据源中获得:数据库、文件、各种异构数据、从各种api中获取的数据 + return BookStoreDb.Instance.Authors.ToList(); + } + + public Authors? GetAuthorById(Guid id) + { + return BookStoreDb.Instance.Authors.SingleOrDefault(item=>item.Id==id); + } +} +``` + +在AuthorsController.cs里写: + +``` +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() + { + return Ok(); + } + [HttpPut("{id?}")] + public IActionResult Put(int id) + { + return Ok(id); + } + [HttpDelete("{id?}")] + public IActionResult Delete(int id) + { + return Ok(id); + } +} +``` + +在BookStoreDb.cs里写: + +``` +using BookStore.Api.Domain; + +namespace BookStore.Api.Db; +public class BookStoreDb +{ + public static BookStoreDb Instance { get; set;}=new BookStoreDb(); + + 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, + }); + } +} +``` + +在Startup.cs里写: + +``` +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) + { + services.AddControllers(); + services.AddScoped(); + } +} +``` \ No newline at end of file