Skip to content

Commit

Permalink
feat : 동아리 생성 이미지
Browse files Browse the repository at this point in the history
  • Loading branch information
Changha-dev committed Nov 25, 2023
1 parent 24322b8 commit 13de2a7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.donggram.back.dto.ClubDto;
import com.donggram.back.dto.NewClubDto;
import com.donggram.back.dto.ProfileUpdateDto;
import com.donggram.back.dto.ResponseDto;
import com.donggram.back.entity.Club;
import com.donggram.back.jwt.JwtTokenProvider;
Expand All @@ -13,6 +14,7 @@
import org.springframework.data.web.PageableDefault;
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 @@ -62,9 +64,9 @@ public ResponseEntity getSelectedClubs(@RequestParam("collegeIds") List<Long> co
}

@PostMapping("/new")
public ResponseEntity postNewClub(@RequestBody NewClubDto newClubDto, @RequestHeader("Access_Token") String token){
public ResponseEntity postNewClub(@RequestHeader("Access_Token") String token, @RequestPart(value = "ImageClub") MultipartFile imageFile, @RequestPart(value = "newClubDto") NewClubDto newClubDto){

ResponseDto responseDto = clubService.postNewClub(newClubDto, token);
ResponseDto responseDto = clubService.postNewClub(newClubDto, imageFile, token);

return ResponseEntity.ok(responseDto);
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/donggram/back/entity/Club.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public class Club {
@Column(name = "CLUB_RECRUITMENT_PERIOD")
private String recruitment_period;

@OneToOne(mappedBy = "club")
ImageClub imageClub;

//일대다, 양방향
@JsonIgnore
@OneToMany(mappedBy = "club")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/donggram/back/entity/ClubRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class ClubRequest {
@JoinColumn(name = "member_id")
private Member member;

public void setImageClub(ImageClub imageClub) {
this.imageClub = imageClub;
}

public void updateStatus(RequestStatus status){
this.status = status;
}
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/com/donggram/back/entity/ImageClub.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ public class ImageClub {
@Column(nullable = false)
private String url;

@OneToOne
@JoinColumn(name = "club_id")
private Club club;

@OneToOne
@JoinColumn(name = "clubRequest_id")
private ClubRequest clubRequest;

@Builder
public ImageClub (String url, Club club){
public ImageClub (String url, ClubRequest clubRequest){
this.url = url;
this.club = club;
this.clubRequest = clubRequest;
}

public void setClubRequest(ClubRequest clubRequest) {
this.clubRequest = clubRequest;
}

public void uploadBasicImage(){
this.url = "https://image-profile-bucket.s3.ap-northeast-2.amazonaws.com/basic_profile.png";
}

public void uploadCustomImage(String image){
this.url = image;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/donggram/back/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public class Member extends BaseTimeEntity implements UserDetails {
@Column
String major2;

@OneToOne(mappedBy = "member")
@OneToOne(mappedBy = "member", orphanRemoval = true)
ImageProfile imageProfile;

@OneToOne
private ClubRequest clubRequest;

// 일대다, 양방향
@JsonIgnore
@OneToMany(mappedBy = "member")
@OneToMany(mappedBy = "member", orphanRemoval = true)
private final List<ClubJoin> clubJoinList = new ArrayList<>();

@ElementCollection(fetch = FetchType.EAGER)
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/com/donggram/back/service/ClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -221,20 +222,26 @@ public ResponseDto postClubJoin(Long clubId, String studentId){

@Transactional
@JsonIgnore
public ResponseDto postNewClub(NewClubDto newClubDto, String token){
public ResponseDto postNewClub(NewClubDto newClubDto, MultipartFile multipartFile, String token){

String studentId = jwtTokenProvider.getUserPk(token);
Member member = memberRepository.findByStudentId(studentId).orElseThrow(() -> new RuntimeException("해당 학번이 존재하지 않습니다."));



//동아리 중복 확인
String clubName = newClubDto.getClubName();
Optional<Club> byClubName = clubRepository.findByClubName(clubName);

if(byClubName.isPresent()){
throw new RuntimeException("이미 존재하는 동아리 입니다.");
}else{
ClubRequest clubRequest = ClubRequest.builder()



ImageClub imageClub = new ImageClub();

ClubRequest clubRequest = ClubRequest.builder()
.college(newClubDto.getCollege())
.division(newClubDto.getDivision())
.clubName(newClubDto.getClubName())
Expand All @@ -246,6 +253,19 @@ public ResponseDto postNewClub(NewClubDto newClubDto, String token){
.member(member)
.build();

if (multipartFile.isEmpty()){
imageClub.uploadBasicImage();
}else {

String s = uploadImage(multipartFile, clubRequest);
imageClub.uploadCustomImage(s);
}


clubRequest.setImageClub(imageClub);
imageClub.setClubRequest(clubRequest);
imageClubRepository.save(imageClub);

clubRequestRepository.save(clubRequest);

ClubRequestDto build = ClubRequestDto.builder()
Expand Down Expand Up @@ -283,23 +303,20 @@ public static String LocalDateTimeToString(){
return formattedDate;
}

private ImageClub uploadImage(MultipartFile file, Club club) {
private String uploadImage(MultipartFile file, ClubRequest clubRequest) {
try {
String imageFileName = "club_" + club.getId() + "_" + file.getOriginalFilename();
String imageFileName = "club_" + clubRequest.getId() + "_" + file.getOriginalFilename();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());

amazonS3Client.putObject(bucket, imageFileName, file.getInputStream(), metadata);

ImageClub image = ImageClub.builder()
.url("https://image-profile-bucket.s3.ap-northeast-2.amazonaws.com/" + imageFileName)
.club(club)
.build();
String imageUrl = "https://image-profile-bucket.s3.ap-northeast-2.amazonaws.com/" + imageFileName;

return imageUrl;

ImageClub save = imageClubRepository.save(image);
return save;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 13de2a7

Please sign in to comment.