Implement Getting Single Items From Store

Bug Fixes
This commit is contained in:
Alan Moon 2025-07-10 17:20:31 -07:00
parent 34a6ac92d3
commit 62ea9facc5
5 changed files with 73 additions and 23 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using qtc_api.Models;
using qtc_api.Services.StoreService;
using System.Security.Claims;
@ -24,10 +25,16 @@ namespace qtc_api.Controllers
return Ok(_storeService.GetStoreItems());
}
[HttpPost]
[Route("buy-item")]
[Authorize]
public async Task<ActionResult<ServiceResponse<OwnedStoreItem>>> BuyStoreItem(int id)
[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;
@ -38,17 +45,55 @@ namespace qtc_api.Controllers
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);
var result = _storeService.GetBoughtStoreItemsFromUser(userId);
return Ok(result);
}
else return Ok(new ServiceResponse<OwnedStoreItem> { Success = false, Message = "User Not Found In Auth Header" });
else return Ok(new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "No UserId In Auth Header" });
}
else return Ok(new ServiceResponse<OwnedStoreItem> { Success = false, Message = "No UserId In Auth Header" });
else return Ok(new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "No Auth Header" });
}
else return Ok(new ServiceResponse<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" });
}
}
}

View File

@ -25,6 +25,14 @@ namespace qtc_api.Services.StoreService
return new ServiceResponse<List<StoreItem>> { Success = true, Data = StoreItems };
}
public ServiceResponse<StoreItem> GetStoreItem(int id)
{
var item = StoreItems.FirstOrDefault(e => e.Id == id);
if (item != null)
return new ServiceResponse<StoreItem> { Success = true, Data = item };
else return new ServiceResponse<StoreItem> { Success = false, Message = "Item Not Found" };
}
public ServiceResponse<OwnedStoreItem> GetBoughtStoreItemFromUser(string userId, int itemId)
{
// find item owned by user
@ -43,7 +51,7 @@ namespace qtc_api.Services.StoreService
else return new ServiceResponse<List<OwnedStoreItem>> { Success = false, Message = "User Owns No Items" };
}
public async Task<ServiceResponse<OwnedStoreItem>> BuyStoreItem(string userId, int id)
public async Task<ServiceResponse<bool>> BuyStoreItem(string userId, int id)
{
// find item in store
var item = StoreItems.FirstOrDefault(e => e.Id == id);
@ -70,13 +78,13 @@ namespace qtc_api.Services.StoreService
await _ctx.SaveChangesAsync();
// return successful service response
return new ServiceResponse<OwnedStoreItem> { Success = true, Data = ownedStoreItem };
return new ServiceResponse<bool> { Success = true, Data = true };
}
else return new ServiceResponse<OwnedStoreItem> { Success = false, Message = "Insufficient Currency" };
else return new ServiceResponse<bool> { Success = false, Message = "Insufficient Currency" };
}
else return new ServiceResponse<OwnedStoreItem> { Success = false, Message = "User Not Found" };
else return new ServiceResponse<bool> { Success = false, Message = "User Not Found" };
}
else return new ServiceResponse<OwnedStoreItem> { Success = false, Message = "Item Not Found" };
else return new ServiceResponse<bool> { Success = false, Message = "Item Not Found" };
}
}
}

View File

@ -206,6 +206,7 @@
dbUser.Username = request.Username;
dbUser.Bio = request.Bio;
dbUser.DateOfBirth = request.DateOfBirth;
dbUser.ActiveProfileCosmetic = request.ProfileCosmeticId;
await _dataContext.SaveChangesAsync();

View File

@ -39,8 +39,4 @@
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="user-content\" />
</ItemGroup>
</Project>