-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
348 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
favor/src/main/java/com/favor/favor/gift/GiftPhotoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.favor.favor.gift; | ||
|
||
import com.favor.favor.common.DefaultResponseDto; | ||
import com.favor.favor.photo.GiftPhoto; | ||
import com.favor.favor.photo.UserPhoto; | ||
import com.favor.favor.user.User; | ||
import com.favor.favor.user.UserPhotoService; | ||
import com.favor.favor.user.UserResponseDto; | ||
import com.favor.favor.user.UserService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
|
||
@Api(tags = "Gift-Photo") | ||
@RestController | ||
@RequestMapping("/giftphotos") | ||
@RequiredArgsConstructor | ||
public class GiftPhotoController { | ||
private final GiftPhotoService giftPhotoService; | ||
private final GiftService giftService; | ||
|
||
@ApiOperation(value = "선물 사진 추가") | ||
@ApiResponses(value={ | ||
@ApiResponse(code = 201, | ||
message = "GIFT_PHOTO_LIST_ADDED", | ||
response = UserResponseDto.class), | ||
@ApiResponse(code = 400, | ||
message = "FIELD_REQUIRED / *_CHARACTER_INVALID / *_LENGTH_INVALID"), | ||
@ApiResponse(code = 401, | ||
message = "UNAUTHORIZED_USER"), | ||
@ApiResponse(code = 404, | ||
message = "GIFT_NOT_FOUND"), | ||
@ApiResponse(code = 500, | ||
message = "SERVER_ERROR") | ||
}) | ||
@ResponseStatus(HttpStatus.CREATED) | ||
@PostMapping | ||
public ResponseEntity<DefaultResponseDto<Object>> addGiftPhotoList( | ||
@ModelAttribute MultipartFile file, | ||
Long giftNo | ||
) { | ||
giftService.isExistingGiftNo(giftNo); | ||
|
||
Gift gift = giftPhotoService.addGiftPhoto(giftNo, file); | ||
|
||
GiftResponseDto dto = giftService.returnDto(gift); | ||
|
||
return ResponseEntity.status(201) | ||
.body(DefaultResponseDto.builder() | ||
.responseCode("GIFT_PHOTO_LIST_ADDED") | ||
.responseMessage("선물 사진 추가") | ||
.data(dto) | ||
.build()); | ||
} | ||
|
||
@ApiOperation("선물 사진 목록 조회") | ||
@ApiResponses(value={ | ||
@ApiResponse(code = 200, | ||
message = "GIFT_PHOTO_LIST_FOUND", | ||
response = UserResponseDto.class), | ||
@ApiResponse(code = 401, | ||
message = "UNAUTHORIZED_USER"), | ||
@ApiResponse(code = 404, | ||
message = "GIFT_NOT_FOUND | FILE_NOT_FOUND"), | ||
@ApiResponse(code = 500, | ||
message = "SERVER_ERROR") | ||
}) | ||
@ResponseStatus(HttpStatus.OK) | ||
@Transactional | ||
@GetMapping | ||
public ResponseEntity<DefaultResponseDto<Object>> getUserProfilePhoto(Long giftNo) { | ||
Gift gift = giftService.findGiftByGiftNo(giftNo); | ||
|
||
List<GiftPhoto> dto = giftPhotoService.getGiftPhotoList(giftNo); | ||
|
||
return ResponseEntity.status(200) | ||
.body(DefaultResponseDto.builder() | ||
.responseCode("GIFT_PHOTO_LIST_FOUND") | ||
.responseMessage("선물 사진 목록 조회 완료") | ||
.data(dto) | ||
.build()); | ||
} | ||
|
||
@ApiOperation("선물 사진 삭제") | ||
@ApiResponses(value={ | ||
@ApiResponse(code = 200, | ||
message = "GIFT_PHOTO_DELETED", | ||
response = UserResponseDto.class), | ||
@ApiResponse(code = 401, | ||
message = "UNAUTHORIZED_USER"), | ||
@ApiResponse(code = 404, | ||
message = "GIFT_NOT_FOUND | FILE_NOT_FOUND"), | ||
@ApiResponse(code = 500, | ||
message = "SERVER_ERROR") | ||
}) | ||
@ResponseStatus(HttpStatus.OK) | ||
@Transactional | ||
@DeleteMapping | ||
public ResponseEntity<DefaultResponseDto<Object>> deleteUserProfilePhoto( | ||
Long giftNo, String fileUrl) { | ||
|
||
Gift gift = giftPhotoService.deleteGiftPhoto(giftNo, fileUrl); | ||
GiftResponseDto dto = new GiftResponseDto(gift); | ||
|
||
return ResponseEntity.status(200) | ||
.body(DefaultResponseDto.builder() | ||
.responseCode("GIFT_PHOTO_DELETED") | ||
.responseMessage("선물 사진 삭제 완료") | ||
.data(dto) | ||
.build()); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
favor/src/main/java/com/favor/favor/gift/GiftPhotoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.favor.favor.gift; | ||
|
||
import com.favor.favor.exception.CustomException; | ||
import com.favor.favor.photo.GiftPhoto; | ||
import com.favor.favor.photo.PhotoService; | ||
import com.favor.favor.user.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.transaction.Transactional; | ||
import java.net.URL; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static com.favor.favor.exception.ExceptionCode.FILE_NOT_FOUND; | ||
import static com.favor.favor.exception.ExceptionCode.SERVER_ERROR; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GiftPhotoService { | ||
private final GiftRepository giftRepository; | ||
private final GiftService giftService; | ||
private final PhotoService photoService; | ||
|
||
//선물 사진 목록에 사진 추가 | ||
@Transactional | ||
public Gift addGiftPhoto(Long giftNo, MultipartFile file){ | ||
Gift gift = giftService.findGiftByGiftNo(giftNo); | ||
List<GiftPhoto> giftPhotoList = gift.getGiftPhotoList(); | ||
|
||
String fileName = file.getOriginalFilename(); | ||
String storedFileName = getGiftFileName(fileName); | ||
|
||
String giftPhotoUrl = photoService.uploadFileToS3(storedFileName, file); | ||
|
||
try{ | ||
GiftPhoto giftPhoto = GiftPhoto.builder() | ||
.photoUrl(giftPhotoUrl) | ||
.build(); | ||
|
||
giftPhotoList.add(giftPhoto); | ||
|
||
}catch(RuntimeException e){ | ||
throw new CustomException(e, SERVER_ERROR); | ||
} | ||
|
||
// for(MultipartFile file : fileList){ | ||
// String fileName = file.getOriginalFilename(); | ||
// String storedFileName = getGiftFileName(fileName); | ||
// | ||
// String giftPhotoUrl = photoService.uploadFileToS3(storedFileName, file); | ||
// try{ | ||
// GiftPhoto giftPhoto = GiftPhoto.builder() | ||
// .photoUrl(giftPhotoUrl) | ||
// .build(); | ||
// | ||
// giftPhotoList.add(giftPhoto); | ||
// | ||
// }catch(RuntimeException e){ | ||
// throw new CustomException(e, SERVER_ERROR); | ||
// } | ||
// } | ||
gift.setGiftPhotoList(giftPhotoList); | ||
return giftRepository.save(gift); | ||
} | ||
|
||
//선물 사진 목록 조회 | ||
public List<GiftPhoto> getGiftPhotoList(Long giftNo){ | ||
return giftService.findGiftByGiftNo(giftNo).getGiftPhotoList(); | ||
} | ||
|
||
//선물 사진 삭제 | ||
public Gift deleteGiftPhoto(Long giftNo, String photoUrl){ | ||
Gift gift = giftService.findGiftByGiftNo(giftNo); | ||
List<GiftPhoto> giftPhotoList = gift.getGiftPhotoList(); | ||
|
||
GiftPhoto temp = null; | ||
|
||
for(GiftPhoto giftPhoto : giftPhotoList){ | ||
if(photoUrl.equals(giftPhoto.getPhotoUrl())){ | ||
temp = giftPhoto; | ||
String fileName = extractGiftPhotoFileName(photoUrl); | ||
photoService.deleteFileFromS3(fileName); | ||
break; | ||
} | ||
} | ||
giftPhotoList.remove(temp); | ||
|
||
return giftRepository.save(gift); | ||
} | ||
|
||
//선물 사진 이름 생성 | ||
public String getGiftFileName(String fileName){ | ||
return "gift/" + UUID.randomUUID()+ fileName.substring(fileName.lastIndexOf('.')); | ||
} | ||
|
||
//선물 사진 이름 추출 | ||
public static String extractGiftPhotoFileName(String photoUrl) { | ||
String fileName; | ||
try { | ||
fileName = photoUrl.substring(photoUrl.indexOf("gift/")); | ||
} catch (Exception e) { | ||
throw new CustomException(e, FILE_NOT_FOUND); | ||
} | ||
|
||
return fileName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.