Skip to content

Commit

Permalink
feat: 게시글 저장 with 토큰
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Jul 31, 2024
1 parent f386061 commit 75d5ff9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -32,11 +34,17 @@ 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 {
String imgUrl = s3Service.upload(multipartFile, "post");
Long id = Long.parseLong(principal.getName());
String loginId = principal.getName();
System.out.println(loginId);

User user = userRepository.findByLoginId(loginId)
.orElseThrow(() -> new IllegalArgumentException("User not found with id = " + loginId));


Location location = locationRepository.findById(postSaveReqDto.locationId())
.orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postSaveReqDto.locationId()));
Expand All @@ -56,6 +64,7 @@ public void postSave(PostSaveReqDto postSaveReqDto, MultipartFile multipartFile,
.category(category)
.mood(mood)
.imgUrl(imgUrl)
.user(user)
.build();

postRepository.save(post);
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 Down Expand Up @@ -40,12 +41,12 @@ public class Post extends Time {

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(String title, String content, Location location, Integer time, Integer price, Category category, Mood mood, String imgUrl, 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 @@ -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,17 +37,16 @@ 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) {
public User(String loginId, String password, String nickname, Role role) {
validateNickname(nickname);
this.loginId = loginId;
this.password = password;
this.nickname = nickname;
this.role = role;
this.surveys = surveys;
}

private void validateNickname(String nickname) {
Expand Down

0 comments on commit 75d5ff9

Please sign in to comment.