api for sdk
This commit is contained in:
parent
eacf80ed5c
commit
f753a0ffc4
|
@ -37,7 +37,7 @@ namespace AscNet.Common
|
||||||
[Option(DefaultValue = (ushort)27017)]
|
[Option(DefaultValue = (ushort)27017)]
|
||||||
ushort Port { get; set; }
|
ushort Port { get; set; }
|
||||||
|
|
||||||
[Option(DefaultValue = "sf")]
|
[Option(DefaultValue = "asc_net")]
|
||||||
string Name { get; set; }
|
string Name { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,40 +14,33 @@ namespace AscNet.Common.Database
|
||||||
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid);
|
return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Account? FromAccessToken(string token)
|
public static Account? FromToken(string token)
|
||||||
{
|
{
|
||||||
return collection.AsQueryable().FirstOrDefault(x => x.AccessToken == token);
|
return collection.AsQueryable().FirstOrDefault(x => x.Token == token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Account? FromPhone(string phone)
|
public static Account? FromUsername(string username)
|
||||||
{
|
{
|
||||||
return collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone);
|
return collection.AsQueryable().FirstOrDefault(x => x.Username == username);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Account? FromPhone(string phone, string password)
|
public static Account? FromUsername(string username, string password)
|
||||||
{
|
{
|
||||||
return collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone && x.Password == password);
|
return collection.AsQueryable().FirstOrDefault(x => x.Username == username && x.Password == password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <exception cref="ArgumentException"></exception>
|
/// <exception cref="ArgumentException"></exception>
|
||||||
public static Account Create(string phone, string password)
|
public static Account Create(string username, string password)
|
||||||
{
|
{
|
||||||
if (collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone) is not null)
|
if (collection.AsQueryable().FirstOrDefault(x => x.Username == username) is not null)
|
||||||
throw new ArgumentException("Phone is already registered!", "phone");
|
throw new ArgumentException("Username is already registered!", "username");
|
||||||
|
|
||||||
Account account = new()
|
Account account = new()
|
||||||
{
|
{
|
||||||
Uid = (collection.AsQueryable().OrderByDescending(x => x.Uid).FirstOrDefault()?.Uid ?? 0) + 1,
|
Uid = (collection.AsQueryable().OrderByDescending(x => x.Uid).FirstOrDefault()?.Uid ?? 0) + 1,
|
||||||
PhoneNum = phone,
|
Username = username,
|
||||||
Email = "",
|
|
||||||
Password = password,
|
Password = password,
|
||||||
AccessToken = Guid.NewGuid().ToString(),
|
Token = Guid.NewGuid().ToString()
|
||||||
Age = 0,
|
|
||||||
IsActivation = false,
|
|
||||||
IsAdult = true,
|
|
||||||
IsGuest = false,
|
|
||||||
UnfreezeTime = 0,
|
|
||||||
IsReal = true
|
|
||||||
};
|
};
|
||||||
|
|
||||||
collection.InsertOne(account);
|
collection.InsertOne(account);
|
||||||
|
@ -61,44 +54,16 @@ namespace AscNet.Common.Database
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public long Uid { get; set; }
|
public long Uid { get; set; }
|
||||||
|
|
||||||
[BsonElement("phone_num")]
|
[BsonElement("username")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string PhoneNum { get; set; }
|
public string Username { get; set; }
|
||||||
|
|
||||||
[BsonElement("email")]
|
|
||||||
[BsonRequired]
|
|
||||||
public string Email { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("password")]
|
[BsonElement("password")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
|
||||||
[BsonElement("access_token")]
|
[BsonElement("token")]
|
||||||
[BsonRequired]
|
[BsonRequired]
|
||||||
public string AccessToken { get; set; }
|
public string Token { get; set; }
|
||||||
|
|
||||||
[BsonElement("age")]
|
|
||||||
[BsonRequired]
|
|
||||||
public int Age { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("is_activation")]
|
|
||||||
[BsonRequired]
|
|
||||||
public bool IsActivation { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("is_adult")]
|
|
||||||
[BsonRequired]
|
|
||||||
public bool IsAdult { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("is_guest")]
|
|
||||||
[BsonRequired]
|
|
||||||
public bool IsGuest { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("unfreeze_time")]
|
|
||||||
[BsonRequired]
|
|
||||||
public int UnfreezeTime { get; set; }
|
|
||||||
|
|
||||||
[BsonElement("is_real")]
|
|
||||||
[BsonRequired]
|
|
||||||
public bool IsReal { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,80 @@
|
||||||
namespace AscNet.SDKServer.Controllers
|
using System.Text;
|
||||||
|
using AscNet.Common.Database;
|
||||||
|
using AscNet.SDKServer.Models;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace AscNet.SDKServer.Controllers
|
||||||
{
|
{
|
||||||
public class AccountController : IRegisterable
|
public class AccountController : IRegisterable
|
||||||
{
|
{
|
||||||
public static void Register(WebApplication app)
|
public static void Register(WebApplication app)
|
||||||
{
|
{
|
||||||
|
app.MapPost("/api/AscNet/register", (HttpContext ctx) =>
|
||||||
|
{
|
||||||
|
AuthRequest? req = JsonConvert.DeserializeObject<AuthRequest>(Encoding.UTF8.GetString(ctx.Request.BodyReader.ReadAsync().Result.Buffer));
|
||||||
|
|
||||||
|
if (req is null)
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
|
code = -1,
|
||||||
|
msg = "Invalid request"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Account account = Account.Create(req.Username, req.Password);
|
||||||
|
|
||||||
|
return JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
|
code = 0,
|
||||||
|
msg = "OK",
|
||||||
|
account
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
|
code = -1,
|
||||||
|
msg = ex.Message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.MapPost("/api/AscNet/login", (HttpContext ctx) =>
|
||||||
|
{
|
||||||
|
AuthRequest? req = JsonConvert.DeserializeObject<AuthRequest>(Encoding.UTF8.GetString(ctx.Request.BodyReader.ReadAsync().Result.Buffer));
|
||||||
|
|
||||||
|
if (req is null)
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
|
code = -1,
|
||||||
|
msg = "Invalid request"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Account? account = Account.FromUsername(req.Username, req.Password);
|
||||||
|
|
||||||
|
if (account == null)
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
|
code = -1,
|
||||||
|
msg = "Invalid credentials!"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonConvert.SerializeObject(new
|
||||||
|
{
|
||||||
|
code = 0,
|
||||||
|
msg = "OK",
|
||||||
|
account
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using AscNet.Common.Util;
|
using AscNet.Common.Util;
|
||||||
using AscNet.SDKServer.Models;
|
using AscNet.SDKServer.Models;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||||
|
namespace AscNet.SDKServer.Models
|
||||||
|
{
|
||||||
|
public class AuthRequest
|
||||||
|
{
|
||||||
|
[JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Username { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using AscNet.Common.Util;
|
using System.Runtime.InteropServices;
|
||||||
|
using AscNet.Common.Util;
|
||||||
|
|
||||||
namespace AscNet
|
namespace AscNet
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue