From 35b0b3547853ce75f6dfbbe381e2425e4586df29 Mon Sep 17 00:00:00 2001 From: ajax Date: Sun, 21 Jul 2024 13:37:59 +0800 Subject: [PATCH] Accepted some IDE suggestions --- ThuInfoWeb/Controllers/ApiController.cs | 44 +++++++++--------------- ThuInfoWeb/Controllers/HomeController.cs | 4 +-- ThuInfoWeb/Controllers/StatController.cs | 19 ++++------ 3 files changed, 25 insertions(+), 42 deletions(-) diff --git a/ThuInfoWeb/Controllers/ApiController.cs b/ThuInfoWeb/Controllers/ApiController.cs index c310609..7bfb8ee 100644 --- a/ThuInfoWeb/Controllers/ApiController.cs +++ b/ThuInfoWeb/Controllers/ApiController.cs @@ -10,19 +10,9 @@ namespace ThuInfoWeb.Controllers; /// [Route("[controller]")] [ApiController] -public class ApiController : ControllerBase +public class ApiController(Data data, VersionManager versionManager, FeedbackNoticeBot feedbackNoticeBot) + : ControllerBase { - private readonly Data _data; - private readonly FeedbackNoticeBot _feedbackNoticeBot; - private readonly VersionManager _versionManager; - - public ApiController(Data data, VersionManager versionManager, FeedbackNoticeBot feedbackNoticeBot) - { - _data = data; - _versionManager = versionManager; - _feedbackNoticeBot = feedbackNoticeBot; - } - /// /// Get announce, get the latest announce simply by no query string(just get /api/announce). If needed, you should only /// enter id or page at one time. @@ -33,16 +23,16 @@ public ApiController(Data data, VersionManager versionManager, FeedbackNoticeBot [Route("Announce")] public async Task Announce([FromQuery] int? id, [FromQuery] int? page) { - if (page is not null && page <= 0) + if (page <= 0) return BadRequest("page必须是正整数"); if (page is not null) { - var a = await _data.GetActiveAnnouncesAsync(page ?? 1, 5); + var a = await data.GetActiveAnnouncesAsync((int)page, 5); return Ok(a); } else { - var a = await _data.GetActiveAnnounceAsync(id); + var a = await data.GetActiveAnnounceAsync(id); return Ok(a); } } @@ -65,13 +55,13 @@ public async Task Feedback(FeedbackDto dto) Contact = dto.Contact, PhoneModel = dto.PhoneModel }; - var result = await _data.CreateFeedbackAsync(feedback); + var result = await data.CreateFeedbackAsync(feedback); if (result != 1) { return BadRequest(); } - _ = _feedbackNoticeBot.PushNoticeAsync( + _ = feedbackNoticeBot.PushNoticeAsync( $"收到新反馈\n{dto.Content}\n请前往http://app.cs.tsinghua.edu.cn/Home/Feedback回复"); return Created("Api/Feedback", null); } @@ -79,10 +69,10 @@ public async Task Feedback(FeedbackDto dto) [Route("RepliedFeedback")] public async Task RepliedFeedback() { - return Ok((await _data.GetAllRepliedFeedbacksAsync()) + return Ok((await data.GetAllRepliedFeedbacksAsync()) .Select(x => new { - content = x.Content, reply = x.Reply, replierName = x.ReplierName ?? "", repliedTime = x.RepliedTime + content = x.Content, reply = x.Reply, replierName = x.ReplierName, repliedTime = x.RepliedTime }).ToList()); } @@ -93,7 +83,7 @@ public async Task RepliedFeedback() [Route("QRCode")] public async Task QRCode() { - return Ok((await _data.GetMiscAsync())?.QrCodeContent ?? ""); + return Ok((await data.GetMiscAsync())?.QrCodeContent ?? ""); } /// @@ -104,7 +94,7 @@ public async Task QRCode() public async Task Apk() { // when start for the first time, if the apkurl is null or empty, this will generate an exception, so set an apkurl value as soon as possible. - return Redirect((await _data.GetMiscAsync())?.ApkUrl ?? ""); + return Redirect((await data.GetMiscAsync())?.ApkUrl ?? ""); } [Route("Socket")] @@ -113,7 +103,7 @@ public async Task Socket([FromQuery] int? sectionId) if (sectionId is null) return Ok(new List()); - return Ok((await _data.GetSocketsAsync(sectionId ?? 0)).Select(x => new SocketDto + return Ok((await data.GetSocketsAsync((int)sectionId)).Select(x => new SocketDto { CreatedTime = x.CreatedTime, SeatId = x.SeatId, @@ -137,7 +127,7 @@ static string Parse(Socket.SocketStatus status) [Route("Socket")] public async Task Socket(SocketDto dto) { - var result = await _data.UpdateSocketAsync(dto.SeatId ?? 0, dto.IsAvailable ?? false); + var result = await data.UpdateSocketAsync(dto.SeatId ?? 0, dto.IsAvailable ?? false); if (result != 1) return BadRequest(); return Ok(); @@ -146,14 +136,14 @@ public async Task Socket(SocketDto dto) [Route("Version/{os}")] public IActionResult Version([FromRoute] string os) { - if (os.ToLower() == "android") - return Ok(_versionManager.GetCurrentVersion(VersionManager.OS.Android)); - return Ok(_versionManager.GetCurrentVersion(VersionManager.OS.IOS)); + return Ok(os.Equals("android", StringComparison.CurrentCultureIgnoreCase) + ? versionManager.GetCurrentVersion(VersionManager.OS.Android) + : versionManager.GetCurrentVersion(VersionManager.OS.IOS)); } [Route("CardIVersion")] public async Task CardIVersion() { - return Ok(new { Version = (await _data.GetMiscAsync())?.CardIVersion ?? -1 }); + return Ok(new { Version = (await data.GetMiscAsync())?.CardIVersion ?? -1 }); } } diff --git a/ThuInfoWeb/Controllers/HomeController.cs b/ThuInfoWeb/Controllers/HomeController.cs index f4e4e55..937ee64 100644 --- a/ThuInfoWeb/Controllers/HomeController.cs +++ b/ThuInfoWeb/Controllers/HomeController.cs @@ -22,7 +22,7 @@ public IActionResult Register() } [HttpPost] - public async Task Register(RegisterViewModel vm) + public IActionResult Register(RegisterViewModel vm) { if (!ModelState.IsValid) return View(vm); @@ -281,7 +281,7 @@ public async Task Misc(MiscViewModel vm) public IActionResult CheckUpdate([FromRoute] string os) { if (!versionManager.IsRunning) - _ = versionManager.CheckUpdateAsync(os.ToLower() == "android" + _ = versionManager.CheckUpdateAsync(os.Equals("android", StringComparison.CurrentCultureIgnoreCase) ? VersionManager.OS.Android : VersionManager.OS.IOS); return RedirectToAction(nameof(Index)); diff --git a/ThuInfoWeb/Controllers/StatController.cs b/ThuInfoWeb/Controllers/StatController.cs index 24d0fb9..6862d2e 100644 --- a/ThuInfoWeb/Controllers/StatController.cs +++ b/ThuInfoWeb/Controllers/StatController.cs @@ -6,22 +6,15 @@ namespace ThuInfoWeb.Controllers; [Route("[controller]/[action]")] [ApiController] -public class StatController : ControllerBase +public class StatController(Data data) : ControllerBase { - private readonly Data _data; - - public StatController(Data data) - { - _data = data; - } - [Route("{function:int}")] public async Task Usage(int function) { if (!Enum.IsDefined(typeof(Usage.FunctionType), function)) return BadRequest("功能不存在"); var usage = new Usage { Function = (Usage.FunctionType)function, CreatedTime = DateTime.Now }; - var result = await _data.CreateUsageAsync(usage); + var result = await data.CreateUsageAsync(usage); if (result != 1) return BadRequest(); return Ok(); @@ -31,13 +24,13 @@ public async Task Usage(int function) [Authorize(Roles = "admin")] public async Task UsageData() { - return Ok(await _data.GetUsageAsync()); + return Ok(await data.GetUsageAsync()); } public async Task Startup() { var s = new Startup { CreatedTime = DateTime.Now }; - var result = await _data.CreateStartupAsync(s); + var result = await data.CreateStartupAsync(s); if (result != 1) return BadRequest(); return Ok(); @@ -47,12 +40,12 @@ public async Task Startup() [Authorize(Roles = "admin")] public async Task StartupData() { - return Ok(await _data.GetStartupDataAsync()); + return Ok(await data.GetStartupDataAsync()); } #if DEBUG public async Task GenStartupData() { - await _data.GenStartupDataAsync(); + await data.GenStartupDataAsync(); return Ok(); } #endif