Adding initial auction system
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using GameAuthApi.Models;
|
||||
using GameAuthApi.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameAuthApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuctionController : ControllerBase
|
||||
{
|
||||
private readonly AuctionService _auctionService;
|
||||
private readonly ItemService _itemService;
|
||||
|
||||
public AuctionController(AuctionService auctionService, ItemService itemService)
|
||||
{
|
||||
_auctionService = auctionService;
|
||||
_itemService = itemService;
|
||||
}
|
||||
|
||||
[HttpPost("list")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> ListItem([FromBody] CreateAuctionRequest req)
|
||||
{
|
||||
var item = await _itemService.GetByIdAsync(req.ItemId);
|
||||
if (item == null)
|
||||
return BadRequest("Invalid item ID.");
|
||||
|
||||
var auction = new Auction
|
||||
{
|
||||
ItemId = req.ItemId,
|
||||
StartingPrice = req.StartingPrice,
|
||||
CurrentPrice = req.StartingPrice,
|
||||
SellerPlayerId = User.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
};
|
||||
|
||||
await _auctionService.CreateAsync(auction);
|
||||
return Ok(auction);
|
||||
}
|
||||
|
||||
[HttpPost("bid")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> PlaceBid([FromBody] BidRequest req)
|
||||
{
|
||||
var auction = await _auctionService.GetByIdAsync(req.AuctionId);
|
||||
if (auction == null)
|
||||
return NotFound("Auction not found.");
|
||||
if (auction.IsClosed)
|
||||
return BadRequest("Auction closed.");
|
||||
if (req.BidAmount <= auction.CurrentPrice)
|
||||
return BadRequest("Bid must be higher than current price.");
|
||||
|
||||
auction.CurrentPrice = req.BidAmount;
|
||||
auction.HighestBidderId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
await _auctionService.UpdateAsync(auction);
|
||||
return Ok(auction);
|
||||
}
|
||||
|
||||
[HttpGet("active")]
|
||||
[Authorize(Roles = "USER,SUPER")]
|
||||
public async Task<IActionResult> GetActive()
|
||||
{
|
||||
var active = await _auctionService.GetActiveAsync();
|
||||
return Ok(active);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user