convert to tab
This commit is contained in:
1 parent
26469ff307
commit
2db6811b55
5 files changed
+114
-113
No files matched your search
@@ -4,14 +4,14 @@ import java.util.Optional;
|
||||
import java.util.List;
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -1,30 +1,30 @@
|
||||
package pdl.backend;
|
||||
|
||||
public class Image {
|
||||
private static Long count = Long.valueOf(0);
|
||||
private Long id;
|
||||
private String name;
|
||||
private byte[] data;
|
||||
private static Long count = Long.valueOf(0);
|
||||
private Long id;
|
||||
private String name;
|
||||
private byte[] data;
|
||||
|
||||
public Image(final String name, final byte[] data) {
|
||||
id = count++;
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
public Image(final String name, final byte[] data) {
|
||||
id = count++;
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -27,68 +27,68 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
@RestController
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
@Autowired
|
||||
private ObjectMapper mapper;
|
||||
|
||||
private final ImageDao imageDao;
|
||||
private final ImageDao imageDao;
|
||||
|
||||
@Autowired
|
||||
public ImageController(ImageDao imageDao) {
|
||||
this.imageDao = imageDao;
|
||||
}
|
||||
@Autowired
|
||||
public ImageController(ImageDao imageDao) {
|
||||
this.imageDao = imageDao;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||
@RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
|
||||
if (image.isPresent()) {
|
||||
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
|
||||
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
if (image.isPresent()) {
|
||||
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
|
||||
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||
@RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
|
||||
if (image.isPresent()) {
|
||||
imageDao.delete(image.get());
|
||||
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
if (image.isPresent()) {
|
||||
imageDao.delete(image.get());
|
||||
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
|
||||
}
|
||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images", method = RequestMethod.POST)
|
||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||
@RequestMapping(value = "/images", method = RequestMethod.POST)
|
||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||
|
||||
String contentType = file.getContentType();
|
||||
if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) {
|
||||
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
String contentType = file.getContentType();
|
||||
if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) {
|
||||
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
|
||||
try {
|
||||
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
|
||||
} catch (IOException e) {
|
||||
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
|
||||
}
|
||||
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
|
||||
}
|
||||
try {
|
||||
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
|
||||
} catch (IOException e) {
|
||||
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
|
||||
}
|
||||
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json")
|
||||
@ResponseBody
|
||||
public ArrayNode getImageList() {
|
||||
List<Image> images = imageDao.retrieveAll();
|
||||
ArrayNode nodes = mapper.createArrayNode();
|
||||
for (Image image : images) {
|
||||
ObjectNode objectNode = mapper.createObjectNode();
|
||||
objectNode.put("id", image.getId());
|
||||
objectNode.put("name", image.getName());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
@RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json")
|
||||
@ResponseBody
|
||||
public ArrayNode getImageList() {
|
||||
List<Image> images = imageDao.retrieveAll();
|
||||
ArrayNode nodes = mapper.createArrayNode();
|
||||
for (Image image : images) {
|
||||
ObjectNode objectNode = mapper.createObjectNode();
|
||||
objectNode.put("id", image.getId());
|
||||
objectNode.put("name", image.getName());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,44 +15,44 @@ 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() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Image> retrieve(final long id) {
|
||||
return Optional.ofNullable(images.get(id));
|
||||
}
|
||||
@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());
|
||||
}
|
||||
@Override
|
||||
public List<Image> retrieveAll() {
|
||||
return new ArrayList<Image>(images.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(final Image img) {
|
||||
images.put(img.getId(), img);
|
||||
}
|
||||
@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"));
|
||||
@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);
|
||||
}
|
||||
images.put(img.getId(), img);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final Image img) {
|
||||
images.remove(img.getId());
|
||||
}
|
||||
@Override
|
||||
public void delete(final Image img) {
|
||||
images.remove(img.getId());
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public class ImageControllerTests {
|
||||
@Test
|
||||
@Order(4)
|
||||
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
|
||||
@@ -76,7 +76,8 @@ public class ImageControllerTests {
|
||||
@Order(7)
|
||||
public void createImageShouldReturnSuccess() throws Exception {
|
||||
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())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user