This commit is contained in:
keahorvath authored and Theophyl Hoguet committed 2023-03-13 20:07:20 +01:00
1 parent 42068a9b2d
commit 93cbf58ba6
3 files changed
+73 -36

No files matched your search

+64 -33
View File
@@ -1,5 +1,6 @@
package pdl.backend; package pdl.backend;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
@@ -15,44 +16,74 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public class ImageDao implements Dao<Image> { public class ImageDao implements Dao<Image> {
private final Map<Long, Image> images = new HashMap<>(); private final Map<Long, Image> images = new HashMap<>();
public ImageDao() { public ImageDao() {
final ClassPathResource imgFile = new ClassPathResource("test.jpg"); try {
byte[] fileContent; final File folder = new File("backend/src/main/resources/images");
try { getAllImages(folder);
fileContent = Files.readAllBytes(imgFile.getFile().toPath()); } catch (final Exception e) {
Image img = new Image("test.jpg", fileContent); e.printStackTrace();
images.put(img.getId(), img); }
} catch (final IOException e) { System.out.println("IMAGEEEEEEEEEEEEEEs");
e.printStackTrace(); for (Image img : retrieveAll()) {
} System.out.println(img.getName());
} }
System.out.println("ENNNNNNNNNNNNNNd");
@Override }
public Optional<Image> retrieve(final long id) {
return Optional.ofNullable(images.get(id));
}
@Override public void getAllImages(final File folder) {
public List<Image> retrieveAll() { for (final File fileEntry : folder.listFiles()) {
return new ArrayList<Image>(images.values()); if (fileEntry.isDirectory()) {
} getAllImages(fileEntry);
} else {
final ClassPathResource imgFile = new ClassPathResource(fileEntry.getName());
String extension = "";
@Override int i = fileEntry.getName().lastIndexOf('.');
public void create(final Image img) { if (i > 0) {
images.put(img.getId(), img); extension = fileEntry.getName().substring(i + 1);
} }
if (!extension.equals("jpg") && !extension.equals("pgn")) {
continue;
}
byte[] fileContent;
try {
fileContent = Files.readAllBytes(imgFile.getFile().toPath());
Image img = new Image(imgFile.getFilename(), fileContent);
images.put(img.getId(), img);
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
@Override @Override
public void update(final Image img, final String[] params) { public Optional<Image> retrieve(final long id) {
img.setName(Objects.requireNonNull(params[0], "Name cannot be null")); return Optional.ofNullable(images.get(id));
}
images.put(img.getId(), img); @Override
} public List<Image> retrieveAll() {
return new ArrayList<Image>(images.values());
}
@Override @Override
public void delete(final Image img) { public void create(final Image img) {
images.remove(img.getId()); images.put(img.getId(), img);
} }
@Override
public void update(final Image img, final String[] params) {
img.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
images.put(img.getId(), img);
}
@Override
public void delete(final Image img) {
images.remove(img.getId());
}
} }
@@ -1,13 +1,19 @@
package pdl.backend; package pdl.backend;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class BackendApplicationTests { class BackendApplicationTests {
@Test @Test
void contextLoads() { void contextLoads() {
} }
@Test
@Order(1)
public void appLaunchShouldReturnError() throws Exception {
}
} }