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 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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ 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.
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package com.landvibe.landlog.controller;

import com.landvibe.landlog.domain.Member;
import com.landvibe.landlog.dto.LoginForm;
import com.landvibe.landlog.dto.MemberForm;
import com.landvibe.landlog.service.MemberService;
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 +31,62 @@ 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:/";
}
}

}
13 changes: 0 additions & 13 deletions src/main/java/com/landvibe/landlog/controller/MemberForm.java

This file was deleted.

28 changes: 28 additions & 0 deletions src/main/java/com/landvibe/landlog/controller/blogController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.landvibe.landlog.controller;

import com.landvibe.landlog.domain.Blog;
import com.landvibe.landlog.dto.AddBlogRequest;
import com.landvibe.landlog.dto.BlogForm;
import com.landvibe.landlog.service.BlogService;
import lombok.RequiredArgsConstructor;
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 java.util.List;

@Controller
@RequiredArgsConstructor
public class blogController {

private final BlogService blogService;

@GetMapping("/blogs")
public String blogList(@RequestParam(name = "name", required = false) String name, Model model) {
model.addAttribute("name", name);
return "blogs/blogList"; // 템플릿 파일의 이름
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/landvibe/landlog/domain/Blog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.landvibe.landlog.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Blog {
private Long creatorId;
private String title;
private String content;

public void update(String title, String content) {
this.title = title;
this.content = content;
}
}
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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/landvibe/landlog/dto/AddBlogRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.landvibe.landlog.dto;

import com.landvibe.landlog.domain.Blog;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AddBlogRequest {
private String creatorId;
private String title;
private String content;

}
12 changes: 12 additions & 0 deletions src/main/java/com/landvibe/landlog/dto/BlogForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.landvibe.landlog.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class BlogForm {
private Long creatorId;
private String title;
private String content;
}
11 changes: 11 additions & 0 deletions src/main/java/com/landvibe/landlog/dto/LoginForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.landvibe.landlog.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LoginForm {
private String email;
private String password;
}
12 changes: 12 additions & 0 deletions src/main/java/com/landvibe/landlog/dto/MemberForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.landvibe.landlog.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MemberForm {
private String name;
private String email;
private String password;
}
11 changes: 11 additions & 0 deletions src/main/java/com/landvibe/landlog/dto/UpdateBlogRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.landvibe.landlog.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UpdateBlogRequest {
private String title;
private String content;
}
20 changes: 20 additions & 0 deletions src/main/java/com/landvibe/landlog/repository/BlogRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.landvibe.landlog.repository;

import com.landvibe.landlog.domain.Blog;
import com.landvibe.landlog.domain.Member;
import org.springframework.stereotype.Repository;

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

public interface BlogRepository {

Blog save(Blog blog);

Optional<Blog> findById(Long id);

void deleteById(Long id);


List<Blog> findAll();
}
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
@@ -0,0 +1,39 @@
package com.landvibe.landlog.repository;

import com.landvibe.landlog.domain.Blog;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class MemoryBlogRepository implements BlogRepository {
private static Map<Long, Blog> store = new HashMap<>();
private static long sequence = 0L;

@Override
public Blog save(Blog blog) {
blog.setCreatorId(++sequence);
store.put(blog.getCreatorId(), blog);
return blog;
}

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

@Override
public List<Blog> findAll() {
return new ArrayList<>(store.values());
}

public void clearStore() {
store.clear();
}

@Override
public void deleteById(Long id) {
store.remove(id);
}

}
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
Loading