diff --git a/src/main/java/com/likelion/innerjoin/user/controller/ClubController.java b/src/main/java/com/likelion/innerjoin/user/controller/ClubController.java index decdada..05ea9e9 100644 --- a/src/main/java/com/likelion/innerjoin/user/controller/ClubController.java +++ b/src/main/java/com/likelion/innerjoin/user/controller/ClubController.java @@ -3,6 +3,7 @@ import com.likelion.innerjoin.common.exception.ErrorCode; import com.likelion.innerjoin.common.response.CommonResponse; import com.likelion.innerjoin.user.model.dto.request.ClubSignUpRequestDto; +import com.likelion.innerjoin.user.model.dto.request.ClubUpdateRequestDto; import com.likelion.innerjoin.user.model.dto.response.ClubCategoryResponseDto; import com.likelion.innerjoin.user.model.dto.request.EmailRequestDto; import com.likelion.innerjoin.user.model.dto.response.ClubResponseDto; @@ -14,6 +15,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -56,4 +58,14 @@ public ResponseEntity> signupClub(@RequestBody ClubSignUp .body(new CommonResponse<>(ErrorCode.CREATED, "회원가입이 완료되었습니다.")); } + @Operation(summary = "동아리 정보 수정 API", description = "동아리 정보를 수정.") + @PutMapping("/{clubId}") + public ResponseEntity> updateClubInfo(@PathVariable Long clubId, + @RequestPart("data") ClubUpdateRequestDto updateRequestDto, + @RequestPart(value = "image", required = false) MultipartFile image, + HttpSession session) { + clubService.updateClubInfo(clubId, updateRequestDto, image, session); + return ResponseEntity.ok(new CommonResponse<>("동아리 정보가 수정되었습니다.")); + } + } diff --git a/src/main/java/com/likelion/innerjoin/user/model/dto/request/ClubUpdateRequestDto.java b/src/main/java/com/likelion/innerjoin/user/model/dto/request/ClubUpdateRequestDto.java new file mode 100644 index 0000000..3cedd95 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/user/model/dto/request/ClubUpdateRequestDto.java @@ -0,0 +1,27 @@ +package com.likelion.innerjoin.user.model.dto.request; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class ClubUpdateRequestDto { + + private String name; + private String school; + private String email; + private String loginId; + private String password; + private Long categoryId; + + @Builder + public ClubUpdateRequestDto(String name, String school, String email, String loginId, String password, Long categoryId) { + this.name = name; + this.school = school; + this.email = email; + this.loginId = loginId; + this.password = password; + this.categoryId = categoryId; + } +} \ No newline at end of file diff --git a/src/main/java/com/likelion/innerjoin/user/service/ClubService.java b/src/main/java/com/likelion/innerjoin/user/service/ClubService.java index 08d27e4..c48d443 100644 --- a/src/main/java/com/likelion/innerjoin/user/service/ClubService.java +++ b/src/main/java/com/likelion/innerjoin/user/service/ClubService.java @@ -7,6 +7,7 @@ import com.likelion.innerjoin.post.model.entity.PostImage; import com.likelion.innerjoin.user.exception.SignUpIDException; import com.likelion.innerjoin.user.model.dto.request.ClubSignUpRequestDto; +import com.likelion.innerjoin.user.model.dto.request.ClubUpdateRequestDto; import com.likelion.innerjoin.user.model.dto.response.ClubCategoryResponseDto; import com.likelion.innerjoin.user.model.dto.response.ClubResponseDto; @@ -21,6 +22,7 @@ import com.likelion.innerjoin.user.repository.ClubRepository; import com.likelion.innerjoin.user.util.SessionVerifier; import jakarta.servlet.http.HttpSession; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -36,6 +38,7 @@ public class ClubService { private final ClubRepository clubRepository; private final ClubCategoryRepository clubCategoryRepository; private final SessionVerifier sessionVerifier; + private final BlobService blobService; //private final BlobService blobService; @@ -152,4 +155,52 @@ public void signupClub(ClubSignUpRequestDto requestDto) { clubRepository.save(club); } + + + @Transactional + public void updateClubInfo(Long clubId, ClubUpdateRequestDto updateRequestDto, MultipartFile image, HttpSession session) { + // 세션에서 클럽 정보 확인 + Club club = checkClub(session); + + // 요청된 clubId와 세션의 clubId 비교 + if (!club.getId().equals(clubId)) { + throw new UnauthorizedException("해당 동아리 접근 권한이 없습니다."); + } + + // 클럽 정보 업데이트 + club.setName(updateRequestDto.getName()); + club.setSchool(updateRequestDto.getSchool()); + club.setEmail(updateRequestDto.getEmail()); + club.setLoginId(updateRequestDto.getLoginId()); + club.setPassword(updateRequestDto.getPassword()); + + // 카테고리 설정 + if (updateRequestDto.getCategoryId() != null) { + ClubCategory category = clubCategoryRepository.findById(updateRequestDto.getCategoryId()) + .orElseThrow(() -> new IllegalArgumentException("Invalid category ID: " + updateRequestDto.getCategoryId())); + club.setCategory(category); + } + + // 기존 이미지 삭제 및 새로운 이미지 저장 + if (image != null && !image.isEmpty()) { + String oldImageUrl = club.getImageUrl(); + if (oldImageUrl != null) { + String oldFilename = oldImageUrl.substring(oldImageUrl.lastIndexOf('/') + 1); + blobService.deleteFile(oldFilename); + } + + try { + String newImageUrl = blobService.storeFile(image.getOriginalFilename(), image.getInputStream(), image.getSize()); + club.setImageUrl(newImageUrl); + } catch (IOException e) { + throw new ImageProcessingException("이미지 업로드 중 오류가 발생했습니다: " + e.getMessage(), e); + } + } + + clubRepository.save(club); + } + + + + }