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주차 미션 / 서버 4조 최재현 #8

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .idea/.name

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

4 changes: 3 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.

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

import HttpRequest.HttpRequest;
import HttpResponse.HttpResponse;

public interface Controller {

public abstract void execute(HttpRequest httpRequest, HttpResponse httpResponse);

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

import HttpRequest.HttpRequest;
import HttpResponse.HttpResponse;

public class ForwardController implements Controller{
public ForwardController() {
}
Copy link
Member

Choose a reason for hiding this comment

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

기본 생성자는 커스텀 생성자가 없을 경우 컴파일러가 자동으로 생성해주기 때문에 굳이 명시적으로 생성해주지 않아도 됩니다!


@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {
System.out.println("ForwardController");
System.out.println("webapp" + httpRequest.getUrl());
Copy link
Member

Choose a reason for hiding this comment

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

실무에서는 System.out을 사용하지 않아요! 지금부터라도 log 사용에 익숙해 지시는건 어떨까요?

httpResponse.forward("webapp" + httpRequest.getUrl());
}
}
17 changes: 17 additions & 0 deletions src/main/java/Controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Controller;

import HttpRequest.HttpRequest;
import HttpResponse.HttpResponse;
import static Enum.Url.HOME_HTML;

public class HomeController implements Controller{

public HomeController() {

}

@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {
httpResponse.forward(HOME_HTML.getUrl());
}
}
23 changes: 23 additions & 0 deletions src/main/java/Controller/ListController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Controller;

import HttpRequest.HttpRequest;
import HttpResponse.HttpResponse;

import static Enum.Url.LIST_HTML;
import static Enum.Url.LOGIN_FORM;

public class ListController implements Controller{
public ListController() {
}

@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {
boolean logined = httpRequest.isLogined();
if (logined) {
httpResponse.forward(LIST_HTML.getUrl());
Copy link
Member

Choose a reason for hiding this comment

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

early return 을 사용했다면 아래 조건문이 생략 가능했을 것 같네요.

}
if (!logined) {
httpResponse.redirect(LOGIN_FORM.getUrl());
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/Controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Controller;

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

import java.util.Map;

import static Enum.Url.HOME;
import static Enum.Url.LOGIN_FAILED;
import static Enum.UserQueryKey.PASSWORD;
import static Enum.UserQueryKey.USER_ID;

public class LoginController implements Controller{

public LoginController() {
}
@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {
String queryString = httpRequest.getBody();
Map<String, String> queryMap = HttpRequestUtils.parseQueryParameter(queryString);

String inputUserId = queryMap.get(USER_ID.getUserQueryKey());
String inputPassword = queryMap.get(PASSWORD.getUserQueryKey());

Repository repository = MemoryUserRepository.getInstance();
User user = repository.findUserById(inputUserId);
if (user != null) {
if (inputPassword.equals(user.getPassword())) {
Copy link
Member

Choose a reason for hiding this comment

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

두 조건식은 한 조건문 안에서 서술해도 좋을 것 같습니다!

httpResponse.redirect(HOME.getUrl(), "logined=true");
return;
}
}
httpResponse.redirect(LOGIN_FAILED.getUrl());
}
}
51 changes: 51 additions & 0 deletions src/main/java/Controller/SignUpController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Controller;

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

import java.util.Map;

import static Enum.Url.HOME;
import static Enum.UserQueryKey.*;

public class SignUpController implements Controller{

public SignUpController() {
}

@Override
public void execute(HttpRequest httpRequest, HttpResponse httpResponse) {
//post 방식으로 회원가입하기
String queryString = httpRequest.getBody();
Map<String, String> queryMap = HttpRequestUtils.parseQueryParameter(queryString);

httpResponse.redirect(HOME.getUrl());
Copy link
Member

Choose a reason for hiding this comment

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

아래 회원가입에 대한 비지니스 로직까지 완료된 이후에 리다이렉트를 응답하는게 좋을 것 같네요. 회원가입 로직에서 오류가 발생한다면 클라이언트에게 해당 오류를 전달하기 어렵지 않을까요?


Repository repository = MemoryUserRepository.getInstance();
repository.addUser(new User(queryMap.get(USER_ID.getUserQueryKey()),
queryMap.get(PASSWORD.getUserQueryKey()),
queryMap.get(NAME.getUserQueryKey()),
queryMap.get(EMAIL.getUserQueryKey())));

// //get 방식으로 회원가입하기
// if (url.equals(SIGNUP_FORM.getUrl())) {
// httpResponse.forward(SIGNUP_FORM_HTML.getUrl());
// }
//
// if (url.startsWith(SIGNUP.getUrl()) && method.equals(GET.getMethod())) {
// String queryString = url.split("\\?")[1];
// Map<String, String> queryMap = HttpRequestUtils.parseQueryParameter(queryString);
//
// httpResponse.redirect(HOME.getUrl());
//
// repository.addUser(new User(queryMap.get(USER_ID.getUserQueryKey()),
// queryMap.get(PASSWORD.getUserQueryKey()),
// queryMap.get(NAME.getUserQueryKey()),
// queryMap.get(EMAIL.getUserQueryKey())));
// }
}
}
22 changes: 22 additions & 0 deletions src/main/java/Enum/HttpHeaderField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Enum;

public enum HttpHeaderField {

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


private final String httpHeader;

HttpHeaderField(String httpHeader) {
this.httpHeader = httpHeader;
}

public String getHttpHeader() {
return httpHeader;
}
}
16 changes: 16 additions & 0 deletions src/main/java/Enum/Method.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Enum;

public enum Method {
GET("GET"),
POST("POST");

private final String method;

Method(String method) {
this.method = method;
}

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

public enum StatusCode {

OK("HTTP/1.1 200 OK "),
FOUND("HTTP/1.1 302 Found ")
Copy link
Member

Choose a reason for hiding this comment

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

status code가 아니라 http response statusLine 에 대한 값이네요. 사용에 혼란이 있을 것 같습니다!

;

private final String statusCode;

StatusCode(String statusCode) {
this.statusCode = statusCode;
}

public String getStatusCode() {
return statusCode;
}
}
Loading