|
6 | 6 | import org.springframework.transaction.annotation.Transactional;
|
7 | 7 | import org.springframework.web.multipart.MultipartFile;
|
8 | 8 |
|
9 |
| -import com.ajou.hertz.common.file.service.FileService; |
10 | 9 | import com.ajou.hertz.domain.instrument.dto.AcousticAndClassicGuitarDto;
|
11 | 10 | import com.ajou.hertz.domain.instrument.dto.AmplifierDto;
|
12 | 11 | import com.ajou.hertz.domain.instrument.dto.AudioEquipmentDto;
|
|
29 | 28 | import com.ajou.hertz.domain.instrument.entity.Instrument;
|
30 | 29 | import com.ajou.hertz.domain.instrument.entity.InstrumentHashtag;
|
31 | 30 | import com.ajou.hertz.domain.instrument.entity.InstrumentImage;
|
| 31 | +import com.ajou.hertz.domain.instrument.exception.InstrumentDeletePermissionDeniedException; |
32 | 32 | import com.ajou.hertz.domain.instrument.mapper.InstrumentMapper;
|
33 | 33 | import com.ajou.hertz.domain.instrument.repository.InstrumentHashtagRepository;
|
34 |
| -import com.ajou.hertz.domain.instrument.repository.InstrumentImageRepository; |
35 | 34 | import com.ajou.hertz.domain.instrument.repository.InstrumentRepository;
|
36 | 35 | import com.ajou.hertz.domain.instrument.strategy.AcousticAndClassicGuitarCreationStrategy;
|
37 | 36 | import com.ajou.hertz.domain.instrument.strategy.AmplifierCreationStrategy;
|
|
50 | 49 | @Service
|
51 | 50 | public class InstrumentCommandService {
|
52 | 51 |
|
53 |
| - private static final String INSTRUMENT_IMAGE_UPLOAD_PATH = "instrument-image/"; |
54 |
| - |
55 | 52 | private final UserQueryService userQueryService;
|
56 |
| - private final FileService fileService; |
| 53 | + private final InstrumentQueryService instrumentQueryService; |
| 54 | + private final InstrumentImageCommandService instrumentImageCommandService; |
57 | 55 | private final InstrumentRepository instrumentRepository;
|
58 | 56 | private final InstrumentHashtagRepository instrumentHashtagRepository;
|
59 |
| - private final InstrumentImageRepository instrumentImageRepository; |
60 | 57 |
|
61 | 58 | /**
|
62 | 59 | * 신규 일렉 기타 매물을 생성 및 저장한다.
|
@@ -137,6 +134,23 @@ public AudioEquipmentDto createNewAudioEquipment(Long sellerId, CreateNewAudioEq
|
137 | 134 | return InstrumentMapper.toAmplifierDto(audioEquipment);
|
138 | 135 | }
|
139 | 136 |
|
| 137 | + /** |
| 138 | + * 악기 매물을 삭제한다. |
| 139 | + * |
| 140 | + * @param userId 악기 매물을 삭제하려는 유저의 id |
| 141 | + * @param instrumentId 삭제할 악기 매물의 id |
| 142 | + * @throws InstrumentDeletePermissionDeniedException 악기를 삭제하려는 유저가 판매자가 아닌 경우 |
| 143 | + */ |
| 144 | + public void deleteInstrumentById(Long userId, Long instrumentId) { |
| 145 | + Instrument instrument = instrumentQueryService.getInstrumentById(instrumentId); |
| 146 | + if (!userId.equals(instrument.getSeller().getId())) { |
| 147 | + throw new InstrumentDeletePermissionDeniedException(); |
| 148 | + } |
| 149 | + instrumentImageCommandService.deleteAllByInstrumentId(instrumentId); |
| 150 | + instrumentHashtagRepository.deleteAllByInstrument(instrument); |
| 151 | + instrumentRepository.delete(instrument); |
| 152 | + } |
| 153 | + |
140 | 154 | /**
|
141 | 155 | * 신규 악기 매물을 생성하여 저장한다.
|
142 | 156 | *
|
@@ -164,16 +178,7 @@ private <T extends Instrument, U extends CreateNewInstrumentRequest<T>> T create
|
164 | 178 | * @param images image list
|
165 | 179 | */
|
166 | 180 | private void registerInstrumentImages(Instrument instrument, List<MultipartFile> images) {
|
167 |
| - List<InstrumentImage> instrumentImages = fileService |
168 |
| - .uploadFiles(images, INSTRUMENT_IMAGE_UPLOAD_PATH) |
169 |
| - .stream() |
170 |
| - .map(fileDto -> InstrumentImage.create( |
171 |
| - instrument, |
172 |
| - fileDto.getOriginalName(), |
173 |
| - fileDto.getStoredName(), |
174 |
| - fileDto.getUrl() |
175 |
| - )).toList(); |
176 |
| - List<InstrumentImage> savedInstrumentImages = instrumentImageRepository.saveAll(instrumentImages); |
| 181 | + List<InstrumentImage> savedInstrumentImages = instrumentImageCommandService.saveImages(instrument, images); |
177 | 182 | instrument.getImages().addAll(savedInstrumentImages);
|
178 | 183 | }
|
179 | 184 |
|
|
0 commit comments