Skip to content

Instantly share code, notes, and snippets.

@Maarten88
Last active August 3, 2017 03:11
Show Gist options
  • Save Maarten88/5728913 to your computer and use it in GitHub Desktop.
Save Maarten88/5728913 to your computer and use it in GitHub Desktop.
using Auction.Service.Contracts;
using Auction.Web.Domain.Commands;
using Auction.Web.Domain.Entities;
using Auction.Web.Domain.Queries;
using Auction.Web.Utility;
using AutoMapper;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
namespace Auction.Web.Hubs
{
[HubName("auctionHub")]
public class AuctionHub : BaseHub
{
public void Initialize()
{
// find the lot that is currently in auction
var lot = Query(new SingleActiveLot());
if (lot != null)
{
// Create client version of internal lot entity
ClientLot clientLot = Mapper.Map<Lot, ClientLot>(lot);
// calculate remaining time
TimeSpan remainingTime = lot.GetRemainingTime(DateTime.UtcNow);
clientLot.TimeRemaining = remainingTime.Ticks / TimeSpan.TicksPerMillisecond;
clientLot.ProgressRemaining = lot.CalculateProgressRemaining(remainingTime);
// respond by sending the lot back to the client
Clients.Caller.AuctionInitialized(clientLot);
}
else
{
// send dummy lot id 0 telling the user there is no auction active now
}
}
[Authorize]
public void PlaceBid(PlaceBidRequest placeBidRequest)
{
// Authorize attribute means we should have a User
Guid userId = UserUtility.GetCurrentUserId(Context.Request.GetHttpContext());
// Place the bid. If succesfull, this Command will Publish an Event that is subscribed by this hub and is sent all Clients
var bid = ExecuteCommand(new CreateNewBid(userId, placeBidRequest.LotId, placeBidRequest.Price));
// respond individually to the calling client
Clients.Caller.BidPlaced(new PlaceBidResult()
{
Lot = new ClientLot() { Id = placeBidRequest.LotId },
BidIsAccepted = bid.IsWinningBid,
ResultCode = bid.IsWinningBid ? PlaceBidResultCode.None : PlaceBidResultCode.NotAuthorized
});
}
[Authorize]
public void CancelBid(CancelBidRequest cancelBidRequest)
{
// ...
}
[Authorize]
public void FinalizeOrder(ClientOrder orderInfo)
{
// ...
}
public void ListProducts(int page)
{
// ...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment