AlanMoonbase 34a6ac92d3 Implement Store Backend
MODEL CHANGE COMMANDS:
`ALTER TABLE Users ADD ActiveProfileCosmetic int(11) NOT NULL;`
`CREATE TABLE OwnedStoreItems (Id int(11) NOT NULL, UserId varchar(255) NOT NULL, StoreItemId int(11) NOT NULL, FOREIGN KEY (UserId) REFERENCES Users(Id));`
2025-07-09 14:51:18 -07:00

55 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using qtc_api.Services.StoreService;
using System.Security.Claims;
namespace qtc_api.Controllers
{
[Route("api/store")]
[ApiController]
public class StoreController : ControllerBase
{
private readonly StoreService _storeService;
private readonly IUserService _userService;
public StoreController(StoreService storeService, IUserService userService)
{
_storeService = storeService;
_userService = userService;
}
[HttpGet]
[Route("all-items")]
public ActionResult<ServiceResponse<List<StoreItem>>> GetAllItems()
{
return Ok(_storeService.GetStoreItems());
}
[HttpPost]
[Route("buy-item")]
[Authorize]
public async Task<ActionResult<ServiceResponse<OwnedStoreItem>>> BuyStoreItem(int id)
{
var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
IEnumerable<Claim> claims = identity.Claims;
var userId = claims.First().Value;
if (userId != null)
{
var user = await _userService.GetUserById(userId);
if(user != null && user.Success && user.Data != null)
{
var result = await _storeService.BuyStoreItem(user.Data.Id, id);
return Ok(result);
}
else return Ok(new ServiceResponse<OwnedStoreItem> { Success = false, Message = "User Not Found In Auth Header" });
}
else return Ok(new ServiceResponse<OwnedStoreItem> { Success = false, Message = "No UserId In Auth Header" });
}
else return Ok(new ServiceResponse<OwnedStoreItem> { Success = false, Message = "No Auth Header" });
}
}
}