Skip to content
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

feature: [회원 관리] 로그인 컴포넌트 #72

Merged
merged 16 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
4 changes: 2 additions & 2 deletions back/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.h2database:h2'
testImplementation 'io.rest-assured:rest-assured:3.3.0'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
compile 'com.h2database:h2'
}

test {
Expand Down
14 changes: 12 additions & 2 deletions back/src/main/java/sellerlee/back/article/domain/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

package sellerlee.back.article.domain;

import sellerlee.back.member.domain.Member;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

import javax.persistence.*;
import sellerlee.back.member.domain.Member;

@Entity
public class Article {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

package sellerlee.back.article.presentation;

import static sellerlee.back.article.presentation.ArticleController.*;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sellerlee.back.article.application.ArticleResponse;
import sellerlee.back.article.application.ArticleService;

import java.util.List;

import static sellerlee.back.article.presentation.ArticleController.ARTICLE_URI;

@RestController
@RequestMapping(ARTICLE_URI)
public class ArticleController {
Expand Down
21 changes: 0 additions & 21 deletions back/src/main/java/sellerlee/back/config/H2ServerConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @author lxxjn0
*/

package sellerlee.back.member.application;

public class MemberLoginRequest {
private String email;
private String password;

protected MemberLoginRequest() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

엔티티가 아니라면 굳이 no-arg constructor를 public이나 protected로 만들지 않아도 됩니다.
엔티티의 경우 JPA에서 리플렉션을 사용하는데 public으로 열기 싫어서 protected로 했던 것이고,
이 경우엔 기본 생성자를 막는 용도로 private이 더 적절해보여요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Request의 경우는 따로 리플랙션으로 값을 저장하는 것이 아닌가요?? 그 부분을 잘 몰라서 이런 방식으로 했는데 상관없으면 private로 바꾸겠습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

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

request dto엔 jpa가 관여하지 않죠. 또한 리플렉션을 통해 객체를 만들어낼 일도 없습니다.
따라서 본인을 포함한 일반적인 사용자(개발자)들이 사용하지 않도록 private으로 변경하는 것이 좋을것같아요.

}

public MemberLoginRequest(String email, String password) {
this.email = email;
this.password = password;
}

public String getEmail() {
return email;
}

public String getPassword() {
return password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @author lxxjn0
*/

package sellerlee.back.member.application;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sellerlee.back.member.domain.IllegalMemberLoginException;
import sellerlee.back.member.domain.Member;
import sellerlee.back.member.domain.MemberRepository;

@Service
public class MemberService {
private final MemberRepository memberRepository;

public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

@Transactional(readOnly = true)
public void login(MemberLoginRequest request) {
Member findMember = memberRepository.findMemberByEmail(request.getEmail())
.orElseThrow(() -> new IllegalMemberLoginException("이메일이 일치하는 회원이 존재하지 않습니다."));

if (!findMember.verify(request.getPassword())) {
throw new IllegalMemberLoginException("비밀번호가 일치하지 않습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @author lxxjn0
*/

package sellerlee.back.member.domain;

public class IllegalMemberLoginException extends IllegalArgumentException {
public IllegalMemberLoginException(String s) {
super(s);
}
}
40 changes: 38 additions & 2 deletions back/src/main/java/sellerlee/back/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @author kouz95
* @author lxxjn0
*/

package sellerlee.back.member.domain;
Expand All @@ -8,6 +8,7 @@
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

@Entity
public class Member {
Expand All @@ -16,10 +17,45 @@ public class Member {
@Column(name = "member_id")
private Long id;

@NotNull
private String email;

@NotNull
private String password;

private Double score;

protected Member() {
}

public Member(Long id) {
public Member(Long id, String email, String password, Double score) {
this.id = id;
this.email = email;
this.password = password;
this.score = score;
}

public Member(String email, String password, Double score) {
this(null, email, password, score);
}

public boolean verify(String password) {
return this.password.equals(password);
}

public Long getId() {
return id;
}

public String getEmail() {
return email;
}

public String getPassword() {
return password;
}

public Double getScore() {
return score;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @author lxxjn0
*/

package sellerlee.back.member.domain;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findMemberByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sellerlee.back.member.presentation;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import sellerlee.back.member.domain.IllegalMemberLoginException;

@ControllerAdvice
public class LoginAdviceController {
@ExceptionHandler(IllegalMemberLoginException.class)
public ResponseEntity<String> handleIllegalMemberLoginException(IllegalMemberLoginException e) {
return ResponseEntity
.badRequest()
.body(e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @author lxxjn0
*/

package sellerlee.back.member.presentation;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import sellerlee.back.member.application.MemberLoginRequest;
import sellerlee.back.member.application.MemberService;

@RestController
public class MemberController {
private final MemberService memberService;

public MemberController(MemberService memberService) {
this.memberService = memberService;
}

@PostMapping("/login")
public ResponseEntity<Void> login(@RequestBody MemberLoginRequest request) {
memberService.login(request);

return ResponseEntity
.ok()
.build();
}
}
21 changes: 15 additions & 6 deletions back/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ spring:
profiles:
active: local

logging:
level:
org.hibernate.type.descriptor.sql: trace
org.springframework.web: debug

---

spring:
profiles: local

jpa:
database-platform: org.hibernate.dialect.H2Dialect
database: H2
Expand All @@ -10,16 +20,15 @@ spring:
hibernate:
ddl-auto: create
properties:
hibernate.format-sql: true
hibernate.format_sql: true
use_sql_comments: true

h2:
console:
enabled: true

datasource:
url: jdbc:h2:mem:testdb;IFEXISTS=FALSE;
url: jdbc:h2:tcp://localhost/${user.dir}/test;IFEXISTS=FALSE;
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

username: sa
password:
driver-class-name: org.h2.Driver

logging:
level:
org.hibernate.type.descriptor.sql: trace
11 changes: 5 additions & 6 deletions back/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
* @author kouz95
*/

insert into member (member_id)
values (1);
insert into member (member_id, email, password, score)
values (1, 'sellerlee@gmail.com', '1234', 4.5),
(2, 'lxxjn0@gmail.com', '0000', 5.0);

insert into article (article_id, category, contents, price, title, member_id)
values (1, 'TEST', 'test contents1', 1234, 'test title1', 1);

insert into article (article_id, category, contents, price, title, member_id)
values (2, 'TEST', 'test contents2', 5678, 'test title2', 1);
values (1, 'TEST', 'test contents1', 1234, 'test title1', 1),
(2, 'TEST', 'test contents2', 5678, 'test title2', 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍


insert into tag (article_id, name)
values (1, 'test tag1'),
Expand Down
Loading