public async Task <Unit> Handle(DeleteScriptCommand request, CancellationToken cancellationToken)
        {
            var script = await _db.Scripts.FirstOrDefaultAsync(w => w.Id == request.ScriptId, cancellationToken);

            if (script == null)
            {
                throw new NotFoundException("Script", request.ScriptId);
            }

            if (script.Status == ScriptStatus.Live)
            {
                await _mediator.Send(new RemovePendingRequestsCommand { ScriptId = script.Id }, cancellationToken);
            }

            _db.Remove(script);

            await _db.Data.AddAsync(new Data
            {
                Key   = $"script:delete:{script.Id}:{script.Name}:by",
                Value = request.Admin.Id.ToString()
            }, cancellationToken);

            await _db.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Ejemplo n.º 2
0
        public async Task <Unit> Handle(ModifyPricePerQuantityCommand request, CancellationToken cancellationToken)
        {
            var item = await _mediator.Send(new GetItemBySkuQuery { Sku = request.Sku }, cancellationToken);

            if (item == null)
            {
                throw new NotFoundException("Item", request.Sku);
            }

            AssertValidRequest(request, item);

            if (item.Type != ItemType.PremiumScript && !request.User.IsOwner)
            {
                throw new AuthorizationException("Permission denied.");
            }

            if (!request.User.IsOwner)
            {
                await AssertValidScriptAccess(item, request, cancellationToken);
            }

            switch (request.Action)
            {
            case CrudAction.Create:
            {
                var recordByQuantity = await _db.PricePerQuantity.FirstOrDefaultAsync(w =>
                                                                                      w.Sku == request.Sku && w.Quantity == request.Quantity, cancellationToken);

                if (recordByQuantity != null)
                {
                    throw new Exception("Record already exists for that quantity.");
                }

                await _db.PricePerQuantity.AddAsync(new PricePerQuantity
                    {
                        Sku      = request.Sku,
                        Price    = request.Price,
                        Quantity = request.Quantity
                    }, cancellationToken);

                break;
            }

            case CrudAction.Delete:
            {
                var record = await _db.PricePerQuantity.FirstOrDefaultAsync(w =>
                                                                            w.Sku == request.Sku && w.Price == request.Price && w.Quantity == request.Quantity,
                                                                            cancellationToken);


                if (record == null)
                {
                    return(Unit.Value);
                }

                _db.Remove(record);
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }


            await _db.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task Handle(DiscordMessageEvent notification, CancellationToken cancellationToken)
        {
            if (!notification.IsDirector || !notification.Content.StartsWith("!"))
            {
                return;
            }

            var client = await _provider.Get();

            var channel = client?.GetGuild(notification.GuildId)
                          .GetTextChannel(notification.ChannelId);

            var split = notification.Content.Split(" ");

            if (split.Length < 3)
            {
                return;
            }
            var command = split[0];
            var key     = split[1];
            var value   = notification.Content
                          .Substring(command.Length + key.Length + 2).Trim();

            var exists = await _db.SiteConfig.FirstOrDefaultAsync(w => w.Key == $"discord:bot:reply:{key}", cancellationToken : cancellationToken);

            if (command == "!add_reply")
            {
                if (exists != null)
                {
                    await channel.SendMessageAsync($"Reponse by {key} already exists. If you wish to change, please delete with !remove_reply");

                    return;
                }

                await _db.SiteConfig.AddAsync(new Domain.Entities.SiteConfig
                {
                    Key   = $"discord:bot:reply:{key}",
                    Value = value
                }, cancellationToken);

                await _redis.Remove("discord:bot:replies");

                await _db.SaveChangesAsync(cancellationToken);

                await channel.SendMessageAsync($"Successfully added response {key}.");
            }

            if (command == "!remove_reply")
            {
                if (exists != null)
                {
                    _db.Remove(exists);
                    await _db.SaveChangesAsync(cancellationToken);

                    var redisKey = $"discord:bot:reply:{key}";
                    await _redis.Remove(redisKey);

                    await _redis.Remove("discord:bot:replies");

                    await channel.SendMessageAsync($"Successfully removed {key}.");
                }
            }
        }