feat: sid redirect controller
This commit is contained in:
		
							parent
							
								
									7cb17409cd
								
							
						
					
					
						commit
						c8cd483d20
					
				
					 1 changed files with 50 additions and 1 deletions
				
			
		|  | @ -1,3 +1,52 @@ | |||
| using System.Diagnostics.CodeAnalysis; | ||||
| using Foxnouns.Backend.Database; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| 
 | ||||
| namespace Foxnouns.Backend.Controllers; | ||||
| 
 | ||||
| public class SidController { } | ||||
| [Route("/sid")] | ||||
| [SuppressMessage( | ||||
|     "Performance", | ||||
|     "CA1862:Use the \'StringComparison\' method overloads to perform case-insensitive string comparisons", | ||||
|     Justification = "Not usable with EFCore" | ||||
| )] | ||||
| public class SidController(Config config, DatabaseContext db) : ApiControllerBase | ||||
| { | ||||
|     [HttpGet("{**id}")] | ||||
|     public async Task<IActionResult> ResolveSidAsync(string id, CancellationToken ct = default) => | ||||
|         id.Length switch | ||||
|         { | ||||
|             5 => await ResolveUserSidAsync(id, ct), | ||||
|             6 => await ResolveMemberSidAsync(id, ct), | ||||
|             _ => Redirect(config.BaseUrl), | ||||
|         }; | ||||
| 
 | ||||
|     private async Task<IActionResult> ResolveUserSidAsync(string id, CancellationToken ct = default) | ||||
|     { | ||||
|         var username = await db | ||||
|             .Users.Where(u => u.Sid == id.ToLowerInvariant() && !u.Deleted) | ||||
|             .Select(u => u.Username) | ||||
|             .FirstOrDefaultAsync(ct); | ||||
|         if (username == null) | ||||
|             return Redirect(config.BaseUrl); | ||||
| 
 | ||||
|         return Redirect($"{config.BaseUrl}/@{username}"); | ||||
|     } | ||||
| 
 | ||||
|     private async Task<IActionResult> ResolveMemberSidAsync( | ||||
|         string id, | ||||
|         CancellationToken ct = default | ||||
|     ) | ||||
|     { | ||||
|         var member = await db | ||||
|             .Members.Include(m => m.User) | ||||
|             .Where(m => m.Sid == id.ToLowerInvariant() && !m.User.Deleted) | ||||
|             .Select(m => new { m.Name, m.User.Username }) | ||||
|             .FirstOrDefaultAsync(ct); | ||||
|         if (member == null) | ||||
|             return Redirect(config.BaseUrl); | ||||
| 
 | ||||
|         return Redirect($"{config.BaseUrl}/@{member.Username}/{member.Name}"); | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue