예제 #1
0
        private async Task <Slug> CreateSlug(string slug, CancellationToken cancellationToken = default)
        {
            var slugEntity = Check.IsNullOrEmpty(slug) ? await _slugManager.Create(CurrentUser, string.Empty, true, cancellationToken)
                                                       : await _slugManager.Create(CurrentUser, slug, false, cancellationToken);

            Check.That <ApplicationException>(Check.IsNull(slugEntity), "Something went wrong while the slug creating.");

            return(slugEntity);
        }
예제 #2
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);
        }