Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
25 changes: 22 additions & 3 deletions src/main/java/com/example/springhw31/entity/User.java
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);
}
32 changes: 30 additions & 2 deletions src/main/java/com/example/springhw31/service/UserService.java
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) {
Comment on lines +22 to +23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

플젝에서는 @transactional 붙여주세용~

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() + "님, 환영합니다!!";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로그인 성공하면 {회원 닉네임}님, 환영합니다!! 라는 문자열 반환해주세용~
지금은 username님, 환영합니다!! 라는 문자열이 반환될거야

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했어!! 3-2도 거의 다해서 오늘 셤 보고 바로 올릴게..!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오키오키 셤 파이팅~~


}
return "아이디 및 비밀번호가 일치하지 않습니다.";
}

}
8 changes: 4 additions & 4 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
username: your_username
password: your_password
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: ssang0402@
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
Expand All @@ -12,4 +12,4 @@ spring:
properties:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
dialect: org.hibernate.dialect.MySQL8Dialect