Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 님이 있는 패키지 기준으로 컴포넌트 스캔을 시작합니다..
@SpringBootApplication
public class KongApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.example.kong;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/* Spring Boot에서 컨트롤러는 어떻게 인식할까요? */
@RestController //@Controller(이쪽으로 가면 View에염) + @ResponseBody(메서드 어노테이션) -> Http Response에 박아버려 View 찾지 마셈
@RequiredArgsConstructor
public class KongController {

private KongService kongService;
private final KongService kongService;

// 내장된 톰캣(Tomcat) 서버나 다른 서블릿 컨테이너 위에서 애플리케이션 실행 시 요청을 컨트롤러로 라우팅.
@GetMapping("/reaction")
public String reaction() {
return kongService.reactToDrink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
import com.example.kong.util.DrinkPrinter;
import com.example.kong.util.DrinkProperties;
import com.example.kong.util.Space;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/* Spring Boot에서 서비스는 어떻게 인식할까요? */
@Service // 동일하게 컴포넌트 스캔 Component 파생 어노테이션
@RequiredArgsConstructor
public class KongService {

/* 어떻게 주입받아야 할까요? */
private DrinkProperties drinkProperties;
/*
* @RequiredArgsConstructor
* final / @NonNull 필드들에 대해 생성자 자동 생성
* DI를 위해 사용, final 키워드가 붙은 모든 필드를 인자로 받아 의존성 주입
* 아래의 긴 코드들을 Lombok이 깨깟하게 정리
*
* @Autowired
* public KongService(DrinkProperties drinkProperties) {
* this.drinkProperties = drinkProperties;
* }
* */
private final DrinkProperties drinkProperties;


// 냄새가 나는 코드지만 예제를 위한 부분이니 신경쓰지 말아주세요..
public String reactToDrink() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*
* 1. 클래스가 Spring Container에 의해 인스턴스화
* 2. Spring Bean으로 관리되어야한다 명시화
*
* -> 컴포넌트 스캔, Spring Context에 bean으로 등록 및 생명주기 관리
* @Autowired 같은 어노테이션을 통해 의존성 주입 가능
* */
@Component // 가 뭘까요?
@Getter
@Getter // Lombok -> Getter 선언. 외부에서도 값을 읽을 수 있다
public class DrinkProperties {

/*
* 프로퍼티 파일, 환경 변수, 커맨드 라인 인자 등에서 값을 주입
* application.properties 또는 application.yml 파일에 정의된 프로퍼티 값을 클래스의 필드에 바인딩
*
* Spring이 관리하는 빈의 초기화 시점에서 applicaiont~ 에서 해당 값을 서치, 계층별로 나타내고 있다.
* -> 클래스 내부 필드에 바인딩(drink에 주입)
*
* BeanCreationException(생성할랬더니 그런거 없는디요?에러)를 방지하기 위해
* @Value("${spring.kong.what-kind-of-drink:coke}")
* 이런 식으로 default 값을 정해줄 수도 있다네용
* */
@Value("${spring.kong.what-kind-of-drink}") // 는 어떻게 값을 가져오는 걸까요?
private String drink;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring:
kong:
what-kind-of-drink: 'coffee'
what-kind-of-drink: 'coke'

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.lent.controller;

import com.example.lent.dto.LentRequest;
import com.example.lent.dto.LentResponse;
import com.example.lent.service.LentService;

/*컨트롤러는 뭘까요?*/
/*컨트롤러는 왜 인터페이스가 없어도 될까요?*/
public class LentController {
private final LentService lentService;

public LentController(LentService lentService) {
this.lentService = lentService;
}

public LentResponse lent(LentRequest request) {
return lentService.lent(request);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.example.lent.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/*Entity가 뭘까요?*/
@Getter
public class Cabinet {
private Long cabinetId;
private CabinetStatus cabinetStatus;
private Long cabinetId;
private CabinetStatus cabinetStatus;

public Cabinet(CabinetStatus status) {
this.cabinetId = null;
this.cabinetStatus = status;
}
public Cabinet(CabinetStatus status) {
this.cabinetId = null;
this.cabinetStatus = status;
}

public void id(Long id) {
this.cabinetId = id;
}
public void id(Long id) {
this.cabinetId = id;
}

public void setStatus(CabinetStatus cabinetStatus) {
this.cabinetStatus = cabinetStatus;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package com.example.lent.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;
import lombok.Getter;

/*Entity가 뭘까요?*/
@Getter
public class LentHistory {
private Long lentHistoryId;
private Long userId;
private Long cabinetId;
private String lentUserName;
private LocalDateTime createdAt;
private LocalDateTime expiredAt;
private Long lentHistoryId;
private Long userId;
private Long cabinetId;
private String lentUserName;
private LocalDateTime createdAt;
private LocalDateTime expiredAt;

public LentHistory(Long cabinetId, Long userId, String lentUserName, LocalDateTime createdAt,
LocalDateTime expiredAt) {
this.cabinetId = cabinetId;
this.userId = userId;
this.lentUserName = lentUserName;
this.createdAt = createdAt;
this.expiredAt = expiredAt;
}

public void id(Long id) {
this.lentHistoryId = id;
}

public LentHistory(Long cabinetId, Long userId, String lentUserName, LocalDateTime createdAt, LocalDateTime expiredAt) {
this.cabinetId = cabinetId;
this.userId = userId;
this.lentUserName = lentUserName;
this.createdAt = createdAt;
this.expiredAt = expiredAt;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.example.lent.dto;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;

/*DTO가 뭘까요?*/
@AllArgsConstructor
@Getter
public class LentResponse {
private Long lentHistoryId;
private Long cabinetId;
private String userName;
private LocalDateTime lentAt;
private LocalDateTime expiredAt;
private Long lentHistoryId;
private Long cabinetId;
private String userName;
private LocalDateTime lentAt;
private LocalDateTime expiredAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.lent.repository;

import com.example.lent.domain.LentHistory;
import java.util.List;

public interface LentHistoryRepository {
LentHistory save(LentHistory lentHistory);

List<LentHistory> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.lent.repository;

import com.example.lent.domain.LentHistory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*리포지토리는 뭘까요?*/
public class LentHistoryRepositoryImpl implements LentHistoryRepository {
private static Long ID_SEQUENCE = 1L;
/*실제 DB를 사용하지 않으므로 내부 Collection으로 대체합니다. - Collection이 뭘까요?*/
private static final List<LentHistory> TABLE = new ArrayList<>();

/**
* @param lentHistory cabinetId, userId, lentUserName, createAt, expiredAt
* @return
*/
@Override
public LentHistory save(LentHistory lentHistory) {
// update
if (lentHistory.getLentHistoryId() != null) {
TABLE.removeIf(history -> history.getLentHistoryId().equals(lentHistory.getLentHistoryId()));
TABLE.add(lentHistory);
return lentHistory;
}
// new
lentHistory.id(ID_SEQUENCE++);
TABLE.add(lentHistory);
return lentHistory;
}

@Override
public List<LentHistory> findAll() {
/*
* unmodifiable vs copyOf vs new ArrayList<>(TABLE)
*
* unmodifiable~
* 복사본의 추가, 삭제, 갱신 등의 변경 작업은 불가.
* 사본 리스트를 변경하려고 하면 UnsupportedOperationException이 발생
* 원본 변경 시 복사본도 변경
*
* copyOf~
* 복사본의 추가, 삭제, 갱신 등의 변경 작업은 불가.
* 사본 리스트를 변경하려고 하면 UnsupportedOperationException이 발생
* 원본 변경해도 복사본은 독립적
*
* new Array~
* 변경 가능, 복사본은 독립적
* */
return Collections.unmodifiableList(TABLE);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.lent;
package com.example.lent.service;

import com.example.lent.dto.LentRequest;
import com.example.lent.dto.LentResponse;

public interface LentService {
LentResponse lent(LentRequest request);
LentResponse lent(LentRequest request);
}
Loading