Skip to content

Commit

Permalink
merge: 회원가입 기능 개발 후, 병합
Browse files Browse the repository at this point in the history
  • Loading branch information
singsangssong committed Jul 11, 2024
2 parents 946bc1d + ffac54f commit 411a516
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.tgwing.tech.blog.controller;

import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import kr.tgwing.tech.blog.exception.PathHasNoPostIdException;
import kr.tgwing.tech.blog.service.PostServiceImpl;
Expand Down Expand Up @@ -61,7 +62,7 @@ public ResponseEntity<PostDto> getPost(@PathVariable(required = false, name = "p
}

@PostMapping("/blog") // 블로그 작성 - POST, /tgwing.kr/blog/post
public ResponseEntity<PostDto> post(@RequestBody PostDto requestDto,
public ResponseEntity<PostDto> post(@RequestBody PostCreationDto requestDto,
Principal principal)
{
System.out.println("-- Post new post --");
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/kr/tgwing/tech/blog/dto/PostCreationDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.tgwing.tech.blog.dto;

import kr.tgwing.tech.blog.entity.PostEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class PostCreationDto {
private String title;
private String content;
private String thumbnail;

public static PostEntity toEntity(PostCreationDto dto) {
return PostEntity.builder()
.title(dto.title)
.content(dto.content)
.thumbnail(dto.thumbnail)
.build();
}

}
4 changes: 4 additions & 0 deletions src/main/java/kr/tgwing/tech/blog/entity/PostEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public void updateContent(PostDto postDto) {
this.content = postDto.getContent();
this.thumbnail = postDto.getThumbnail();
}

public void setWriter(Long writer) {
this.writer = writer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface PostRepository extends JpaRepository<PostEntity, Long> {
@Override
List<PostEntity> findAll();

List<PostEntity> findByWriter(Long id);

// List<PostEntity> findByTitleContains(String search);
// List<PostEntity> findByContentContains(String search);
Page<PostEntity> findAllByOrderByIdDesc(Pageable pageable);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/kr/tgwing/tech/blog/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.tgwing.tech.blog.service;

import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -9,7 +10,7 @@

public interface PostService {
public PostDto getPost(Long postId);
public PostDto createPost(PostDto requestDto, String token);
public PostDto createPost(PostCreationDto requestDto, String token);
public PostDto updatePost(PostDto postDto, Long postId, String utilStudentId);
public void deletePost(Long postId, String utilStudentId);
public Page<PostDto> getPostsInPage(String text, Pageable pageable);
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.tgwing.tech.blog.service;


import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import kr.tgwing.tech.blog.entity.PostEntity;
import kr.tgwing.tech.blog.exception.PostNotFoundException;
Expand Down Expand Up @@ -55,20 +56,20 @@ private Page<PostEntity> getAllPosts(PageRequest pageRequest) { // 모든 공지
}

@Override
public PostDto createPost(PostDto requestDto, String utilStudentId) // 공지 생성하기
public PostDto createPost(PostCreationDto requestDto, String utilStudentId) // 공지 생성하기
{
// 글을 작성할 user 조회
Optional<UserEntity> userById = userRepository.findById(requestDto.getWriter());
// // userEntity를 받아오지 못한 경우 - 회원을 찾을 수 없음 (Exception)
UserEntity userEntity = userById.orElseThrow(UserNotFoundException::new);
Optional<UserEntity> byStudentId = userRepository.findByStudentId(utilStudentId);
UserEntity userEntity = byStudentId.orElseThrow(UserNotFoundException::new);

// 현재 사용자와 요청 시 들어온 writer가 같은지 확인
if (!Objects.equals(userEntity.getStudentId(), utilStudentId)) {
throw new WrongPostRequestException();
}

// entity에는 저장된 썸네일의 uri만 추가로 넣어서 저장
PostEntity postEntity = toEntity(requestDto);
// entity에 작성자 Id 설정해서 저장하기
PostEntity postEntity = PostCreationDto.toEntity(requestDto);
postEntity.setWriter(userEntity.getId());
PostEntity savedEntity = postRepository.save(postEntity);

return toDto(savedEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import kr.tgwing.tech.blog.entity.PostEntity;
import kr.tgwing.tech.common.ApiResponse;
import kr.tgwing.tech.user.dto.profiledto.ProfileDTO;
import kr.tgwing.tech.user.dto.profiledto.ProfileReqDTO;
Expand All @@ -11,6 +12,7 @@
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -30,6 +32,16 @@ public ResponseEntity<ApiResponse<ProfileDTO>> showProfile(
return ResponseEntity.ok(ApiResponse.ok(profileDTO));
}

@Operation(summary = "내가 쓴 글 가져오기" )
@GetMapping("/mine")
public ResponseEntity<ApiResponse<List<PostEntity>>> showMyPost(Principal principal){
String studentId = principal.getName();
List<PostEntity> blog = userService.showMyBlog(studentId);

return ResponseEntity.ok(ApiResponse.ok(blog));

}

@Operation(summary = "유저 정보 수정 완료" )
@PutMapping("")
public ResponseEntity<ApiResponse<Long>> changeProfile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public interface UserRepository extends JpaRepository<UserEntity, Long> {
@Query("UPDATE UserEntity U SET U.name = :name, U.phoneNumber = :phoneNumber, U.profilePicture = :profilePicture WHERE U.studentId = :id")
void changeUser(String id, String name, String phoneNumber, String profilePicture);

@Query("SELECT U FROM UserEntity U WHERE U.studentId = :studentId")
UserEntity getEntity(String studentId);

@Query("SELECT u FROM UserEntity u WHERE u.role IS NULL")
List<UserEntity> findWaitingMember();
}

8 changes: 8 additions & 0 deletions src/main/java/kr/tgwing/tech/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package kr.tgwing.tech.user.service;


import kr.tgwing.tech.blog.entity.PostEntity;
import kr.tgwing.tech.user.dto.*;
import kr.tgwing.tech.user.dto.checkdto.CheckUserDTO;
import kr.tgwing.tech.user.dto.checkdto.PasswordCheckDTO;
import kr.tgwing.tech.user.dto.profiledto.ProfileDTO;
import kr.tgwing.tech.user.dto.profiledto.ProfileReqDTO;
import kr.tgwing.tech.user.dto.registerdto.EmailDto;
import kr.tgwing.tech.user.dto.registerdto.UserDTO;
import kr.tgwing.tech.user.entity.UserEntity;

import java.util.List;

public interface UserService{

Expand All @@ -19,8 +23,12 @@ public interface UserService{

Long removeUser(String name);

UserEntity getUserEntity(String studentId);

ProfileDTO showUser(String name);

List<PostEntity> showMyBlog(String studentId);

Boolean checkUser(CheckUserDTO checkUserDTO); // 본인 확인하기

String sendEmail(EmailMessageDTO emailMessage); // 메일로 인증번호 전송하기
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/kr/tgwing/tech/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import kr.tgwing.tech.blog.entity.PostEntity;
import kr.tgwing.tech.blog.repository.PostRepository;
import kr.tgwing.tech.user.dto.*;
import kr.tgwing.tech.user.dto.checkdto.CheckUserDTO;
import kr.tgwing.tech.user.dto.checkdto.PasswordCheckDTO;
import kr.tgwing.tech.user.dto.profiledto.ProfileDTO;
import kr.tgwing.tech.user.dto.profiledto.ProfileReqDTO;
import kr.tgwing.tech.user.dto.registerdto.UserDTO;
import kr.tgwing.tech.user.entity.User;
import kr.tgwing.tech.user.entity.UserEntity;
import kr.tgwing.tech.user.exception.MessageException;
import kr.tgwing.tech.user.exception.PasswordException;
Expand All @@ -23,13 +24,15 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

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

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

private final PostRepository postRepository;
private final JavaMailSender javaMailSender;
private final UserRepository userRepository;
private final SpringTemplateEngine templateEngine;
Expand Down Expand Up @@ -80,6 +83,11 @@ public Long removeUser(String studentId){
return null;
}

@Override
public UserEntity getUserEntity(String studentId) {
UserEntity getId = userRepository.getEntity(studentId);
return getId;
}


@Override
Expand All @@ -101,6 +109,16 @@ public ProfileDTO showUser(String studentId){
return profileDTO;
}

@Override
public List<PostEntity> showMyBlog(String studentId){
UserEntity id = userRepository.getEntity(studentId);
Long userId = id.getId();
System.out.println(userId);
List<PostEntity> myBlog = postRepository.findByWriter(userId);

return myBlog;
}

@Override
public Boolean checkUser(CheckUserDTO checkUserDTO) {
UserEntity user = userRepository.findByStudentId(checkUserDTO.getStudentId())
Expand Down

0 comments on commit 411a516

Please sign in to comment.