From 578384c0069d5750082ed8199f78713bcd78b496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=82=9C=E7=BF=94?= <2307953404@qq.com> Date: Mon, 19 Aug 2024 16:37:40 +0800 Subject: [PATCH] 12 --- asg_form/Controllers/news.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/asg_form/Controllers/news.cs b/asg_form/Controllers/news.cs index e6a0a3d..02d5309 100644 --- a/asg_form/Controllers/news.cs +++ b/asg_form/Controllers/news.cs @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc; using System.Net.Mail; using System.Net; using System.Security.Claims; +using Microsoft.EntityFrameworkCore; +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace asg_form.Controllers { @@ -55,12 +57,20 @@ namespace asg_form.Controllers /// [Route("api/v1/news/")] [HttpGet] - public async Task>> getnews() + public async Task>> getnews([FromQuery] string type = null) { TestDbContext test =new TestDbContext(); - return test.news.OrderByDescending(a => a.Id).ToList(); + + var query = test.news.AsQueryable(); + + if (!string.IsNullOrEmpty(type)) + { + query = query.Where(n => n.Type == type); + } + + return query.OrderByDescending(a => a.Id).ToList(); } @@ -140,7 +150,7 @@ namespace asg_form.Controllers { using (TestDbContext ctx = new TestDbContext()) { - var qwq= await ctx.news.Where(n => n.Id == newsid && n.Type == req_News.Type).FindAsync(newsid); + var qwq= await ctx.news.FindAsync(newsid); if (qwq == null) { return NotFound("News item not found or type mismatch.");