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주차 과제] 박해원 과제 제출합니다. #17

Open
wants to merge 6 commits into
base: haewon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Binary file added .gradle/7.6.1/checksums/checksums.lock
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file added .gradle/7.6.1/fileChanges/last-build.bin
Binary file not shown.
Binary file added .gradle/7.6.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file added .gradle/7.6.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file not shown.
Empty file added .gradle/7.6.1/gc.properties
Empty file.
Binary file added .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
2 changes: 2 additions & 0 deletions .gradle/buildOutputCleanup/cache.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Thu Aug 15 22:04:20 KST 2024
gradle.version=7.6.1
Binary file added .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file added .gradle/file-system.probe
Binary file not shown.
Empty file added .gradle/vcs-1/gc.properties
Empty file.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.
10 changes: 10 additions & 0 deletions build/resources/main/templates/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>
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
7 changes: 7 additions & 0 deletions build/resources/main/templates/members/createMemberForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<div class="form-group">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
<br>
<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이메일을 입력하세요">
<br>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="비밀번호를 입력하세요">
<br>
</div>
<button type="submit">등록</button>
</form>
Expand Down
18 changes: 18 additions & 0 deletions build/resources/main/templates/members/loginForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/login" method="post">

<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이메일을 입력하세요">
<br>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="비밀번호를 입력하세요">
<br>

<button type="submit">로그인</button>
</form>
</div> <!-- /container -->
</body>
</html>
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Optional;

@Controller
public class MemberController {
Expand All @@ -27,6 +29,8 @@ public String create(MemberForm form) {
Member member = new Member();
member.setName(form.getName());
memberService.join(member);
member.setEmail(form.getEmail()); //추가
member.setPassword(form.getPassword());

Choose a reason for hiding this comment

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

setter를 지양하는게 어떨까요? 지금 상태로는 생성자만으로 충분히 요구사항을 지킬 수 있을것 같습니다!!

return "redirect:/";
}

Expand All @@ -36,4 +40,33 @@ public String list(Model model) {
model.addAttribute("members", members);
return "members/memberList";
}
//폼 데이터: 사용자가 HTML 폼을 통해 제출한 데이터, MemberForm은 사용자가 입력한 데이터를 담기위한 객체
//모델: 컨트롤러에서 처리된 결과를 뷰에 넘겨서 표시하기 위해 사용(사용자목록)
@PostMapping(value = "/members/login")
public String loginPost(MemberForm form, Model model){

Choose a reason for hiding this comment

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

이메일로 사용자를 찾고 비밀번호가 일치한지 판단하는 로직은 비즈니스 로직입니다.
일반적으로 service에서 비즈니스 로직을 수행합니다!!

String email = form.getEmail();
String password = form.getPassword();
Optional<Member> member = memberService.findByEmail(email);
if(member.isPresent()){ //비번일치-> 블로그페이지, 불일치->홈
if(member.get().getPassword().equals(password)){
return "redirect:/blogs?creatorId="+member.get().getId();
}
else{
return "redirect:/";
}
}
else{
return "redirect:/"; //로그인 실패
}
}
@GetMapping(value = "/members/login")
public String login(){
return "members/loginForm";
}
@GetMapping(value = "/blogs")
public String blog(@RequestParam("creatorId")Long Id,Model model){ //url에 적힌값 복사해서 변수에 넣기
Optional<Member> member = memberService.findOne(Id);
model.addAttribute("name",member.get().getName());
return "blogList";
}
}
13 changes: 11 additions & 2 deletions src/main/java/com/landvibe/landlog/controller/MemberForm.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.landvibe.landlog.controller;

public class MemberForm {
public class MemberForm { // 게터세터추가
private String name;

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

public void setName(String name) {
this.name = name;
}

public String getEmail(){return email;}

public void setEmail(String email){this.email = email;}

public String getPassword(){return password;}

public void setPassword(String password){this.password = password;}
}
22 changes: 21 additions & 1 deletion src/main/java/com/landvibe/landlog/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ public class Member {

private Long id;
private String name;
private String email;
private String password;

public Member() {
}

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

public Long getId() {
Expand All @@ -28,4 +32,20 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

Choose a reason for hiding this comment

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

말했듯 최대한 setter를 지양하고 왜 setter를 지양하는지 찾아보세요!!

public String getEmail() {
return email;
}

public void setEmail(String email){this.email=email;}

public String getPassword() {
return password;
}

public void setPassword(String password){this.password=password;}




}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface MemberRepository {

Optional<Member> findById(Long id);

Optional<Member> findByEmail(String email);

Choose a reason for hiding this comment

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

함수 추가 잘했어요!!

Copy link
Author

Choose a reason for hiding this comment

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

하하하 감사함니다


Optional<Member> findByName(String name);

List<Member> findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,43 @@
import org.springframework.stereotype.Repository;

import java.util.*;

//디비 사용하지않는 메모리 기반 저장소.
@Repository
public class MemoryMemberRepository implements MemberRepository {

private static Map<Long, Member> store = new HashMap<>();
private static long sequence = 0L;
private static Map<Long, Member> store = new HashMap<>(); // ID, 회원객체
private static long sequence = 0L; //회원ID자동증가를 위한 변수

@Override
public Member save(Member member) {
public Member save(Member member) { //새로운 회원 저장 sequence증가시켜 새로운 회원 ID생성
member.setId(++sequence);
store.put(member.getId(), member);
return member;
store.put(member.getId(), member); // 해당 ID를 member객체에 설정
return member;
}

@Override
public Optional<Member> findById(Long id) {
return Optional.ofNullable(store.get(id));
}

//주어진 ID에 해당하는 회원 찾기.
@Override
public Optional<Member> findByName(String name) {
public Optional<Member> findByName(String name) { //주어진 이름에 해당하는 회원 찾기
return store.values().stream()
.filter(member -> member.getName().equals(name))
.findAny();
}

@Override
public Optional<Member> findByEmail(String email){
return store.values().stream()
.filter(member -> member.getEmail().equals(email))
.findAny();
}
@Override
public List<Member> findAll() {
return new ArrayList<>(store.values());
}
} //저장된 모든회원 ArrayList로 변환하여 반환

public void clearStore() {
store.clear();
}
} //store에 저장된 모든 회원 데이터 삭제
}
5 changes: 3 additions & 2 deletions src/main/java/com/landvibe/landlog/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public Long join(Member member) {
public Long join(Member member) { //새로운 회원 등록+ 중복검증
validateDuplicateMember(member); //중복 회원 검증
memberRepository.save(member);
memberRepository.save(member); //회원객체저장역할
return member.getId();
}

Expand All @@ -32,6 +32,7 @@ public List<Member> findMembers() {
return memberRepository.findAll();
}

public Optional<Member> findByEmail(String email){return memberRepository.findByEmail(email);}
public Optional<Member> findOne(Long memberId) {
return memberRepository.findById(memberId);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/templates/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>
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
7 changes: 7 additions & 0 deletions src/main/resources/templates/members/createMemberForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<div class="form-group">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
<br>
<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이메일을 입력하세요">
<br>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="비밀번호를 입력하세요">
<br>
</div>
<button type="submit">등록</button>
</form>
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/templates/members/loginForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/login" method="post">

<label for="email">이메일</label>
<input type="email" id="email" name="email" placeholder="이메일을 입력하세요">
<br>
<label for="password">비밀번호</label>
<input type="password" id="password" name="password" placeholder="비밀번호를 입력하세요">
<br>

<button type="submit">로그인</button>
</form>
</div> <!-- /container -->
</body>
</html>