Esempio n. 1
0
        public async Task <ActionResult> CreateSlugCollection(IEnumerable <SlugForCreationDto> slugsToAdd)
        {
            _logger.LogInformation($"bulk add new slugs");

            if (slugsToAdd == null || slugsToAdd.Count() == 0)
            {
                return(BadRequest());
            }

            List <SlugEntity> slugsToReturn = new List <SlugEntity>();
            int slugLength = _configuration.GetValue <short>("SlugLength", 5);

            foreach (var slug in slugsToAdd)
            {
                if (string.IsNullOrWhiteSpace(slug.Slug))
                {
                    slug.Slug = NanoidService.Create(slugLength);
                }

                SlugEntity newSlug = new SlugEntity
                {
                    Slug = slug.Slug,
                    Url  = slug.Url
                };
                _murlRepository.AddSlug(newSlug);
                slugsToReturn.Add(newSlug);
            }

            await _murlRepository.SaveChangesAsync();

            string slugs = string.Join(",", slugsToAdd.Select(s => s.Slug));

            return(CreatedAtRoute(nameof(GetSlugCollection), new { slugs }, slugsToAdd));
        }
Esempio n. 2
0
        public async Task <ActionResult <SlugDto> > CreateSlug([FromBody] SlugForCreationDto slugForCreation)
        {
            _logger.LogInformation(string.Format($"creating new slug for URL: {slugForCreation.Url}"));

            // map to entity
            SlugEntity slugToAdd = new SlugEntity()
            {
                Url = slugForCreation.Url
            };

            int slugLength = _configuration.GetValue <short>("SlugLength", 5);

            slugToAdd.Slug = NanoidService.Create(slugLength);
            _murlRepository.AddSlug(slugToAdd);
            await _murlRepository.SaveChangesAsync();

            return(CreatedAtRoute(nameof(GetSlug), new { slug = slugToAdd.Slug }, slugToAdd));
        }