Implement Basic Currency System
This commit is contained in:
parent
eb0b957020
commit
818ba48924
@ -152,5 +152,22 @@ namespace qtc_api.Controllers
|
||||
var result = await _userService.DeleteUser(id);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPost("update-user-currency")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<ServiceResponse<int>>> UpdateUserCurrency(int amount, bool isSpinClaim)
|
||||
{
|
||||
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
||||
|
||||
if (identity != null)
|
||||
{
|
||||
IEnumerable<Claim> claims = identity.Claims;
|
||||
var id = claims.First().Value;
|
||||
|
||||
if (id != null) return Ok(await _userService.AddCurrencyToUser(id, amount, isSpinClaim));
|
||||
else return Ok(new ServiceResponse<int> { Success = false, Message = "Identity Has No User ID" });
|
||||
}
|
||||
else return Ok(new ServiceResponse<int> { Success = false, Message = "No Identity" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,5 +10,6 @@
|
||||
public DateTime DateOfBirth { get; set; } = new DateTime();
|
||||
public DateTime CreatedAt { get; set; } = new DateTime();
|
||||
public int Status { get; set; } = 0;
|
||||
public int CurrencyAmount { get; set; } = 0;
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddDbContext<DataContext>();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSignalR();
|
||||
|
||||
@ -34,9 +35,10 @@ builder.Services.AddAuthentication().AddJwtBearer(options =>
|
||||
ValidAudience = builder.Configuration["Jwt:Audience"]
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("JWT_KEY")))
|
||||
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("JWT_KEY")!));
|
||||
else options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!));
|
||||
var jwtKey = Environment.GetEnvironmentVariable("JWT_KEY");
|
||||
if (jwtKey != null)
|
||||
options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey));
|
||||
else throw new Exception("Cannot Find Environment Variables 'JWT_KEY'. Please Check Environment.");
|
||||
});
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
builder.Services.AddScoped<ITokenService, TokenService>();
|
||||
|
@ -2,10 +2,11 @@
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||
"DB_PROVIDER": "SQLite",
|
||||
"DB_CONNECTION_STRING": "Data Source=qtcdev.db",
|
||||
"JWT_KEY": "iPBR1ZO9GHsP04SUf*jE4ILh5jwz$3$A"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5268"
|
||||
|
@ -15,5 +15,7 @@ namespace qtc_api.Services.UserService
|
||||
public Task<ServiceResponse<FileContentResult>> GetUserPic(string userId);
|
||||
public Task<ServiceResponse<UserStatusDto>> UpdateStatus(UserStatusDto request);
|
||||
public Task<ServiceResponse<User>> DeleteUser(string id);
|
||||
public Task<ServiceResponse<int>> AddCurrencyToUser(string id, int amount, bool isSpinClaim);
|
||||
public Task<ServiceResponse<int>> RemoveCurrencyFromUser(string id, int amount);
|
||||
}
|
||||
}
|
||||
|
@ -106,6 +106,7 @@
|
||||
x.Role = user.Role;
|
||||
x.Bio = user.Bio;
|
||||
x.DateOfBirth = user.DateOfBirth;
|
||||
x.CurrencyAmount = user.CurrencyAmount;
|
||||
|
||||
userInfoList.Add(x);
|
||||
}
|
||||
@ -134,6 +135,7 @@
|
||||
x.Status = user.Status;
|
||||
x.DateOfBirth = user.DateOfBirth;
|
||||
x.CreatedAt = user.CreatedAt;
|
||||
x.CurrencyAmount = user.CurrencyAmount;
|
||||
|
||||
onlineUsers.Add(x);
|
||||
}
|
||||
@ -170,6 +172,7 @@
|
||||
dto.DateOfBirth = user.DateOfBirth;
|
||||
dto.CreatedAt = user.CreatedAt;
|
||||
dto.Status = user.Status;
|
||||
dto.CurrencyAmount = user.CurrencyAmount;
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Data = dto;
|
||||
@ -214,6 +217,7 @@
|
||||
infoDto.DateOfBirth = request.DateOfBirth;
|
||||
infoDto.CreatedAt = dbUser.CreatedAt;
|
||||
infoDto.Status = dbUser.Status;
|
||||
infoDto.CurrencyAmount = dbUser.CurrencyAmount;
|
||||
|
||||
serviceResponse.Success = true;
|
||||
serviceResponse.Data = infoDto;
|
||||
@ -329,6 +333,51 @@
|
||||
return serviceResponse;
|
||||
}
|
||||
|
||||
public async Task<ServiceResponse<int>> AddCurrencyToUser(string id, int amount, bool isSpinClaim)
|
||||
{
|
||||
var response = new ServiceResponse<int>();
|
||||
var user = _dataContext.Users.FirstOrDefault(e => e.Id == id);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
user.CurrencyAmount += amount;
|
||||
if (isSpinClaim) user.LastCurrencySpin = DateTime.UtcNow;
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
||||
response.Success = true;
|
||||
response.Data = user.CurrencyAmount;
|
||||
return response;
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Success = false;
|
||||
response.Message = "User Not Found";
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ServiceResponse<int>> RemoveCurrencyFromUser(string id, int amount)
|
||||
{
|
||||
var response = new ServiceResponse<int>();
|
||||
var user = _dataContext.Users.FirstOrDefault(e => e.Id == id);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
user.CurrencyAmount -= amount;
|
||||
await _dataContext.SaveChangesAsync();
|
||||
|
||||
response.Success = true;
|
||||
response.Data = user.CurrencyAmount;
|
||||
return response;
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Success = false;
|
||||
response.Message = "User Not Found";
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
private long LongRandom(long min, long max, Random rnd)
|
||||
{
|
||||
long result = rnd.Next((int)(min >> 32), (int)(max >> 32));
|
||||
|
Loading…
x
Reference in New Issue
Block a user