-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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); | ||
} | ||
|
||
} |
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(); | ||
} | ||
} |
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"; | ||
} | ||
} |
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 | ||
public MemberController(MemberService memberService, MemberRepository memberRepository) { | ||
this.memberService = memberService; | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
@GetMapping("/members/new") | ||
public String createForm() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어떤 경우에 쓰이는 메서드인가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지우는거라면 다른 Mapping이 있지않을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:/"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package PKNU.first_spring.controller; | ||
|
||
public class MemberForm { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dto로 사용한다면, 차라리 Record를 사용하는게 어떨까요?! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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"; | ||
} | ||
} | ||
} |
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; | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인터페이스 활용하신 점도 좋아유 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 직접 꺼내는것보다.. member,sameName() 으로 바꿀 수 있지 않을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 서비스 계층 분리해서 작성해주신점 좋습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.application.name=first-spring |
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> |
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> |
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> |
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> |
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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 애너테이션이 없어도 동작할겁니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
생성자가 한 개인 경우 생략이 가능하군요! 감사합니다!