add ID generators
This commit is contained in:
parent
78d7972d28
commit
f5ce95e2dc
4 changed files with 39 additions and 2 deletions
|
@ -13,7 +13,6 @@ public class CategoryUseCase {
|
|||
}
|
||||
|
||||
public void addCategory(CategoryName name) {
|
||||
|
||||
var id = this.categoryIdGenerator.generateId();
|
||||
this.categoryRepository.add(new Category(name, id));
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package category;
|
||||
|
||||
public class RandomCategoryIdGenerator implements CategoryIdGenerator {
|
||||
|
||||
private final CategoryRepository categoryRepository;
|
||||
|
||||
public RandomCategoryIdGenerator(CategoryRepository categoryRepository) {
|
||||
this.categoryRepository = categoryRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CategoryId generateId() {
|
||||
var randomID = (int) (Math.random() * 10000);
|
||||
|
||||
if (categoryRepository.getById(new CategoryId(randomID)).isPresent()) {
|
||||
return generateId();
|
||||
}
|
||||
return new CategoryId(randomID);
|
||||
}
|
||||
}
|
Reference in a new issue