-
Notifications
You must be signed in to change notification settings - Fork 2
3-1숙제 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daye200
wants to merge
2
commits into
main
Choose a base branch
from
daye
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
3-1숙제 #8
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 18 additions & 2 deletions
20
src/main/java/com/example/springhw31/controller/UserController.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,16 +1,32 @@ | ||
| package com.example.springhw31.controller; | ||
|
|
||
| import com.example.springhw31.dto.UserDto; | ||
| import com.example.springhw31.service.UserService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * 회원가입 메서드처럼 UserDto를 파라미터로 받아주세요 | ||
| */ | ||
|
|
||
| @RequiredArgsConstructor | ||
| @RestController | ||
| public class UserController { | ||
| private final UserService userService; | ||
|
|
||
|
|
||
| @PostMapping("/join") | ||
| public UserDto join(@ModelAttribute UserDto userDto) { | ||
|
|
||
| return userService.join(userDto); | ||
|
|
||
| } | ||
| @PostMapping("/login") | ||
| public String login(@ModelAttribute UserDto userDto){ | ||
|
|
||
| return userService.login(userDto); | ||
|
|
||
|
|
||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| package com.example.springhw31.entity; | ||
|
|
||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
| * TODO: 완성된 코드 아닙니다~ | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| @Entity | ||
| public class User { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| private Long id; | ||
|
|
||
| @Column(unique = true,nullable = false) | ||
| private String username; | ||
|
|
||
| @Column(nullable = false) | ||
| private String password; | ||
|
|
||
| @Column(unique = true,nullable = false) | ||
| private String nickname; | ||
| } |
9 changes: 8 additions & 1 deletion
9
src/main/java/com/example/springhw31/repository/UserRepository.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| package com.example.springhw31.repository; | ||
|
|
||
| public class UserRepository { | ||
| import com.example.springhw31.entity.User; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
|
|
||
| public interface UserRepository extends JpaRepository<User,Long> { | ||
| Boolean existsByUsername(String username); | ||
| Boolean existsByNickname(String nickname); | ||
| } |
48 changes: 47 additions & 1 deletion
48
src/main/java/com/example/springhw31/service/UserService.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,59 @@ | ||
| package com.example.springhw31.service; | ||
|
|
||
| import com.example.springhw31.dto.UserDto; | ||
| import com.example.springhw31.entity.User; | ||
| import com.example.springhw31.repository.UserRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| /** | ||
| * Controller에 반환할 때도 DTO 객체를 반환합니다. | ||
| */ | ||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class UserService { | ||
|
|
||
| private final UserRepository userRepository; | ||
| //회원가입 | ||
| public UserDto join(UserDto userDto) { | ||
| //중복된 경우 회원가입불가 | ||
| if (userRepository.existsByUsername(userDto.getUsername())) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "이미 존재하는 아이디입니다."); | ||
| } else if (userRepository.existsByNickname(userDto.getNickname())) { | ||
| throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "이미 존재하는 닉네임입니다."); | ||
| } | ||
|
|
||
| User user = new User(); | ||
| user.setPassword(userDto.getPassword()); | ||
| user.setNickname(userDto.getNickname()); | ||
| user.setPassword(userDto.getPassword()); | ||
| userRepository.save(user); | ||
|
|
||
|
|
||
|
|
||
| return userDto; | ||
|
|
||
| } | ||
| //로그인 | ||
|
|
||
| public String login(UserDto userDto){ | ||
| if (userRepository.existsByUsername(userDto.getUsername())&& | ||
| userRepository.existsByNickname(userDto.getNickname())) { | ||
|
Comment on lines
+40
to
+42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그인할때는 아이디랑 비밀번호만 받을텐데....ㅎㅎ |
||
| return userDto.getNickname()+" 님, 환영합니다!!"; | ||
| } else { | ||
|
|
||
| return "아이디 및 비밀번호가 일치하지 않습니다"; | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확실하게 눈으로 보고싶으면 코드 작성한 것 처럼 직접 throw new 사용해서 예외처리하면 되구,
지금 엔티티 코드에서 Column unique = true설정했으니까 20~25번째 줄 없어도 DB에 저장될 때 알아서 예외 던지기도 해요
음...추천하는 방식은 둘다 좋지만 눈으로 CustomException만들어서 한번 더 던지면 좋을 것 같네요 ㅎㅎ
지금 방법 good