Skip to content

Commit

Permalink
Merge pull request #9 from Kusitms-28th-Hackathon-D/feat/eckrin
Browse files Browse the repository at this point in the history
fix: namecard-keyword간 연관관계 수정, keyword리스트 가져오기 api 추가
  • Loading branch information
eckrin authored Oct 7, 2023
2 parents c8bbfc3 + bd10d25 commit 5929793
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.groupD.server.domain.dto.SignUpRequestDto;
import com.groupD.server.service.AuthService;
import io.swagger.annotations.ApiOperation;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/groupD/server/controller/NameCardController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.groupD.server.controller;

import com.groupD.server.domain.dto.EditNameCardRequestDto;
import com.groupD.server.domain.dto.GetKeywordListResponseDto;
import com.groupD.server.domain.dto.GetNameCardResponseDto;
import com.groupD.server.domain.dto.SignUpRequestDto;
import com.groupD.server.security.Auth;
import com.groupD.server.security.AuthInfo;
Expand All @@ -25,4 +27,18 @@ public class NameCardController {
public void editNameCard(@Auth AuthInfo authInfo, @Valid @RequestBody EditNameCardRequestDto dto) {
nameCardService.editNameCard(authInfo, dto);
}

@ApiOperation("명함 정보 가져오기")
@GetMapping
@ResponseStatus(HttpStatus.OK)
public GetNameCardResponseDto getNameCard(@Auth AuthInfo authInfo) {
return nameCardService.getNameCard(authInfo);
}

@ApiOperation("존재하는 모든 키워드 가져오기")
@GetMapping("/keywords")
@ResponseStatus(HttpStatus.OK)
public GetKeywordListResponseDto getAllKeywords() {
return nameCardService.getAllKeywords();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.groupD.server.controller.handler;

import com.groupD.server.domain.ErrorCode;
import com.groupD.server.domain.ErrorResponse;
import com.groupD.server.exception.member.MemberNotExistsException;
import com.groupD.server.exception.namecard.KeywordNotExistsException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class NamecardExceptionHandler {
@ExceptionHandler(KeywordNotExistsException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse<?> handleKeywordNotExistsException(KeywordNotExistsException e, HttpServletRequest request) {
log.warn("Namecard-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage());
return new ErrorResponse<>(ErrorCode.KEYWORD_NOT_EXISTS_ERROR);
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/groupD/server/domain/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public enum ErrorCode {
INVALID_REFRESH_TOKEN_ERROR(false, HttpStatus.BAD_REQUEST.value(), "RefreshToken 정보를 찾을 수 없습니다."),

//member
MEMBER_NOT_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 사용자입니다.");
MEMBER_NOT_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 사용자입니다."),
//namecard
KEYWORD_NOT_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 키워드입니다.");
;

private Boolean isSuccess;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.groupD.server.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GetKeywordListResponseDto {
private List<String> keywords;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.groupD.server.domain.dto;

import com.groupD.server.domain.Disability;
import com.groupD.server.domain.PreferJob;
import com.groupD.server.domain.Role;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GetNameCardResponseDto {
private String memberName;
private String disabilities;
private List<String> jobPriorities;
private List<String> keywords;
}
7 changes: 2 additions & 5 deletions src/main/java/com/groupD/server/domain/entity/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "keywords")
@Data
Expand All @@ -20,9 +22,4 @@ public class Keyword{

private String value;

// Keyword와 Namecard 간의 Many-to-One 관계
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "namecard_id")
private Namecard namecard;

}
31 changes: 31 additions & 0 deletions src/main/java/com/groupD/server/domain/entity/NameCardKeyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.groupD.server.domain.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "namecard_keyword")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NameCardKeyword {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "namecard_keyword_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "namecard_id")
private Namecard namecard;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "keyword_id")
private Keyword keyword;
}
8 changes: 0 additions & 8 deletions src/main/java/com/groupD/server/domain/entity/Namecard.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "namecards")
Expand All @@ -25,10 +23,4 @@ public class Namecard{
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

// Namecard와 Keyword 간의 One-to-Many 관계
@OneToMany(mappedBy = "namecard", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Keyword> keywords = new ArrayList<>();

// 다른 필드들을 추가할 수 있습니다.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.groupD.server.exception.namecard;

public class KeywordNotExistsException extends RuntimeException{
public KeywordNotExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
import com.groupD.server.domain.entity.Keyword;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface KeywordRepository extends JpaRepository<Keyword, Long> {
Optional<Keyword> findByValue(String value);
List<Keyword> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.groupD.server.repository;

import com.groupD.server.domain.entity.Member;
import com.groupD.server.domain.entity.NameCardKeyword;
import com.groupD.server.domain.entity.Namecard;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface NameCardKeywordRepository extends JpaRepository<NameCardKeyword, Long> {
public List<NameCardKeyword> findAllByNamecard(Namecard namecard);
public void deleteAllByNamecard(Namecard namecard);
}
42 changes: 40 additions & 2 deletions src/main/java/com/groupD/server/service/NameCardService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package com.groupD.server.service;

import com.groupD.server.domain.dto.EditNameCardRequestDto;
import com.groupD.server.domain.dto.GetKeywordListResponseDto;
import com.groupD.server.domain.dto.GetNameCardResponseDto;
import com.groupD.server.domain.entity.Keyword;
import com.groupD.server.domain.entity.Member;
import com.groupD.server.domain.entity.NameCardKeyword;
import com.groupD.server.domain.entity.Namecard;
import com.groupD.server.exception.member.MemberNotExistsException;
import com.groupD.server.exception.namecard.KeywordNotExistsException;
import com.groupD.server.repository.KeywordRepository;
import com.groupD.server.repository.MemberRepository;
import com.groupD.server.repository.NameCardKeywordRepository;
import com.groupD.server.repository.NameCardRepository;
import com.groupD.server.security.AuthInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -22,10 +31,11 @@ public class NameCardService {
private final NameCardRepository nameCardRepository;
private final MemberRepository memberRepository;
private final KeywordRepository keywordRepository;
private final NameCardKeywordRepository nameCardKeywordRepository;

@Transactional
public void createNameCard(Member member) {
nameCardRepository.save(new Namecard(null, member, null));
nameCardRepository.save(new Namecard(null, member));
}

@Transactional
Expand All @@ -35,8 +45,36 @@ public void editNameCard(AuthInfo authInfo, EditNameCardRequestDto dto) {
);
Namecard nameCard = nameCardRepository.findByMember(member);

nameCardKeywordRepository.deleteAllByNamecard(nameCard);
for(String desc: dto.getKeywords()) {
keywordRepository.save(new Keyword(null, desc, nameCard));
Keyword keyword = keywordRepository.findByValue(desc).orElseThrow(()-> new KeywordNotExistsException("키워드가 존재하지 않습니다"));
nameCardKeywordRepository.save(new NameCardKeyword(null, nameCard, keyword));
}
}

@Transactional
public GetNameCardResponseDto getNameCard(AuthInfo authInfo) {
Member member = memberRepository.findByEmail(
authInfo.getEmail()).orElseThrow(()-> new MemberNotExistsException("멤버가 존재하지 않습니다.")
);
Namecard namecard = nameCardRepository.findByMember(member);
List<String> jobPriorities = new ArrayList<>();
jobPriorities.add(member.getJobPriority1().toString());
jobPriorities.add(member.getJobPriority2().toString());
jobPriorities.add(member.getJobPriority3().toString());

List<NameCardKeyword> keywordList = nameCardKeywordRepository.findAllByNamecard(namecard);
List<String> keywords = new ArrayList<>();
for(NameCardKeyword nameCardKeyword: keywordList) {
keywords.add(nameCardKeyword.getKeyword().getValue());
}

return new GetNameCardResponseDto(member.getEmail(), member.getDisability().toString(), jobPriorities, keywords);
}


@Transactional
public GetKeywordListResponseDto getAllKeywords() {
return new GetKeywordListResponseDto(keywordRepository.findAll().stream().map(Keyword::toString).collect(Collectors.toList()));
}
}

0 comments on commit 5929793

Please sign in to comment.