Skip to content

Commit

Permalink
feat : socket, user 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
YASICJUNWOO committed Oct 15, 2023
1 parent ae242d1 commit d39dd4f
Show file tree
Hide file tree
Showing 40 changed files with 1,438 additions and 185 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ dependencies {

//테스트를 위한 의존
implementation 'com.google.code.gson:gson:2.8.7'

//jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
}

tasks.named('test') {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/KJW/MBTIcoummunity/Auth/AuthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package KJW.MBTIcoummunity.Auth;

import KJW.MBTIcoummunity.Auth.Dto.LoginDto;
import KJW.MBTIcoummunity.Auth.Service.LoginService;
import KJW.MBTIcoummunity.Security.JWT.TokenInfo;
import KJW.MBTIcoummunity.Auth.Dto.UserCreateDto;
import KJW.MBTIcoummunity.User.UserService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "http://localhost:3000")
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class AuthController {

private final Logger logger = LoggerFactory.getLogger(AuthController.class);

private final LoginService loginService;
private final UserService userService;

@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody LoginDto loginDto) {
System.out.println("loginDto = " + loginDto.getEmail()+loginDto.getPassword());
TokenInfo token = loginService.login(loginDto.getEmail(), loginDto.getPassword());
return (token!=null ? new ResponseEntity<>(token,HttpStatus.OK) : new ResponseEntity<>("없음",HttpStatus.BAD_REQUEST));

}
@PostMapping("/signup")
public ResponseEntity<?> postUser(@RequestBody UserCreateDto user) {
String username = userService.createUser(user);
return new ResponseEntity<>(username, HttpStatus.OK);
}


}
17 changes: 17 additions & 0 deletions src/main/java/KJW/MBTIcoummunity/Auth/Dto/LoginDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package KJW.MBTIcoummunity.Auth.Dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class LoginDto {

private String email;
private String password;

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package KJW.MBTIcoummunity.User.Dto;
package KJW.MBTIcoummunity.Auth.Dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/KJW/MBTIcoummunity/Auth/Service/LoginService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package KJW.MBTIcoummunity.Auth.Service;

import KJW.MBTIcoummunity.Security.JWT.JwtTokenProvider;
import KJW.MBTIcoummunity.Security.JWT.TokenInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service

@RequiredArgsConstructor
public class LoginService {

private final AuthenticationManagerBuilder authenticationManagerBuilder;
private final JwtTokenProvider jwtTokenProvider;

//org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
// 에러로 외부 treansactional 삭제해줌
public TokenInfo login(String email, String password) {

try{
//전달 받은 정보들을 통해 토큰 생성
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, password);
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);

TokenInfo token = jwtTokenProvider.generateToken(authentication);
return token;
}
catch (Exception e){
return null;
}


}


}
7 changes: 0 additions & 7 deletions src/main/java/KJW/MBTIcoummunity/Chat/ChatController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package KJW.MBTIcoummunity.Chat;

import KJW.MBTIcoummunity.Chat.Entity.Chat;
import KJW.MBTIcoummunity.Chat.Entity.ChatRequestDto;
import KJW.MBTIcoummunity.Chat.Repository.ChatRepository;
import KJW.MBTIcoummunity.Chat.Repository.ChatRoomRepository;
import KJW.MBTIcoummunity.User.UserService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequiredArgsConstructor
public class ChatController {
private final Logger logger = LoggerFactory.getLogger(ChatController.class);

private final SimpMessagingTemplate messagingTemplate;
private final ChatRepository chatRepository;
private final ChatRoomRepository chatRoomRepository;
private final ChatService chatService;

@MessageMapping("/chat")
public void message(@RequestBody ChatRequestDto dto) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/KJW/MBTIcoummunity/Config/WebSocketConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
Expand All @@ -12,6 +15,7 @@

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{

@Override
Expand All @@ -21,9 +25,17 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
.withSockJS();
}


@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/sub");
long[] heartbeat = {30000l, 30000l};

registry.enableSimpleBroker("/sub").setTaskScheduler(heartBeatScheduler()).setHeartbeatValue(heartbeat);
registry.setApplicationDestinationPrefixes("/pub");
}

@Bean
public TaskScheduler heartBeatScheduler(){
return new ThreadPoolTaskScheduler();
}
}
18 changes: 18 additions & 0 deletions src/main/java/KJW/MBTIcoummunity/Global/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package KJW.MBTIcoummunity.Global;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.UNAUTHORIZED;

@Getter
@AllArgsConstructor
public enum ErrorCode {

/* 401 UNAUTHORIZED : 인증되지 않은 사용자 */
NOT_EXIST_AUTH_TOKEN(UNAUTHORIZED, "권한 정보가 없는 토큰입니다");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package KJW.MBTIcoummunity.Post.Controller;

import KJW.MBTIcoummunity.Post.Dto.PostCreateDto;
import KJW.MBTIcoummunity.Post.Entity.Post;
import KJW.MBTIcoummunity.Post.Setvice.PostService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//http://mbti.ap-northeast-2.elasticbeanstalk.com/

@RestController
@AllArgsConstructor
@RequestMapping("/post")
public class PostController {

private final Logger logger = LoggerFactory.getLogger(PostController.class);

private final PostService service;

/**
* @methodName : getPost
* @param :
* @return :
* @Description:
* @note: newest와 popular구분해야함
**/
@GetMapping("/list/new")
public List<Post> getPost() {
return service.getEntity();
}

@GetMapping("/list/hot")
public List<Post> getHotPost() {
return service.getHotPost();
}

@PostMapping("/create")
public ResponseEntity<?> createEntity(@AuthenticationPrincipal UserDetails user, @RequestBody PostCreateDto dto) {
try {
return new ResponseEntity<>(service.createPost(user, dto), HttpStatus.CREATED);
} catch (IllegalArgumentException e) {
logger.error(e.getMessage());
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}

/**
* @methodName : likePost
* @param : postId
* @return : 졸아요가 +1 된 수
* @Description:
* @note: 이후 좋아요를 누른 사용자도 저장
**/
@PostMapping("/{postId}/like")
public ResponseEntity<?> likePost(@PathVariable("postId") Long postId){
System.out.println("PostController.likePost");
return new ResponseEntity<>( service.like(postId),HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package KJW.MBTIcoummunity.Post.Controller;

import KJW.MBTIcoummunity.Post.Dto.ReplyPostDto;
import KJW.MBTIcoummunity.Post.Dto.ReplyResponseDto;
import KJW.MBTIcoummunity.Post.Setvice.ReplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/reply")
@RequiredArgsConstructor
public class ReplyController {

private final ReplyService service;

@PostMapping
public ResponseEntity<?> createReply(@AuthenticationPrincipal UserDetails user, @RequestBody ReplyPostDto dto) {
service.createReply(user, dto);
return new ResponseEntity<>(ReplyResponseDto.toDto(dto.getContent(), user.getUsername()), HttpStatus.CREATED);
}

@GetMapping("/list")
public ResponseEntity<?> getReplyList(@RequestParam("postId") Long postId) {
return new ResponseEntity<>(service.getList(postId), HttpStatus.CREATED);
}
}
6 changes: 3 additions & 3 deletions src/main/java/KJW/MBTIcoummunity/Post/Dto/PostCreateDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Getter;

@Getter
public class PostCreateDto {
String title;
String content;
public class PostCreateDto {
String title;
String content;
}
18 changes: 18 additions & 0 deletions src/main/java/KJW/MBTIcoummunity/Post/Dto/ReplyPostDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package KJW.MBTIcoummunity.Post.Dto;

import lombok.Getter;

@Getter
public class ReplyPostDto {

private String content;
private Long postId;

@Override
public String toString() {
return "ReplyPostDto{" +
"content='" + content + '\'' +
", postId=" + postId +
'}';
}
}
19 changes: 19 additions & 0 deletions src/main/java/KJW/MBTIcoummunity/Post/Dto/ReplyResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package KJW.MBTIcoummunity.Post.Dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ReplyResponseDto {

private String content;
private String username;

public static ReplyResponseDto toDto(String content, String username) {
return ReplyResponseDto.builder()
.content(content)
.username(username)
.build();
}
}
Loading

0 comments on commit d39dd4f

Please sign in to comment.