Ejemplo n.º 1
0
        public override async Task <List <ShortUrlDto> > Handle(BulkCreateShortUrlCommand request, CancellationToken cancellationToken)
        {
            ValidateRequest(request);
            List <Slug>     slugs     = new List <Slug>();
            List <ShortUrl> shortUrls = new List <ShortUrl>();
            Slug            slug;

            foreach (var url in request.LongUrls)
            {
                //Generate shortUrl
                var shortUrl = new ShortUrl(url)
                {
                    CreatedBy = CurrentUser.UserId,
                    UpdatedBy = CurrentUser.UserId
                };
                slug = await _slugManager.Create(CurrentUser, string.Empty, true, cancellationToken);

                slugs.Add(slug);

                shortUrl.SetSlug(slug);

                shortUrls.Add(shortUrl);
            }

            //Add or Update to database
            await _shortUrlRepository.BulkInsert(shortUrls, cancellationToken);

            // Data
            var result = shortUrls.AsQueryable().Select(s => ShortUrlDto.MapFromEntity(s, _globalSettings.RouterDomain)).ToList();

            //Publish Event
            _mediator.Publish(new ShortUrlsCreated {
                ShortUrls = shortUrls
            }).Forget();

            return(result);
        }