Skip to content

Commit 9b74066

Browse files
committed
Fix image functionality
1 parent 21fb133 commit 9b74066

File tree

6 files changed

+3
-51
lines changed

6 files changed

+3
-51
lines changed

src/main/java/com/darwin/simplestore/controllers/ImageController.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,4 @@ public ResponseEntity<Void> updateImage(@PathVariable final Long imageId, @Reque
6565
imageService.updateImage(imageDto);
6666
return ResponseEntity.ok().build();
6767
}
68-
69-
/**
70-
* Delete an image
71-
* @param imageId The id of the image
72-
* @return Response object
73-
* @throws ResourceNotFoundException If the image could not be found
74-
*/
75-
@DeleteMapping("/{imageId}")
76-
public ResponseEntity<Void> deleteImage(@PathVariable final Long imageId) throws ResourceNotFoundException {
77-
imageService.deleteImage(imageId);
78-
return ResponseEntity.ok().build();
79-
}
8068
}

src/main/java/com/darwin/simplestore/entities/Product.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Product {
3030
@Enumerated(EnumType.STRING)
3131
private ProductCategory category;
3232

33-
@OneToOne(fetch = FetchType.LAZY)
33+
@OneToOne(fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL)
3434
@JoinColumn(name = "image_id", referencedColumnName = "id", nullable = true)
3535
private Image image;
3636
}

src/main/java/com/darwin/simplestore/services/ImageService.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,6 @@ public void updateImage(final ImageDto imageDto) throws ResourceNotFoundExceptio
5353
imageRepository.save(image);
5454
}
5555

56-
/**
57-
* Delete an image
58-
* @param id The id of the image
59-
* @throws ResourceNotFoundException If the requested image does not exist
60-
*/
61-
public void deleteImage(final Long id) throws ResourceNotFoundException {
62-
if (!imageRepository.existsById(id)) {
63-
throw new ResourceNotFoundException("No image found with id: " + id);
64-
}
65-
66-
imageRepository.deleteById(id);
67-
}
68-
6956
/**
7057
* Convert a new image DTO to n entity
7158
* @param newImageDto The new image DTO

src/main/java/com/darwin/simplestore/services/ProductService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.darwin.simplestore.exceptions.ResourceNotFoundException;
1010
import com.darwin.simplestore.repositories.ImageRepository;
1111
import com.darwin.simplestore.repositories.ProductRepository;
12+
import jakarta.transaction.Transactional;
1213
import lombok.RequiredArgsConstructor;
1314
import org.springframework.data.domain.Page;
1415
import org.springframework.data.domain.Pageable;
@@ -157,6 +158,7 @@ public void setImage(final Long productId, final Long imageId) throws ResourceNo
157158
* @return The image DTO
158159
* @throws ResourceNotFoundException If the product could not be found
159160
*/
161+
@Transactional
160162
public Optional<ImageDto> getImage(final Long productId) throws ResourceNotFoundException {
161163
final Product product = productRepository.findById(productId).orElseThrow(() -> new ResourceNotFoundException("Product with id " + productId + " does not exist"));
162164

src/test/java/com/darwin/simplestore/controllers/ImageControllerTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,4 @@ public void testUpdateImage() throws Exception {
7272
.content(objectMapper.writeValueAsString(new NewImageDto("base64"))))
7373
.andExpect(status().isOk());
7474
}
75-
76-
@Test
77-
public void testDeleteImage() throws Exception {
78-
mvc.perform(delete("/images/1"))
79-
.andExpect(status().isOk());
80-
}
8175
}

src/test/java/com/darwin/simplestore/services/ImageServiceTest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,4 @@ public void testUpdateImageException() {
9494

9595
verify(imageRepository, times(1)).existsById(anyLong());
9696
}
97-
98-
@Test
99-
public void testDeleteImage() {
100-
when(imageRepository.existsById(anyLong())).thenReturn(true);
101-
102-
assertDoesNotThrow(() -> imageService.deleteImage(image.getId()));
103-
104-
verify(imageRepository, times(1)).existsById(anyLong());
105-
verify(imageRepository, times(1)).deleteById(anyLong());
106-
}
107-
108-
@Test
109-
public void testDeleteImageException() {
110-
when(imageRepository.existsById(anyLong())).thenReturn(false);
111-
112-
assertThrowsExactly(ResourceNotFoundException.class, () -> imageService.deleteImage(image.getId()));
113-
114-
verify(imageRepository, times(1)).existsById(anyLong());
115-
}
11697
}

0 commit comments

Comments
 (0)