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

이상윤 API 구축 코드 #9

Open
wants to merge 2 commits into
base: main
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
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ensure consistent line endings
* text=auto

# Define LF for specific file types
*.java text eol=lf
*.properties text eol=lf
*.md text eol=lf
*.gradle text eol=lf
13 changes: 13 additions & 0 deletions src/main/java/PKNU/first_spring/FirstSpringApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package PKNU.first_spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FirstSpringApplication {

public static void main(String[] args) {
SpringApplication.run(FirstSpringApplication.class, args);
}

}
26 changes: 26 additions & 0 deletions src/main/java/PKNU/first_spring/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package PKNU.first_spring;

import PKNU.first_spring.repository.MemberRepository;
import PKNU.first_spring.repository.MemoryMemberRepository;
import PKNU.first_spring.service.MemberService;
import PKNU.first_spring.service.QuizService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {
@Bean
public MemberService memberService() {
return new MemberService(memberRepository());
}

@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository(); //이후 DbMemberRepository로 바뀔 예정
}

@Bean
public QuizService quizService() {
return new QuizService();
}
}
13 changes: 13 additions & 0 deletions src/main/java/PKNU/first_spring/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package PKNU.first_spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

@GetMapping("/")
public String home() {
return "home";
}
}
62 changes: 62 additions & 0 deletions src/main/java/PKNU/first_spring/controller/MemberController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package PKNU.first_spring.controller;

import PKNU.first_spring.domain.Member;
import PKNU.first_spring.repository.MemberRepository;
import PKNU.first_spring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MemberController {
private final MemberService memberService;
private final MemberRepository memberRepository;

@Autowired
Copy link
Member

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.

생성자가 한 개인 경우 생략이 가능하군요! 감사합니다!

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

@GetMapping("/members/new")
public String createForm() {
Copy link
Contributor

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.

api 경로 수정했습니다!

return "members/createMemberForm";
}

@PostMapping("/members/new")
public String createMember(MemberForm form) {
Member member = new Member();
member.setName(form.getName());

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

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


@GetMapping("/members/json")
@ResponseBody
public List<Member> listMembersJson() {
return memberService.findMembers();
}

@GetMapping("/members/delete")
public String delete(){
return "/members/deleteMemberForm";
}

@PostMapping("/members/delete")
Copy link
Member

Choose a reason for hiding this comment

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

지우는거라면 다른 Mapping이 있지않을까요?

Copy link
Author

Choose a reason for hiding this comment

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

html폼이 delete 메소드를 전송을 못해서 일단 저렇게 했었는데 찾아보니 다른 방법이 있어 수정했습니다. 변경해서 노션에 정리해놓겠습니다!

public String delete(MemberForm form){
memberService.delete(form.getName());
return "redirect:/";
}
}
13 changes: 13 additions & 0 deletions src/main/java/PKNU/first_spring/controller/MemberForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package PKNU.first_spring.controller;

public class MemberForm {
Copy link
Member

Choose a reason for hiding this comment

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

dto로 사용한다면, 차라리 Record를 사용하는게 어떨까요?!

Copy link
Author

Choose a reason for hiding this comment

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

헉 Record 쓰니까 이렇게까지 줄어드네요 감사합니다! 변경 후 노션에 정리해놓겠습니다

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
34 changes: 34 additions & 0 deletions src/main/java/PKNU/first_spring/controller/QuizController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package PKNU.first_spring.controller;

import PKNU.first_spring.service.QuizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Objects;

@Controller
public class QuizController {
private final QuizService quizService;

@Autowired
public QuizController(QuizService quizService){
this.quizService = quizService;
}

@GetMapping("/quiz")
public String quiz(){
return "quiz/quizForm";
}

@PostMapping("/quiz")
public String quiz(@RequestParam("answer") String answer) {
if (quizService.isCorrect(answer)){
return "quiz/correctForm";
} else {
return "quiz/incorrectForm";
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/PKNU/first_spring/domain/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package PKNU.first_spring.domain;

public class Member {
private Long id;
private String name;

public String getName() {
return name;
}

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

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}
13 changes: 13 additions & 0 deletions src/main/java/PKNU/first_spring/repository/MemberRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package PKNU.first_spring.repository;

import PKNU.first_spring.domain.Member;

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

public interface MemberRepository {
Copy link
Contributor

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.

감사합니다 :)

Member save(Member member);
void deleteMember(Member member);
Optional<Member> findByName(String name);
List<Member> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package PKNU.first_spring.repository;

import PKNU.first_spring.domain.Member;

import java.util.*;

//@Repository
public class MemoryMemberRepository implements MemberRepository {

public static Map<Long, Member> store = new HashMap<>();
private static long sequence = 0L;

@Override
public Member save(Member member) {
member.setId(++sequence);
store.put(member.getId(), member);
return member;
}

@Override
public void deleteMember(Member member) {
store.remove(member.getId());
}

@Override
public Optional<Member> findByName(String name) {
return store.values().stream()
.filter(member -> member.getName().equals(name))
.findAny();
Copy link
Member

Choose a reason for hiding this comment

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

직접 꺼내는것보다.. member,sameName() 으로 바꿀 수 있지 않을까요?

Copy link
Author

Choose a reason for hiding this comment

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

아하 이렇게 해서 캡슐화를.. 변경해서 노션에 정리해놓겠습니다! 감사합니다!

}

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

public void clearStore(){
store.clear();
}
}
32 changes: 32 additions & 0 deletions src/main/java/PKNU/first_spring/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package PKNU.first_spring.service;

import PKNU.first_spring.domain.Member;
import PKNU.first_spring.repository.MemberRepository;
import PKNU.first_spring.repository.MemoryMemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

public class MemberService {
Copy link
Contributor

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.

감사합니다!

private final MemberRepository memberRepository;

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

public Long join(Member member) {
memberRepository.save(member);
return member.getId();
}

public void delete(String name){
Member member = memberRepository.findByName(name).get();
memberRepository.deleteMember(member);
}

public List<Member> findMembers() {
return memberRepository.findAll();
}
}
9 changes: 9 additions & 0 deletions src/main/java/PKNU/first_spring/service/QuizService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package PKNU.first_spring.service;

import java.util.Objects;

public class QuizService {
public boolean isCorrect(String answer) {
return answer == "한화 이글스" || answer == "Hanwha Eagles";
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=first-spring
Binary file added src/main/resources/static/수리2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/수리짤.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/수리짤2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<div>
<h1>First Spring</h1>
<p>회원 기능</p>
<p>
<a href="/members/new">회원 가입</a>
<a href="/members">회원 목록</a>
<a href="/members/json">회원 목록(Json)</a>
<a href="/members/delete">회원 삭제</a>
</p>
<h1>QUIZ!</h1>
<p><a href="/quiz"> Simple Quiz</a></p>
</div>
</div> <!-- /container -->
</body>
</html>
13 changes: 13 additions & 0 deletions src/main/resources/templates/members/createMemberForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/new" method="post"> <div class="form-group">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="이름을 입력하세요">
</div>
<button type="submit">등록</button>
</form>
</div> <!-- /container -->
</body>
</html>
13 changes: 13 additions & 0 deletions src/main/resources/templates/members/deleteMemberForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/delete" method="post"> <div class="form-group">
<label for="name">이름</label>
<input type="text" id="name" name="name" placeholder="삭제할 이름을 입력하세요">
</div>
<button type="submit">삭제</button>
</form>
</div> <!-- /container -->
</body>
</html>
23 changes: 23 additions & 0 deletions src/main/resources/templates/members/memberList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<div>
<table>
<thead>
<tr>
<th>#</th>
<th>이름</th>
</tr>
</thead>
<tbody>
<tr th:each="member : ${members}">
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
</tr>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
11 changes: 11 additions & 0 deletions src/main/resources/templates/quiz/correctForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<div>
<h1>정답입니다! 야구 볼 줄 아시네요?</h1>
<a href="/">홈으로</a>
</div>
</div> <!-- /container -->
</body>
</html>
Loading