Ejemplo n.º 1
0
        public async Task <DeleteReplayResponse> Handle(DeleteReplay request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(request.Hash))
            {
                throw new ArgumentException("Hash must be a valid string");
            }

            var match = await _apiDbContext.Replays.FirstOrDefaultAsync(x => x.ReplayHashStub == request.Hash);

            var blobFilename = request.Hash + ".zip";
            await _blobStorage.DeleteBlobIfExists("files", blobFilename);

            if (match == null)
            {
                return new DeleteReplayResponse()
                       {
                           Success = false, Message = "Replay with this hash was not found"
                       }
            }
            ;

            _apiDbContext.Replays.Remove(match);
            await _apiDbContext.SaveChangesAsync(cancellationToken);

            return(new DeleteReplayResponse()
            {
                Success = true, Message = "Deleted successfully"
            });
        }
    }
Ejemplo n.º 2
0
        public async Task <CartModel> Handle(AddProductCommand request, CancellationToken cancellationToken)
        {
            var product = new Product {
                ProductId = request.ProductId, Quantity = request.Quantity
            };

            var cart = await _apiDbContext
                       .Carts
                       .Include(c => c.Products)
                       .Where(c => c.Id == request.CartId).SingleOrDefaultAsync(cancellationToken);

            var existingProduct = cart.Products.SingleOrDefault(p => p.ProductId == product.ProductId);

            if (existingProduct != null)
            {
                existingProduct.Quantity += product.Quantity;
            }
            else
            {
                cart.Products.Add(product);
            }

            await _apiDbContext.SaveChangesAsync(cancellationToken);

            return(CartModel.Create(cart));
        }
Ejemplo n.º 3
0
        public async Task <CreateReplayResponse> Handle(CreateReplay request, CancellationToken cancellationToken)
        {
            try
            {
                var fileStream   = request.File.Content;
                var parsedReplay = await SD2Tools.ReplayTools.Replay.FromStream(fileStream);

                fileStream.Seek(0, SeekOrigin.Begin);
                var sha        = SHA1.Create();
                var now        = DateTime.UtcNow;
                var hashSource = BitConverter.ToString(sha.ComputeHash(fileStream))
                                 + $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
                var hashStub = BitConverter.ToString(sha.ComputeHash(Encoding.ASCII.GetBytes(hashSource)))
                               .Replace("-", String.Empty)
                               .Substring(0, 10)
                               .ToLower();

                var blobUrl = await UploadToStorage(fileStream, hashStub);

                var replay = new Replay()
                {
                    Name            = request.Name,
                    Description     = request.Description,
                    Date            = DateTime.UtcNow,
                    ReplayHashStub  = hashStub,
                    ReplayRawFooter = parsedReplay.ReplayFooterRaw,
                    ReplayRawHeader = parsedReplay.ReplayHeaderRaw,
                    //ReplayHeader = new ReplayHeader()
                    //{
                    //    Game = parsedReplay.ReplayHeader.Game.ToDomainReplayHeaderGame(), // todo use automapper
                    //    Players = parsedReplay.ReplayHeader.Players.ToDomainReplayPlayerList()
                    //},
                    //ReplayFooter = new ReplayFooter()
                    //{
                    //    result = new ReplayFooter.ReplayFooterResult()
                    //    {
                    //        Duration = parsedReplay.ReplayFooter.Result.Duration,
                    //        Score = parsedReplay.ReplayFooter.Result.Score,
                    //        Victory = parsedReplay.ReplayFooter.Result.Victory
                    //    }
                    //},
                    ReplayHeader = _mapper.Map <ReplayHeader>(parsedReplay.ReplayHeader),
                    ReplayFooter = _mapper.Map <ReplayFooter>(parsedReplay.ReplayFooter),
                    BinaryUrl    = blobUrl
                };

                _apiDbContext.Replays.Add(replay);
                await _apiDbContext.SaveChangesAsync(cancellationToken);

                return(new CreateReplayResponse()
                {
                    ReplayHash = replay.ReplayHashStub
                });
            }
            catch (Exception e)
            {
                throw new ReplayProcessingFailedException("Processing and saving the replay failed", e);
            }
        }
Ejemplo n.º 4
0
        public async Task <CartModel> Handle(CreateCartCommand request, CancellationToken cancellationToken)
        {
            var cart = new Cart();

            _apiDbContext.Carts.Add(cart);

            await _apiDbContext.SaveChangesAsync(cancellationToken);

            return(CartModel.Create(cart));
        }
Ejemplo n.º 5
0
        public async Task <CartModel> Handle(ClearCartCommand request, CancellationToken cancellationToken)
        {
            var cart = await _apiDbContext
                       .Carts
                       .Include(c => c.Products)
                       .Where(c => c.Id == request.CartId)
                       .SingleOrDefaultAsync(cancellationToken);

            if (cart == null)
            {
                return(null);
            }

            cart.Products.Clear();

            await _apiDbContext.SaveChangesAsync(cancellationToken);

            return(CartModel.Create(cart));
        }
Ejemplo n.º 6
0
 public async Task <int> Add(Customer customer, CancellationToken ct)
 {
     _context.Customers.Add(customer);
     return(await _context.SaveChangesAsync(ct));
 }
 public async Task <int> Add(Order order, CancellationToken ct)
 {
     order.Date = DateTime.Now;
     _context.Orders.Add(order);
     return(await _context.SaveChangesAsync(ct));
 }
 public async Task <int> Add(Salesperson person, CancellationToken ct)
 {
     _context.Salesperson.Add(person);
     return(await _context.SaveChangesAsync(ct));
 }
Ejemplo n.º 9
0
 public async Task <int> Add(OrderItem item, CancellationToken ct)
 {
     _context.OrderItems.Add(item);
     return(await _context.SaveChangesAsync(ct));
 }
Ejemplo n.º 10
0
 public async Task <int> Add(Product product, CancellationToken ct)
 {
     _context.Products.Add(product);
     return(await _context.SaveChangesAsync(ct));
 }