add CategoryPersistence Adapter implemenation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a453a01ff9
commit
f558de5fa9
|
@ -1,11 +1,21 @@
|
|||
package main;
|
||||
|
||||
import persistence.GenericCSVDAO;
|
||||
import persistence.category.CSVCategoryPersistenceAdapter;
|
||||
import persistence.category.CategoryEntity;
|
||||
import tag.TagName;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TagName test = new TagName("ds");
|
||||
|
||||
var csvCategoryPersistenceAdapter =
|
||||
new CSVCategoryPersistenceAdapter(new GenericCSVDAO<CategoryEntity>(new File("dsa"),
|
||||
CategoryEntity::new));
|
||||
|
||||
System.out.println(test);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,22 +2,33 @@ package persistence.category;
|
|||
|
||||
import abstraction.PersistenceAdapter;
|
||||
import category.Category;
|
||||
import persistence.GenericDAO;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CSVCategoryPersistenceAdapter implements PersistenceAdapter<Category> {
|
||||
|
||||
@Override
|
||||
public Set<Category> getAll() {
|
||||
return null;
|
||||
static Function<String[], CategoryEntity> constuctor = CategoryEntity::new;
|
||||
private final GenericDAO<CategoryEntity> categoryDAO;
|
||||
|
||||
public CSVCategoryPersistenceAdapter(GenericDAO<CategoryEntity> categoryDAO) {
|
||||
this.categoryDAO = categoryDAO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Category value) {
|
||||
public Set<Category> getAll() {
|
||||
return categoryDAO.getALl().stream().map(CategoryEntity::toCategory).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Category category) {
|
||||
categoryDAO.remove(new CategoryEntity(category));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(Category category) {
|
||||
|
||||
categoryDAO.add(new CategoryEntity(category));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,39 @@
|
|||
package persistence.category;
|
||||
|
||||
public record CategoryEntity(String name, int id) {
|
||||
import category.Category;
|
||||
import category.CategoryId;
|
||||
import category.CategoryName;
|
||||
import persistence.csv.CSVSerializable;
|
||||
|
||||
public class CategoryEntity implements CSVSerializable {
|
||||
|
||||
private final String name;
|
||||
private final int id;
|
||||
|
||||
public CategoryEntity(String name, int id) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public CategoryEntity(String[] fields) {
|
||||
this(fields[0], Integer.parseInt(fields[1]));
|
||||
}
|
||||
|
||||
public CategoryEntity(Category category) {
|
||||
this(category.getName().toString(), category.getId().id());
|
||||
}
|
||||
|
||||
public Category toCategory() {
|
||||
return new Category(new CategoryName(name), new CategoryId(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getHeaders() {
|
||||
return new String[]{"name", "id"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toCSVString() {
|
||||
return name + CSVSerializable.seperator + Integer.toString(id);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue