mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
170 lines
4.9 KiB
C#
170 lines
4.9 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text.Json;
|
|
using RadicadeServer.DB;
|
|
using RadicadeServer.Extensions;
|
|
using RadicadeServer.RecNet.Responses;
|
|
|
|
namespace RadicadeServer.API.InfluencerPartnerProgram
|
|
{
|
|
[Route("api/influencerpartnerprogram")]
|
|
[Route("econ/api/influencerpartnerprogram")]
|
|
public class InfluencerPartnerProgramAPI : Controller
|
|
{
|
|
[HttpGet("influencers")]
|
|
[HttpGet("v1/influencers")]
|
|
public IActionResult GetInfluencers([FromQuery] int? take)
|
|
{
|
|
return Ok(new
|
|
{
|
|
ContinuationToken = (string?)null,
|
|
InfluencerIds = DBConfig.TestInfluencerAccountIds.ToArray(),
|
|
});
|
|
}
|
|
|
|
[HttpGet("myinfluencer")]
|
|
public IActionResult GetMyInfluencer()
|
|
{
|
|
var accountId = User.GetUserId() ?? 0;
|
|
return Ok(DBConfig.TestInfluencerAccountIds.Contains(accountId) ? accountId : 0L);
|
|
}
|
|
|
|
[HttpGet("influencer")]
|
|
public IActionResult GetInfluencer([FromQuery] long accountId) =>
|
|
DBConfig.TestInfluencerAccountIds.Contains(accountId)
|
|
? Ok(accountId)
|
|
: NotFound();
|
|
|
|
[HttpGet("isinfluencer")]
|
|
public IActionResult IsInfluencer([FromQuery] long accountId) =>
|
|
Ok(DBConfig.TestInfluencerAccountIds.Contains(accountId));
|
|
|
|
[HttpGet("code/{accountId:long}")]
|
|
[HttpGet("v1/code/{accountId:long}")]
|
|
public IActionResult GetInfluencerCode(long accountId)
|
|
{
|
|
if (!DBConfig.TestInfluencerAccountIds.Contains(accountId))
|
|
return NotFound();
|
|
return Ok(new { AccountId = accountId, Code = "COACH" });
|
|
}
|
|
|
|
[HttpPost("redeem")]
|
|
[HttpPost("v1/redeem")]
|
|
[HttpPost("useCode")]
|
|
public async Task<IActionResult> RedeemCode()
|
|
{
|
|
var fields = await ReadRequestFieldsAsync();
|
|
var request = new InfluencerCodeRequest
|
|
{
|
|
Code = FirstField(fields, "Code", "code", "InfluencerCode", "influencerCode")
|
|
};
|
|
|
|
if (!string.Equals(request.Code?.Trim(), "COACH", StringComparison.OrdinalIgnoreCase))
|
|
return BadRequest(new { Success = false, Message = "Invalid code." });
|
|
return Ok(new { Success = true, Code = "COACH", InfluencerAccountId = 1L });
|
|
}
|
|
|
|
[HttpGet("validate")]
|
|
public IActionResult ValidateCode([FromQuery] string? code) =>
|
|
string.Equals(code?.Trim(), "COACH", StringComparison.OrdinalIgnoreCase)
|
|
? Ok(new { Success = true, Code = "COACH", InfluencerAccountId = 1L })
|
|
: BadRequest(new { Success = false, Message = "Invalid code." });
|
|
|
|
[HttpPost("support")]
|
|
[HttpPost("v1/support")]
|
|
public IActionResult SupportCoach() => Ok(new
|
|
{
|
|
Success = true,
|
|
Supported = true,
|
|
InfluencerAccountId = 1L,
|
|
Code = "COACH"
|
|
});
|
|
|
|
[HttpPost("remove")]
|
|
public async Task<IActionResult> RemoveInfluencer()
|
|
{
|
|
var fields = await ReadRequestFieldsAsync();
|
|
var rawAccountId = FirstField(fields, "influencerAccountId", "InfluencerAccountId", "accountId", "AccountId");
|
|
if (!int.TryParse(rawAccountId, out var influencerAccountId))
|
|
return BadRequest(Success.Error("Missing request body."));
|
|
|
|
var request = new InfluencerRemoveRequest { influencerAccountId = influencerAccountId };
|
|
return Ok(Success.Ok);
|
|
}
|
|
|
|
private async Task<Dictionary<string, string>> ReadRequestFieldsAsync()
|
|
{
|
|
var fields = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
foreach (var kv in Request.Query)
|
|
fields[kv.Key] = kv.Value.ToString();
|
|
|
|
if (Request.HasFormContentType)
|
|
{
|
|
var form = await Request.ReadFormAsync();
|
|
foreach (var kv in form)
|
|
fields[kv.Key] = kv.Value.ToString();
|
|
return fields;
|
|
}
|
|
|
|
if (Request.ContentLength is not > 0) return fields;
|
|
|
|
try
|
|
{
|
|
Request.EnableBuffering();
|
|
Request.Body.Position = 0;
|
|
string body;
|
|
using (var reader = new StreamReader(Request.Body, leaveOpen: true))
|
|
body = await reader.ReadToEndAsync();
|
|
Request.Body.Position = 0;
|
|
|
|
var trimmed = body.TrimStart();
|
|
if (trimmed.StartsWith("{", StringComparison.Ordinal))
|
|
{
|
|
using var doc = JsonDocument.Parse(body);
|
|
if (doc.RootElement.ValueKind == JsonValueKind.Object)
|
|
foreach (var prop in doc.RootElement.EnumerateObject())
|
|
fields[prop.Name] = prop.Value.ValueKind == JsonValueKind.String
|
|
? prop.Value.GetString() ?? ""
|
|
: prop.Value.ToString();
|
|
}
|
|
else if (trimmed.Contains('='))
|
|
{
|
|
foreach (var pair in body.Split('&', StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
var eq = pair.IndexOf('=');
|
|
if (eq < 0)
|
|
{
|
|
fields[Uri.UnescapeDataString(pair)] = "";
|
|
continue;
|
|
}
|
|
|
|
fields[Uri.UnescapeDataString(pair.Substring(0, eq))] = Uri.UnescapeDataString(pair.Substring(eq + 1));
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
return fields;
|
|
}
|
|
|
|
private static string? FirstField(Dictionary<string, string> fields, params string[] names)
|
|
{
|
|
foreach (var name in names)
|
|
if (fields.TryGetValue(name, out var value) && !string.IsNullOrWhiteSpace(value))
|
|
return value;
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public sealed class InfluencerCodeRequest
|
|
{
|
|
public string? Code { get; set; }
|
|
}
|
|
|
|
public sealed class InfluencerRemoveRequest
|
|
{
|
|
public int influencerAccountId { get; set; }
|
|
}
|
|
}
|