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

[6주차 과제] 정건우 과제 제출합니다. #4

Open
wants to merge 4 commits into
base: gunwoo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}

tasks.named('test') {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified build/classes/java/main/com/landvibe/landlog/domain/Member.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions build/resources/main/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<h1>Hello Spring</h1>
<p>회원 기능</p>
<p>
<a href="/members/login">로그인</a>
<a href="/members/new">회원 가입</a>
<a href="/members">회원 목록</a>
</p>
Expand Down
8 changes: 8 additions & 0 deletions build/resources/main/templates/members/createMemberForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
</div>
<div class="form-group">
<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이름을 입력하세요">
</div>
<div class="form-group">
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="이름을 입력하세요">
</div>
<button type="submit">등록</button>
</form>
</div> <!-- /container -->
Expand Down
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
11 changes: 11 additions & 0 deletions src/main/java/com/landvibe/landlog/controller/LoginForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.landvibe.landlog.controller;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LoginForm {
private String email;
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import com.landvibe.landlog.domain.Member;
import com.landvibe.landlog.service.MemberService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

@Controller
public class MemberController {
Expand All @@ -22,18 +30,66 @@ public String createForm() {
return "members/createMemberForm";
}

@GetMapping(value = "/members/login")
public String login() {
return "members/login";
}

@PostMapping(value = "/members/new")
public String create(MemberForm form) {
public String create(@ModelAttribute MemberForm form) {
// 멤버 폼 바인딩이 제대로 되었는지 확인
// MemberForm에 @Setter 가 없으니 바인딩이 되지 않음 -> ??
// @ModelAttribute 는 생략이 가능
System.out.println("Form Data: " + form.getName() + ", " + form.getEmail() + ", " + form.getPassword());

Member member = new Member();
jgw1202 marked this conversation as resolved.
Show resolved Hide resolved
member.setName(form.getName());
member.setEmail(form.getEmail());
member.setPassword(form.getPassword());

System.out.println("Member Data: " + member.getName() + ", " + member.getEmail() + ", " + member.getPassword());

memberService.join(member);
return "redirect:/";
}




@GetMapping(value = "/members")
public String list(Model model) {
List<Member> members = memberService.findMembers();
model.addAttribute("members", members);
return "members/memberList";
}

@PostMapping("/members/login")
public String login(@ModelAttribute LoginForm form, RedirectAttributes redirectAttributes) {

// 로그인 폼 바인딩 확인
System.out.println("Login Form Data : " + form.getEmail() + " " + form.getPassword());


Optional<Member> memberOpt = memberService.login(form.getEmail(), form.getPassword());

if (memberOpt.isPresent()) {
// 로그인 성공 시 /blogs 페이지로 리다이렉트
Member member = memberOpt.get();
String encodedName;
try {
encodedName = URLEncoder.encode(member.getName(), StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
encodedName = ""; // 기본 값 처리
}
return "redirect:/blogs?name=" + encodedName;
} else {
redirectAttributes.addFlashAttribute("error", "로그인 실패! 이메일 또는 비밀번호를 확인하세요.");
return "redirect:/";
}
}
@GetMapping("/blogs")
public String blogList(@RequestParam(name = "name", required = false) String name, Model model) {
model.addAttribute("name", name);
return "members/blogList"; // 템플릿 파일의 이름
}
}
15 changes: 7 additions & 8 deletions src/main/java/com/landvibe/landlog/controller/MemberForm.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.landvibe.landlog.controller;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MemberForm {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
private String email;
private String password;
}
29 changes: 8 additions & 21 deletions src/main/java/com/landvibe/landlog/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
package com.landvibe.landlog.domain;

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Member {

private Long id;
private String name;

public Member() {
}

public Member(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
private String email;

public String getName() {
return name;
}
private String password;

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface MemberRepository {

Optional<Member> findByName(String name);

Optional<Member> findByEmail(String email);

List<Member> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public Optional<Member> findByName(String name) {
.findAny();
}

@Override
public Optional<Member> findByEmail(String email) {
jgw1202 marked this conversation as resolved.
Show resolved Hide resolved
return store.values().stream()
.filter(member -> member.getEmail().equals(email))
.findAny();
}

@Override
public List<Member> findAll() {
return new ArrayList<>(store.values());
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/com/landvibe/landlog/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,47 @@ public MemberService(MemberRepository memberRepository) {
}

public Long join(Member member) {
validateDuplicateMember(member); //중복 회원 검증
validateDuplicateMemberName(member); //중복 회원 이름 검증
jgw1202 marked this conversation as resolved.
Show resolved Hide resolved
validateDuplicateMemberEmail(member); // 중복 회원 이메일 검증
memberRepository.save(member);
return member.getId();
}

private void validateDuplicateMember(Member member) {
private void validateDuplicateMemberName(Member member) {
memberRepository.findByName(member.getName())
.ifPresent(m -> {
throw new IllegalStateException("이미 존재하는 회원입니다.");
});
}

private void validateDuplicateMemberEmail(Member member) {
memberRepository.findByEmail(member.getEmail())
.ifPresent(m -> {
throw new IllegalStateException("이미 존재하는 이메일입니다.");
});
}

public List<Member> findMembers() {
return memberRepository.findAll();
}

public Optional<Member> findOne(Long memberId) {
return memberRepository.findById(memberId);
}

// 이메일과 비밀번호를 검증하여 회원을 찾는 메서드
public Optional<Member> login(String email, String password) {
Optional<Member> memberOpt = memberRepository.findByEmail(email);
if (memberOpt.isEmpty()) {
return Optional.empty(); // 이메일이 존재하지 않으면 빈 Optional 반환
}

Member member = memberOpt.get();
// 입력한 비밀번호와 저장된 비밀번호 비교 (단순 문자열 비교)
if (password.equals(member.getPassword())) {
return Optional.of(member); // 비밀번호가 일치하면 회원 반환
} else {
return Optional.empty(); // 비밀번호가 일치하지 않으면 빈 Optional 반환
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<h1>Hello Spring</h1>
<p>회원 기능</p>
<p>
<a href="/members/login">로그인</a>
<a href="/members/new">회원 가입</a>
<a href="/members">회원 목록</a>
</p>
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/templates/members/blogList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<div>
<h1><span th:text="${name}"/>의 블로그</h1>
</div>
</div> <!-- /container -->
</body>
</html>
8 changes: 8 additions & 0 deletions src/main/resources/templates/members/createMemberForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
</div>
<div class="form-group">
<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이름을 입력하세요">
</div>
<div class="form-group">
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="이름을 입력하세요">
</div>
<button type="submit">등록</button>
</form>
</div> <!-- /container -->
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/templates/members/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/login" method="post">
<div class="form-group">
<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이메일을 입력하세요">
</div>
<div class="form-group">
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="비밀번호를 입력하세요">
</div>
<button type="submit">로그인</button>
</form>
<!-- 오류 메시지 표시 -->
<div th:if="${error}">
<p th:text="${error}"></p>
</div>
</div> <!-- /container -->
</body>
</html>