Skip to content

Commit

Permalink
Accepted some IDE suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
84634E1A607A committed Jul 21, 2024
1 parent af60fd3 commit 35b0b35
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
44 changes: 17 additions & 27 deletions ThuInfoWeb/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@ namespace ThuInfoWeb.Controllers;
/// </summary>
[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;
}

/// <summary>
/// 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.
Expand All @@ -33,16 +23,16 @@ public ApiController(Data data, VersionManager versionManager, FeedbackNoticeBot
[Route("Announce")]
public async Task<IActionResult> 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);
}
}
Expand All @@ -65,24 +55,24 @@ public async Task<IActionResult> 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);
}

[Route("RepliedFeedback")]
public async Task<IActionResult> 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());
}

Expand All @@ -93,7 +83,7 @@ public async Task<IActionResult> RepliedFeedback()
[Route("QRCode")]
public async Task<IActionResult> QRCode()
{
return Ok((await _data.GetMiscAsync())?.QrCodeContent ?? "");
return Ok((await data.GetMiscAsync())?.QrCodeContent ?? "");
}

/// <summary>
Expand All @@ -104,7 +94,7 @@ public async Task<IActionResult> QRCode()
public async Task<IActionResult> 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")]
Expand All @@ -113,7 +103,7 @@ public async Task<IActionResult> Socket([FromQuery] int? sectionId)
if (sectionId is null)
return Ok(new List<SocketDto>());

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,
Expand All @@ -137,7 +127,7 @@ static string Parse(Socket.SocketStatus status)
[Route("Socket")]
public async Task<IActionResult> 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();
Expand All @@ -146,14 +136,14 @@ public async Task<IActionResult> 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<IActionResult> CardIVersion()
{
return Ok(new { Version = (await _data.GetMiscAsync())?.CardIVersion ?? -1 });
return Ok(new { Version = (await data.GetMiscAsync())?.CardIVersion ?? -1 });
}
}
4 changes: 2 additions & 2 deletions ThuInfoWeb/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IActionResult Register()
}

[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel vm)
public IActionResult Register(RegisterViewModel vm)
{
if (!ModelState.IsValid)
return View(vm);
Expand Down Expand Up @@ -281,7 +281,7 @@ public async Task<IActionResult> 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));
Expand Down
19 changes: 6 additions & 13 deletions ThuInfoWeb/Controllers/StatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActionResult> 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();
Expand All @@ -31,13 +24,13 @@ public async Task<IActionResult> Usage(int function)
[Authorize(Roles = "admin")]
public async Task<IActionResult> UsageData()
{
return Ok(await _data.GetUsageAsync());
return Ok(await data.GetUsageAsync());
}

public async Task<IActionResult> 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();
Expand All @@ -47,12 +40,12 @@ public async Task<IActionResult> Startup()
[Authorize(Roles = "admin")]
public async Task<IActionResult> StartupData()
{
return Ok(await _data.GetStartupDataAsync());
return Ok(await data.GetStartupDataAsync());
}
#if DEBUG
public async Task<IActionResult> GenStartupData()
{
await _data.GenStartupDataAsync();
await data.GenStartupDataAsync();
return Ok();
}
#endif
Expand Down

0 comments on commit 35b0b35

Please sign in to comment.