add ID generators
This commit is contained in:
parent
78d7972d28
commit
f5ce95e2dc
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -41,7 +41,6 @@ public class LinkUseCase {
|
|||
var link = new Link(id, creator, url, categoryIds, tags);
|
||||
|
||||
linkRepository.add(link);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
19
2-Application/src/main/java/link/RandomLinkIdGenerator.java
Normal file
19
2-Application/src/main/java/link/RandomLinkIdGenerator.java
Normal file
|
@ -0,0 +1,19 @@
|
|||
package link;
|
||||
|
||||
public class RandomLinkIdGenerator implements LinkIdGenerator {
|
||||
private final LinkRepository linkRepository;
|
||||
|
||||
public RandomLinkIdGenerator(LinkRepository linkRepository) {
|
||||
this.linkRepository = linkRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkId generateId() {
|
||||
var randomID = (int) (Math.random() * 10000);
|
||||
|
||||
if (linkRepository.getById(new LinkId(randomID)).isPresent()) {
|
||||
return generateId();
|
||||
}
|
||||
return new LinkId(randomID);
|
||||
}
|
||||
}
|
Reference in a new issue