Skip to content

Commit

Permalink
Merge pull request #110 from HaegyeongKim01/week11-haegyeong
Browse files Browse the repository at this point in the history
refactor: 명함 수정 부분 리팩토링
  • Loading branch information
HaegyeongKim01 authored Nov 15, 2024
2 parents ccf3dfc + bf3d24a commit 1ca943a
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.devcard.devcard.auth.model.OauthMemberDetails;
import com.devcard.devcard.card.dto.CardResponseDto;
import com.devcard.devcard.card.dto.CardRequestDto;
import com.devcard.devcard.card.dto.CardUpdateDto;
import com.devcard.devcard.card.service.CardService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -61,11 +62,11 @@ public ResponseEntity<List<CardResponseDto>> getCards() {
@PutMapping("/{id}")
public ResponseEntity<CardResponseDto> updateCard(
@PathVariable Long id,
@Valid @RequestBody CardRequestDto cardRequestDto,
@Valid @RequestBody CardUpdateDto cardUpdateDto,
@AuthenticationPrincipal OauthMemberDetails oauthMemberDetails) {

Member member = oauthMemberDetails.getMember();
CardResponseDto responseDto = cardService.updateCard(id, cardRequestDto, member);
CardResponseDto responseDto = cardService.updateCard(id, cardUpdateDto, member);
return ResponseEntity.ok(responseDto);
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/devcard/devcard/card/dto/CardResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@ public class CardResponseDto {
private final String githubId;
private final String bio;
private final String profileImg;
private final String linkedin;
private final String notion;
private final String certification;
private final String extra;
private final boolean techStack;
private final boolean repository;
private final boolean contributions;
private final LocalDateTime createdAt;
private final LocalDateTime updatedAt;

public CardResponseDto(Long id, String name, String nickname, String company, String position, String email,
String phone, String githubId, String bio, String profileImg,
String linkedin, String notion, String certification, String extra,
boolean techStack, boolean repository, boolean contributions,
LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.name = name;
Expand All @@ -35,6 +44,13 @@ public CardResponseDto(Long id, String name, String nickname, String company, St
this.githubId = githubId;
this.bio = bio;
this.profileImg = profileImg;
this.linkedin = linkedin;
this.notion = notion;
this.certification = certification;
this.extra = extra;
this.techStack = techStack;
this.repository = repository;
this.contributions = contributions;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
Expand All @@ -49,6 +65,13 @@ public CardResponseDto(Long id, String name, String nickname, String company, St
public String getGithubId() { return githubId; }
public String getBio() { return bio; }
public String getProfileImg() { return profileImg; }
public String getLinkedin() { return linkedin; }
public String getNotion() { return notion; }
public String getCertification() { return certification; }
public String getExtra() { return extra; }
public boolean isTechStack() { return techStack; }
public boolean isRepository() { return repository; }
public boolean isContributions() { return contributions; }
public LocalDateTime getCreatedAt() { return createdAt; }
public LocalDateTime getUpdatedAt() { return updatedAt; }

Expand All @@ -65,6 +88,13 @@ public static CardResponseDto fromEntity(Card card) {
member.getGithubId(),
card.getBio(),
member.getProfileImg(),
card.getLinkedin(),
card.getNotion(),
card.getCertification(),
card.getExtra(),
card.isTechStack(),
card.isRepository(),
card.isContributions(),
card.getCreatedAt(),
card.getUpdatedAt()
);
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/devcard/devcard/card/dto/CardUpdateDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.devcard.devcard.card.dto;

public class CardUpdateDto {

private final String company;
private final String position;
private final String phone;
private final String bio;
private final String email;
private final String cardName;
private final String profileImg;
private final String linkedin;
private final String notion;
private final String certification;
private final String extra;
private final boolean techStack;
private final boolean repository;
private final boolean contributions;

public CardUpdateDto(String company, String position, String phone, String bio, String email, String cardName, String profileImg,
String linkedin, String notion, String certification, String extra,
boolean techStack, boolean repository, boolean contributions) {
this.company = company;
this.position = position;
this.phone = phone;
this.bio = bio;
this.email = email;
this.cardName = cardName;
this.profileImg = profileImg;
this.linkedin = linkedin;
this.notion = notion;
this.certification = certification;
this.extra = extra;
this.techStack = techStack;
this.repository = repository;
this.contributions = contributions;
}

public String getCompany() { return company; }
public String getPosition() { return position; }
public String getPhone() { return phone; }
public String getBio() { return bio; }
public String getEmail() { return email; }
public String getCardName() { return cardName; }
public String getProfileImg() { return profileImg; }
public String getLinkedin() { return linkedin; }
public String getNotion() { return notion; }
public String getCertification() { return certification; }
public String getExtra() { return extra; }
public boolean isTechStack() { return techStack; }
public boolean isRepository() { return repository; }
public boolean isContributions() { return contributions; }
}
88 changes: 81 additions & 7 deletions src/main/java/com/devcard/devcard/card/entity/Card.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.devcard.devcard.card.entity;

import com.devcard.devcard.auth.entity.Member;
import com.devcard.devcard.card.dto.CardRequestDto;
import com.devcard.devcard.card.dto.CardUpdateDto;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -29,6 +29,15 @@ public class Card {
private String position;
private String phone;
private String bio;
private String linkedin;
private String notion;
private String certification;
private String extra;

private boolean techStack;
private boolean repository;
private boolean contributions;

private LocalDateTime createdAt;
private LocalDateTime updatedAt;

Expand All @@ -46,11 +55,17 @@ private Card(Builder builder) {
this.position = builder.position;
this.phone = builder.phone;
this.bio = builder.bio;
this.linkedin = builder.linkedin;
this.notion = builder.notion;
this.certification = builder.certification;
this.extra = builder.extra;
this.techStack = builder.techStack;
this.repository = builder.repository;
this.contributions = builder.contributions;
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}


// 빌더 패턴을 위한 생성자
public static class Builder {
private final Member member;
Expand All @@ -61,6 +76,13 @@ public static class Builder {
private String position;
private String phone;
private String bio;
private String linkedin;
private String notion;
private String certification;
private String extra;
private boolean techStack;
private boolean repository;
private boolean contributions;

public Builder(Member member) {
this.member = member;
Expand Down Expand Up @@ -101,6 +123,41 @@ public Builder bio(String bio) {
return this;
}

public Builder linkedin(String linkedin) {
this.linkedin = linkedin;
return this;
}

public Builder notion(String notion) {
this.notion = notion;
return this;
}

public Builder certification(String certification) {
this.certification = certification;
return this;
}

public Builder extra(String extra) {
this.extra = extra;
return this;
}

public Builder techStack(boolean techStack) {
this.techStack = techStack;
return this;
}

public Builder repository(boolean repository) {
this.repository = repository;
return this;
}

public Builder contributions(boolean contributions) {
this.contributions = contributions;
return this;
}

public Card build() {
return new Card(this);
}
Expand All @@ -116,6 +173,13 @@ public Card build() {
public String getPosition() { return position; }
public String getPhone() { return phone; }
public String getBio() { return bio; }
public String getLinkedin() { return linkedin; }
public String getNotion() { return notion; }
public String getCertification() { return certification; }
public String getExtra() { return extra; }
public boolean isTechStack() { return techStack; }
public boolean isRepository() { return repository; }
public boolean isContributions() { return contributions; }
public LocalDateTime getCreatedAt() { return createdAt; }
public LocalDateTime getUpdatedAt() { return updatedAt; }
public List<Group> getGroups() { return groups; }
Expand All @@ -125,11 +189,21 @@ public void setGroups(List<Group> groups) {
}

// DTO 기반 업데이트 메서드
public void updateFromDto(CardRequestDto dto) {
if (dto.getCompany() != null) this.company = dto.getCompany();
if (dto.getPosition() != null) this.position = dto.getPosition();
if (dto.getPhone() != null) this.phone = dto.getPhone();
if (dto.getBio() != null) this.bio = dto.getBio();
public void updateFromDto(CardUpdateDto dto) {
this.company = dto.getCompany();
this.position = dto.getPosition();
this.phone = dto.getPhone();
this.bio = dto.getBio();
this.email = dto.getEmail();
this.nickname = dto.getCardName();
this.profileImg = dto.getProfileImg();
this.linkedin = dto.getLinkedin();
this.notion = dto.getNotion();
this.certification = dto.getCertification();
this.extra = dto.getExtra();
this.techStack = dto.isTechStack();
this.repository = dto.isRepository();
this.contributions = dto.isContributions();
this.updatedAt = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.devcard.devcard.auth.entity.Member;
import com.devcard.devcard.card.dto.CardRequestDto;
import com.devcard.devcard.card.dto.CardResponseDto;
import com.devcard.devcard.card.dto.CardUpdateDto;
import com.devcard.devcard.card.entity.Group;
import com.devcard.devcard.card.exception.CardNotFoundException;
import com.devcard.devcard.card.repository.CardRepository;
Expand Down Expand Up @@ -76,7 +77,7 @@ public List<CardResponseDto> getCards() {
}

@Transactional
public CardResponseDto updateCard(Long id, CardRequestDto cardRequestDto, Member member) {
public CardResponseDto updateCard(Long id, CardUpdateDto cardUpdateDto, Member member) {
Card existingCard = cardRepository.findById(id)
.orElseThrow(() -> new CardNotFoundException("해당 명함을 찾을 수 없습니다."));

Expand All @@ -86,7 +87,7 @@ public CardResponseDto updateCard(Long id, CardRequestDto cardRequestDto, Member
}

// 기존 엔티티의 필드 업데이트
existingCard.updateFromDto(cardRequestDto);
existingCard.updateFromDto(cardUpdateDto);

return CardResponseDto.fromEntity(existingCard);
}
Expand Down
Loading

0 comments on commit 1ca943a

Please sign in to comment.