diff --git a/build.gradle b/build.gradle index 9d34b33..53bc517 100644 --- a/build.gradle +++ b/build.gradle @@ -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') { diff --git a/build/classes/java/main/com/landvibe/landlog/controller/MemberController.class b/build/classes/java/main/com/landvibe/landlog/controller/MemberController.class index 6d0737d..b306676 100644 Binary files a/build/classes/java/main/com/landvibe/landlog/controller/MemberController.class and b/build/classes/java/main/com/landvibe/landlog/controller/MemberController.class differ diff --git a/build/classes/java/main/com/landvibe/landlog/controller/MemberForm.class b/build/classes/java/main/com/landvibe/landlog/controller/MemberForm.class deleted file mode 100644 index 10f5ce3..0000000 Binary files a/build/classes/java/main/com/landvibe/landlog/controller/MemberForm.class and /dev/null differ diff --git a/build/classes/java/main/com/landvibe/landlog/domain/Member.class b/build/classes/java/main/com/landvibe/landlog/domain/Member.class index 7b2fa53..d9b80f1 100644 Binary files a/build/classes/java/main/com/landvibe/landlog/domain/Member.class and b/build/classes/java/main/com/landvibe/landlog/domain/Member.class differ diff --git a/build/classes/java/main/com/landvibe/landlog/repository/MemberRepository.class b/build/classes/java/main/com/landvibe/landlog/repository/MemberRepository.class index e0954e0..56273ef 100644 Binary files a/build/classes/java/main/com/landvibe/landlog/repository/MemberRepository.class and b/build/classes/java/main/com/landvibe/landlog/repository/MemberRepository.class differ diff --git a/build/classes/java/main/com/landvibe/landlog/repository/MemoryMemberRepository.class b/build/classes/java/main/com/landvibe/landlog/repository/MemoryMemberRepository.class index ca2c90e..d5cf190 100644 Binary files a/build/classes/java/main/com/landvibe/landlog/repository/MemoryMemberRepository.class and b/build/classes/java/main/com/landvibe/landlog/repository/MemoryMemberRepository.class differ diff --git a/build/classes/java/main/com/landvibe/landlog/service/MemberService.class b/build/classes/java/main/com/landvibe/landlog/service/MemberService.class index 4020fe9..3fb0ad1 100644 Binary files a/build/classes/java/main/com/landvibe/landlog/service/MemberService.class and b/build/classes/java/main/com/landvibe/landlog/service/MemberService.class differ diff --git a/build/resources/main/templates/home.html b/build/resources/main/templates/home.html index 109d59d..15a4e6f 100644 --- a/build/resources/main/templates/home.html +++ b/build/resources/main/templates/home.html @@ -6,6 +6,7 @@

Hello Spring

회원 기능

+ 로그인 회원 가입 회원 목록

diff --git a/build/resources/main/templates/members/createMemberForm.html b/build/resources/main/templates/members/createMemberForm.html index c0353f2..9047652 100644 --- a/build/resources/main/templates/members/createMemberForm.html +++ b/build/resources/main/templates/members/createMemberForm.html @@ -7,6 +7,14 @@ +
+ + +
+
+ + +
diff --git a/build/tmp/compileJava/previous-compilation-data.bin b/build/tmp/compileJava/previous-compilation-data.bin index 1a81f91..d828b81 100644 Binary files a/build/tmp/compileJava/previous-compilation-data.bin and b/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/src/main/java/com/landvibe/landlog/controller/MemberController.java b/src/main/java/com/landvibe/landlog/controller/MemberController.java index dcc95a3..9f8e017 100644 --- a/src/main/java/com/landvibe/landlog/controller/MemberController.java +++ b/src/main/java/com/landvibe/landlog/controller/MemberController.java @@ -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 { @@ -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(); 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 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 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:/"; + } + } + } diff --git a/src/main/java/com/landvibe/landlog/controller/MemberForm.java b/src/main/java/com/landvibe/landlog/controller/MemberForm.java deleted file mode 100644 index 32d7a01..0000000 --- a/src/main/java/com/landvibe/landlog/controller/MemberForm.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.landvibe.landlog.controller; - -public class MemberForm { - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/main/java/com/landvibe/landlog/controller/blogController.java b/src/main/java/com/landvibe/landlog/controller/blogController.java new file mode 100644 index 0000000..0ec5ec4 --- /dev/null +++ b/src/main/java/com/landvibe/landlog/controller/blogController.java @@ -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"; // 템플릿 파일의 이름 + } +} diff --git a/src/main/java/com/landvibe/landlog/domain/Blog.java b/src/main/java/com/landvibe/landlog/domain/Blog.java new file mode 100644 index 0000000..571733b --- /dev/null +++ b/src/main/java/com/landvibe/landlog/domain/Blog.java @@ -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; + } +} diff --git a/src/main/java/com/landvibe/landlog/domain/Member.java b/src/main/java/com/landvibe/landlog/domain/Member.java index acadd35..bee6c43 100644 --- a/src/main/java/com/landvibe/landlog/domain/Member.java +++ b/src/main/java/com/landvibe/landlog/domain/Member.java @@ -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; - } } diff --git a/src/main/java/com/landvibe/landlog/dto/AddBlogRequest.java b/src/main/java/com/landvibe/landlog/dto/AddBlogRequest.java new file mode 100644 index 0000000..8b5612f --- /dev/null +++ b/src/main/java/com/landvibe/landlog/dto/AddBlogRequest.java @@ -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; + +} diff --git a/src/main/java/com/landvibe/landlog/dto/BlogForm.java b/src/main/java/com/landvibe/landlog/dto/BlogForm.java new file mode 100644 index 0000000..dd3da47 --- /dev/null +++ b/src/main/java/com/landvibe/landlog/dto/BlogForm.java @@ -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; +} diff --git a/src/main/java/com/landvibe/landlog/dto/LoginForm.java b/src/main/java/com/landvibe/landlog/dto/LoginForm.java new file mode 100644 index 0000000..6f6ecfe --- /dev/null +++ b/src/main/java/com/landvibe/landlog/dto/LoginForm.java @@ -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; +} diff --git a/src/main/java/com/landvibe/landlog/dto/MemberForm.java b/src/main/java/com/landvibe/landlog/dto/MemberForm.java new file mode 100644 index 0000000..276937f --- /dev/null +++ b/src/main/java/com/landvibe/landlog/dto/MemberForm.java @@ -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; +} diff --git a/src/main/java/com/landvibe/landlog/dto/UpdateBlogRequest.java b/src/main/java/com/landvibe/landlog/dto/UpdateBlogRequest.java new file mode 100644 index 0000000..c5b82a3 --- /dev/null +++ b/src/main/java/com/landvibe/landlog/dto/UpdateBlogRequest.java @@ -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; +} diff --git a/src/main/java/com/landvibe/landlog/repository/BlogRepository.java b/src/main/java/com/landvibe/landlog/repository/BlogRepository.java new file mode 100644 index 0000000..673c2c3 --- /dev/null +++ b/src/main/java/com/landvibe/landlog/repository/BlogRepository.java @@ -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 findById(Long id); + + void deleteById(Long id); + + + List findAll(); +} \ No newline at end of file diff --git a/src/main/java/com/landvibe/landlog/repository/MemberRepository.java b/src/main/java/com/landvibe/landlog/repository/MemberRepository.java index 0034fe9..eb4126c 100644 --- a/src/main/java/com/landvibe/landlog/repository/MemberRepository.java +++ b/src/main/java/com/landvibe/landlog/repository/MemberRepository.java @@ -13,5 +13,7 @@ public interface MemberRepository { Optional findByName(String name); + Optional findByEmail(String email); + List findAll(); } diff --git a/src/main/java/com/landvibe/landlog/repository/MemoryBlogRepository.java b/src/main/java/com/landvibe/landlog/repository/MemoryBlogRepository.java new file mode 100644 index 0000000..73d2d98 --- /dev/null +++ b/src/main/java/com/landvibe/landlog/repository/MemoryBlogRepository.java @@ -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 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 findById(Long id) { + return Optional.ofNullable(store.get(id)); + } + + @Override + public List findAll() { + return new ArrayList<>(store.values()); + } + + public void clearStore() { + store.clear(); + } + + @Override + public void deleteById(Long id) { + store.remove(id); + } + +} diff --git a/src/main/java/com/landvibe/landlog/repository/MemoryMemberRepository.java b/src/main/java/com/landvibe/landlog/repository/MemoryMemberRepository.java index d84e250..99cebbf 100644 --- a/src/main/java/com/landvibe/landlog/repository/MemoryMemberRepository.java +++ b/src/main/java/com/landvibe/landlog/repository/MemoryMemberRepository.java @@ -30,6 +30,13 @@ public Optional findByName(String name) { .findAny(); } + @Override + public Optional findByEmail(String email) { + return store.values().stream() + .filter(member -> member.getEmail().equals(email)) + .findAny(); + } + @Override public List findAll() { return new ArrayList<>(store.values()); diff --git a/src/main/java/com/landvibe/landlog/service/BlogService.java b/src/main/java/com/landvibe/landlog/service/BlogService.java new file mode 100644 index 0000000..2fa0e7b --- /dev/null +++ b/src/main/java/com/landvibe/landlog/service/BlogService.java @@ -0,0 +1,49 @@ +package com.landvibe.landlog.service; + +import com.landvibe.landlog.domain.Blog; +import com.landvibe.landlog.dto.AddBlogRequest; +import com.landvibe.landlog.dto.UpdateBlogRequest; +import com.landvibe.landlog.repository.BlogRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class BlogService { + + private final BlogRepository blogRepository; + + public Long save(Blog blog) { + blogRepository.save(blog); + return blog.getCreatorId(); + } + + // 블로그 글 목록 조회 메소드 + public List findAll() { + return blogRepository.findAll(); + } + + // 블로그 글 1개 조회 메소드 + public Blog findById(long id) { + return blogRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("not found : " + id)); + } + + // 블로그 글 삭제 메소드 + public void delete(long id) { + blogRepository.deleteById(id); + } + + // 블로그 글 수정 메소드 + public Blog update(long id, UpdateBlogRequest request) { + + Blog blog = blogRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("not found : " + id)); + + blog.update(request.getTitle(), request.getContent()); + + return blog; + } +} diff --git a/src/main/java/com/landvibe/landlog/service/MemberService.java b/src/main/java/com/landvibe/landlog/service/MemberService.java index c4ac876..5b83875 100644 --- a/src/main/java/com/landvibe/landlog/service/MemberService.java +++ b/src/main/java/com/landvibe/landlog/service/MemberService.java @@ -2,32 +2,45 @@ import com.landvibe.landlog.domain.Member; import com.landvibe.landlog.repository.MemberRepository; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service +@RequiredArgsConstructor public class MemberService { - private final MemberRepository memberRepository; - public MemberService(MemberRepository memberRepository) { - this.memberRepository = memberRepository; - } + private final MemberRepository memberRepository; public Long join(Member member) { - validateDuplicateMember(member); //중복 회원 검증 + + validateDuplicateMember(member); + memberRepository.save(member); return member.getId(); } private void validateDuplicateMember(Member member) { + validateDuplicateMemberEmail(member); + validateDuplicateMemberName(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 findMembers() { return memberRepository.findAll(); } @@ -35,4 +48,20 @@ public List findMembers() { public Optional findOne(Long memberId) { return memberRepository.findById(memberId); } + + // 이메일과 비밀번호를 검증하여 회원을 찾는 메서드 + public Optional login(String email, String password) { + Optional 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 반환 + } + } } diff --git a/src/main/resources/templates/blogs/blogList.html b/src/main/resources/templates/blogs/blogList.html new file mode 100644 index 0000000..63e5e5e --- /dev/null +++ b/src/main/resources/templates/blogs/blogList.html @@ -0,0 +1,37 @@ + + + +
+
+

의 블로그

+

+ 생성 +

+ + + + + + + + + + + + + + + + + + + +
#제목내용
수정 +
+ +
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/blogs/createBlogForm.html b/src/main/resources/templates/blogs/createBlogForm.html new file mode 100644 index 0000000..d12a998 --- /dev/null +++ b/src/main/resources/templates/blogs/createBlogForm.html @@ -0,0 +1,20 @@ + + + +
+

의 블로그

+
+ +
+ + +
+
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/blogs/updateBlogForm.html b/src/main/resources/templates/blogs/updateBlogForm.html new file mode 100644 index 0000000..3a15367 --- /dev/null +++ b/src/main/resources/templates/blogs/updateBlogForm.html @@ -0,0 +1,21 @@ + + + +
+

의 블로그

+
+ + +
+ + +
+
+ + +
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index 109d59d..15a4e6f 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -6,6 +6,7 @@

Hello Spring

회원 기능

+ 로그인 회원 가입 회원 목록

diff --git a/src/main/resources/templates/members/createMemberForm.html b/src/main/resources/templates/members/createMemberForm.html index c0353f2..9047652 100644 --- a/src/main/resources/templates/members/createMemberForm.html +++ b/src/main/resources/templates/members/createMemberForm.html @@ -7,6 +7,14 @@ +
+ + +
+
+ + +
diff --git a/src/main/resources/templates/members/login.html b/src/main/resources/templates/members/login.html new file mode 100644 index 0000000..a87daa1 --- /dev/null +++ b/src/main/resources/templates/members/login.html @@ -0,0 +1,22 @@ + + + +
+
+
+ + +
+
+ + +
+ +
+ +
+

+
+
+ + \ No newline at end of file