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