Skip to content

[Spring MVC] 한재원 미션 제출합니다 #324

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

Open
wants to merge 2 commits into
base: breadquokka
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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/roomescape/Controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package roomescape.Controller;

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

@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model1) {
model1.addAttribute("name", "jaewon");
return "home";
}
}


41 changes: 41 additions & 0 deletions src/main/java/roomescape/Controller/ReservationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package roomescape.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import roomescape.Reservation;

import java.util.ArrayList;
import java.util.List;

@RestController
//@RequestMapping("/reservation")

Choose a reason for hiding this comment

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

불필요한 주석은 제거해 주세요~

public class ReservationController {

List<Reservation> reservation = new ArrayList<>();// 전역변수.

Choose a reason for hiding this comment

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

여기 주석도 마찬가지!


@GetMapping("/reservationData")

Choose a reason for hiding this comment

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

공백이 이상하게 들어가있네요 🥲

public List<Reservation> getReservations() {
// 예약 목록을 담을 리스트 생성

reservation.add(new Reservation("1", "브라운", "2023-01-01", "10:00"));
reservation.add(new Reservation("2", "브라운", "2023-01-02", "11:00"));

// 예약 목록 반환
Comment on lines +23 to +28

Choose a reason for hiding this comment

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

이 주석도 굳이 없어도 코드를 이해하는 데 문제가 없기 때문에 없어도 될 것 같네요

return reservation;
}




@GetMapping(value= "/reservation",produces = "text/html")
public String world(Model model) {

Choose a reason for hiding this comment

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

"world"라는 메서드 이름은 이 메서드가 무슨 일을 하는지 잘 알려주지 못하는 것 같아요! 다른 이름으로 수정해 보는 건 어떨까요?

model.addAttribute("reservationData", reservation);

Choose a reason for hiding this comment

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

이 속성이 html에 존재하는 속성인가요? 어떤 이유로 추가해 주셨나요~?

return "reservationpage";//html 페이지를 못찾아요
Copy link
Author

Choose a reason for hiding this comment

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

html실행이 안되고, reservationpage라는 문자열만 나타나요..

Choose a reason for hiding this comment

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

@RestController 대신 @Controller를 사용하면 아마 html 페이지가 잘 응답할 거에요~
@RestController@Controller는 무슨 차이가 있길래 이런 일이 발생한 것일까요?

}
}

49 changes: 49 additions & 0 deletions src/main/java/roomescape/Reservation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package roomescape;

public class Reservation {
private String id;
private String name;
private String date;
private String time;
Comment on lines +3 to +7
Copy link

@kwonyj1022 kwonyj1022 Aug 4, 2024

Choose a reason for hiding this comment

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

id가 식별자의 역할을 하고 있는데요, 그렇다면 id가 동일하면 동일한 객체인데, 지금 상태로는 new를 통해 id가 같은 객체를 두 개 만들면 두 객체가 다른 객체로 인식될 거예요. 그 이유는 무엇이고, 이를 방지하기 위해 어떤 것을 할 수 있을까요?


// 생성자
public Reservation(String id, String name, String date, String time) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
}

// getter와 setter
Comment on lines +9 to +17

Choose a reason for hiding this comment

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

이러한 주석은 주석 없이도 메서드이름만으로 그 역할을 잘 나타내기 때문에 굳이 필요 없을 것 같습니다!

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

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

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<nav id="sidebar" class="text-white">
<!-- 로고 영역 -->
<div class="sidebar-logo p-4">
<a href="/">
<a href="/static">

Choose a reason for hiding this comment

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

이렇게 수정하신 이유가 궁금해요!

<img src="https://avatars.githubusercontent.com/u/141792611?s=96&v=4" alt="logo" class="img-fluid rounded-circle">
</a>
</div>
Expand Down
25 changes: 23 additions & 2 deletions src/test/java/roomescape/MissionStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,37 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import static org.hamcrest.Matchers.is;



@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class MissionStepTest {

@Test
void 일단계() {
RestAssured.given().log().all()
.when().get("/")
.then().log().all()
.statusCode(200);
}
}


//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
//@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
Comment on lines +23 to +24

Choose a reason for hiding this comment

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

여기도 주석 제거해주세요!



@Test
void 이단계() {
RestAssured.given().log().all()
.when().get("/reservation")
.then().log().all()
.statusCode(200);

RestAssured.given().log().all()
.when().get("/reservations")
.then().log().all()
.statusCode(200)
.body("size()", is(3)); // 아직 생성 요청이 없으니 Controller에서 임의로 넣어준 Reservation 갯수 만큼 검증하거나 0개임을 확인하세요.
}
}