Skip to content

Commit

Permalink
Improvement: Add IResultAction controller action to SearchStudent, (n…
Browse files Browse the repository at this point in the history
…o non-ASCII matching rn), uwuify response for invalid enrollment number to annoy @LakshayGMZ
  • Loading branch information
martian0x80 committed May 20, 2024
1 parent faeeb62 commit 59723ba
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions IPUSenpaiBackend/Controllers/IPUSenpaiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using IPUSenpaiBackend.IPUSenpai;
using IPUSenpaiBackend.CustomEntities;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -254,7 +255,7 @@ public async Task<List<Response>> GetBatches(string programme, string institute)
["BACHELOR OF COMPUTER APPLICATIONS"] = 3,
["BACHELOR OF EDUCATION"] = 4,
["BACHELOR OF TECHNOLOGY"] = 4,
["I HAVE NO IDEA?"] = -1,
["I HAVE NO IDEA?"] = 0,
["INTEGRATED"] = 5,
["MASTER OF BUSINESS ADMINISTRATION"] = 2,
["MASTER OF COMPUTER APPLICATIONS"] = 2,
Expand Down Expand Up @@ -496,7 +497,11 @@ public IActionResult GetStudent(string enrollment)
var student = _api.GetStudent(enrollment);
if (student == null)
{
return NotFound(new { message = "Student ki jankaari nahi mili, bhai" });
return NotFound(new
{
message =
"S-Student ki jankaawi ^w^ nyahi miwi, OwO bhai.\nInvawid *runs away* ow (・`ω\u00b4・) nyon-existent *sweats* e-enwowwment nyumbew."
});
}

if (_enableCache)
Expand All @@ -514,25 +519,29 @@ public IActionResult GetStudent(string enrollment)
[HttpGet]
//[Route("student/search/name={Name}&institute={Institute}&prog={Programme}&batch={Batch}")]
[Route("student/search/{name}")]
public async Task<List<StudentSearchSenpai>> SearchStudent(string name = "", string? institute = "",
public async Task<IActionResult> SearchStudent(string name = "", string? institute = "",
string? programme = "", string? batch = "")
{
if (name.Length < 3)
{
return new List<StudentSearchSenpai>();
return BadRequest(new { message = "Name must be at least 3 characters long" });
}

// if (!Regex.IsMatch(name, @"[a-zA-Z\s]"))
// {
// return BadRequest(new { message = "Name must contain only ASCII characters" });
// }

if (_enableCache)
{
var cachedSearch = await _cache.GetStringAsync($"SearchStudent_{name}_{institute}_{programme}_{batch}");
if (!string.IsNullOrEmpty(cachedSearch))
{
try
{
return JsonSerializer.Deserialize<List<StudentSearchSenpai>>(cachedSearch) ??
new List<StudentSearchSenpai>(new[] { new StudentSearchSenpai() });
return Ok(JsonSerializer.Deserialize<List<StudentSearchSenpai>>(cachedSearch));
}
catch (JsonException e)
catch (Exception e)
{
_logger.LogError(e, "Error deserializing cached student search");
}
Expand All @@ -552,7 +561,7 @@ await _cache.SetStringAsync($"SearchStudent_{name}_{institute}_{programme}_{batc
JsonSerializer.Serialize(search), CacheOptions);
}

return search;
return Ok(search);
}

[HttpGet]
Expand Down

0 comments on commit 59723ba

Please sign in to comment.