-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae242d1
commit d39dd4f
Showing
40 changed files
with
1,438 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...BTIcoummunity/User/Dto/UserCreateDto.java → ...BTIcoummunity/Auth/Dto/UserCreateDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/KJW/MBTIcoummunity/Auth/Service/LoginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/KJW/MBTIcoummunity/Post/Controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/KJW/MBTIcoummunity/Post/Controller/ReplyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/KJW/MBTIcoummunity/Post/Dto/ReplyPostDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/KJW/MBTIcoummunity/Post/Dto/ReplyResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.