convert to tab

This commit is contained in:
Theophyl Hoguet committed 2023-03-03 22:01:38 +01:00
1 parent 26469ff307
commit 2db6811b55
5 files changed
+114 -113

No files matched your search

+6 -6
View File
@@ -4,14 +4,14 @@ import java.util.Optional;
import java.util.List; import java.util.List;
public interface Dao<T> { public interface Dao<T> {
void create(final T t);
Optional<T> retrieve(final long id); void create(final T t);
List<T> retrieveAll(); Optional<T> retrieve(final long id);
void update(final T t, final String[] params); List<T> retrieveAll();
void delete(final T t); void update(final T t, final String[] params);
void delete(final T t);
} }
+21 -21
View File
@@ -1,30 +1,30 @@
package pdl.backend; package pdl.backend;
public class Image { public class Image {
private static Long count = Long.valueOf(0); private static Long count = Long.valueOf(0);
private Long id; private Long id;
private String name; private String name;
private byte[] data; private byte[] data;
public Image(final String name, final byte[] data) { public Image(final String name, final byte[] data) {
id = count++; id = count++;
this.name = name; this.name = name;
this.data = data; this.data = data;
} }
public long getId() { public long getId() {
return id; return id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(final String name) { public void setName(final String name) {
this.name = name; this.name = name;
} }
public byte[] getData() { public byte[] getData() {
return data; return data;
} }
} }
@@ -27,68 +27,68 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@RestController @RestController
public class ImageController { public class ImageController {
@Autowired @Autowired
private ObjectMapper mapper; private ObjectMapper mapper;
private final ImageDao imageDao; private final ImageDao imageDao;
@Autowired @Autowired
public ImageController(ImageDao imageDao) { public ImageController(ImageDao imageDao) {
this.imageDao = imageDao; this.imageDao = imageDao;
} }
@RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) @RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<?> getImage(@PathVariable("id") long id) { public ResponseEntity<?> getImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id); Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) { if (image.isPresent()) {
InputStream inputStream = new ByteArrayInputStream(image.get().getData()); InputStream inputStream = new ByteArrayInputStream(image.get().getData());
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream)); return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
} }
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
} }
@RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE) @RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) { public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
Optional<Image> image = imageDao.retrieve(id); Optional<Image> image = imageDao.retrieve(id);
if (image.isPresent()) { if (image.isPresent()) {
imageDao.delete(image.get()); imageDao.delete(image.get());
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK); return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
} }
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
} }
@RequestMapping(value = "/images", method = RequestMethod.POST) @RequestMapping(value = "/images", method = RequestMethod.POST)
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
String contentType = file.getContentType(); String contentType = file.getContentType();
if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) { if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) {
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE); return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
} }
try { try {
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes())); imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
} catch (IOException e) { } catch (IOException e) {
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT); return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
} }
return new ResponseEntity<>("Image uploaded", HttpStatus.OK); return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
} }
@RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json") @RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json")
@ResponseBody @ResponseBody
public ArrayNode getImageList() { public ArrayNode getImageList() {
List<Image> images = imageDao.retrieveAll(); List<Image> images = imageDao.retrieveAll();
ArrayNode nodes = mapper.createArrayNode(); ArrayNode nodes = mapper.createArrayNode();
for (Image image : images) { for (Image image : images) {
ObjectNode objectNode = mapper.createObjectNode(); ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("id", image.getId()); objectNode.put("id", image.getId());
objectNode.put("name", image.getName()); objectNode.put("name", image.getName());
nodes.add(objectNode); nodes.add(objectNode);
} }
return nodes; return nodes;
} }
} }
+33 -33
View File
@@ -15,44 +15,44 @@ 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"); final ClassPathResource imgFile = new ClassPathResource("test.jpg");
byte[] fileContent; byte[] fileContent;
try { try {
fileContent = Files.readAllBytes(imgFile.getFile().toPath()); fileContent = Files.readAllBytes(imgFile.getFile().toPath());
Image img = new Image("test.jpg", fileContent); Image img = new Image("test.jpg", fileContent);
images.put(img.getId(), img); images.put(img.getId(), img);
} catch (final IOException e) { } catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
public Optional<Image> retrieve(final long id) { public Optional<Image> retrieve(final long id) {
return Optional.ofNullable(images.get(id)); return Optional.ofNullable(images.get(id));
} }
@Override @Override
public List<Image> retrieveAll() { public List<Image> retrieveAll() {
return new ArrayList<Image>(images.values()); return new ArrayList<Image>(images.values());
} }
@Override @Override
public void create(final Image img) { public void create(final Image img) {
images.put(img.getId(), img); images.put(img.getId(), img);
} }
@Override @Override
public void update(final Image img, final String[] params) { public void update(final Image img, final String[] params) {
img.setName(Objects.requireNonNull(params[0], "Name cannot be null")); img.setName(Objects.requireNonNull(params[0], "Name cannot be null"));
images.put(img.getId(), img); images.put(img.getId(), img);
} }
@Override @Override
public void delete(final Image img) { public void delete(final Image img) {
images.remove(img.getId()); images.remove(img.getId());
} }
} }
@@ -57,7 +57,7 @@ public class ImageControllerTests {
@Test @Test
@Order(4) @Order(4)
public void deleteImagesShouldReturnMethodNotAllowed() throws Exception { public void deleteImagesShouldReturnMethodNotAllowed() throws Exception {
this.mockMvc.perform(delete("/images")).andDo(print()).andExpect(status().isMethodNotAllowed()); this.mockMvc.perform(delete("/images")).andDo(print()).andExpect(status().isMethodNotAllowed());
} }
@Test @Test
@@ -76,7 +76,8 @@ public class ImageControllerTests {
@Order(7) @Order(7)
public void createImageShouldReturnSuccess() throws Exception { public void createImageShouldReturnSuccess() throws Exception {
final ClassPathResource imgFile = new ClassPathResource("test.jpg"); final ClassPathResource imgFile = new ClassPathResource("test.jpg");
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "image/jpeg", imgFile.getInputStream()); MockMultipartFile multipartFile = new MockMultipartFile("file", "test.jpg", "image/jpeg",
imgFile.getInputStream());
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/images").file(multipartFile)).andDo(print()) this.mockMvc.perform(MockMvcRequestBuilders.multipart("/images").file(multipartFile)).andDo(print())
.andExpect(status().isOk()); .andExpect(status().isOk());
} }