99 lines
3.3 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using qtc_api.Models;
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;
public StoreController(StoreService storeService)
{
_storeService = storeService;
}
[HttpGet]
[Route("all-items")]
public ActionResult<ServiceResponse<List<StoreItem>>> GetAllItems()
{
return Ok(_storeService.GetStoreItems());
}
[HttpGet]
[Route("item")]
public ActionResult<ServiceResponse<StoreItem>> GetItem(int id)
{
return Ok(_storeService.GetStoreItem(id));
}
[HttpGet]
[Route("bought-items")]
public ActionResult<ServiceResponse<List<OwnedStoreItem>>> GetBoughtStoreItems()
{
var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null)
{
IEnumerable<Claim> claims = identity.Claims;
var userId = claims.First().Value;
if (userId != null)
{
var result = _storeService.GetBoughtStoreItemsFromUser(userId);
return Ok(result);
}
else return Ok(new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "No UserId In Auth Header" });
}
else return Ok(new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "No Auth Header" });
}
[HttpGet]
[Route("bought-item")]
public ActionResult<ServiceResponse<OwnedStoreItem>> GetBoughtItem(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 result = _storeService.GetBoughtStoreItemFromUser(userId, id);
return Ok(result);
}
else return Ok(new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "No UserId In Auth Header" });
}
else return Ok(new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "No Auth Header" });
}
[HttpPost]
[Route("buy-item")]
[Authorize]
public async Task<ActionResult<ServiceResponse<bool>>> 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 result = await _storeService.BuyStoreItem(userId, id);
return Ok(result);
}
else return Ok(new ServiceResponse<bool> { Success = false, Message = "No UserId In Auth Header" });
}
else return Ok(new ServiceResponse<bool> { Success = false, Message = "No Auth Header" });
}
}
}