protected async Task <string> GenerateSlug( string name, int?id = null, CancellationToken cancellationToken = default) { // Though not compulsory, since multiple todo-items with the same name will probably be created but will have different execution day or time // In order to reduce the chance of collision with existing slugs, a random string is appended to the slug name. var randomString = RandomStringGenerator.Get(7); var slug = new SlugHelper().GenerateSlug($"{name} ${randomString}"); // Ensure uniqueness List <string> similarSlugs = await DbSet .Where(todoItem => todoItem.Id != id) .Where(todoItem => EF.Functions.Like(todoItem.Slug, $"{slug}%")) .Select(todoItem => todoItem.Slug).AsNoTracking().ToListAsync(cancellationToken); if (!similarSlugs.Contains(slug)) { return(slug); } var alternativeSlug = ""; var suffix = 2; do { alternativeSlug = $"{slug}-{suffix}"; suffix++; } while(similarSlugs.Contains(alternativeSlug)); return(alternativeSlug); }
private async Task SeedTodoItems() { if (!await Uow.UserRepository.IsEmpty()) { return; } var randomUser = await Uow.UserRepository.Add(new User { IdFromIdp = $"random-{RandomStringGenerator.Get(14)}" }); var todoItems = new TodoItem[] { new TodoItem{ Name="Sleep", User = randomUser }, new TodoItem{ Name="Eat", User = randomUser }, new TodoItem{ Name="Drink", User = randomUser } }; List<Task<TodoItem>> tasks = new List<Task<TodoItem>>(); foreach (TodoItem todoItem in todoItems) { tasks.Add(Uow.TodoItemRepository.Add(todoItem)); } await Task.WhenAll(tasks); await Uow.Commit(); }