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

[JDBC 라이브러리 구현하기 - 1단계] 홍고(홍여진) 미션 제출합니다. #374

Merged
merged 10 commits into from
Oct 7, 2023
Merged
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
37 changes: 37 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: SonarCloud
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build and analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
distribution: 'corretto' # Alternative distribution options are available
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: ./gradlew clean build codeCoverageReport sonar --info -x :study:build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,5 @@ Temporary Items

tomcat.*
tomcat.*/**

**/WEB-INF/classes/**
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# JDBC 라이브러리 구현하기

* [x] 데이터 업데이트 기능 구현
* [x] 데이터 조회 기능 구현
* [x] 데이터 리스트 조회 기능 구현

Choose a reason for hiding this comment

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

기능 목록 작성 👍

49 changes: 23 additions & 26 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
plugins {
id 'java'
id 'idea'
id "java"
id "idea"
id "jacoco"
}

group 'org.example'
version '1.0-SNAPSHOT'
group "org.example"
version "1.0-SNAPSHOT"

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
Expand All @@ -14,27 +15,23 @@ repositories {
}

dependencies {
implementation project(':mvc')
implementation project(':jdbc')

implementation 'org.springframework:spring-tx:5.3.23'
implementation 'org.springframework:spring-jdbc:5.3.23'

implementation 'org.apache.tomcat.embed:tomcat-embed-core:10.1.0-M16'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:10.1.0-M16'
implementation 'ch.qos.logback:logback-classic:1.2.10'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4'
implementation 'com.h2database:h2:2.1.214'

testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testImplementation 'org.mockito:mockito-core:3.+'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
useJUnitPlatform()
implementation project(":mvc")
implementation project(":jdbc")

implementation "org.springframework:spring-tx:5.3.23"
implementation "org.springframework:spring-jdbc:5.3.23"

implementation "org.apache.tomcat.embed:tomcat-embed-core:10.1.13"
implementation "org.apache.tomcat.embed:tomcat-embed-jasper:10.1.13"
implementation "ch.qos.logback:logback-classic:1.2.12"
implementation "org.apache.commons:commons-lang3:3.13.0"
implementation "com.fasterxml.jackson.core:jackson-databind:2.15.2"
implementation "com.h2database:h2:2.2.220"

testImplementation "org.assertj:assertj-core:3.24.2"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.7.2"
testImplementation "org.mockito:mockito-core:5.4.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.7.2"
}

idea {
Expand All @@ -46,6 +43,6 @@ idea {

sourceSets {
main {
java.destinationDirectory.set(file('src/main/webapp/WEB-INF/classes'))
java.destinationDirectory.set(file("src/main/webapp/WEB-INF/classes"))
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.techcourse;

import jakarta.servlet.ServletContext;
import nextstep.mvc.DispatcherServlet;
import nextstep.mvc.controller.asis.ControllerHandlerAdapter;
import nextstep.mvc.controller.tobe.AnnotationHandlerMapping;
import nextstep.mvc.controller.tobe.HandlerExecutionHandlerAdapter;
import nextstep.web.WebApplicationInitializer;
import webmvc.org.springframework.web.servlet.mvc.DispatcherServlet;
import webmvc.org.springframework.web.servlet.mvc.asis.ControllerHandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerExecutionHandlerAdapter;
import web.org.springframework.web.WebApplicationInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/techcourse/ManualHandlerMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.techcourse.controller.*;
import jakarta.servlet.http.HttpServletRequest;
import nextstep.mvc.HandlerMapping;
import nextstep.mvc.controller.asis.Controller;
import nextstep.mvc.controller.asis.ForwardController;
import webmvc.org.springframework.web.servlet.mvc.HandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.asis.Controller;
import webmvc.org.springframework.web.servlet.mvc.asis.ForwardController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/techcourse/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.techcourse.repository.InMemoryUserRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import nextstep.mvc.view.JspView;
import nextstep.mvc.view.ModelAndView;
import nextstep.web.annotation.Controller;
import nextstep.web.annotation.RequestMapping;
import nextstep.web.support.RequestMethod;
import webmvc.org.springframework.web.servlet.view.JspView;
import webmvc.org.springframework.web.servlet.ModelAndView;
import context.org.springframework.stereotype.Controller;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import nextstep.mvc.controller.asis.Controller;
import webmvc.org.springframework.web.servlet.mvc.asis.Controller;

public class LogoutController implements Controller {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.techcourse.repository.InMemoryUserRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import nextstep.mvc.view.JspView;
import nextstep.mvc.view.ModelAndView;
import nextstep.web.annotation.Controller;
import nextstep.web.annotation.RequestMapping;
import nextstep.web.support.RequestMethod;
import webmvc.org.springframework.web.servlet.view.JspView;
import webmvc.org.springframework.web.servlet.ModelAndView;
import context.org.springframework.stereotype.Controller;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RegisterController {
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/techcourse/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.techcourse.repository.InMemoryUserRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import nextstep.mvc.view.JsonView;
import nextstep.mvc.view.ModelAndView;
import nextstep.web.annotation.Controller;
import nextstep.web.annotation.RequestMapping;
import nextstep.web.support.RequestMethod;
import webmvc.org.springframework.web.servlet.view.JsonView;
import webmvc.org.springframework.web.servlet.ModelAndView;
import context.org.springframework.stereotype.Controller;
import web.org.springframework.web.bind.annotation.RequestMapping;
import web.org.springframework.web.bind.annotation.RequestMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
116 changes: 27 additions & 89 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,121 +1,59 @@
package com.techcourse.dao;

import com.techcourse.domain.User;
import nextstep.jdbc.JdbcTemplate;
import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class UserDao {

private static final Logger log = LoggerFactory.getLogger(UserDao.class);

private final DataSource dataSource;
private static final RowMapper<User> ROW_MAPPER = (resultSet ->
new User(
resultSet.getLong("id"),
resultSet.getString("account"),
resultSet.getString("password"),
resultSet.getString("email")
)
);

private final JdbcTemplate jdbcTemplate;

public UserDao(final DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public UserDao(final JdbcTemplate jdbcTemplate) {
this.dataSource = null;
this.jdbcTemplate = jdbcTemplate;
}

public void insert(final User user) {
final var sql = "insert into users (account, password, email) values (?, ?, ?)";

Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);

log.debug("query : {}", sql);

pstmt.setString(1, user.getAccount());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {}
}
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail());
}

public void update(final User user) {
// todo
final var sql = "update users set account = ?, password = ?, email = ? where id = ?";
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId());
}

public List<User> findAll() {
// todo
return null;
final var sql = "select * from users";
return jdbcTemplate.query(sql, ROW_MAPPER);
}

public User findById(final Long id) {
public Optional<User> findById(final Long id) {
final var sql = "select id, account, password, email from users where id = ?";

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();

log.debug("query : {}", sql);

if (rs.next()) {
return new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4));
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {}

try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException ignored) {}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {}
}
return jdbcTemplate.queryForObject(sql, ROW_MAPPER, id);
}

public User findByAccount(final String account) {
// todo
return null;
public Optional<User> findByAccount(final String account) {
final var sql = "select id, account, password, email from users where account = ?";
return jdbcTemplate.queryForObject(sql, ROW_MAPPER, account);
}

Choose a reason for hiding this comment

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

account는 유니크한 값이 아니라, 만약에 두 개 이상의 값이 나오게 된다면 현재는 그냥 첫 번째 결과를 반환하고 있는 것 같아요.

홍고는 하나의 값을 쿼리하는 경우에 2개 이상이 존재하는 경우에
첫 번째 값 반환, 예외 처리 등등 여러 방법이 있을 것 같은데 왜 현재 방법을 선택했는지 궁금합니다!

Choose a reason for hiding this comment

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

또 아래에서는 값이 존재하지 않는 경우에 예외 처리를 했는데, 여기서는 왜 예외 처리를 하지 않았는지도 궁금합니당

Copy link
Author

@hgo641 hgo641 Oct 6, 2023

Choose a reason for hiding this comment

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

account는 유니크한 값이 아니라, 만약에 두 개 이상의 값이 나오게 된다면 현재는 그냥 첫 번째 결과를 반환하고 있는 것 같아요.

홍고는 하나의 값을 쿼리하는 경우에 2개 이상이 존재하는 경우에
첫 번째 값 반환, 예외 처리 등등 여러 방법이 있을 것 같은데 왜 현재 방법을 선택했는지 궁금합니다!

앗 ㅎㅎ... 여기는 그냥 account가 유니크한 값이라고 가정하고 코드를 작성했습니다! 미션 요구사항만 만족하는데에 집중하고 다른 부분은 크게 신경쓰지 못했습니다ㅠㅠ 일단 sql문에 unique옵션 추가하고, InMemoryUserRepository도 unique한 account만 들어오게 변경해놓겠습니다

e2ff1e2

Copy link
Author

Choose a reason for hiding this comment

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

또 아래에서는 값이 존재하지 않는 경우에 예외 처리를 했는데, 여기서는 왜 예외 처리를 하지 않았는지도 궁금합니당

이건 UserService에서는 예외 처리를 했는데 UserDao에선 예외 처리를 하지 않은 이유를 여쭤보시는 게 맞나요? 😀
제 개인적인 선호인데, "account와 일치하는 유저가 없을 시 예외를 던진다"는 해당 서비스의 비즈니스 요구사항으로 보고 Service객체에 몰았습니다! 활용도가 높은 Dao에서는 Optional로 반환하는 것이 편해서 이렇게 구현했습니다.

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/techcourse/dao/UserHistoryDao.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.techcourse.dao;

import com.techcourse.domain.UserHistory;
import nextstep.jdbc.JdbcTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class InMemoryUserRepository {
}

public static void save(final User user) {
if (database.containsKey(user.getAccount())) {
throw new IllegalArgumentException("중복되는 Account입니다.");
}
database.put(user.getAccount(), user);
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/techcourse/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public UserService(final UserDao userDao, final UserHistoryDao userHistoryDao) {
}

public User findById(final long id) {
return userDao.findById(id);
return userDao.findById(id)
.orElseThrow(() -> new IllegalArgumentException("해당 ID에 해당하는 유저가 존재하지 않습니다."));
hgo641 marked this conversation as resolved.
Show resolved Hide resolved
}

public void insert(final User user) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
create table if not exists users (
id bigint auto_increment,
account varchar(100) not null,
account varchar(100) not null unique,
password varchar(100) not null,
email varchar(100) not null,
email varchar(100) not null unique,
primary key(id)
);

Expand Down
Loading
Loading