From 5debbbafbcf970567437e9c1cd19fc3d87c08113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=AD=9D=E5=9D=9A?= <2091501917@qq.com> Date: Tue, 18 Jun 2024 03:38:13 +0000 Subject: [PATCH 1/2] 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 唐孝坚 <2091501917@qq.com> --- .../pass/App.vue" | 75 +++++++++++++++++++ .../pass/BlogApi.http" | 39 ++++++++++ .../pass/Controllers/BlogController.cs" | 56 ++++++++++++++ .../pass/Db/BlogDbContext.cs" | 8 ++ .../pass/Interfaces/IBlogsRepository.cs" | 11 +++ .../pass/Models/Blogs.cs" | 10 +++ .../pass/Program.cs" | 23 ++++++ .../pass/Repository/BlogsRepository.cs" | 34 +++++++++ .../pass/appsettings.json" | 12 +++ 9 files changed, 268 insertions(+) create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/App.vue" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/Program.cs" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" create mode 100644 "\345\224\220\345\255\235\345\235\232/pass/appsettings.json" diff --git "a/\345\224\220\345\255\235\345\235\232/pass/App.vue" "b/\345\224\220\345\255\235\345\235\232/pass/App.vue" new file mode 100644 index 0000000..c0f988c --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/App.vue" @@ -0,0 +1,75 @@ + + + \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" "b/\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" new file mode 100644 index 0000000..9db20a7 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/BlogApi.http" @@ -0,0 +1,39 @@ +@BlogApi_HostAddress = http://localhost:3000 + +###根据id获取书籍信息l +GET {{BlogApi_HostAddress}}/api/blog/10 + + +###获取全部书籍信息 +GET {{BlogApi_HostAddress}}/api/blog + + +###添加书籍 +POST {{BlogApi_HostAddress}}/api/blog +Content-Type: application/json + +{ + "title": "西游", + "author": "吴承恩", + "tag":"小说" +} + + + +###更新书籍 +PUT {{BlogApi_HostAddress}}/api/blog/13 +Content-Type: application/json + +{ + "id": 13, + "title": "红楼", + "author": "吴承恩", + "tag":"小说" +} + + +###删除书籍 +DELETE {{BlogApi_HostAddress}}/api/blog/9 + + +### diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" new file mode 100644 index 0000000..a59c30a --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Controllers/BlogController.cs" @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Mvc; +using BlogApi.Interfaces; +using BlogApi.Models; +[Route("api/[controller]")] +[ApiController] +public class BlogController:ControllerBase{ + private readonly IBlogsRepository _blogRepository; + public BlogController(IBlogsRepository blogRepository){ + _blogRepository = blogRepository; + } + [HttpGet] + public IActionResult Get(){ + var blogs = _blogRepository.GetList(); + return Ok(blogs); + } + [HttpGet("{id}")] + public IActionResult GetById(int id){ + var blog = _blogRepository.GetById(id); + if(blog==null){ + return NotFound(); + } + return Ok(blog); + } + [HttpPost] + public IActionResult Post(Blog blog){ + _blogRepository.Add(blog); + return CreatedAtAction(nameof(GetById), new { id = blog.Id }, blog); + } + [HttpPut("{id}")] + public IActionResult Put(int id,Blog blog){ + if(id!=blog.Id){ + return BadRequest(); + } + _blogRepository.UpDate(blog); + return NoContent(); + } + [HttpDelete("{id}")] + public IActionResult Delete(int id){ + var blog = _blogRepository.GetById(id); + if(blog==null){ + return NotFound(); + } + _blogRepository.Delete(blog); + return NoContent(); + } + [HttpGet("search")] + public IActionResult Search(string keyword) + { + var blogs = _blogRepository.GetList().Where(b => b.Title.Contains(keyword) || b.Author.Contains(keyword) || b.Tag.Contains(keyword)); + if (blogs.Count() == 0) + { + return NotFound(); + } + return Ok(blogs); + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" new file mode 100644 index 0000000..46368c0 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Db/BlogDbContext.cs" @@ -0,0 +1,8 @@ +using Microsoft.EntityFrameworkCore; +using BlogApi.Models; +namespace BlogApi.Db{ + public class BlogDbContext:DbContext{ + public BlogDbContext(DbContextOptionsoptions):base(options){} + public DbSetBlogs{ get; set; } + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" new file mode 100644 index 0000000..093684d --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Interfaces/IBlogsRepository.cs" @@ -0,0 +1,11 @@ +namespace BlogApi.Interfaces +{ + public interface IBlogsRepository + { + T GetById(int id); + IEnumerable GetList(); + void Add(T entity); + void UpDate(T entity); + void Delete(T entity); + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" new file mode 100644 index 0000000..5dbc31a --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Models/Blogs.cs" @@ -0,0 +1,10 @@ +namespace BlogApi.Models +{ + public class Blog + { + public int Id { get; set; } + public string Title { get; set; } = ""; + public string Author { get; set; } = ""; + public string Tag { get; set; }=""; + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Program.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Program.cs" new file mode 100644 index 0000000..70679e7 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Program.cs" @@ -0,0 +1,23 @@ +using BlogApi.Db; +using BlogApi.Models; +using BlogApi.Repository; +using BlogApi.Interfaces; +using Microsoft.EntityFrameworkCore; +//创建web程序构建器 +var builder = WebApplication.CreateBuilder(args); +//添加控制器服务 +builder.Services.AddControllers(); +//获取连接sqlserver服务器的字符串 +string sqlserverstring = builder.Configuration.GetConnectionString("sqlserver"); +//注册数据库上下文 +builder.Services.AddDbContext(options => options.UseSqlServer(sqlserverstring)); +//注册仓储接口 +builder.Services.AddScoped, BlogsRepository>(); +//构建应用程序 +var app = builder.Build(); +//配置跨域策略 +app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); +// 使用控制器服务 +app.MapControllers(); +//运行 +app.Run(); \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" "b/\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" new file mode 100644 index 0000000..cecb91c --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/Repository/BlogsRepository.cs" @@ -0,0 +1,34 @@ +using BlogApi.Db; +using BlogApi.Interfaces; +using BlogApi.Models; +using Microsoft.EntityFrameworkCore; +namespace BlogApi.Repository +{ + public class BlogsRepository: IBlogsRepository{ + private readonly BlogDbContext _context; + public BlogsRepository(BlogDbContext context){ + _context = context; + } + public void Add(Blog entity){ + _context.Blogs.Add(entity); + _context.SaveChanges(); + } + public void Delete(Blog entity){ + _context.Blogs.Remove(entity); + _context.SaveChanges(); + } + + public Blog GetById(int id) + { + return _context.Blogs.Find(id) ?? throw new Exception("Blog not found"); + } + + public IEnumerable GetList(){ + return _context.Blogs.ToList(); + } + public void UpDate(Blog entity){ + _context.Entry(entity).State = EntityState.Modified; + _context.SaveChanges(); + } + } +} \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/pass/appsettings.json" "b/\345\224\220\345\255\235\345\235\232/pass/appsettings.json" new file mode 100644 index 0000000..95280ba --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/pass/appsettings.json" @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ConnectionStrings": { + "sqlserver":"server=.;Database=Blog;user=sa;password=123456;trustservercertificate=true" + }, + "AllowedHosts": "*" +} -- Gitee From 962f0b105e7976c14c8d8bd7c83b18df40535132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E5=AD=9D=E5=9D=9A?= <2091501917@qq.com> Date: Sun, 23 Jun 2024 13:54:37 +0000 Subject: [PATCH 2/2] 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 唐孝坚 <2091501917@qq.com> --- .../20240617_\350\267\250\345\237\237.md" | 20 ++++++++++++++ ...32\345\256\242\347\273\203\344\271\240.md" | 26 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 "\345\224\220\345\255\235\345\235\232/20240617_\350\267\250\345\237\237.md" create mode 100644 "\345\224\220\345\255\235\345\235\232/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" diff --git "a/\345\224\220\345\255\235\345\235\232/20240617_\350\267\250\345\237\237.md" "b/\345\224\220\345\255\235\345\235\232/20240617_\350\267\250\345\237\237.md" new file mode 100644 index 0000000..ffcdc36 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/20240617_\350\267\250\345\237\237.md" @@ -0,0 +1,20 @@ +## 在ASP.NET Core应用程序中配置CORS(跨域资源共享)策略 +~~~c# +public void ConfigureServices(IServiceCollection services) +{ + // 添加跨域服务 + services.AddCors(options => { + // "AllowOrigin"是策略的名称,你可以根据需要命名它 + options.AddPolicy("AllowOrigin",builder => { + builder.AllowAnyOrigin() //指定允许来自任何来源的请求,WithOrigins制定对某些域名开放 + .AllowAnyMethod() //指定允许任何HTTP方法 + .AllowAnyHeader(); //指定允许任何请求头 + }); + }); +} +public void Configure(IApplicationBuilder app,IHostEnvironment env) +{ + // 使用 "AllowOrigin" 策略,要在启用路由中间件后使用 + app.UseCors("AllowOrigin"); +} +~~~ \ No newline at end of file diff --git "a/\345\224\220\345\255\235\345\235\232/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" "b/\345\224\220\345\255\235\345\235\232/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" new file mode 100644 index 0000000..e76a0b0 --- /dev/null +++ "b/\345\224\220\345\255\235\345\235\232/20240619_\345\215\232\345\256\242\347\273\203\344\271\240.md" @@ -0,0 +1,26 @@ +## 博客表考前梳理 +### 后端webapi +1. 配置入口文件Program.cs +2. 配置启动类,注册依赖Startup.cs +3. 创建封装表格Domain/Blog.cs +4. 创建表格数据实体类Dto:BlogCreateDto、BlogDto、BlogUpdateDto +5. 在配置文件中编写数据库连接字符串 +~~~js + "ConnectionStrings": { + "SqlServer": "server=.;database=BlogDemo;uid=sa;pwd=123456;TrustServerCertificate=true;" + } +~~~ +6. 创建数据库上下文,连接数据库Db/BlogsDbContext +7. 创建通用仓储接口Interfaces/IRepositoryBase.cs +8. 实现接口Services/RepositoryBase.cs +9. 编写控制器 +10. 生成迁移和同步数据库 +~~~js +// 全局安装dotnet-ef工具 +dotnet tool install --global dotnet-ef +// 生成迁移 +dotnet ef migrations add +// 同步数据库 +dotnet ef batadase update +~~~ + -- Gitee