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

[Spring Core] 정연희 미션 제출합니다 #278

Open
wants to merge 9 commits into
base: spig0126
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'jakarta.validation:jakarta.validation-api:3.0.0'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
testImplementation "junit:junit:4.12"
}

test {
useJUnitPlatform()
jvmArgs '-Xshare:off'
}
35 changes: 35 additions & 0 deletions src/main/java/roomescape/advice/GlobalControllerAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package roomescape.advice;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import roomescape.exception.DatabaseOperationException;
import roomescape.exception.ReservationNotFoundException;
import roomescape.exception.TimeNotFoundException;

@ControllerAdvice
public class GlobalControllerAdvice {
@ExceptionHandler(ReservationNotFoundException.class)
public ResponseEntity<String> handleReservationNotFoundException(ReservationNotFoundException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}

@ExceptionHandler(DatabaseOperationException.class)
public ResponseEntity<String> handleDatabaseOperationException(DatabaseOperationException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<String> handleValidationExceptions(MethodArgumentNotValidException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}

@ExceptionHandler(TimeNotFoundException.class)
public ResponseEntity handleTimeNotFoundException(TimeNotFoundException ex) {
System.out.println("---------------------HI");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}
50 changes: 50 additions & 0 deletions src/main/java/roomescape/controller/TimeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package roomescape.controller;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import roomescape.domain.Time;
import roomescape.service.TimeService;

import java.net.URI;
import java.util.List;

@Controller
public class TimeController {
@Autowired
private TimeService timeService;

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

@GetMapping("/times")
public ResponseEntity<List<Time>> getTimes() {
List<Time> times = timeService.getAllTimes();
return ResponseEntity.ok(times);
}

@GetMapping("/times/{id}")
@ResponseBody
public ResponseEntity<Time> getTimeById(@PathVariable long id) {
Time time = timeService.getTimeById(id);
return ResponseEntity.ok(time);
}

@PostMapping("/times")
@ResponseBody
public ResponseEntity<Time> addTime(@Valid @RequestBody Time time) {
Time newTime = timeService.createTime(time);
String uri = "/times/" + newTime.getId();
return ResponseEntity.created(URI.create(uri)).body(newTime);
}

@DeleteMapping("/times/{id}")
public ResponseEntity<Void> deleteTime(@PathVariable long id) {
timeService.deleteTimeById(id);
return ResponseEntity.noContent().build();
}
}
25 changes: 25 additions & 0 deletions src/main/java/roomescape/domain/Time.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package roomescape.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotBlank;

@Entity
public class Time {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotBlank(message="Time cannot be empty")
private String time;

public Time(){}

public long getId() {return this.id;}
public String getTime() {return this.time;}

public long setId(long id) {return this.id = id;}
public String setTime(String time) {return this.time = time;}
}
7 changes: 7 additions & 0 deletions src/main/java/roomescape/exception/TimeNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package roomescape.exception;

public class TimeNotFoundException extends RuntimeException {
public TimeNotFoundException(String message) {
super(message);
}
}
10 changes: 10 additions & 0 deletions src/main/java/roomescape/repository/TimeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package roomescape.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import roomescape.domain.Time;

@Repository
public interface TimeRepository extends JpaRepository<Time, Long> {
}
39 changes: 39 additions & 0 deletions src/main/java/roomescape/service/TimeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package roomescape.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import roomescape.domain.Time;
import roomescape.exception.TimeNotFoundException;
import roomescape.repository.TimeRepository;

import java.util.List;

@Service
public class TimeService {
Copy link

Choose a reason for hiding this comment

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

서비스 계층을 나눠주셨군요? 계층을 나눴을 때의 장점은 어떤 것이 있었나요?

@Autowired
Copy link

Choose a reason for hiding this comment

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

필드 주입의 단점을 알려주세요!

private TimeRepository timeRepository;

public List<Time> getAllTimes() {
return timeRepository.findAll();
}

public Time getTimeById(long id) {
if (!timeRepository.existsById(id)) {
throw new TimeNotFoundException("Time with id " + id + " not found");
}
return timeRepository.getReferenceById(id);
}
Copy link

Choose a reason for hiding this comment

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

데이터베이스에 두 번 쿼리하는 과정을 한 번으로 줄여볼 수 없을까요?


public Time createTime(Time time) {
return timeRepository.save(time);
}

public void deleteTimeById(long id) {
if (!timeRepository.existsById(id)) {
throw new TimeNotFoundException("Time with id " + id + " not found");
}
Time time = getTimeById(id);
Copy link

Choose a reason for hiding this comment

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

getTimeById(id) 메서드에서 이미 존재하는 아이디인지 검증하고 있는 것 맞죠? 제거해도 좋을 것 같아요.

timeRepository.delete(time);
}
}
16 changes: 16 additions & 0 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE time
(
id BIGINT NOT NULL AUTO_INCREMENT,
time VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE reservation
(
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
date VARCHAR(255) NOT NULL,
time_id BIGINT,
PRIMARY KEY (id),
FOREIGN KEY (time_id) REFERENCES time(id)
);