Skip to content

Commit

Permalink
~
Browse files Browse the repository at this point in the history
  • Loading branch information
firefox1234123 committed Aug 1, 2024
1 parent 10bcbd4 commit c3fea5e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public ResponseEntity<PostListResDto> postFindByMoodId(@PathVariable("moodId") L
}

// 글 작성자별 조회(내 글 조회)
/*@GetMapping("/users/{userId}")
@GetMapping("/users/{userId}")
public ResponseEntity<PostListResDto> postFindByUserId(@PathVariable("userId") Long userId) {
PostListResDto postListResDto = postService.postFindByUserId(userId);
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}*/
}

// 글 검색 조회

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,8 +37,8 @@ public class User {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Survey> surveys;

/* @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();*/
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();

@Builder
public User(String loginId, String password, String nickname, Role role, List<Survey> surveys) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spring:
jpa:
database: mysql
hibernate:
ddl-auto: create
ddl-auto: update
show-sql: true
properties:
hibernate:
Expand Down

0 comments on commit c3fea5e

Please sign in to comment.