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

3주차 미션 / 서버 3조 이윤희 #19

Open
wants to merge 7 commits into
base: main
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
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

import http.util.HttpRequest;
import http.util.HttpResponse;

public interface Controller {
void execute(HttpRequest request, HttpResponse response) throws Exception;
}
11 changes: 11 additions & 0 deletions src/main/java/Controller/ForwardController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Controller;

import http.util.HttpRequest;
import http.util.HttpResponse;

public class ForwardController implements Controller {
@Override
public void execute(HttpRequest request, HttpResponse response) throws Exception {
response.forward(request.getPath());
}
}
11 changes: 11 additions & 0 deletions src/main/java/Controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Controller;

import http.util.HttpRequest;
import http.util.HttpResponse;

public class HomeController implements Controller {
@Override
public void execute(HttpRequest request, HttpResponse response) throws Exception {
response.forward("/index.html");

Choose a reason for hiding this comment

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

url을 enum으로 관리하니까 enum을 사용하는 게 어떨까요?

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

import http.util.HttpRequest;
import http.util.HttpResponse;

public class ListController implements Controller {
@Override
public void execute(HttpRequest request, HttpResponse response) throws Exception {
if ("logined=true".equals(request.getHeaders().get("Cookie"))) {
response.forward("/user/list.html");
} else {
response.redirect("/user/login.html");
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/Controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Controller;

import db.MemoryUserRepository;
import db.Repository;
import http.util.HttpRequest;
import http.util.HttpRequestUtils;
import http.util.HttpResponse;
import model.User;

import java.util.Map;

import static enumClass.Url.INDEX_PAGE;
import static enumClass.Url.USER_LOGIN_FAILED_PAGE;
import static enumClass.UserQueryKey.*;

public class LoginController implements Controller {
private final Repository repository = MemoryUserRepository.getInstance();

Choose a reason for hiding this comment

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

필드 주입보다는 생성자 주입을 더 권하고 싶어요. 필드 주입을 사용하면 다른 repository 객체로 바꿀 때 이 부분을 수정해야하거든요


@Override
public void execute(HttpRequest request, HttpResponse response) throws Exception {
Map<String, String> params = request.getBody() != null ? HttpRequestUtils.parseQueryParameter(request.getBody()) : null;

Choose a reason for hiding this comment

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

파라미터를 파싱하는 부분은 httpRequest에서 처리해보는 건 어떨까요?

if (params != null) {
User user = repository.findUserById(params.get(USERID.getValue()));
if (user != null && user.getPassword().equals(params.get(PASSWORD.getValue()))) {
response.redirect(INDEX_PAGE.getValue());

Choose a reason for hiding this comment

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

Set-cookie는 어디서 하는 걸까요...?

} else {
response.redirect(USER_LOGIN_FAILED_PAGE.getValue());
}
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/Controller/SignUpController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package Controller;

import db.MemoryUserRepository;
import db.Repository;
import http.util.HttpRequest;
import http.util.HttpRequestUtils;
import http.util.HttpResponse;
import model.User;

import java.util.Map;

import static enumClass.Url.INDEX_PAGE;
import static enumClass.Url.USER_FORM_PAGE;
import static enumClass.UserQueryKey.*;

public class SignUpController implements Controller {
private final Repository repository = MemoryUserRepository.getInstance();

@Override
public void execute(HttpRequest request, HttpResponse response) throws Exception {
Map<String, String> params = request.getBody() != null ? HttpRequestUtils.parseQueryParameter(request.getBody()) : null;
if (params != null) {
User user = new User(params.get(USERID.getValue()), params.get(PASSWORD.getValue()), params.get(NAME.getValue()), params.get(EMAIL.getValue()));
repository.addUser(user);
response.redirect(INDEX_PAGE.getValue());
} else {
response.redirect(USER_FORM_PAGE.getValue());
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/enumClass/HttpHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package enumClass;

public enum HttpHeader {

CONTENT_TYPE("Content-Type"),
CONTENT_LENGTH("Content-Length"),
LOCATION("Location"),
SET_COOKIE("Set-Cookie"),
COOKIE("Cookie");

String value;

private HttpHeader(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
17 changes: 17 additions & 0 deletions src/main/java/enumClass/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package enumClass;

public enum HttpMethod {

GET("GET"),
POST("POST");

final String value;

private HttpMethod(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
18 changes: 18 additions & 0 deletions src/main/java/enumClass/StatusCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package enumClass;

public enum StatusCode {

OK("200 OK"),
FOUND("302 Found"),
NOTFOUND("404 Not Found");

final String value;

private StatusCode(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
26 changes: 26 additions & 0 deletions src/main/java/enumClass/Url.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package enumClass;

public enum Url {

WEBAPP("webapp"),
ROOT("/"),
INDEX_PAGE("/index.html"),
USER_FORM_PAGE("/user/form.html"),
USER_SIGNUP("/user/signup"),
USER_LOGIN("/user/login"),
USER_LOGIN_PAGE("/user/login.html"),
USER_LOGIN_FAILED_PAGE("/user/login_failed.html"),
USER_USERLIST("/user/userList"),
USER_LIST_PAGE("/user/list.html"),
STYLE_CSS(".css");

final String value;

private Url(String value){
this.value = value;
}

public String getValue() {
return value;
}
}
19 changes: 19 additions & 0 deletions src/main/java/enumClass/UserQueryKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package enumClass;

public enum UserQueryKey {

USERID("userId"),
PASSWORD("password"),
NAME("name"),
EMAIL("email");

String value;

private UserQueryKey(String value){
this.value = value;
}

public String getValue(){
return value;
}
}
Loading