diff --git a/src/main/java/net/skhu/likelion12thteam03be/global/jwt/JwtAuthorizationFilter.java b/src/main/java/net/skhu/likelion12thteam03be/global/jwt/JwtAuthorizationFilter.java index 1efb450..6379ace 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/global/jwt/JwtAuthorizationFilter.java +++ b/src/main/java/net/skhu/likelion12thteam03be/global/jwt/JwtAuthorizationFilter.java @@ -46,9 +46,10 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse private String resolveToken(HttpServletRequest request) { String token = request.getHeader(AUTHORIZATION_HEADER); + System.out.println("Jwt token : " + token); if (StringUtils.hasText(token) && token.startsWith(BEARER_PREFIX)) { - return token.substring(BEARER_PREFIX.length()); + return token.substring(BEARER_PREFIX.length()).trim(); } return null; diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java index ceedf9e..492b017 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java @@ -66,11 +66,11 @@ public ResponseEntity postFindByMoodId(@PathVariable("moodId") L } // 글 작성자별 조회(내 글 조회) - /*@GetMapping("/users/{userId}") + @GetMapping("/users/{userId}") public ResponseEntity postFindByUserId(@PathVariable("userId") Long userId) { PostListResDto postListResDto = postService.postFindByUserId(userId); return new ResponseEntity<>(postListResDto, HttpStatus.OK); - }*/ + } // 글 검색 조회 diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java index bf0817d..0c3dd1c 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java @@ -14,6 +14,10 @@ import net.skhu.likelion12thteam03be.post.domain.Post; import net.skhu.likelion12thteam03be.post.domain.repository.PostRepository; import net.skhu.likelion12thteam03be.s3.S3Service; +import net.skhu.likelion12thteam03be.user.domain.User; +import net.skhu.likelion12thteam03be.user.domain.repository.UserRepository; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -32,11 +36,28 @@ public class PostService { private final LocationRepository locationRepository; private final S3Service s3Service; private final MoodRepository moodRepository; + private final UserRepository userRepository; @Transactional public void postSave(PostSaveReqDto postSaveReqDto, MultipartFile multipartFile, Principal principal) throws IOException { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || !authentication.isAuthenticated()) { + throw new IllegalArgumentException("인증되지 않은 사용자입니다."); + } + String loginId = authentication.getName(); + System.out.println("In PostService : loginId = " + loginId); String imgUrl = s3Service.upload(multipartFile, "post"); - Long id = Long.parseLong(principal.getName()); +/* System.out.println("---------------------------"); + System.out.println(principal.getName()); + System.out.println("---------------------------");*/ +// String LoginId = principal.getName(); +// Long id = Long.parseLong(principal.getName()); + + /*User user = userRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 사용자가 존재하지 않습니다. id = " + id));*/ + + User user = userRepository.findByLoginId(loginId) + .orElseThrow(() -> new IllegalArgumentException("해당 사용자가 존재하지 않습니다. LoginId = " + loginId)); Location location = locationRepository.findById(postSaveReqDto.locationId()) .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postSaveReqDto.locationId())); @@ -48,6 +69,7 @@ public void postSave(PostSaveReqDto postSaveReqDto, MultipartFile multipartFile, .orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postSaveReqDto.moodId())); Post post = Post.builder() + .user(user) .title(postSaveReqDto.title()) .content(postSaveReqDto.content()) .location(location) diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java index 9505a60..068a2b6 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java @@ -9,6 +9,7 @@ import net.skhu.likelion12thteam03be.location.domain.Location; import net.skhu.likelion12thteam03be.mood.domain.Mood; import net.skhu.likelion12thteam03be.post.api.dto.request.PostUpdateReqDto; +import net.skhu.likelion12thteam03be.user.domain.User; @Entity @Getter @@ -17,35 +18,36 @@ public class Post extends Time { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "postId") + @Column(name = "post_id") private Long postId; private String title; // 제목 private String content; // 내용 @ManyToOne - @JoinColumn(name = "locationId") + @JoinColumn(name = "location_id") private Location location; // 거래 장소 private Integer time; // 거래 시간 private Integer price; // 가격 @ManyToOne - @JoinColumn(name = "categoryId") + @JoinColumn(name = "category_id") private Category category; @ManyToOne - @JoinColumn(name = "moodId") + @JoinColumn(name = "mood_id") private Mood mood; // 감정 키워드 private String imgUrl; // 사진 -/* @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") - private User user;*/ + private User user; @Builder - public Post(String title, String content, Location location, Integer time, Integer price, Category category, Mood mood, String imgUrl) { + public Post(User user, String title, String content, Location location, Integer time, Integer price, Category category, Mood mood, String imgUrl) { + this.user = user; this.title = title; this.content = content; this.location = location; diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java b/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java index d1f7f9c..5819426 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/domain/repository/PostRepository.java @@ -4,6 +4,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface PostRepository extends JpaRepository { } diff --git a/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java b/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java index 738d09a..0bc2153 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java +++ b/src/main/java/net/skhu/likelion12thteam03be/user/domain/User.java @@ -5,10 +5,12 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import net.skhu.likelion12thteam03be.post.domain.Post; import net.skhu.likelion12thteam03be.survey.domain.Survey; import net.skhu.likelion12thteam03be.user.exception.InvalidNickNameAddressException; import net.skhu.likelion12thteam03be.user.exception.InvalidUserException; +import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,8 +37,8 @@ public class User { @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List surveys; -/* @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) - private List posts = new ArrayList<>();*/ + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) + private List posts = new ArrayList<>(); @Builder public User(String loginId, String password, String nickname, Role role, List surveys) { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f541a71..d0925c2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,7 +9,7 @@ spring: jpa: database: mysql hibernate: - ddl-auto: create + ddl-auto: update show-sql: true properties: hibernate: