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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
using GameAuthApi.Controllers;
|
||||||
|
using GameAuthApi.Models;
|
||||||
|
using GameAuthApi.Services;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
public class AuctionControllerTests
|
||||||
|
{
|
||||||
|
private static IConfiguration cfg = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection(new Dictionary<string,string>
|
||||||
|
{
|
||||||
|
{"MongoDB:ConnectionString","mongodb://localhost"},
|
||||||
|
{"MongoDB:DatabaseName","test"}
|
||||||
|
}).Build();
|
||||||
|
|
||||||
|
private static ControllerContext Ctx(string id="u")
|
||||||
|
{
|
||||||
|
var ctx = new DefaultHttpContext();
|
||||||
|
ctx.User = new ClaimsPrincipal(new ClaimsIdentity(new[]
|
||||||
|
{
|
||||||
|
new Claim(ClaimTypes.NameIdentifier,id)
|
||||||
|
}, "mock"));
|
||||||
|
return new ControllerContext { HttpContext = ctx };
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ListItem_InvalidItem_ReturnsBadRequest()
|
||||||
|
{
|
||||||
|
var itemSvc = new Mock<ItemService>(cfg);
|
||||||
|
itemSvc.Setup(i => i.GetByIdAsync("bad")).ReturnsAsync((Item?)null);
|
||||||
|
var controller = new AuctionController(new Mock<AuctionService>(cfg).Object, itemSvc.Object)
|
||||||
|
{
|
||||||
|
ControllerContext = Ctx()
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = await controller.ListItem(new CreateAuctionRequest { ItemId="bad", StartingPrice=1 });
|
||||||
|
|
||||||
|
Assert.IsType<BadRequestObjectResult>(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PlaceBid_AuctionNotFound()
|
||||||
|
{
|
||||||
|
var aucSvc = new Mock<AuctionService>(cfg);
|
||||||
|
aucSvc.Setup(a => a.GetByIdAsync("a")).ReturnsAsync((Auction?)null);
|
||||||
|
var controller = new AuctionController(aucSvc.Object, new Mock<ItemService>(cfg).Object)
|
||||||
|
{
|
||||||
|
ControllerContext = Ctx()
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = await controller.PlaceBid(new BidRequest { AuctionId="a", BidAmount=5 });
|
||||||
|
|
||||||
|
Assert.IsType<NotFoundObjectResult>(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetActive_ReturnsOk()
|
||||||
|
{
|
||||||
|
var aucSvc = new Mock<AuctionService>(cfg);
|
||||||
|
var list = new List<Auction>();
|
||||||
|
aucSvc.Setup(a => a.GetActiveAsync()).ReturnsAsync(list);
|
||||||
|
var controller = new AuctionController(aucSvc.Object, new Mock<ItemService>(cfg).Object)
|
||||||
|
{
|
||||||
|
ControllerContext = Ctx()
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = await controller.GetActive();
|
||||||
|
|
||||||
|
var ok = Assert.IsType<OkObjectResult>(result);
|
||||||
|
Assert.Equal(list, ok.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
|
|
||||||
|
namespace GameAuthApi.Models;
|
||||||
|
|
||||||
|
public class Auction
|
||||||
|
{
|
||||||
|
[BsonId]
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string ItemId { get; set; }
|
||||||
|
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string SellerPlayerId { get; set; }
|
||||||
|
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string HighestBidderId { get; set; }
|
||||||
|
|
||||||
|
public int StartingPrice { get; set; }
|
||||||
|
public int CurrentPrice { get; set; }
|
||||||
|
public bool IsClosed { get; set; } = false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace GameAuthApi.Models;
|
||||||
|
|
||||||
|
public class BidRequest
|
||||||
|
{
|
||||||
|
public string AuctionId { get; set; }
|
||||||
|
public int BidAmount { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace GameAuthApi.Models;
|
||||||
|
|
||||||
|
public class CreateAuctionRequest
|
||||||
|
{
|
||||||
|
public string ItemId { get; set; }
|
||||||
|
public int StartingPrice { get; set; }
|
||||||
|
}
|
||||||
@@ -52,6 +52,7 @@ builder.Services.AddSingleton<PlayerService>();
|
|||||||
builder.Services.AddSingleton<SpawnableService>();
|
builder.Services.AddSingleton<SpawnableService>();
|
||||||
builder.Services.AddSingleton<ItemService>();
|
builder.Services.AddSingleton<ItemService>();
|
||||||
builder.Services.AddSingleton<BlacklistService>();
|
builder.Services.AddSingleton<BlacklistService>();
|
||||||
|
builder.Services.AddSingleton<AuctionService>();
|
||||||
|
|
||||||
var jwtKey = builder.Configuration["Jwt:Key"];
|
var jwtKey = builder.Configuration["Jwt:Key"];
|
||||||
var keyBytes = Encoding.UTF8.GetBytes(jwtKey);
|
var keyBytes = Encoding.UTF8.GetBytes(jwtKey);
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using GameAuthApi.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace GameAuthApi.Services;
|
||||||
|
|
||||||
|
public class AuctionService
|
||||||
|
{
|
||||||
|
private readonly IMongoCollection<Auction> _auctions;
|
||||||
|
|
||||||
|
public AuctionService(IConfiguration config)
|
||||||
|
{
|
||||||
|
var client = new MongoClient(config["MongoDB:ConnectionString"]);
|
||||||
|
var db = client.GetDatabase(config["MongoDB:DatabaseName"]);
|
||||||
|
_auctions = db.GetCollection<Auction>("Auctions");
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual async Task CreateAsync(Auction auction) =>
|
||||||
|
await _auctions.InsertOneAsync(auction);
|
||||||
|
|
||||||
|
public virtual async Task<Auction> GetByIdAsync(string id) =>
|
||||||
|
await _auctions.Find(a => a.Id == id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
public virtual async Task UpdateAsync(Auction auction) =>
|
||||||
|
await _auctions.ReplaceOneAsync(a => a.Id == auction.Id, auction);
|
||||||
|
|
||||||
|
public virtual async Task<List<Auction>> GetActiveAsync() =>
|
||||||
|
await _auctions.Find(a => !a.IsClosed).ToListAsync();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user