Skip to content

Commit

Permalink
feat: 동아리 정보 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
gorapang committed Dec 25, 2024
1 parent e21195b commit 47897e4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -56,4 +58,14 @@ public ResponseEntity<CommonResponse<String>> signupClub(@RequestBody ClubSignUp
.body(new CommonResponse<>(ErrorCode.CREATED, "회원가입이 완료되었습니다."));
}

@Operation(summary = "동아리 정보 수정 API", description = "동아리 정보를 수정.")
@PutMapping("/{clubId}")
public ResponseEntity<CommonResponse<String>> 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<>("동아리 정보가 수정되었습니다."));
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/likelion/innerjoin/user/service/ClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;


Expand Down Expand Up @@ -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);
}




}

0 comments on commit 47897e4

Please sign in to comment.