-
Notifications
You must be signed in to change notification settings - Fork 2
Sanghee #4
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
base: main
Are you sure you want to change the base?
Sanghee #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,31 @@ | ||
| package com.example.springhw31.controller; | ||
|
|
||
| import com.example.springhw31.dto.UserDto; | ||
| import com.example.springhw31.service.UserService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * 회원가입 메서드처럼 UserDto를 파라미터로 받아주세요 | ||
| */ | ||
|
|
||
| @RequestMapping("/user") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,33 @@ | ||
| package com.example.springhw31.entity; | ||
|
|
||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
| * TODO: 완성된 코드 아닙니다~ | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| @Entity | ||
| public class User { | ||
|
|
||
| private String username; | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| private Long id; | ||
|
|
||
| @Column(unique = true, nullable = false) | ||
| private String username; | ||
|
|
||
| @Column(nullable = false) | ||
| private String password; | ||
|
|
||
| private String password; | ||
| @Column(unique = true, nullable = false) | ||
| private String nickname; | ||
|
|
||
| private String nickname; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| 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; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
|
|
||
| @Repository | ||
| public interface UserRepository extends JpaRepository<User,Long> { | ||
|
|
||
| Optional<User> findByUsername(String username); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,42 @@ | ||
| 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.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Controller에 반환할 때도 DTO 객체를 반환합니다. | ||
| */ | ||
| @Service | ||
| @Transactional | ||
| @RequiredArgsConstructor | ||
| public class UserService { | ||
|
|
||
| public UserDto join(UserDto userDto) { | ||
| private final UserRepository userRepository; | ||
|
|
||
| public UserDto join(UserDto userDto) { | ||
| User newUser = new User(); | ||
| newUser.setUsername(userDto.getUsername()); | ||
| newUser.setPassword(userDto.getPassword()); | ||
| newUser.setNickname(userDto.getNickname()); | ||
|
|
||
| userRepository.save(newUser); | ||
| return userDto; | ||
| } | ||
|
|
||
| public String login(UserDto userDto) { | ||
| User user = userRepository.findByUsername(userDto.getUsername()).orElseThrow(); | ||
|
|
||
| if(user.getPassword().equals(userDto.getPassword())) | ||
| return userDto.getUsername() + "님, 환영합니다!!"; | ||
|
Member
Author
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. 로그인 성공하면 {회원 닉네임}님, 환영합니다!! 라는 문자열 반환해주세용~ 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. 수정했어!! 3-2도 거의 다해서 오늘 셤 보고 바로 올릴게..!!
Member
Author
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 "아이디 및 비밀번호가 일치하지 않습니다."; | ||
| } | ||
|
|
||
| } | ||
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.
플젝에서는 @transactional 붙여주세용~