Skip to content

Commit

Permalink
Merge pull request #187 from hseong3243/feat/pagination
Browse files Browse the repository at this point in the history
[박혜성] step-8 페이징 구현하기
  • Loading branch information
honux77 authored Aug 7, 2024
2 parents b940689 + 2a0b1db commit 1620555
Show file tree
Hide file tree
Showing 62 changed files with 800 additions and 465 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CD

on:
push:
branches: [ "hseong3243" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
token: ${{secrets.ACTION_TOKEN}}
submodules: true

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Build with Gradle
uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0
with:
arguments: build

- name: Build and save Docker image
run: |
docker build -t jsp-cafe:${{ github.sha }} .
docker save -o jsp-cafe.tar jsp-cafe:${{ github.sha }}
chmod 664 jsp-cafe.tar
- name: copy file via ssh password
uses: appleboy/[email protected]
with:
host: ${{ secrets.AWS_HOST }}
username: ${{ secrets.AWS_USERNAME }}
key: ${{ secrets.AWS_PRIVATE_KEY }}
source: "jsp-cafe.tar"
target: "/home/ubuntu"

- name: executing remote ssh commands using password
uses: appleboy/[email protected]
with:
username: ${{ secrets.AWS_USERNAME }}
host: ${{ secrets.AWS_HOST }}
key: ${{ secrets.AWS_PRIVATE_KEY }}
script: |
docker stop $(docker ps -a -q) || true
docker rmi $(docker images -q)
docker load -i jsp-cafe.tar
docker run -d -p 8080:8080 --add-host=host.docker.internal:host-gateway jsp-cafe:${{ github.sha }}
rm jsp-cafe.tar
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM tomcat:jdk17

WORKDIR /usr/local/tomcat

ARG WAR_FILE=build/libs/*.war

COPY ${WAR_FILE} /usr/local/tomcat/webapps/ROOT.war

EXPOSE 8080

ENTRYPOINT ["catalina.sh", "run"]
3 changes: 2 additions & 1 deletion src/main/java/com/woowa/DispatcherServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) {

private void dispatch(HttpServletRequest req, HttpServletResponse resp) throws Throwable {
if(req.getRequestURI().endsWith(".jsp")) {
handleJspRequest(req, resp);
RequestDispatcher dispatcher = req.getRequestDispatcher(req.getRequestURI());
dispatcher.forward(req, resp);
return;
}

Expand Down
20 changes: 6 additions & 14 deletions src/main/java/com/woowa/DispatcherServletContainer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.woowa;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woowa.database.UserDatabase;
import com.woowa.database.user.UserDatabase;
import com.woowa.filter.ErrorHandlingFilter;
import com.woowa.filter.HttpMethodFilter;
import com.woowa.framework.ApplicationInitializer;
Expand All @@ -10,20 +10,17 @@
import com.woowa.handler.QuestionHandler;
import com.woowa.handler.ReplyHandler;
import com.woowa.handler.UserHandler;
import com.woowa.servlet.FindQuestionServlet;
import com.woowa.servlet.ListQuestionServlet;
import com.woowa.servlet.QuestionDetailServlet;
import com.woowa.servlet.LoginServlet;
import com.woowa.servlet.LogoutServlet;
import com.woowa.servlet.QuestionServlet;
import com.woowa.servlet.QuestionsServlet;
import com.woowa.servlet.SignupServlet;
import com.woowa.servlet.UserProfileServlet;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.FilterRegistration;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRegistration.Dynamic;
import java.util.EnumSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -48,8 +45,7 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti

private void addServlet(ServletContext ctx, DispatcherServlet dispatcherServlet, BeanFactory beanFactory) {
Dynamic helloServlet = ctx.addServlet("helloServlet", dispatcherServlet);
helloServlet.addMapping("/users", "/css/*", "/js/*", "/images/*", "/fonts/*", "/favicon.ico", "/user/*",
"/qna/*");
helloServlet.addMapping("/users", "/css/*", "/js/*", "/images/*", "/fonts/*", "/favicon.ico");

Dynamic signupServlet = ctx.addServlet("signupServlet", new SignupServlet());
signupServlet.addMapping("/signup");
Expand All @@ -60,7 +56,7 @@ private void addServlet(ServletContext ctx, DispatcherServlet dispatcherServlet,
userProfileServlet.addMapping("/users/*");

Dynamic questionServlet = ctx.addServlet("questionServlet",
new QuestionServlet(beanFactory.getBean(QuestionHandler.class)));
new QuestionsServlet(beanFactory.getBean(QuestionHandler.class)));
questionServlet.addMapping("/questions");

Dynamic loginServlet = ctx.addServlet("loginServlet",
Expand All @@ -71,11 +67,7 @@ private void addServlet(ServletContext ctx, DispatcherServlet dispatcherServlet,
new LogoutServlet(beanFactory.getBean(LoginHandler.class)));
logoutServlet.addMapping("/logout");

Dynamic listQuestionServlet = ctx.addServlet("listQuestionServlet",
new ListQuestionServlet(beanFactory.getBean(QuestionHandler.class)));
listQuestionServlet.addMapping("/");

Dynamic findQuestionServlet = ctx.addServlet("findQuestionServlet", new FindQuestionServlet(
Dynamic findQuestionServlet = ctx.addServlet("findQuestionServlet", new QuestionDetailServlet(
beanFactory.getBean(QuestionHandler.class),
beanFactory.getBean(ReplyHandler.class),
beanFactory.getBean(ObjectMapper.class)));
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/woowa/Main.java

This file was deleted.

14 changes: 6 additions & 8 deletions src/main/java/com/woowa/config/DatabaseConfig.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.woowa.config;

import com.woowa.database.QuestionDatabase;
import com.woowa.database.QuestionJdbcDatabase;
import com.woowa.database.QuestionMemoryDatabase;
import com.woowa.database.ReplyDatabase;
import com.woowa.database.ReplyJdbcDatabase;
import com.woowa.database.UserDatabase;
import com.woowa.database.UserJdbcDatabase;
import com.woowa.database.UserMemoryDatabase;
import com.woowa.database.question.QuestionDatabase;
import com.woowa.database.question.QuestionJdbcDatabase;
import com.woowa.database.reply.ReplyDatabase;
import com.woowa.database.reply.ReplyJdbcDatabase;
import com.woowa.database.user.UserDatabase;
import com.woowa.database.user.UserJdbcDatabase;
import com.woowa.framework.Bean;

public class DatabaseConfig {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/woowa/config/HandlerConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.woowa.config;

import com.woowa.database.QuestionDatabase;
import com.woowa.database.ReplyDatabase;
import com.woowa.database.UserDatabase;
import com.woowa.database.question.QuestionDatabase;
import com.woowa.database.reply.ReplyDatabase;
import com.woowa.database.user.UserDatabase;
import com.woowa.framework.Bean;
import com.woowa.handler.ReplyHandler;
import com.woowa.handler.LoginHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/woowa/database/DBConnectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class DBConnectionUtils {

private static final String JDBC_URL = "jdbc:mysql://localhost:3306/cafe";
private static final String JDBC_URL = "jdbc:mysql://host.docker.internal:3306/cafe";
private static final String USER = "foxrain";
private static final String PASSWORD = "test1234";
private static final Logger log = LoggerFactory.getLogger(DBConnectionUtils.class);
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/com/woowa/database/Page.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.woowa.database;

import java.util.List;
import java.util.Objects;

public class Page<T> {
private final List<T> content;
private final Long totalElements;
private final int totalPages;
private final boolean hasNext;
private final int page;
private final int size;

public Page(List<T> content, Long totalElements, int totalPages, boolean hasNext, int page, int size) {
this.content = content;
this.totalElements = totalElements;
this.totalPages = totalPages;
this.hasNext = hasNext;
this.page = page;
this.size = size;
}

public static <T> Page<T> of(List<T> content, Long count, int page, int size) {
int totalPages = (int) Math.ceil((double) count / size);
boolean hasNext = (long) page * size + size < count;
return new Page<>(content, count, totalPages, hasNext, page, size);
}

public List<T> getContent() {
return content;
}

public Long getTotalElements() {
return totalElements;
}

public int getTotalPages() {
return totalPages;
}

public boolean isHasNext() {
return hasNext;
}

public int getPage() {
return page;
}

public int getSize() {
return size;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Page<?> page1 = (Page<?>) o;
return totalPages == page1.totalPages && hasNext == page1.hasNext && page == page1.page && size == page1.size
&& Objects.equals(content, page1.content) && Objects.equals(totalElements,
page1.totalElements);
}

@Override
public int hashCode() {
return Objects.hash(content, totalElements, totalPages, hasNext, page, size);
}

@Override
public String toString() {
return "Page{" +
"content=" + content +
", totalElements=" + totalElements +
", totalPages=" + totalPages +
", hasNext=" + hasNext +
", page=" + page +
", size=" + size +
'}';
}
}
32 changes: 0 additions & 32 deletions src/main/java/com/woowa/database/ReplyMemoryDatabase.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.woowa.database;
package com.woowa.database.question;

import com.woowa.database.Page;
import com.woowa.model.Question;
import java.util.List;
import java.util.Optional;
Expand All @@ -9,7 +10,7 @@ public interface QuestionDatabase {

List<Question> findAll();

List<Question> findAllOrderByCreatedAt(int page, int size);
Page<Question> findAllOrderByCreatedAt(int page, int size);

Optional<Question> findById(String questionId);

Expand Down
Loading

0 comments on commit 1620555

Please sign in to comment.