code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,32 @@ +package blackjack.domain.card; + +import blackjack.domain.card.strategy.DrawStrategy; +import blackjack.domain.card.strategy.RandomDrawStrategy; +import lombok.Getter; + +import java.util.LinkedList; +import java.util.List; + +public class Deck { + + private static final String ALERT_NO_CARD_LEFT = "์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์นด๋“œ๋ฅผ ๋ชจ๋‘ ์†Œ์ง„ํ•˜์˜€์Šต๋‹ˆ๋‹ค."; + + @Getter + private final List<Card> deck; + + public Deck() { + this.deck = new LinkedList<>(DeckFactory.createDeck()); + } + + public Card drawCard() { + if (deck.isEmpty()) { + throw new RuntimeException(ALERT_NO_CARD_LEFT); + } + return deck.remove(generateNextDrawCardIndex(new RandomDrawStrategy())); + } + + private int generateNextDrawCardIndex(DrawStrategy drawStrategy) { + return drawStrategy.getNextIndex(deck); + } + +}
Java
DrawStrategy ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๋Š” RandomDrawStrategy ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,39 @@ +package blackjack.domain.card; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class DeckFactory { + + private static final List<Card> cardsDeck; + + static { + cardsDeck = createCardsDeck(); + } + + private static List<Card> createCardsDeck() { + List<Card> cards = new ArrayList<>(); + + for (Denomination denomination : Denomination.values()) { + cards.addAll(createCards(denomination)); + } + return cards; + } + + private static List<Card> createCards(Denomination denomination) { + List<Card> cards = new ArrayList<>(); + + for (Type type : Type.values()) { + cards.add(new Card(denomination, type)); + } + + return cards; + } + + public static List<Card> createDeck() { + return Collections.unmodifiableList(cardsDeck); + } +} + +
Java
์Šคํƒœํ‹ฑ๋ธ”๋ก์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋Šฅ์€ ๋“ฑํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์™€ ์ •ํ™•ํžˆ ๋™์ผํ•œ๊ฑด๊ฐ€์š”?
@@ -0,0 +1,38 @@ +package blackjack.domain.card; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CardTest { + + @DisplayName("์นด๋“œ ๊ฐ์ฒด์˜ ์ˆซ์ž์™€ ์ข…๋ฅ˜๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋กœ ํŒ๋‹จํ•œ๋‹ค") + @Test + void equalsTest() { + //given, when, then + assertThat(new Card(Denomination.TWO, Type.DIAMOND)) + .isEqualTo(new Card(Denomination.TWO, Type.DIAMOND)); + + } + + @DisplayName("์นด๋“œ ๊ฐ์ฒด์˜ ์ˆซ์ž์™€ ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค") + @Test + void printCardTwoDiamond() { + //given + Card card = new Card(Denomination.TWO, Type.DIAMOND); + + //when, then + assertThat(card).hasToString("2๋‹ค์ด์•„๋ชฌ๋“œ"); + } + + @DisplayName("์นด๋“œ ๊ฐ์ฒด์˜ ์ˆซ์ž์™€ ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค") + @Test + void printCardAceHeart() { + //given + Card card = new Card(Denomination.ACE, Type.HEART); + + //when, then + assertThat(card.toString()).isEqualTo("Aํ•˜ํŠธ"); + } +}
Java
assert๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ๋Š” hasToString๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,102 @@ +package blackjack.domain.participant; + +import blackjack.domain.card.Card; +import blackjack.domain.card.Denomination; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Getter +public abstract class Participant { + private static final int DIFFERENCE_OF_ACE_SCORE = 10; + private static final int BLACKJACK = 21; + private static final int ZERO = 0; + private static final String CHECK_NULL_OR_EMPTY = "์ด๋ฆ„์ด ๋นˆ ์นธ ํ˜น์€ null ๊ฐ’์ด ์•„๋‹Œ์ง€ ํ™•์ธํ•ด์ฃผ์„ธ์š”."; + private static final String CHECK_CONTAINING_ONLY_LETTERS_AND_DIGITS = "์ด๋ฆ„์€ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์€ ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ ์ง€์ •ํ•ด์ฃผ์„ธ์š”."; + + private final String name; + private final List<Card> cards; + + protected Participant(String name) { + validateName(name); + + this.name = name; + this.cards = new ArrayList<>(); + } + + private void validateName(String name) { + validateNullOrEmpty(name); + validateAlphaNumeric(name); + } + + private void validateNullOrEmpty(String name) { + if (StringUtils.isBlank(name)) { + throw new IllegalArgumentException(CHECK_NULL_OR_EMPTY); + } + } + + private void validateAlphaNumeric(String name) { + if (!StringUtils.isAlphanumericSpace(name)) { + throw new IllegalArgumentException(CHECK_CONTAINING_ONLY_LETTERS_AND_DIGITS); + } + } + + protected abstract boolean drawable(); + + public void receiveCard(Card card) { + cards.add(card); + } + + public int getCardsSum() { + int scoreOfAceAsEleven = sumOfCardsScore(); + int aceCount = getAceCount(); + + while (canCountAceAsOne(scoreOfAceAsEleven, aceCount)) { + scoreOfAceAsEleven = scoreOfAceAsOne(scoreOfAceAsEleven); + aceCount--; + } + + return scoreOfAceAsEleven; + } + + private int scoreOfAceAsOne(int scoreOfAceAsEleven) { + return scoreOfAceAsEleven - DIFFERENCE_OF_ACE_SCORE; + } + + private boolean canCountAceAsOne(int scoreOfAceAsEleven, int aceCount) { + return scoreOfAceAsEleven > BLACKJACK && aceCount > ZERO; + } + + private int getAceCount() { + return (int) cards.stream() + .filter(Card::isAce) + .count(); + } + + private int sumOfCardsScore() { + return cards.stream() + .map(Card::getDenomination) + .mapToInt(Denomination::getScore) + .sum(); + } + + public boolean isBust() { + return getCardsSum() > BLACKJACK; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Participant that = (Participant) o; + return Objects.equals(name, that.name) && Objects.equals(cards, that.cards); + } + + @Override + public int hashCode() { + return Objects.hash(name, cards); + } +}
Java
์ด ๋ถ€๋ถ„ ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ ๋˜๋Š”๋ฐ์š”, ์ œ ์ƒ๊ฐ์—๋Š” Participant๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ ๋ชฉ๋ก์— ๋”ฐ๋ผ ์—์ด์Šค๋ฅผ 1๋กœ ๊ณ„์‚ฐํ•  ์ง€, 11๋กœ ๊ณ„์‚ฐํ•  ์ง€ ๋‹ค๋ฅด๊ฒŒ ํŒ๋‹จํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Participant์˜ ๋กœ์ง์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. Denomination ์™ธ๋ถ€์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ 1 ํ˜น์€ 11๋กœ ๊ณ„์‚ฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— Denomination์„ ํฌํ•จํ•œ ์ƒ์œ„ ๊ตฌ์กฐ์—์„œ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒŒ ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. Denomination์— score๋ฅผ 1๋กœ ๊ฐ€์ง€๋Š” enum์„ ์ถ”๊ฐ€ํ•ด์„œ ๋ฆฌํŒฉํ† ๋ง์„ ์‹œ๋„ํ•ด๋ดค๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•  ์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,27 @@ +package blackjack.domain.participant; + +import lombok.Getter; + +@Getter +public class Player extends Participant { + + private static final int PLAYER_DRAW_THRESHOLD = 21; + private static final String DEALER_NAME = "๋”œ๋Ÿฌ"; + private static final String CHECK_SAME_NAME_AS_DEALER = "์ด๋ฆ„์€ ๋”œ๋Ÿฌ์™€ ๊ฐ™์„ ์ˆ˜ ์—†์œผ๋‹ˆ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ์„ธ์š”."; + + public Player(String name) { + super(name); + validateDealerNameDuplicated(name); + } + + private void validateDealerNameDuplicated(String name) { + if (name.equals(DEALER_NAME)) { + throw new IllegalArgumentException(CHECK_SAME_NAME_AS_DEALER); + } + } + + @Override + public boolean drawable() { + return getCardsSum() < PLAYER_DRAW_THRESHOLD; + } +}
Java
๋ง์”€ํ•˜์‹  ๋ถ€๋ถ„์ด ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์ƒ์„ฑ ๋กœ์ง์„ view๋กœ ์ด๋™ํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,27 @@ +package blackjack.domain.participant; + +import lombok.Getter; + +@Getter +public class Player extends Participant { + + private static final int PLAYER_DRAW_THRESHOLD = 21; + private static final String DEALER_NAME = "๋”œ๋Ÿฌ"; + private static final String CHECK_SAME_NAME_AS_DEALER = "์ด๋ฆ„์€ ๋”œ๋Ÿฌ์™€ ๊ฐ™์„ ์ˆ˜ ์—†์œผ๋‹ˆ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ์„ธ์š”."; + + public Player(String name) { + super(name); + validateDealerNameDuplicated(name); + } + + private void validateDealerNameDuplicated(String name) { + if (name.equals(DEALER_NAME)) { + throw new IllegalArgumentException(CHECK_SAME_NAME_AS_DEALER); + } + } + + @Override + public boolean drawable() { + return getCardsSum() < PLAYER_DRAW_THRESHOLD; + } +}
Java
orElseThrow๋กœ ์ˆ˜์ •ํ•˜๊ณ , ๋ฉ”์„ธ์ง€๋ฅผ ๋‹ด์€ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -1,16 +1,28 @@ package webserver; -import java.io.DataOutputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import webserver.controller.*; +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; public class RequestHandler implements Runnable { private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class); + private static final Map<String, Controller> controllers = new HashMap<>(); + + static { + controllers.put("/user/create", new CreateUserController()); + controllers.put("/user/login", new LoginController()); + controllers.put("/user/list", new ListUserController()); + controllers.put("/user/list.html", new ListUserController()); + } private Socket connection; @@ -21,35 +33,12 @@ public RequestHandler(Socket connectionSocket) { public void run() { logger.debug("New Client Connect! Connected IP : {}, Port : {}", connection.getInetAddress(), connection.getPort()); - try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { - // TODO ์‚ฌ์šฉ์ž ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” ์ด ๊ณณ์— ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. - DataOutputStream dos = new DataOutputStream(out); - byte[] body = "Hello World".getBytes(); - response200Header(dos, body.length); - responseBody(dos, body); - } catch (IOException e) { - logger.error(e.getMessage()); - } - } - - private void response200Header(DataOutputStream dos, int lengthOfBodyContent) { - try { - dos.writeBytes("HTTP/1.1 200 OK \r\n"); - dos.writeBytes("Content-Type: text/html;charset=utf-8\r\n"); - dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n"); - dos.writeBytes("\r\n"); - } catch (IOException e) { - logger.error(e.getMessage()); - } - } - - private void responseBody(DataOutputStream dos, byte[] body) { - try { - dos.write(body, 0, body.length); - dos.flush(); + HttpRequest httpRequest = new HttpRequest(in); + HttpResponse httpResponse = new HttpResponse(out); + controllers.getOrDefault(httpRequest.getPath(), new DefaultController()).service(httpRequest, httpResponse); } catch (IOException e) { logger.error(e.getMessage()); } } -} +} \ No newline at end of file
Java
path์— ๋งž๋Š” Controller ์ •์˜ ์ข‹์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,80 @@ +package webserver.controller; + +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; +import webserver.domain.HttpStatusCode; +import webserver.exceptions.InvalidRequestException; +import webserver.exceptions.NotYetSentException; + +public abstract class AbstractController implements Controller { + + public void service(HttpRequest request, HttpResponse response) { + try { + System.out.println(request.getMethod().name() + " " + request.getPath()); + methodMapping(request, response); + } catch (Exception e) { + if(!response.isSent()){ + System.out.println("์ž์‹ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ฒ˜๋ฆฌ ๋ˆ„๋ฝ๋œ ์˜ˆ์™ธ ์ƒํ™ฉ : " + e.getMessage()); + e.printStackTrace(); + response.send(HttpStatusCode.NOT_IMPLEMENTED, e.getMessage()); + } + } finally { + if (!response.isSent()) { + response.send(HttpStatusCode.INTERNAL_SERVER_ERROR); + throw new NotYetSentException("response์˜ ๋ฉ”์„œ๋“œ send()๊ฐ€ ํ˜ธ์ถœ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค"); + } + } + } + + private void methodMapping(HttpRequest request, HttpResponse response) { + switch (request.getMethod()) { + case GET: + validateGetRequest(request, response); + doGet(request, response); + return; + case POST: + validatePostRequest(request, response); + doPost(request, response); + return; + case PUT: + validatePutRequest(request, response); + doPut(request, response); + return; + case DELETE: + validateDeleteRequest(request, response); + doDelete(request, response); + return; + default: + response.send(HttpStatusCode.METHOD_NOT_ALLOWED); + } + } + + public void doPost(HttpRequest request, HttpResponse response) { + response.send(HttpStatusCode.METHOD_NOT_ALLOWED); + } + + public void doGet(HttpRequest request, HttpResponse response) { + response.send(HttpStatusCode.METHOD_NOT_ALLOWED); + } + + public void doPut(HttpRequest request, HttpResponse response) { + response.send(HttpStatusCode.METHOD_NOT_ALLOWED); + } + + public void doDelete(HttpRequest request, HttpResponse response) { + response.send(HttpStatusCode.METHOD_NOT_ALLOWED); + } + + // ํ•„์š”์‹œ์— ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜์—ฌ ์ธํ’‹๊ฐ’์„ ๊ฒ€์ฆํ•œ๋‹ค + public void validatePostRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + } + + public void validateGetRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + } + + public void validatePutRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + } + + public void validateDeleteRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + } +}
Java
์ถ”์ƒ ํด๋ž˜์Šค service ๋ฉ”์†Œ๋“œ์— ํ๋ฆ„์„ ์ •๋ฆฌํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์œผ๋กœ ์ ์šฉํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘ ๐Ÿ’ฏ
@@ -0,0 +1,21 @@ +package webserver.controller; + +import utils.FileIoUtils; +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; +import webserver.domain.HttpStatusCode; + +import java.io.IOException; +import java.net.URISyntaxException; + +public class DefaultController extends AbstractController { + @Override + public void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { + try { + httpResponse.send(HttpStatusCode.OK, FileIoUtils.readStaticHttpFile(httpRequest.getPath())); + } catch (URISyntaxException | IOException e) { + httpResponse.send(HttpStatusCode.NOT_FOUND); + } + } + +}
Java
http://localhost:8080/index.html ๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ ์ •์ƒ์ ์ธ ํŽ˜์ด์ง€ ํ˜ธ์ถœ์ด ๋˜์ง€ ์•Š๊ณ  ์žˆ์–ด์š”. (index ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ „์ฒด์ ์œผ๋กœ ์›นํŽ˜์ด์ง€ ์‘๋‹ต์ด ์ž˜๋ชป ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!) ์ด ๋ถ€๋ถ„ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋Š” Controller์˜ ๊ด€์‹ฌ์‚ฌ๊ฐ€ ์•„๋‹Œ๊ฒƒ ๊ฐ™์•„์š”! ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,32 @@ +package webserver.controller; + +import webserver.domain.HttpHeader; +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; +import webserver.domain.HttpStatusCode; +import webserver.exceptions.InvalidRequestException; + +import java.io.IOException; + +import static service.JwpService.getProfilePage; +import static service.JwpService.isLogin; + +public class ListUserController extends AbstractController { + @Override + public void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { + try { + httpResponse.send(HttpStatusCode.OK, getProfilePage()); + } catch (IOException e) { + httpResponse.send(HttpStatusCode.INTERNAL_SERVER_ERROR, "์ฝ์–ด๋“ค์ด๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค"); + } + } + + @Override + public void validateGetRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + if (!isLogin(request.getHeaders().get(HttpHeader.COOKIE))) { + response.getHeaders().add(HttpHeader.LOCATION, "/user/login.html"); + response.send(HttpStatusCode.FOUND); + throw new InvalidRequestException("์ž˜๋ชป๋œ ๋กœ๊ทธ์ธ ์š”์ฒญ"); + } + } +}
Java
TemplateView๋ฅผ ๋งŒ๋“œ๋Š” ๋ถ€๋ถ„๋„ Controller ๋ณด๋‹ค๋Š” ๋‹ค๋ฅธ ๊ณณ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,32 @@ +package webserver.controller; + +import webserver.domain.HttpHeader; +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; +import webserver.domain.HttpStatusCode; +import webserver.exceptions.InvalidRequestException; + +import java.io.IOException; + +import static service.JwpService.getProfilePage; +import static service.JwpService.isLogin; + +public class ListUserController extends AbstractController { + @Override + public void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { + try { + httpResponse.send(HttpStatusCode.OK, getProfilePage()); + } catch (IOException e) { + httpResponse.send(HttpStatusCode.INTERNAL_SERVER_ERROR, "์ฝ์–ด๋“ค์ด๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค"); + } + } + + @Override + public void validateGetRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + if (!isLogin(request.getHeaders().get(HttpHeader.COOKIE))) { + response.getHeaders().add(HttpHeader.LOCATION, "/user/login.html"); + response.send(HttpStatusCode.FOUND); + throw new InvalidRequestException("์ž˜๋ชป๋œ ๋กœ๊ทธ์ธ ์š”์ฒญ"); + } + } +}
Java
login ์ƒํƒœ์ธ์ง€ ์—ฌ๋ถ€๋ฅผ Request๊ฐ€ ์•Œ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? ํ•ด๋‹น ํ–‰๋™์„ Request์—๊ฒŒ ์œ„์ž„ํ•ด์ค์‹œ๋‹ค!
@@ -0,0 +1,46 @@ +package webserver.controller; + +import db.DataBase; +import model.User; +import webserver.domain.*; +import webserver.exceptions.InvalidRequestException; + +public class LoginController extends AbstractController { + private static final String INVALID_REQUEST_MESSAGE = "userId, password๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค"; + @Override + public void doPost(HttpRequest httpRequest, HttpResponse httpResponse) { + User findUser = DataBase.findUserById(httpRequest.getParameters().get("userId")); + if(findUser == null){ + sendLoginFailed(httpResponse); + return; + } + if (!findUser.getPassword().equals(httpRequest.getParameters().get("password"))) { + sendLoginFailed(httpResponse); + return; + } + sendLoginSucceeded(httpResponse); + } + + @Override + public void validatePostRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + HttpParameters httpParameters = request.getParameters(); + if (httpParameters.contain("userId") && httpParameters.contain("password")) { + return; + } + response.send(HttpStatusCode.BAD_REQUEST, INVALID_REQUEST_MESSAGE); + throw new InvalidRequestException(INVALID_REQUEST_MESSAGE); + } + + private void sendLoginSucceeded(HttpResponse httpResponse) { + httpResponse.getHeaders().add(HttpHeader.SET_COOKIE, "logined=true; Path=/"); + httpResponse.getHeaders().add(HttpHeader.LOCATION, "/index.html"); + httpResponse.send(HttpStatusCode.FOUND); + } + + private void sendLoginFailed(HttpResponse httpResponse) { + httpResponse.getHeaders().add(HttpHeader.SET_COOKIE, "logined=false; Path=/"); + httpResponse.getHeaders().add(HttpHeader.LOCATION, "/user/login_failed.html"); + httpResponse.send(HttpStatusCode.FOUND); + } + +}
Java
response header์— cookie, location ์…‹ํŒ…ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์€ Response๋กœ ์ฑ…์ž„์„ ์ด๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? Controller์—์„œ ๋กœ๊ทธ์ธ์—ฌ๋ถ€์™€ location๋งŒ httpResponse์—๊ฒŒ ๋„˜๊ธฐ๋ฉด ์•Œ์•„์„œ ์…‹ํŒ… ๋˜์–ด์•ผํ•  ๊ฒƒ ๊ฐ™์•„์š” :)
@@ -0,0 +1,80 @@ +package webserver.domain; + +import utils.IOUtils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + + +public class HttpRequest { + private final HttpHeaders headers; + private final HttpParameters parameters; + private HttpMethod method; + private String body; + private String path; + + public HttpRequest(InputStream in) throws IOException { + headers = new HttpHeaders(); + parameters = new HttpParameters(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + readFirstLine(reader); + readHeaders(reader); + if (headers.contain(HttpHeader.CONTENT_LENGTH)) { + body = IOUtils.readData(reader, Integer.parseInt(headers.get(HttpHeader.CONTENT_LENGTH))); + parameters.parseAndSet(body); + } + } + + private void readFirstLine(BufferedReader reader) throws IOException { + String[] firstLineTokens = reader.readLine().split(" "); + String requestUrl = firstLineTokens[1]; + method = HttpMethod.valueOf(firstLineTokens[0]); + parseUrl(requestUrl); + } + + private void readHeaders(BufferedReader reader) throws IOException { + String line = reader.readLine(); + while (isValid(line)) { + String[] header = line.split(": "); + headers.add(header[0], header[1]); + line = reader.readLine(); + } + } + + private boolean isValid(String line) { + return (line != null) && !"".equals(line); + } + + private void parseUrl(String requestUrl) { + if (requestUrl.indexOf('?') == -1) { + path = requestUrl; + return; + } + String[] urlToken = requestUrl.split("\\?"); + path = urlToken[0]; + parameters.parseAndSet(urlToken[1]); + } + + public HttpHeaders getHeaders() { + return headers; + } + + public HttpParameters getParameters() { + return parameters; + } + + public HttpMethod getMethod() { + return method; + } + + public String getBody() { + return body; + } + + public String getPath() { + return path; + } +}
Java
Request-Line์„ ํŒŒ์‹ฑํ•  ๋•Œ 1, 0 ๊ณผ ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ์ฐพ๊ธฐ ์–ด๋ ค์šด ๋กœ์ง์ด ์กด์žฌํ•˜๋Š”๋ฐ์š”! firstLine์€ ์ŠคํŽ™์ƒ Method SP Request-URI SP HTTP-Version CRLF ์œผ๋กœ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์ŠคํŽ™์„ ๋งค์น˜ ํŒจํ„ด์œผ๋กœ ์ •์˜ํ•˜๊ณ  ๊ทธ์— ๋งž๊ฒŒ Index ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•ด์ฃผ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,21 @@ +package webserver.controller; + +import utils.FileIoUtils; +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; +import webserver.domain.HttpStatusCode; + +import java.io.IOException; +import java.net.URISyntaxException; + +public class DefaultController extends AbstractController { + @Override + public void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { + try { + httpResponse.send(HttpStatusCode.OK, FileIoUtils.readStaticHttpFile(httpRequest.getPath())); + } catch (URISyntaxException | IOException e) { + httpResponse.send(HttpStatusCode.NOT_FOUND); + } + } + +}
Java
์›นํŽ˜์ด์ง€๊ฐ€ ์ž˜ ๋œจ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ์„œ ์†Œ์Šค๋Š” ์ž˜ ๋ฐ›์•„์˜ค๋Š”๋ฐ ์™œ ๊ทธ๋žฌ๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ, ๋ฒ„๊ทธ์˜€๋„ค์š”. ์ž˜ ๊ณ ์ณ์„œ ๋ฐ˜์˜ํ• ๊ฒŒ์š”!
@@ -0,0 +1,80 @@ +package webserver.domain; + +import utils.IOUtils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + + +public class HttpRequest { + private final HttpHeaders headers; + private final HttpParameters parameters; + private HttpMethod method; + private String body; + private String path; + + public HttpRequest(InputStream in) throws IOException { + headers = new HttpHeaders(); + parameters = new HttpParameters(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + readFirstLine(reader); + readHeaders(reader); + if (headers.contain(HttpHeader.CONTENT_LENGTH)) { + body = IOUtils.readData(reader, Integer.parseInt(headers.get(HttpHeader.CONTENT_LENGTH))); + parameters.parseAndSet(body); + } + } + + private void readFirstLine(BufferedReader reader) throws IOException { + String[] firstLineTokens = reader.readLine().split(" "); + String requestUrl = firstLineTokens[1]; + method = HttpMethod.valueOf(firstLineTokens[0]); + parseUrl(requestUrl); + } + + private void readHeaders(BufferedReader reader) throws IOException { + String line = reader.readLine(); + while (isValid(line)) { + String[] header = line.split(": "); + headers.add(header[0], header[1]); + line = reader.readLine(); + } + } + + private boolean isValid(String line) { + return (line != null) && !"".equals(line); + } + + private void parseUrl(String requestUrl) { + if (requestUrl.indexOf('?') == -1) { + path = requestUrl; + return; + } + String[] urlToken = requestUrl.split("\\?"); + path = urlToken[0]; + parameters.parseAndSet(urlToken[1]); + } + + public HttpHeaders getHeaders() { + return headers; + } + + public HttpParameters getParameters() { + return parameters; + } + + public HttpMethod getMethod() { + return method; + } + + public String getBody() { + return body; + } + + public String getPath() { + return path; + } +}
Java
๋งค์น˜ ํŒจํ„ด ์ข‹๋„ค์š” ์•„์ง ๋ฐ˜์˜์„ ๋ชปํ•˜์˜€์ง€๋งŒ ๋งค์น˜ ํŒจํ„ด์ด๋ผ๋Š” ์•„์ด๋””์–ด ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,46 @@ +package webserver.controller; + +import db.DataBase; +import model.User; +import webserver.domain.*; +import webserver.exceptions.InvalidRequestException; + +public class LoginController extends AbstractController { + private static final String INVALID_REQUEST_MESSAGE = "userId, password๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค"; + @Override + public void doPost(HttpRequest httpRequest, HttpResponse httpResponse) { + User findUser = DataBase.findUserById(httpRequest.getParameters().get("userId")); + if(findUser == null){ + sendLoginFailed(httpResponse); + return; + } + if (!findUser.getPassword().equals(httpRequest.getParameters().get("password"))) { + sendLoginFailed(httpResponse); + return; + } + sendLoginSucceeded(httpResponse); + } + + @Override + public void validatePostRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + HttpParameters httpParameters = request.getParameters(); + if (httpParameters.contain("userId") && httpParameters.contain("password")) { + return; + } + response.send(HttpStatusCode.BAD_REQUEST, INVALID_REQUEST_MESSAGE); + throw new InvalidRequestException(INVALID_REQUEST_MESSAGE); + } + + private void sendLoginSucceeded(HttpResponse httpResponse) { + httpResponse.getHeaders().add(HttpHeader.SET_COOKIE, "logined=true; Path=/"); + httpResponse.getHeaders().add(HttpHeader.LOCATION, "/index.html"); + httpResponse.send(HttpStatusCode.FOUND); + } + + private void sendLoginFailed(HttpResponse httpResponse) { + httpResponse.getHeaders().add(HttpHeader.SET_COOKIE, "logined=false; Path=/"); + httpResponse.getHeaders().add(HttpHeader.LOCATION, "/user/login_failed.html"); + httpResponse.send(HttpStatusCode.FOUND); + } + +}
Java
Response์— ๋„˜๊ฒจ์ฃผ๋ ค๊ณ  ํ•˜์˜€์ง€๋งŒ Response๊ฐ€ ์ˆœ์ˆ˜ํ•˜๊ฒŒ HTTP ์ŠคํŽ™์— ๋”ฐ๋ฅธ ์ „์†ก๋งŒ ๋˜๋„๋ก ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์งœ์—ฌ์ ธ์žˆ์–ด์„œ ์ค‘๊ฐ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๊ณ ๋ฏผํ•ด๋ณด๊ณ  ์žˆ์–ด์š”.
@@ -0,0 +1,32 @@ +package webserver.controller; + +import webserver.domain.HttpHeader; +import webserver.domain.HttpRequest; +import webserver.domain.HttpResponse; +import webserver.domain.HttpStatusCode; +import webserver.exceptions.InvalidRequestException; + +import java.io.IOException; + +import static service.JwpService.getProfilePage; +import static service.JwpService.isLogin; + +public class ListUserController extends AbstractController { + @Override + public void doGet(HttpRequest httpRequest, HttpResponse httpResponse) { + try { + httpResponse.send(HttpStatusCode.OK, getProfilePage()); + } catch (IOException e) { + httpResponse.send(HttpStatusCode.INTERNAL_SERVER_ERROR, "์ฝ์–ด๋“ค์ด๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์Šต๋‹ˆ๋‹ค"); + } + } + + @Override + public void validateGetRequest(HttpRequest request, HttpResponse response) throws InvalidRequestException { + if (!isLogin(request.getHeaders().get(HttpHeader.COOKIE))) { + response.getHeaders().add(HttpHeader.LOCATION, "/user/login.html"); + response.send(HttpStatusCode.FOUND); + throw new InvalidRequestException("์ž˜๋ชป๋œ ๋กœ๊ทธ์ธ ์š”์ฒญ"); + } + } +}
Java
Request ์— ์œ„์ž„ํ•˜๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋งคํ•‘ํ•˜๊ณ , ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” Request ์—ญํ• ๊นŒ์ง€๋งŒ ๋งก๊ธด ์ƒํƒœ๋ผ ์ค‘๊ฐ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ณ ๋ฏผ์„ ํ•ด๋ณด์•˜์–ด์š”. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€, Spring ์— ์žˆ๋Š” ArgumentResolver ์™€ ์œ ์‚ฌํ•˜๊ฒŒ, ๊ฒ€์ฆ์ด ํ•„์š”ํ•œ ๋•Œ์—๋งŒ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜๋„๋ก AbstractController์— validate...Request ๋ผ๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ์œ ์‚ฌํ•œ ์ปจํŠธ๋กค๋Ÿฌ์— ๋Œ€ํ•ด์„œ ์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค๋Š” ๋ถ€๋‹ด์ด ์žˆ๋Š”๋ฐ, ๋กœ๊ทธ์ธ ์„œ๋น„์Šค๋ฅผ ํ†ตํ•ฉํ•˜๋Š” ์ถ”์ƒ ๊ฐ์ฒด์ธ ๋กœ๊ทธ์ธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜, ๋‚˜์ค‘์— MVC ๋ชจ๋ธ์ธ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ ์šฉํ• ๊นŒํ•˜๋Š”๋ฐ ์ด๋Ÿฐ ๋ฐฉ์‹์€ ์–ด๋–ค๊ฐ€์š”?
@@ -0,0 +1,45 @@ +package controller; + +import controller.handler.Handler; +import exception.utils.NoFileException; +import model.request.HttpRequest; +import model.response.HttpResponse; +import model.PathInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public abstract class Controller { + private static final Logger log = LoggerFactory.getLogger( Controller.class ); + protected final Map<PathInfo, Handler> handlers = new HashMap<>(); + protected String basePath = ""; + + public void putHandler(String path, String method, Handler handler) { + handlers.put(new PathInfo(basePath + path, method), handler); + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public boolean hasSameBasePath(String path) { + return path.startsWith(basePath); + } + + public boolean handle(HttpRequest request, HttpResponse response) throws NoFileException, IOException { + for (Map.Entry<PathInfo, Handler> entry : handlers.entrySet()) { + log.info("matching {} with controller {}", request.getPath(), entry.getKey().getPath()); + + if (request.getPath().matches(entry.getKey().getPath())) { + log.info("handling {} with controller {}", request.getPath(), entry.getKey().getPath()); + entry.getValue().handle(request, response); + return true; + } + } + return false; + } +}
Java
handlers ๋Š” ๊ตณ์ด LinkedHashMap ์„ ์‚ฌ์šฉํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,30 @@ +package model; + +import java.util.Objects; + +public class PathInfo { + private final String path; + private final String method; + + public PathInfo(String path, String method) { + this.path = path; + this.method = method; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PathInfo that = (PathInfo) o; + return Objects.equals(path, that.path) && Objects.equals(method, that.method); + } + + @Override + public int hashCode() { + return Objects.hash(path, method); + } + + public String getPath() { + return path; + } +}
Java
PathInfo ๊ฐ์ฒด๋ฅผ ๋ณ„๋„๋กœ ์„ ์–ธํ•˜์…จ๊ตฐ์š” ๐Ÿ’ฏ
@@ -0,0 +1,44 @@ +package controller; + +import controller.handler.SecuredHandler; +import model.request.HttpRequest; +import model.response.HttpResponse; +import service.UserService; +import view.View; + +import java.io.IOException; +import java.io.OutputStream; + +public class UserController extends Controller { + { + setBasePath("/user"); + putHandler("/create", "POST", this::handleCreate); + putHandler("/login", "POST", this::handleLogin); + putHandler("/list", "GET", new SecuredHandler(this::handleUserList)); + putHandler("/logout", "GET", new SecuredHandler(this::handleLogout)); + } + + private UserService userService = new UserService(); + + public void handleCreate(HttpRequest request, HttpResponse response) { + userService.addUser(request.getParsedBody()); + response.sendRedirect("/index.html"); + } + + public void handleLogin(HttpRequest request, HttpResponse response) { + if (!userService.login(request.getParsedBody())) { + response.setCookie("logined=false").sendRedirect("/user/login_failed.html"); + return; + } + response.setCookie("logined=true").sendRedirect("/index.html"); + } + + public void handleUserList(HttpRequest request, HttpResponse response) throws IOException { + byte[] body = View.getUsersView(userService.findAll(), "/user/list"); + response.sendView(body); + } + + public void handleLogout(HttpRequest request, HttpResponse response) throws IOException { + response.setCookie("logined=false").sendRedirect("/index.html"); + } +}
Java
DataBase ์— ์ง์ ‘ ์ ‘๊ทผํ•˜์ง€ ๋ง๊ณ  ๋ณ„๋„์˜ ์„œ๋น„์Šค ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,44 @@ +package controller; + +import controller.handler.SecuredHandler; +import model.request.HttpRequest; +import model.response.HttpResponse; +import service.UserService; +import view.View; + +import java.io.IOException; +import java.io.OutputStream; + +public class UserController extends Controller { + { + setBasePath("/user"); + putHandler("/create", "POST", this::handleCreate); + putHandler("/login", "POST", this::handleLogin); + putHandler("/list", "GET", new SecuredHandler(this::handleUserList)); + putHandler("/logout", "GET", new SecuredHandler(this::handleLogout)); + } + + private UserService userService = new UserService(); + + public void handleCreate(HttpRequest request, HttpResponse response) { + userService.addUser(request.getParsedBody()); + response.sendRedirect("/index.html"); + } + + public void handleLogin(HttpRequest request, HttpResponse response) { + if (!userService.login(request.getParsedBody())) { + response.setCookie("logined=false").sendRedirect("/user/login_failed.html"); + return; + } + response.setCookie("logined=true").sendRedirect("/index.html"); + } + + public void handleUserList(HttpRequest request, HttpResponse response) throws IOException { + byte[] body = View.getUsersView(userService.findAll(), "/user/list"); + response.sendView(body); + } + + public void handleLogout(HttpRequest request, HttpResponse response) throws IOException { + response.setCookie("logined=false").sendRedirect("/index.html"); + } +}
Java
์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ง์ ‘ DataBase ์— ์ ‘๊ทผํ•˜๊ธฐ ๋ณด๋‹ค ์„œ๋น„์Šค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–จ๊นŒ์š”? ๋„˜๊ฒจ์ค€ ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋กœ๊ทธ์ธ ๋˜์—ˆ๋Š”์ง€ ๋˜์ง€ ์•Š์•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ boolean ๊ฐ’์œผ๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข€ ๋” ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋กœ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,45 @@ +package controller; + +import controller.handler.Handler; +import exception.utils.NoFileException; +import model.request.HttpRequest; +import model.response.HttpResponse; +import model.PathInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public abstract class Controller { + private static final Logger log = LoggerFactory.getLogger( Controller.class ); + protected final Map<PathInfo, Handler> handlers = new HashMap<>(); + protected String basePath = ""; + + public void putHandler(String path, String method, Handler handler) { + handlers.put(new PathInfo(basePath + path, method), handler); + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public boolean hasSameBasePath(String path) { + return path.startsWith(basePath); + } + + public boolean handle(HttpRequest request, HttpResponse response) throws NoFileException, IOException { + for (Map.Entry<PathInfo, Handler> entry : handlers.entrySet()) { + log.info("matching {} with controller {}", request.getPath(), entry.getKey().getPath()); + + if (request.getPath().matches(entry.getKey().getPath())) { + log.info("handling {} with controller {}", request.getPath(), entry.getKey().getPath()); + entry.getValue().handle(request, response); + return true; + } + } + return false; + } +}
Java
handle ๋ฉ”์„œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” HttpRequest ์ธ๋ฐ, ๋‘๋ฒˆ์งธ ์ธ์ž๋Š” OutputStream ์ด๋„ค์š”. HttpRequest / HttpResponse ํ˜•ํƒœ๊ฐ€ ๋” ์ ํ•ฉํ•ด ๋ณด์ž…๋‹ˆ๋‹ค ๐Ÿ˜„
@@ -0,0 +1,66 @@ +package controller; + +import controller.error.ErrorHandlerFactory; +import controller.handler.Handler; +import exception.utils.NoFileException; +import model.request.HttpRequest; +import model.response.HttpResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import webserver.RequestHandler; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class ControllerManager { + private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); + private static final List<Controller> controllers = new ArrayList<>(); + static { + registerController(new ViewController()); + registerController(new UserController()); + } + + public static void registerController(Controller controller) { + controllers.add(controller); + } + + public static void runControllers(Socket connection) { + try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) { + HttpRequest httpRequest = HttpRequest.of(in); + HttpResponse httpResponse = HttpResponse.of(out); + handleRequest(httpRequest, httpResponse); + } catch (IOException e) { + log.error("{} {}", e.getMessage(), e.getStackTrace()); + } + } + + private static void handleRequest(HttpRequest httpRequest, HttpResponse httpResponse) { + List<Controller> selectedControllers = controllers.stream() + .filter(controller -> controller.hasSameBasePath(httpRequest.getPath())) + .collect(Collectors.toList()); + try { + for (Controller controller : selectedControllers) { + if (controller.handle(httpRequest, httpResponse)) return; + } + + defaultHandle(httpRequest, httpResponse); + } catch (NoFileException | IOException | RuntimeException e) { + Handler errorHandler = ErrorHandlerFactory.getHandler(e); + try { + errorHandler.handle(httpRequest, httpResponse); + log.warn(e.getMessage()); + } catch (Exception handleError) { + log.error("{} {}", handleError.getMessage(), handleError.getStackTrace()); + } + } + } + + public static void defaultHandle(HttpRequest request, HttpResponse response) throws NoFileException { + response.forward("./templates", request.getPath()); + } +}
Java
์ปจํŠธ๋กค๋Ÿฌ ๋งค๋‹ˆ์ €๊นŒ์ง€ ์ž‘์„ฑํ•˜์…จ๋„ค์š” ๐Ÿ‘
@@ -1,16 +1,48 @@ package model; +import exception.user.WrongEmailException; +import exception.user.WrongIdException; +import exception.user.WrongNameException; +import exception.user.WrongPasswordException; + public class User { - private String userId; - private String password; - private String name; - private String email; + private final String userId; + private final String password; + private final String name; + private final String email; public User(String userId, String password, String name, String email) { - this.userId = userId; - this.password = password; - this.name = name; - this.email = email; + this.userId = validateId(userId); + this.password = validatePw(password); + this.name = validateName(name); + this.email = validateEmail(email.replace("%40", "@")); + } + + private String validatePw(String password) { + if(!password.matches("^[\\w]{1,30}$")){ + throw new WrongPasswordException(); + } + return password; + } + + private String validateId(String userId) { + if(!userId.matches("^[\\w]{1,30}$")) + throw new WrongIdException(); + return userId; + } + + private String validateName(String name) { + if (!name.matches("^[\\w๊ฐ€-ํžฃ]{1,30}$")) { + throw new WrongNameException(); + } + return name; + } + + private String validateEmail(String email) { + if(!email.matches("^[\\w]{1,30}@[\\w]{1,30}.[\\w]{1,30}$")){ + throw new WrongEmailException(); + } + return email; } public String getUserId() {
Java
validation ๋กœ์ง์„ ์ถ”๊ฐ€ํ•˜์‹  ๊ฒƒ์€ ๊ธ์ •์ ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ User ๊ฐ์ฒด๊ฐ€ ์ธ์Šคํ„ด์Šคํ™” ๋  ๋•Œ๋งˆ๋‹ค validate ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ฒŒ ๋˜๋Š”๋ฐ์š”. User ๋ผ๋Š” ๋„๋ฉ”์ธ ๋ชจ๋ธ์ด ๊ฐ–์ถ”๊ณ  ์žˆ์–ด์•ผํ•  ํ•„์ˆ˜์ ์ธ validation ์ธ์ง€ ์•„๋‹ˆ๋ฉด ์ƒˆ๋กœ์šด User ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์—์„œ๋งŒ ํ•„์š”ํ•œ validation ์ธ์ง€์— ๋”ฐ๋ผ์„œ User ๊ฐ์ฒด์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ์ ํ•ฉํ•  ์ˆ˜๋„ ์žˆ๊ณ  / Controller ์˜์—ญ์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚˜์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ถ€๋ถ„์€ ํ•œ๋ฒˆ ๊ณ ๋ฏผํ•ด๋ด์ฃผ์„ธ์š” ๐Ÿ˜„
@@ -0,0 +1,7 @@ +package exception.utils; + +public class NoFileException extends Exception { + public NoFileException(String path) { + super(path + " ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } +}
Java
NoFIleException ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†๋Š”์ง€ ํ‘œ์‹œ๋  ์ˆ˜ ์žˆ๊ฒŒ ๊ฐœ์„ ๋˜๋ฉด ๋” ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,30 @@ +package controller; + +import exception.utils.NoFileException; +import model.request.HttpRequest; +import model.response.HttpResponse; + +import java.io.IOException; +import java.io.OutputStream; + +public class ViewController extends Controller { + + { + setBasePath(""); + putHandler("/js/.*", "GET", this::handleFile); + putHandler("/css/.*", "GET", this::handleFile); + putHandler("/fonts/.*", "GET", this::handleFile); + putHandler("/favicon.ico", "GET", this::handleFile); + putHandler("/index.html", "GET", this::handleView); + putHandler("/", "GET", this::handleView); + } + + public void handleFile(HttpRequest request, HttpResponse response) throws NoFileException { + response.forward("./static", request.getPath()); + } + + public void handleView(HttpRequest request, HttpResponse response) throws NoFileException { + response.forward("./templates", + (request.getPath().equals("/") ? "/index.html" : request.getPath())); + } +}
Java
![image](https://user-images.githubusercontent.com/1266944/106766656-68cad300-667d-11eb-9e7b-52fffd02632d.png) Fonts ํŒŒ์ผ์ด ๋กœ๋”ฉ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ™•์ธ ๋ถ€ํƒ๋“œ๋ ค์š” :smile
@@ -0,0 +1,45 @@ +package controller; + +import controller.handler.Handler; +import exception.utils.NoFileException; +import model.request.HttpRequest; +import model.response.HttpResponse; +import model.PathInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public abstract class Controller { + private static final Logger log = LoggerFactory.getLogger( Controller.class ); + protected final Map<PathInfo, Handler> handlers = new HashMap<>(); + protected String basePath = ""; + + public void putHandler(String path, String method, Handler handler) { + handlers.put(new PathInfo(basePath + path, method), handler); + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } + + public boolean hasSameBasePath(String path) { + return path.startsWith(basePath); + } + + public boolean handle(HttpRequest request, HttpResponse response) throws NoFileException, IOException { + for (Map.Entry<PathInfo, Handler> entry : handlers.entrySet()) { + log.info("matching {} with controller {}", request.getPath(), entry.getKey().getPath()); + + if (request.getPath().matches(entry.getKey().getPath())) { + log.info("handling {} with controller {}", request.getPath(), entry.getKey().getPath()); + entry.getValue().handle(request, response); + return true; + } + } + return false; + } +}
Java
Handlers ๊ฐ€ ์ˆœ์„œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋“ฑ๋ก๋˜์–ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค๋ฉด ์ฐจํ›„์— ์‹ค์ˆ˜ํ•˜๊ธฐ ์‰ฌ์šด ๊ตฌ์กฐ ์ž…๋‹ˆ๋‹ค. path ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•  handler ๊ฐ€ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ๋งค์นญ๋  ์ˆ˜๋Š” ์—†์„์ง€ ๊ณ ๋ฏผํ•ด๋ด์ฃผ์„ธ์š” ๐Ÿ˜„
@@ -0,0 +1,85 @@ +package model.request; + +import utils.IOUtils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class HttpRequest { + private HttpRequestLine httpRequestLine; + private HttpRequestHeader httpRequestHeader; + + private String body = ""; + + private HttpRequest(BufferedReader br) throws IOException { + String line = br.readLine(); + httpRequestLine = new HttpRequestLine(line); + httpRequestHeader = new HttpRequestHeader(br); + setBody(br); + } + + public static HttpRequest of(InputStream in) throws IOException { + return new HttpRequest(new BufferedReader(new InputStreamReader(in))); + } + + private void setBody(BufferedReader br) throws IOException { + while(br.ready()){ + body = IOUtils.readData(br, Integer.parseInt(getHeader("Content-Length"))); + } + } + + public String getBody() { + return body; + } + + public Map<String, String> getParsedBody(){ + Map<String, String> parsedBody = new HashMap<>(); + String[] queryParams = body.split("&"); + Arrays.stream(queryParams).forEach((param) -> { + String[] args = param.split("=", 2); + parsedBody.put(args[0].trim(), args[1].trim()); + }); + return parsedBody; + } + + public String getCookie(String cookieParam){ + Map<String, String> parsedCookie = new HashMap<>(); + String cookieHeader = getHeader("Cookie"); + + String[] cookies = cookieHeader.split(";"); + Arrays.stream(cookies).forEach((cookie) -> { + String[] args = cookie.split("=", 2); + parsedCookie.put(args[0].trim(), args[1].trim()); + }); + return parsedCookie.get(cookieParam); + } + + public Map<String, String> getHeaderMap() { + return httpRequestHeader.getHeaderMap(); + } + + public String getHeader(String key){ + return httpRequestHeader.getHeader(key); + } + + public String getMethod() { + return httpRequestLine.getMethod(); + } + + public String getPath() { + return httpRequestLine.getPath(); + } + + public String getProtocol() { + return httpRequestLine.getProtocol(); + } + + public Map<String, String> getQueryParameterMap() { + return httpRequestLine.getQueryParameterMap(); + } +}
Java
InputStream ์„ ๋ฐ›์•„์„œ HttpRequest ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” static method ๋กœ ๋ณ€๊ฒฝํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
```suggestion className={`loginBtn ${ idValue.indexOf('@') > -1 && pwValue.length >= 5 ? 'loginBtnLive' : '' }`} ``` className์˜ ๋™์  ์‚ฌ์šฉ์„ ์ ์šฉํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ์ธ๋ผ์ธ ์Šคํƒ€์ผ์˜ ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๋†’์•„์„œ, ํŒ€์›์ด class๋ฅผ ์ˆ˜์ •ํ–ˆ์„ ๋•Œ class๊ฐ€ ์ ์šฉ์ด ์•ˆ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด์š”. ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•ด์„œ className์„ ๋™์ ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ์Œ className={์›๋ž˜ ํด๋ž˜์Šค +ํ™œ์„ฑํ™” ํด๋ž˜์Šค <-- ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋กœ ํด๋ž˜์Šค ์ถ”๊ฐ€ ์—ฌ๋ถ€ ๊ฒฐ์ •}
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
className ๋™์  ์‚ฌ์šฉ์„ ํ•˜๊ฒŒ ๋˜๋ฉด style ์Šคํ…Œ์ดํŠธ๊ฐ€ ํ•„์š”์—†๊ฒŒ ๋œ๋‹ค๊ณ  ํ•ด์š”! disabled๋„ state๋กœ ๋‚จ๊ฒจ๋‘˜์ง€ ๊ณ ๋ฏผํ•ด๋ณด๋ผ๊ณ  ํ•˜์‹ญ๋‹ˆ๋‹ค (from ๋ฉ˜ํ† ๋‹˜..)
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
className ๋™์  ์‚ฌ์šฉ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ๋‹ค ์ง€์›Œ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
```suggestion import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import './Login.scss'; ``` scss๋ฅผ ๋ฐ‘์œผ๋กœ ๋‚ด๋ ค์ฃผ๋ฉด ์ข‹์„๊ฑฐ๊ฐ™๋„ค์š”
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
ํ›… ๋ฐ‘์œผ๋กœ ๋ถ™์—ฌ์ฃผ๋Š”๊ฒŒ ์ข‹์ง€ ์•Š์„๊นŒ์š”?
@@ -1,8 +1,43 @@ -import './Main.scss'; +import { useState, useEffect } from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from './Feed'; +import MainRight from './MainRight'; import '../../../styles/variables.scss'; +import './Main.scss'; function Main() { - return <></>; + const [articles, setArticles] = useState([]); + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/feedData.json') + .then(res => res.json()) + .then(data => { + setArticles(data); + }); + }, []); + + return ( + <div className="main"> + <Nav /> + <main> + <div className="feeds"> + {articles.map((content, index) => { + return ( + <div key={index}> + <Feed + userName={content.userName} + feedImg={content.feedImg} + userMan={content.userMan} + profileLogo={content.profileLogo} + /> + </div> + ); + })} + </div> + <MainRight /> + </main> + </div> + ); } export default Main;
JavaScript
์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„์„ Feed ๋กœ ๋ฐ”๊ฟ”๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!! ์ด๋ฆ„์— ์˜๋ฏธ๋ฅผ ๋” ๋‹ด์•„์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -1,8 +1,43 @@ -import './Main.scss'; +import { useState, useEffect } from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from './Feed'; +import MainRight from './MainRight'; import '../../../styles/variables.scss'; +import './Main.scss'; function Main() { - return <></>; + const [articles, setArticles] = useState([]); + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/feedData.json') + .then(res => res.json()) + .then(data => { + setArticles(data); + }); + }, []); + + return ( + <div className="main"> + <Nav /> + <main> + <div className="feeds"> + {articles.map((content, index) => { + return ( + <div key={index}> + <Feed + userName={content.userName} + feedImg={content.feedImg} + userMan={content.userMan} + profileLogo={content.profileLogo} + /> + </div> + ); + })} + </div> + <MainRight /> + </main> + </div> + ); } export default Main;
JavaScript
mainRight๋„ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด main.js ๋‚ด์šฉ, scss ๋‚ด์šฉ๋„ ๋” ๊ฐ„๋‹จํ•ด์งˆ ๊ฒƒ ๊ฐ™์Œ! ```suggestion <MainRight /> ```
@@ -1,8 +1,43 @@ -import './Main.scss'; +import { useState, useEffect } from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from './Feed'; +import MainRight from './MainRight'; import '../../../styles/variables.scss'; +import './Main.scss'; function Main() { - return <></>; + const [articles, setArticles] = useState([]); + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/feedData.json') + .then(res => res.json()) + .then(data => { + setArticles(data); + }); + }, []); + + return ( + <div className="main"> + <Nav /> + <main> + <div className="feeds"> + {articles.map((content, index) => { + return ( + <div key={index}> + <Feed + userName={content.userName} + feedImg={content.feedImg} + userMan={content.userMan} + profileLogo={content.profileLogo} + /> + </div> + ); + })} + </div> + <MainRight /> + </main> + </div> + ); } export default Main;
JavaScript
```suggestion <Footer /> ``` ์ปดํฌ๋„ŒํŠธ๋กœ ๋ถ„๋ฆฌํ•ด์ฃผ๊ณ , footer ์‹œ๋ฉ˜ํ‹ฑ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์ข‹์„ ๋“ฏ!
@@ -1,8 +1,43 @@ -import './Main.scss'; +import { useState, useEffect } from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from './Feed'; +import MainRight from './MainRight'; import '../../../styles/variables.scss'; +import './Main.scss'; function Main() { - return <></>; + const [articles, setArticles] = useState([]); + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/feedData.json') + .then(res => res.json()) + .then(data => { + setArticles(data); + }); + }, []); + + return ( + <div className="main"> + <Nav /> + <main> + <div className="feeds"> + {articles.map((content, index) => { + return ( + <div key={index}> + <Feed + userName={content.userName} + feedImg={content.feedImg} + userMan={content.userMan} + profileLogo={content.profileLogo} + /> + </div> + ); + })} + </div> + <MainRight /> + </main> + </div> + ); } export default Main;
JavaScript
๊ฐ ๋ฉ”๋‰ด๋ฅผ JS ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ , map ๋ฉ”์†Œ๋“œ๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•ด๋ณด๋ฉด ์ข‹์„ ๋“ฏํ•ด์šฉ
@@ -0,0 +1,16 @@ +.main { + background-color: #fafafa; + + main { + display: flex; + justify-content: center; + align-items: flex-start; + margin: 0px 8px 8px 8px; + + .feeds { + width: 750px; + padding-top: 80px; + padding-bottom: 30px; + } + } +}
Unknown
์ด๋Ÿฐ ์„ ํƒ์ž ํ™œ์šฉ์œผ๋กœ ์Šคํฌ๋กค ๊ฐ์ถ”์‹  ๋ถ€๋ถ„ ์ข‹๋„ค์š”..
@@ -1,8 +1,43 @@ -import './Main.scss'; +import { useState, useEffect } from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from './Feed'; +import MainRight from './MainRight'; import '../../../styles/variables.scss'; +import './Main.scss'; function Main() { - return <></>; + const [articles, setArticles] = useState([]); + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/feedData.json') + .then(res => res.json()) + .then(data => { + setArticles(data); + }); + }, []); + + return ( + <div className="main"> + <Nav /> + <main> + <div className="feeds"> + {articles.map((content, index) => { + return ( + <div key={index}> + <Feed + userName={content.userName} + feedImg={content.feedImg} + userMan={content.userMan} + profileLogo={content.profileLogo} + /> + </div> + ); + })} + </div> + <MainRight /> + </main> + </div> + ); } export default Main;
JavaScript
feedcontainer ์•ˆ feed
@@ -1,7 +1,49 @@ import './Nav.scss'; +import '../../styles/variables.scss'; +import { Link } from 'react-router-dom'; function Nav() { - return <></>; + return ( + <div className="nav"> + <nav className="navbar"> + <div className="navbarLeft1"> + <div className="navbarLeft1Inner"> + <Link to="/" className="navbarItem logoImg"> + <img alt="logoIcon" className="navIcon" src="/images/logo.png" /> + </Link> + <Link to="/" className="navbarItem logoFont"> + Westagram + </Link> + </div> + </div> + <div className="navbarLeft2"> + <div className="searchWrapper navbarItem"> + <img alt="searchIcon" src="/images/search.png" /> + <input placeholder="๊ฒ€์ƒ‰" /> + </div> + </div> + <div className="navbarRight"> + <Link to="/" className="navbarItem"> + <img + alt="exploreIcon" + className="navIcon" + src="/images/explore.png" + /> + </Link> + <Link to="/" className="navbarItem"> + <img alt="heartIcon" className="navIcon" src="/images/heart.png" /> + </Link> + <Link to="/" className="navbarItem"> + <img + alt="profileIcon" + className="navIcon" + src="/images/profile.png" + /> + </Link> + </div> + </nav> + </div> + ); } export default Nav;
JavaScript
๊ณตํ†ต ํŒŒ์ผ
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
state๋ผ๋Š” ๋ช…์นญ์ด ๋ฌด์Šจ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Œ. ๋ช…ํ™•ํ•œ ๋ช…์นญ์œผ๋กœ ์ˆ˜์ •
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
disabled, style ๋“ฑ์€ state๋กœ ๋งŒ๋“ค ํ•„์š” ์—†์Œ. [๋ฆฌ์•กํŠธ๋กœ ์‚ฌ๊ณ ํ•˜๊ธฐ](https://ko.reactjs.org/docs/thinking-in-react.html) ์ฐธ๊ณ  1. ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ props๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋ฉ๋‹ˆ๊นŒ? ๊ทธ๋Ÿฌ๋ฉด ํ™•์‹คํžˆ state๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. 2. ์‹œ๊ฐ„์ด ์ง€๋‚˜๋„ ๋ณ€ํ•˜์ง€ ์•Š๋‚˜์š”? ๊ทธ๋Ÿฌ๋ฉด ํ™•์‹คํžˆ state๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. 3. ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ๋‹ค๋ฅธ state๋‚˜ props๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•œ๊ฐ€์š”? ๊ทธ๋ ‡๋‹ค๋ฉด state๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
- ๊ธฐ๋Šฅ์„ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์€ ์ข‹์Œ - ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•  ํ•„์š” ์—†์ด, ์กฐ๊ฑด ์ž์ฒด๋กœ boolean์ด ๋  ์ˆ˜ ์žˆ์Œ ```suggestion const checkId = idValue => { return idValue.includes('@'); }; const checkPw = pwValue => { return pwValue.length >= 5; }; ``` - ์ถ”๊ฐ€์ ์œผ๋กœ, ```js const isInputValid = idValue.includes('@') && pwValue.length >= 5 ``` ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•œ๊บผ๋ฒˆ์— ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค์–ด ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Œ
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
๋ช…์นญ๋งŒ ๋ดค์„ ๋•Œ๋Š” id ๋ถ€๋ถ„๋งŒ ๊ด€๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ธ ๊ฒƒ ๊ฐ™์€๋ฐ, ๋™์ž‘์„ ๋ณด๋ฉด ์•„๋‹˜. ์ˆ˜์ •
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น! ```suggestion const { type, value } = event.target; if (type === 'text') { setState(value); } else if (type === 'password') { setPasswordValue(value); } ```
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
JS๋กœ ๊ตฌํ˜„ํ•˜๋˜ ๋ฐฉ์‹ ๊ทธ๋Œ€๋กœ ํ•˜์…จ๋Š”๋ฐ, ๊ธฐ์กด state๋กœ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ state
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
๊น€ํ˜œ์ง„ ๋ฉ˜ํ† ๋‹˜ ๋ฆฌ๋ทฐ ๋ฐ˜์˜ํ•˜๊ธฐ
@@ -1,7 +1,128 @@ +import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import './Login.scss'; function Login() { - return <></>; + // const [idState, setIdState] = useState(''); + // const [passwordValue, setPasswordValue] = useState(''); + // const [disabled, setDisabled] = useState(true); + // const [style, setStyle] = useState({ opacity: 0.5, cursor: 'default' }); + + const navigate = useNavigate(); + const [loginValues, setLoginValues] = useState({ + id: '', + password: '', + }); + + const handleInputValue = e => { + const { name, value } = e.target; + setLoginValues({ ...loginValues, [name]: value }); + }; + + // const { type, value } = event.target; + + // function handleLogin(event) { + // if (type === 'text') { + // setIdState(value); + // } else if (type === 'password') { + // setPasswordValue(value); + // } + // handleButton(); + // } + + // const checkId = idValue => { + // return idValue.includes('@') ? true : false; + // }; + + // const checkPw = pwValue => { + // return pwValue.length >= 5 ? true : false; + // }; + + // const handleButton = () => { + // const isValidId = checkId(state); + // const isValidPw = checkPw(passwordValue); + + // if (isValidId && isValidPw) { + // buttonColor(true); + // } else { + // buttonColor(false); + // } + // }; + + // const buttonColor = btnValid => { + // setDisabled(!btnValid); + // setStyle({ + // opacity: btnValid ? 1 : 0.5, + // cursor: btnValid ? 'pointer' : 'default', + // }); + // }; + + const goToMain = () => { + fetch('http://10.58.4.238:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: loginValues.id, + password: loginValues.password, + // name: '๋ž˜์˜', + // phone_number: '010-1111-111', + }), + }) + .then(response => response.json()) + .then(result => { + // console.log(result); + if (result.token) { + //localStorage.setItem('token', result.token); + alert('ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!'); + navigate('/main-han'); + } else if (result.message === 'INVALID_USER') { + alert('ID์™€ PW๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.'); + } + }); + }; + + const isInputValid = + loginValues.id.includes('@') && + loginValues.id.length >= 6 && + loginValues.password.length >= 6; + // console.log(loginValues.id.includes('@') && loginValues.id.length >= 6); + // console.log(loginValues.password.length >= 6); + // console.log(isInputValid); + + return ( + <div className="login"> + <form className="loginBox"> + <h1>Westagram</h1> + <input + id="id" + name="id" + className="userId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleInputValue} + /> + <input + id="password" + name="password" + className="userPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleInputValue} + /> + <button + type="button" + className={`loginButton ${isInputValid ? 'loginBtnLive' : ''}`} + onClick={goToMain} + disabled={!isInputValid} + //style={style} + > + ๋กœ๊ทธ์ธ + </button> + <div className="findPassword"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </form> + </div> + ); } export default Login;
JavaScript
+์‚ผํ•ญ์—ฐ์‚ฐ์ž์˜ ์กฐ๊ฑด์€ ์œ„ ๋ฆฌ๋ทฐ์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ
@@ -0,0 +1,14 @@ +import './Comment.scss'; + +function Comment({ userName, content }) { + return ( + <div className="comment"> + <p> + <strong className="commentMan">{userName}</strong> + {content} + </p> + </div> + ); +} + +export default Comment;
JavaScript
๋งค๊ฐœ๋ณ€์ˆ˜ ์ž๋ฆฌ์—์„œ ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น ๊ตณ
@@ -0,0 +1,102 @@ +import { useState, useEffect } from 'react'; +import './Feed.scss'; +import Comment from './Comment'; + +function Feed(props) { + const [comment, setComment] = useState(''); + const [addCommentList, setAddCommentList] = useState([]); + const [cmtContList, setCmtContList] = useState([]); + const userName = 'test01'; + + const onChange = event => { + setComment(event.target.value); + }; + + const addComment = event => { + event.preventDefault(); + setAddCommentList([ + ...addCommentList, + { + name: userName, + content: comment, + }, + ]); + setComment(''); + }; + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/commentData.json') + .then(res => res.json()) + .then(data => { + setCmtContList(data); + }); + }, []); + + return ( + <article className="feed"> + <div className="feedTop"> + <div className="feedTopOne"> + <div className="profileLogo"> + <img className="profileLogoImg" src={props.profileLogo} /> + </div> + <div className="profileName"> + <div className="profileNameOne">{props.userName}</div> + <span className="profileNameTwo">Wecode - ์œ„์ฝ”๋“œ</span> + </div> + </div> + <i className="fas fa-ellipsis-h" /> + </div> + <img alt="ํ”ผ๋“œ ์ด๋ฏธ์ง€" className="feedImg" src={props.feedImg} /> + <div className="feedThree"> + <div> + <i className="far fa-heart" /> + <i className="far fa-comment" /> + <i className="fas fa-upload" /> + </div> + <i className="far fa-bookmark" /> + </div> + <div className="feedFour"> + <img alt="์ข‹์•„์š” ๋ˆ„๋ฅธ ์‚ฌ๋žŒ" className="man" src={props.userMan} /> + <span className="feedFourWord">seungyoun_iain</span> + &nbsp;์™ธ&nbsp; + <span className="feedFourWordTwo">4๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </div> + <div className="feedFive"> + <span className="feedFiveWord">wecode_bootcamp</span>"์œ„์ฝ”๋“œ๋Š” ๋‹จ์ˆœ + ๊ต์œก์—…์ฒด๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ์ž…๋‹ˆ๋‹ค. Wecode์—์„œ ๋ฐฐ์šฐ๊ณ  ์ €๋Š” ์ด 5๊ฐœ + ํšŒ์‚ฌ์—์„œ ์˜คํผ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. + </div> + <div className="comment"> + {cmtContList.map(content => { + return ( + <Comment + key={content.id} + userName={content.userName} + content={content.content} + /> + ); + })} + {addCommentList.map((content, index) => { + return ( + <div key={index}> + <Comment content={content.content} userName={content.name} /> + </div> + ); + })} + </div> + <span className="postTime">54๋ถ„ ์ „</span> + <form className="commentSection" onSubmit={addComment}> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + className="comment" + onChange={onChange} + value={comment} + /> + <button className="postButton">๊ฒŒ์‹œ</button> + </form> + </article> + ); +} + +export default Feed;
JavaScript
- ์žฌํ• ๋‹น๋˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜๋Š” const๋กœ ์„ ์–ธํ•˜๊ธฐ - ์ด ๋ถ€๋ถ„์€ ๋ณ€์ˆ˜ ์„ ์–ธ๋ณด๋‹จ ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น ์ ์šฉํ•˜๊ธฐ
@@ -0,0 +1,102 @@ +import { useState, useEffect } from 'react'; +import './Feed.scss'; +import Comment from './Comment'; + +function Feed(props) { + const [comment, setComment] = useState(''); + const [addCommentList, setAddCommentList] = useState([]); + const [cmtContList, setCmtContList] = useState([]); + const userName = 'test01'; + + const onChange = event => { + setComment(event.target.value); + }; + + const addComment = event => { + event.preventDefault(); + setAddCommentList([ + ...addCommentList, + { + name: userName, + content: comment, + }, + ]); + setComment(''); + }; + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/commentData.json') + .then(res => res.json()) + .then(data => { + setCmtContList(data); + }); + }, []); + + return ( + <article className="feed"> + <div className="feedTop"> + <div className="feedTopOne"> + <div className="profileLogo"> + <img className="profileLogoImg" src={props.profileLogo} /> + </div> + <div className="profileName"> + <div className="profileNameOne">{props.userName}</div> + <span className="profileNameTwo">Wecode - ์œ„์ฝ”๋“œ</span> + </div> + </div> + <i className="fas fa-ellipsis-h" /> + </div> + <img alt="ํ”ผ๋“œ ์ด๋ฏธ์ง€" className="feedImg" src={props.feedImg} /> + <div className="feedThree"> + <div> + <i className="far fa-heart" /> + <i className="far fa-comment" /> + <i className="fas fa-upload" /> + </div> + <i className="far fa-bookmark" /> + </div> + <div className="feedFour"> + <img alt="์ข‹์•„์š” ๋ˆ„๋ฅธ ์‚ฌ๋žŒ" className="man" src={props.userMan} /> + <span className="feedFourWord">seungyoun_iain</span> + &nbsp;์™ธ&nbsp; + <span className="feedFourWordTwo">4๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </div> + <div className="feedFive"> + <span className="feedFiveWord">wecode_bootcamp</span>"์œ„์ฝ”๋“œ๋Š” ๋‹จ์ˆœ + ๊ต์œก์—…์ฒด๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ์ž…๋‹ˆ๋‹ค. Wecode์—์„œ ๋ฐฐ์šฐ๊ณ  ์ €๋Š” ์ด 5๊ฐœ + ํšŒ์‚ฌ์—์„œ ์˜คํผ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. + </div> + <div className="comment"> + {cmtContList.map(content => { + return ( + <Comment + key={content.id} + userName={content.userName} + content={content.content} + /> + ); + })} + {addCommentList.map((content, index) => { + return ( + <div key={index}> + <Comment content={content.content} userName={content.name} /> + </div> + ); + })} + </div> + <span className="postTime">54๋ถ„ ์ „</span> + <form className="commentSection" onSubmit={addComment}> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + className="comment" + onChange={onChange} + value={comment} + /> + <button className="postButton">๊ฒŒ์‹œ</button> + </form> + </article> + ); +} + +export default Feed;
JavaScript
- input์˜ value๊ฐ€ ๋“ค์–ด์˜ค๋Š” ๋ถ€๋ถ„์ธ๋ฐ, ์ดˆ๊ธฐ๊ฐ’์ด string์ด ์•„๋‹Œ ๋ฐฐ์—ด์ด ๋“ค์–ด์žˆ์Œ - cmtContents๋ผ๋Š” ๋ณ€์ˆ˜๋ช…๊ณผ ๋ฐฐ์—ด์ธ ์ดˆ๊ธฐ๊ฐ’์˜ ์กฐํ•ฉ์ด ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์ƒ์‹œํ‚ด - ๋ช…ํ™•ํ•˜์ง€๋งŒ ๊ฐ„๊ฒฐํ•œ state๋ช…์œผ๋กœ ์ˆ˜์ •. ex) `comment` / `commentList`
@@ -0,0 +1,102 @@ +import { useState, useEffect } from 'react'; +import './Feed.scss'; +import Comment from './Comment'; + +function Feed(props) { + const [comment, setComment] = useState(''); + const [addCommentList, setAddCommentList] = useState([]); + const [cmtContList, setCmtContList] = useState([]); + const userName = 'test01'; + + const onChange = event => { + setComment(event.target.value); + }; + + const addComment = event => { + event.preventDefault(); + setAddCommentList([ + ...addCommentList, + { + name: userName, + content: comment, + }, + ]); + setComment(''); + }; + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/commentData.json') + .then(res => res.json()) + .then(data => { + setCmtContList(data); + }); + }, []); + + return ( + <article className="feed"> + <div className="feedTop"> + <div className="feedTopOne"> + <div className="profileLogo"> + <img className="profileLogoImg" src={props.profileLogo} /> + </div> + <div className="profileName"> + <div className="profileNameOne">{props.userName}</div> + <span className="profileNameTwo">Wecode - ์œ„์ฝ”๋“œ</span> + </div> + </div> + <i className="fas fa-ellipsis-h" /> + </div> + <img alt="ํ”ผ๋“œ ์ด๋ฏธ์ง€" className="feedImg" src={props.feedImg} /> + <div className="feedThree"> + <div> + <i className="far fa-heart" /> + <i className="far fa-comment" /> + <i className="fas fa-upload" /> + </div> + <i className="far fa-bookmark" /> + </div> + <div className="feedFour"> + <img alt="์ข‹์•„์š” ๋ˆ„๋ฅธ ์‚ฌ๋žŒ" className="man" src={props.userMan} /> + <span className="feedFourWord">seungyoun_iain</span> + &nbsp;์™ธ&nbsp; + <span className="feedFourWordTwo">4๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </div> + <div className="feedFive"> + <span className="feedFiveWord">wecode_bootcamp</span>"์œ„์ฝ”๋“œ๋Š” ๋‹จ์ˆœ + ๊ต์œก์—…์ฒด๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ์ž…๋‹ˆ๋‹ค. Wecode์—์„œ ๋ฐฐ์šฐ๊ณ  ์ €๋Š” ์ด 5๊ฐœ + ํšŒ์‚ฌ์—์„œ ์˜คํผ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. + </div> + <div className="comment"> + {cmtContList.map(content => { + return ( + <Comment + key={content.id} + userName={content.userName} + content={content.content} + /> + ); + })} + {addCommentList.map((content, index) => { + return ( + <div key={index}> + <Comment content={content.content} userName={content.name} /> + </div> + ); + })} + </div> + <span className="postTime">54๋ถ„ ์ „</span> + <form className="commentSection" onSubmit={addComment}> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + className="comment" + onChange={onChange} + value={comment} + /> + <button className="postButton">๊ฒŒ์‹œ</button> + </form> + </article> + ); +} + +export default Feed;
JavaScript
ํ•จ์ˆ˜๋ช… ์ˆ˜์ •! - comment๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜ - ์—”ํ„ฐํ‚ค๋ฅผ ์ณค์„ ๋•Œ ์œ„ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜ ๋กœ ๋ถ„ํ• ํ•˜๋ฉด ์ข‹์„ ๋“ฏ
@@ -0,0 +1,102 @@ +import { useState, useEffect } from 'react'; +import './Feed.scss'; +import Comment from './Comment'; + +function Feed(props) { + const [comment, setComment] = useState(''); + const [addCommentList, setAddCommentList] = useState([]); + const [cmtContList, setCmtContList] = useState([]); + const userName = 'test01'; + + const onChange = event => { + setComment(event.target.value); + }; + + const addComment = event => { + event.preventDefault(); + setAddCommentList([ + ...addCommentList, + { + name: userName, + content: comment, + }, + ]); + setComment(''); + }; + + useEffect(() => { + fetch('http://localhost:3000/data/younghyunHan/commentData.json') + .then(res => res.json()) + .then(data => { + setCmtContList(data); + }); + }, []); + + return ( + <article className="feed"> + <div className="feedTop"> + <div className="feedTopOne"> + <div className="profileLogo"> + <img className="profileLogoImg" src={props.profileLogo} /> + </div> + <div className="profileName"> + <div className="profileNameOne">{props.userName}</div> + <span className="profileNameTwo">Wecode - ์œ„์ฝ”๋“œ</span> + </div> + </div> + <i className="fas fa-ellipsis-h" /> + </div> + <img alt="ํ”ผ๋“œ ์ด๋ฏธ์ง€" className="feedImg" src={props.feedImg} /> + <div className="feedThree"> + <div> + <i className="far fa-heart" /> + <i className="far fa-comment" /> + <i className="fas fa-upload" /> + </div> + <i className="far fa-bookmark" /> + </div> + <div className="feedFour"> + <img alt="์ข‹์•„์š” ๋ˆ„๋ฅธ ์‚ฌ๋žŒ" className="man" src={props.userMan} /> + <span className="feedFourWord">seungyoun_iain</span> + &nbsp;์™ธ&nbsp; + <span className="feedFourWordTwo">4๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </div> + <div className="feedFive"> + <span className="feedFiveWord">wecode_bootcamp</span>"์œ„์ฝ”๋“œ๋Š” ๋‹จ์ˆœ + ๊ต์œก์—…์ฒด๊ฐ€ ์•„๋‹Œ ๊ฐœ๋ฐœ์ž ์ปค๋ฎค๋‹ˆํ‹ฐ์ž…๋‹ˆ๋‹ค. Wecode์—์„œ ๋ฐฐ์šฐ๊ณ  ์ €๋Š” ์ด 5๊ฐœ + ํšŒ์‚ฌ์—์„œ ์˜คํผ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. + </div> + <div className="comment"> + {cmtContList.map(content => { + return ( + <Comment + key={content.id} + userName={content.userName} + content={content.content} + /> + ); + })} + {addCommentList.map((content, index) => { + return ( + <div key={index}> + <Comment content={content.content} userName={content.name} /> + </div> + ); + })} + </div> + <span className="postTime">54๋ถ„ ์ „</span> + <form className="commentSection" onSubmit={addComment}> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + className="comment" + onChange={onChange} + value={comment} + /> + <button className="postButton">๊ฒŒ์‹œ</button> + </form> + </article> + ); +} + +export default Feed;
JavaScript
๋™์ผํ•œ ์Šคํƒ€์ผ์„ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„์ธ๋ฐ, one two three ์ด๋Ÿฐ ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ํ•„์š” ์—†์ด ๋‚ด์šฉ์„ ํฌ๊ด„ํ•˜๋Š” ๋ช…์นญ์˜ ๋™์ผํ•œ className์œผ๋กœ ์‚ฌ์šฉ
@@ -0,0 +1,132 @@ +.feed { + background-color: white; + margin-bottom: 50px; + + .feedTop { + display: flex; + justify-content: space-between; + position: relative; + padding: 10px; + + .feedTopOne { + display: flex; + align-items: center; + + .profileLogo { + display: flex; + align-items: center; + justify-content: center; + height: 35px; + width: 35px; + margin-right: 10px; + color: white; + background-color: black; + border-radius: 50%; + font-size: 7px; + font-weight: bold; + overflow: hidden; // ๋ถ€๋ชจ div๋งŒํผ ์ด๋ฏธ์ง€ ์ž๋ฆ„ + + .profileLogoImg { + width: 100%; + height: 100%; + object-fit: cover; // ๊ฐ€๋กœ, ์„ธ๋กœ ๋น„์œจ์ด ์•ˆ ๋งž์œผ๋ฉด ๋”ฑ ๋งž๊ฒŒ ์•ˆ ์ฐŒ๊ทธ๋Ÿฌ์ง€๊ฒŒ ๋งž์ถฐ์คŒ. + } + } + + .profileNameOne { + margin-bottom: 0px; + font-size: 15px; + font-weight: bold; + } + + .profileNameTwo { + color: #a3a49a; + font-size: 12px; + font-weight: bold; + } + } + + .fa-ellipsis-h { + position: absolute; + right: 20px; + top: 20px; + } + } + + .feedImg { + width: 100%; + height: 500px; + } + + .feedThree { + display: flex; + justify-content: space-between; + align-items: center; + padding: 10px; + font-size: 25px; + + .fa-heart, + .fa-comment { + margin-right: 10px; + } + } + + .feedFour { + display: flex; + align-items: center; + padding: 0px 10px 10px 10px; + + .man { + width: 20px; + height: 20px; + margin-right: 10px; + border-radius: 50%; + } // ์œ„ ์ฝ”๋“œ ์ฒ˜๋Ÿผ ํ•˜๋ ค๋ฉด div๋กœ ์ด๋ฏธ์ง€ ํ•œ๋ฒˆ ๋” ๊ฐ์จ์„œ ๊ทธ div์— overflow:hidden ์ฃผ๊ณ  + // imgํŒŒ์ผ์— width: 100%, height: 100%, object-fit: cover์ฃผ๋ฉด ๋จ + + .feedFourWord, + .feedFourWordTwo { + font-weight: bold; + } + } + + .feedFive { + padding: 0px 10px 3px 10px; + + .feedFiveWord { + font-weight: bold; + } + } + + .comment { + padding-top: 5px; + padding-left: 5px; + padding-bottom: 5px; + } + + .postTime { + display: inline-block; + padding: 10px 10px 15px 10px; + color: #9c9c9c; + } + + .commentSection { + position: relative; + + .comment { + width: 100%; + padding: 15px 10px; + border: 1px solid #f3f3f3; + } + + .postButton { + position: absolute; + bottom: 13px; + right: 20px; + color: #c5e1fc; + background-color: white; + border: 1px solid white; + font-size: 15px; + } + } +}
Unknown
- article ํƒœ๊ทธ๊ฐ€ ์•„๋‹ˆ๋ผ feed๋ผ๋Š” className์œผ๋กœ nestingํ•˜๊ธฐ - ์ตœ์ƒ์œ„ div๋ฅผ article๋กœ ๋ฐ”๊พธ๊ณ  1 depth ์ค„์ด๋ฉด ๋  ๋“ฏ
@@ -6,13 +6,14 @@ import ReviewContent from "./ReviewContent"; import ArrowDown from "@/assets/images/icons/arrow_down.svg"; import MenuCircle from "@/assets/images/icons/menu-circle.svg"; import Dropdown from "@/components/common/Dropdown"; +import useReviewModals from "@/hooks/review/useReviewModals"; import { type ReviewInfoProps } from "@/types/review"; import { type RatingStyle } from "@/types/review"; export default function Review({ reviewInfo }: ReviewInfoProps) { const router = useRouter(); + const { handleDeleteClick } = useReviewModals(); const [isOpen, setIsOpen] = useState(false); - const reviewTextStyle: RatingStyle = { 0: "text-purple-200", 1: "text-blue-200", @@ -37,7 +38,7 @@ export default function Review({ reviewInfo }: ReviewInfoProps) { }; const handleClickDelete = () => { - alert("๋ฆฌ๋ทฐ ์‚ญ์ œ์ž…๋‹ˆ๋‹ค."); + handleDeleteClick(reviewInfo.reviewId); }; const handleClickDetail = (e: React.MouseEvent<HTMLButtonElement>) => { @@ -48,8 +49,7 @@ export default function Review({ reviewInfo }: ReviewInfoProps) { return ( <div className='rounded-[12px] bg-gray-900 p-4' - onClick={handleClickReview} - aria-label={"routeMeet"} + onClick={reviewInfo.isMyReview ? handleClickReview : undefined} > <div className='relative'> <div className='flex justify-between'>
Unknown
@ITHPARK ๋ฆฌ๋ทฐ ์‚ญ์ œ api ์—ฐ๊ฒฐํ•˜๋ฉด์„œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ ์ผ๋ถ€ ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค!
@@ -11,6 +11,8 @@ export interface ReviewInfo { isMyWritten?: boolean; eventId?: number; eventType?: string; + reviewId: number; + editable?: boolean; } export interface ReviewInfoProps {
TypeScript
๋ฆฌ๋ทฐ ํƒ€์ž…์— `reviewId`, `editable` ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,109 @@ +package chess.domain.board; + +import chess.domain.command.MoveOptions; +import chess.domain.piece.Color; +import chess.domain.piece.Piece; +import chess.domain.player.Player; +import chess.domain.player.Position; + +import java.util.Collection; + +public class Board { + private final Player white; + private final Player black; + + public Board() { + this.white = new Player(Color.WHITE); + this.black = new Player(Color.BLACK); + } + + public void move(final MoveOptions moveOptions, final boolean isWhiteTurn) { + Player player = currentPlayer(isWhiteTurn); + Player enemy = currentPlayer(!isWhiteTurn); + Position source = moveOptions.getSource(); + Position target = moveOptions.getTarget(); + + validate(player, enemy, source, target); + + enemy.removePieceOn(target); + movePiece(player, source, target); + } + + private Player currentPlayer(final boolean isWhiteTurn) { + if (isWhiteTurn) { + return white; + } + return black; + } + + private void validate(final Player player, final Player enemy, final Position source, final Position target) { + validateSourceOwner(enemy, source); + validateSamePosition(source, target); + validateTarget(player, target); + validateKingMovable(player, enemy, source, target); + } + + private void validateSourceOwner(final Player enemy, final Position source) { + if (enemy.hasPieceOn(source)) { + throw new IllegalArgumentException("์ž์‹ ์˜ ๊ธฐ๋ฌผ๋งŒ ์›€์ง์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateSamePosition(final Position source, final Position target) { + if (source.equals(target)) { + throw new IllegalArgumentException("์ถœ๋ฐœ ์œ„์น˜์™€ ๋„์ฐฉ ์œ„์น˜๊ฐ€ ๊ฐ™์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateTarget(final Player player, final Position target) { + if (player.hasPieceOn(target)) { + throw new IllegalArgumentException("๊ฐ™์€ ์ƒ‰์ƒ์˜ ๊ธฐ๋ฌผ์€ ๊ณต๊ฒฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateKingMovable(final Player player, final Player enemy, final Position source, final Position target) { + if (player.hasKingOn(source) && enemy.canAttack(target)) { + throw new IllegalArgumentException("ํ‚น์€ ์ƒ๋Œ€๋ฐฉ์ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private void movePiece(final Player player, final Position source, final Position target) { + Collection<Position> paths = player.findPaths(source, target); + validatePathsEmpty(paths); + player.update(source, target); + } + + private void validatePathsEmpty(final Collection<Position> paths) { + boolean isWhiteBlocked = paths.stream() + .anyMatch(white::hasPieceOn); + boolean isBlackBlocked = paths.stream() + .anyMatch(black::hasPieceOn); + + if (isWhiteBlocked || isBlackBlocked) { + throw new IllegalArgumentException("๊ธฐ๋ฌผ์„ ํ†ต๊ณผํ•˜์—ฌ ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + public Piece findBy(final Position position) { + if (white.hasPieceOn(position)) { + return white.findPieceBy(position); + } + + return black.findPieceBy(position); + } + + public boolean isEmpty(final Position position) { + return !white.hasPieceOn(position) && !black.hasPieceOn(position); + } + + public Status getStatus() { + double whiteScore = white.sumScores(); + double blackScore = black.sumScores(); + + return new Status(whiteScore, blackScore, white.isKingDead(), black.isKingDead()); + } + + public boolean isEnd() { + return white.isKingDead() || black.isKingDead(); + } +}
Java
์ ์ˆ˜ ๊ณ„์‚ฐ์— ๋Œ€ํ•œ ๋ช…๋ น์–ด(`status`) ์ ์šฉ์ด ์•ˆ๋˜์–ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”! :)
@@ -0,0 +1,58 @@ +package chess.domain.command; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public enum Command { + + START("start"), + END("end"), + MOVE("move"), + STATUS("status"); + + private static final Map<String, Command> COMMANDS = createCommands(); + + private static Map<String, Command> createCommands() { + Map<String, Command> commands = new HashMap<>(); + + Arrays.stream(values()) + .forEach(command -> commands.put(command.command, command)); + + return commands; + } + + private final String command; + + Command(final String command) { + this.command = command; + } + + public static Command of(final String command) { + Command foundCommand = COMMANDS.get(command); + + if (foundCommand == null) { + throw new IllegalArgumentException("์œ ํšจํ•˜์ง€ ์•Š์€ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค."); + } + + return foundCommand; + } + + public void validateInitialCommand() { + if (isStatus() || isMove()) { + throw new IllegalArgumentException(String.format("%s ๋˜๋Š” %s๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.", START.command, END.command)); + } + } + + public boolean isEnd() { + return this.equals(END); + } + + public boolean isMove() { + return this.equals(MOVE); + } + + public boolean isStatus() { + return this.equals(STATUS); + } +}
Java
Command๋„ Enum์œผ๋กœ ๊ด€๋ฆฌํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? :)
@@ -0,0 +1,127 @@ +package chess.domain.player; + +import chess.domain.board.File; +import chess.domain.piece.Color; +import chess.domain.piece.Piece; +import chess.domain.piece.PieceFactory; +import chess.domain.piece.PieceResolver; + +import java.util.Collection; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Player { + + private final Map<Position, Piece> pieces; + private final AttackRange attackRange; + + public Player(final Color color) { + pieces = new HashMap<>(PieceFactory.createPieces(color)); + attackRange = new AttackRange(pieces); + } + + public double sumScores() { + double pawnScores = calculatePawnScores(); + double scoresExceptPawn = calculateScoresExceptPawn(); + + return pawnScores + scoresExceptPawn; + } + + private double calculatePawnScores() { + List<Position> pawnPositions = findPawnPositions(); + Map<File, Integer> pawnCountsOnSameFile = extractPawnCountsOnSameFile(pawnPositions); + + return pawnCountsOnSameFile.values() + .stream() + .mapToDouble(PieceResolver::sumPawnScores) + .sum(); + } + + private List<Position> findPawnPositions() { + return pieces.keySet() + .stream() + .filter(position -> { + Piece piece = pieces.get(position); + return piece.isPawn(); + }) + .collect(Collectors.toList()); + } + + private Map<File, Integer> extractPawnCountsOnSameFile(final List<Position> pawnPositions) { + Map<File, Integer> pawnCountOnSameFile = new EnumMap<>(File.class); + + pawnPositions.stream() + .map(Position::getFile) + .forEach(file -> pawnCountOnSameFile.put(file, pawnCountOnSameFile.getOrDefault(file, 0) + 1)); + + return pawnCountOnSameFile; + } + + private double calculateScoresExceptPawn() { + return pieces.values() + .stream() + .filter(Piece::isNotPawn) + .mapToDouble(PieceResolver::findScoreBy) + .sum(); + } + + public Collection<Position> findPaths(final Position source, final Position target) { + Piece sourcePiece = findPieceBy(source); + + return sourcePiece.findPath(source, target); + } + + public Piece findPieceBy(final Position position) { + if (isEmptyOn(position)) { + throw new IllegalArgumentException("ํ•ด๋‹น ์œ„์น˜์— ๊ธฐ๋ฌผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } + + return pieces.get(position); + } + + private boolean isEmptyOn(final Position position) { + return !pieces.containsKey(position); + } + + public void update(final Position source, final Position target) { + Piece sourcePiece = findPieceBy(source); + movePiece(source, target, sourcePiece); + attackRange.update(source, target, sourcePiece); + } + + private void movePiece(final Position source, final Position target, final Piece sourcePiece) { + pieces.put(target, sourcePiece); + pieces.remove(source); + } + + public boolean hasKingOn(final Position position) { + Piece piece = findPieceBy(position); + + return piece.isKing(); + } + + public boolean isKingDead() { + return pieces.values().stream() + .noneMatch(Piece::isKing); + } + + public boolean canAttack(final Position position) { + return attackRange.contains(position); + } + + public void removePieceOn(final Position position) { + if (isEmptyOn(position)) { + return; + } + + attackRange.remove(position, pieces.get(position)); + pieces.remove(position); + } + + public boolean hasPieceOn(final Position position) { + return pieces.containsKey(position); + } +}
Java
์ €๋Š” ๋ถ€์ •์—ฐ์‚ฐ์ž(`!`)๋ฅผ ์›ฌ๋งŒํ•˜๋ฉด ํ”ผํ•˜๋Š”๋ฐ์š” ใ…Žใ…Ž ๊ฐ€๋…์„ฑ์˜ ๋ฌธ์ œ๋„ ์žˆ๊ณ , ์ฝ๋Š” ์‚ฌ๋žŒ์ด ํ•œ๋ฒˆ ๋” ํ•ด์„์„ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ชฝ์—์„œ ํ•œ๋ฒˆ ๋” ์ถ”์ƒํ™”์‹œํ‚ค๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ใ…Žใ…Ž `piece.isNotPawn()`
@@ -0,0 +1,127 @@ +package chess.domain.player; + +import chess.domain.board.File; +import chess.domain.piece.Color; +import chess.domain.piece.Piece; +import chess.domain.piece.PieceFactory; +import chess.domain.piece.PieceResolver; + +import java.util.Collection; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Player { + + private final Map<Position, Piece> pieces; + private final AttackRange attackRange; + + public Player(final Color color) { + pieces = new HashMap<>(PieceFactory.createPieces(color)); + attackRange = new AttackRange(pieces); + } + + public double sumScores() { + double pawnScores = calculatePawnScores(); + double scoresExceptPawn = calculateScoresExceptPawn(); + + return pawnScores + scoresExceptPawn; + } + + private double calculatePawnScores() { + List<Position> pawnPositions = findPawnPositions(); + Map<File, Integer> pawnCountsOnSameFile = extractPawnCountsOnSameFile(pawnPositions); + + return pawnCountsOnSameFile.values() + .stream() + .mapToDouble(PieceResolver::sumPawnScores) + .sum(); + } + + private List<Position> findPawnPositions() { + return pieces.keySet() + .stream() + .filter(position -> { + Piece piece = pieces.get(position); + return piece.isPawn(); + }) + .collect(Collectors.toList()); + } + + private Map<File, Integer> extractPawnCountsOnSameFile(final List<Position> pawnPositions) { + Map<File, Integer> pawnCountOnSameFile = new EnumMap<>(File.class); + + pawnPositions.stream() + .map(Position::getFile) + .forEach(file -> pawnCountOnSameFile.put(file, pawnCountOnSameFile.getOrDefault(file, 0) + 1)); + + return pawnCountOnSameFile; + } + + private double calculateScoresExceptPawn() { + return pieces.values() + .stream() + .filter(Piece::isNotPawn) + .mapToDouble(PieceResolver::findScoreBy) + .sum(); + } + + public Collection<Position> findPaths(final Position source, final Position target) { + Piece sourcePiece = findPieceBy(source); + + return sourcePiece.findPath(source, target); + } + + public Piece findPieceBy(final Position position) { + if (isEmptyOn(position)) { + throw new IllegalArgumentException("ํ•ด๋‹น ์œ„์น˜์— ๊ธฐ๋ฌผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } + + return pieces.get(position); + } + + private boolean isEmptyOn(final Position position) { + return !pieces.containsKey(position); + } + + public void update(final Position source, final Position target) { + Piece sourcePiece = findPieceBy(source); + movePiece(source, target, sourcePiece); + attackRange.update(source, target, sourcePiece); + } + + private void movePiece(final Position source, final Position target, final Piece sourcePiece) { + pieces.put(target, sourcePiece); + pieces.remove(source); + } + + public boolean hasKingOn(final Position position) { + Piece piece = findPieceBy(position); + + return piece.isKing(); + } + + public boolean isKingDead() { + return pieces.values().stream() + .noneMatch(Piece::isKing); + } + + public boolean canAttack(final Position position) { + return attackRange.contains(position); + } + + public void removePieceOn(final Position position) { + if (isEmptyOn(position)) { + return; + } + + attackRange.remove(position, pieces.get(position)); + pieces.remove(position); + } + + public boolean hasPieceOn(final Position position) { + return pieces.containsKey(position); + } +}
Java
์œ„์—๋„ ๋ถ€์ •์—ฐ์‚ฐ์ž ํ”ผ๋“œ๋ฐฑ์„ ๋‚จ๊ฒผ๋Š”๋ฐ์š”, ์—ฌ๊ธฐ๋„ ํ•œ๋ฒˆ๋” ์ถ”์ƒํ™”์‹œํ‚ค๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ใ…Žใ…Ž ์ „์ฒด์ ์œผ๋กœ ํ•œ๋ฒˆ ๋” ์ฒดํฌํ•ด๋ด๋„ ์ข‹๊ฒ ๋„ค์š” :)
@@ -0,0 +1,109 @@ +package chess.domain.board; + +import chess.domain.command.MoveOptions; +import chess.domain.piece.Color; +import chess.domain.piece.Piece; +import chess.domain.player.Player; +import chess.domain.player.Position; + +import java.util.Collection; + +public class Board { + private final Player white; + private final Player black; + + public Board() { + this.white = new Player(Color.WHITE); + this.black = new Player(Color.BLACK); + } + + public void move(final MoveOptions moveOptions, final boolean isWhiteTurn) { + Player player = currentPlayer(isWhiteTurn); + Player enemy = currentPlayer(!isWhiteTurn); + Position source = moveOptions.getSource(); + Position target = moveOptions.getTarget(); + + validate(player, enemy, source, target); + + enemy.removePieceOn(target); + movePiece(player, source, target); + } + + private Player currentPlayer(final boolean isWhiteTurn) { + if (isWhiteTurn) { + return white; + } + return black; + } + + private void validate(final Player player, final Player enemy, final Position source, final Position target) { + validateSourceOwner(enemy, source); + validateSamePosition(source, target); + validateTarget(player, target); + validateKingMovable(player, enemy, source, target); + } + + private void validateSourceOwner(final Player enemy, final Position source) { + if (enemy.hasPieceOn(source)) { + throw new IllegalArgumentException("์ž์‹ ์˜ ๊ธฐ๋ฌผ๋งŒ ์›€์ง์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateSamePosition(final Position source, final Position target) { + if (source.equals(target)) { + throw new IllegalArgumentException("์ถœ๋ฐœ ์œ„์น˜์™€ ๋„์ฐฉ ์œ„์น˜๊ฐ€ ๊ฐ™์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateTarget(final Player player, final Position target) { + if (player.hasPieceOn(target)) { + throw new IllegalArgumentException("๊ฐ™์€ ์ƒ‰์ƒ์˜ ๊ธฐ๋ฌผ์€ ๊ณต๊ฒฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private void validateKingMovable(final Player player, final Player enemy, final Position source, final Position target) { + if (player.hasKingOn(source) && enemy.canAttack(target)) { + throw new IllegalArgumentException("ํ‚น์€ ์ƒ๋Œ€๋ฐฉ์ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + private void movePiece(final Player player, final Position source, final Position target) { + Collection<Position> paths = player.findPaths(source, target); + validatePathsEmpty(paths); + player.update(source, target); + } + + private void validatePathsEmpty(final Collection<Position> paths) { + boolean isWhiteBlocked = paths.stream() + .anyMatch(white::hasPieceOn); + boolean isBlackBlocked = paths.stream() + .anyMatch(black::hasPieceOn); + + if (isWhiteBlocked || isBlackBlocked) { + throw new IllegalArgumentException("๊ธฐ๋ฌผ์„ ํ†ต๊ณผํ•˜์—ฌ ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } + } + + public Piece findBy(final Position position) { + if (white.hasPieceOn(position)) { + return white.findPieceBy(position); + } + + return black.findPieceBy(position); + } + + public boolean isEmpty(final Position position) { + return !white.hasPieceOn(position) && !black.hasPieceOn(position); + } + + public Status getStatus() { + double whiteScore = white.sumScores(); + double blackScore = black.sumScores(); + + return new Status(whiteScore, blackScore, white.isKingDead(), black.isKingDead()); + } + + public boolean isEnd() { + return white.isKingDead() || black.isKingDead(); + } +}
Java
์•—.. ํŽ˜์–ด๋ถ„ repo์—์„œ pullํ•˜๊ณ  ํ‘ธ์‹œ๋ฅผ ์•ˆํ–ˆ์—ˆ๋„ค์š” ์ฃ„์†กํ•ฉ๋‹ˆ๋‹คใ… ใ…  ๋ฐ˜์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,127 @@ +package chess.domain.player; + +import chess.domain.board.File; +import chess.domain.piece.Color; +import chess.domain.piece.Piece; +import chess.domain.piece.PieceFactory; +import chess.domain.piece.PieceResolver; + +import java.util.Collection; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Player { + + private final Map<Position, Piece> pieces; + private final AttackRange attackRange; + + public Player(final Color color) { + pieces = new HashMap<>(PieceFactory.createPieces(color)); + attackRange = new AttackRange(pieces); + } + + public double sumScores() { + double pawnScores = calculatePawnScores(); + double scoresExceptPawn = calculateScoresExceptPawn(); + + return pawnScores + scoresExceptPawn; + } + + private double calculatePawnScores() { + List<Position> pawnPositions = findPawnPositions(); + Map<File, Integer> pawnCountsOnSameFile = extractPawnCountsOnSameFile(pawnPositions); + + return pawnCountsOnSameFile.values() + .stream() + .mapToDouble(PieceResolver::sumPawnScores) + .sum(); + } + + private List<Position> findPawnPositions() { + return pieces.keySet() + .stream() + .filter(position -> { + Piece piece = pieces.get(position); + return piece.isPawn(); + }) + .collect(Collectors.toList()); + } + + private Map<File, Integer> extractPawnCountsOnSameFile(final List<Position> pawnPositions) { + Map<File, Integer> pawnCountOnSameFile = new EnumMap<>(File.class); + + pawnPositions.stream() + .map(Position::getFile) + .forEach(file -> pawnCountOnSameFile.put(file, pawnCountOnSameFile.getOrDefault(file, 0) + 1)); + + return pawnCountOnSameFile; + } + + private double calculateScoresExceptPawn() { + return pieces.values() + .stream() + .filter(Piece::isNotPawn) + .mapToDouble(PieceResolver::findScoreBy) + .sum(); + } + + public Collection<Position> findPaths(final Position source, final Position target) { + Piece sourcePiece = findPieceBy(source); + + return sourcePiece.findPath(source, target); + } + + public Piece findPieceBy(final Position position) { + if (isEmptyOn(position)) { + throw new IllegalArgumentException("ํ•ด๋‹น ์œ„์น˜์— ๊ธฐ๋ฌผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } + + return pieces.get(position); + } + + private boolean isEmptyOn(final Position position) { + return !pieces.containsKey(position); + } + + public void update(final Position source, final Position target) { + Piece sourcePiece = findPieceBy(source); + movePiece(source, target, sourcePiece); + attackRange.update(source, target, sourcePiece); + } + + private void movePiece(final Position source, final Position target, final Piece sourcePiece) { + pieces.put(target, sourcePiece); + pieces.remove(source); + } + + public boolean hasKingOn(final Position position) { + Piece piece = findPieceBy(position); + + return piece.isKing(); + } + + public boolean isKingDead() { + return pieces.values().stream() + .noneMatch(Piece::isKing); + } + + public boolean canAttack(final Position position) { + return attackRange.contains(position); + } + + public void removePieceOn(final Position position) { + if (isEmptyOn(position)) { + return; + } + + attackRange.remove(position, pieces.get(position)); + pieces.remove(position); + } + + public boolean hasPieceOn(final Position position) { + return pieces.containsKey(position); + } +}
Java
๋„ต๋„ต! ํ•ด๋‹น ํ”ผ๋“œ๋ฐฑ ์ฝ์€ ํ›„ ๋ถ€์ •์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•˜๋Š” ๊ณณ ๋ชจ๋‘ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,58 @@ +package chess.domain.command; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public enum Command { + + START("start"), + END("end"), + MOVE("move"), + STATUS("status"); + + private static final Map<String, Command> COMMANDS = createCommands(); + + private static Map<String, Command> createCommands() { + Map<String, Command> commands = new HashMap<>(); + + Arrays.stream(values()) + .forEach(command -> commands.put(command.command, command)); + + return commands; + } + + private final String command; + + Command(final String command) { + this.command = command; + } + + public static Command of(final String command) { + Command foundCommand = COMMANDS.get(command); + + if (foundCommand == null) { + throw new IllegalArgumentException("์œ ํšจํ•˜์ง€ ์•Š์€ ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค."); + } + + return foundCommand; + } + + public void validateInitialCommand() { + if (isStatus() || isMove()) { + throw new IllegalArgumentException(String.format("%s ๋˜๋Š” %s๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.", START.command, END.command)); + } + } + + public boolean isEnd() { + return this.equals(END); + } + + public boolean isMove() { + return this.equals(MOVE); + } + + public boolean isStatus() { + return this.equals(STATUS); + } +}
Java
์ƒ์ˆ˜๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— Enum์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ํŽธ์ด ๋” ์ข‹๊ฒ ๋„ค์š”! Enum ๊ฐ์ฒด๋กœ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,93 @@ +const context = describe; + +describe('์˜ํ™”๋ชฉ๋ก API ํ…Œ์ŠคํŠธ', () => { + context('์ธ๊ธฐ ์˜ํ™”๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด', () => { + it('20๊ฐœ์˜ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + const baseUrl = 'https://api.themoviedb.org/3/movie/popular'; + const param = new URLSearchParams({ + api_key: Cypress.env('API_KEY'), + language: 'ko-KR', + page: 1, + }); + + cy.request('GET', `${baseUrl}?${param}`).as('popularMovies'); + + cy.get('@popularMovies').its('status').should('eq', 200); + cy.get('@popularMovies').its('body.results').should('have.length', 20); + }); + }); +}); + +describe('์˜ํ™”๋ชฉ๋ก e2e ํ…Œ์ŠคํŠธ', () => { + beforeEach(() => { + const baseUrl = 'https://api.themoviedb.org/3/movie/popular'; + const param = new URLSearchParams({ + api_key: Cypress.env('API_KEY'), + language: 'ko-KR', + page: 1, + }); + + cy.intercept( + { + method: 'GET', + url: `${baseUrl}?${param}`, + }, + { fixture: 'movie-popular.json' } + ).as('popularMovies'); + + cy.visit('http://localhost:8080'); + }); + + context('์˜ํ™” ๋ชฉ๋ก ์กฐํšŒ ์‹œ', () => { + it('800ms ๋™์•ˆ Skeleton UI๊ฐ€ ๋…ธ์ถœ๋˜์—ˆ๋‹ค๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.', () => { + cy.wait('@popularMovies'); + cy.get('.skeleton').should('have.length', 60); + cy.wait(800); + cy.get('.skeleton').should('have.length', 0); + }); + }); + + context('ํŽ˜์ด์ง€ ์ง„์ž… ์‹œ ์ธ๊ธฐ์˜ํ™” ๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด', () => { + it('20๊ฐœ์˜ .item-card๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.', () => { + cy.get('.item-card').should('have.length', 20); + }); + }); + + context('๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด', () => { + it('40๊ฐœ์˜ item-card ๋…ธ์ถœ๋œ๋‹ค.', () => { + cy.get('.btn').click(); + cy.get('.item-card').should('have.length', 40); + }); + }); +}); + +describe('์งง์€ ์˜ํ™”๋ชฉ๋ก e2e ํ…Œ์ŠคํŠธ', () => { + beforeEach(() => { + const baseUrl = 'https://api.themoviedb.org/3/movie/popular'; + const param = new URLSearchParams({ + api_key: Cypress.env('API_KEY'), + language: 'ko-KR', + page: 1, + }); + + cy.intercept( + { + method: 'GET', + url: `${baseUrl}?${param}`, + }, + { fixture: 'movie-popular-short.json' } + ).as('popularMovies'); + + cy.visit('http://localhost:8080'); + }); + + context('total_pages ๊ฐ€ 1์ธ ์ธ๊ธฐ์˜ํ™” ๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด', () => { + it('18๊ฐœ์˜ .item-card๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.', () => { + cy.get('.item-card').should('have.length', 18); + }); + + it('๋” ๋ณด๊ธฐ ๋ฒ„ํŠผ์ด ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.', () => { + cy.contains('๋”๋ณด๊ธฐ').should('not.exist'); + }); + }); +});
JavaScript
ํ˜น์‹œ 18๊ฐœ์ธ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,10 @@ +์˜ํ™” ๋ชฉ๋ก API +- ์ธ๊ธฐ ์˜ํ™”๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด 20๊ฐœ์˜ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. + +ํŽ˜์ด์ง€ +- ํŽ˜์ด์ง€๋ฅผ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. + +UI +- ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์Œ ์˜ํ™” ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. +- ์˜ํ™” ๋ชฉ๋ก์˜ ํŽ˜์ด์ง€ ๋์— ๋„๋‹ฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. +- ์˜ํ™” ๋ชฉ๋ก ์•„์ดํ…œ์€ Skeleton UI๋ฅผ ๊ฐ€์ง„๋‹ค.
Unknown
ํŽ˜์ด์ง€๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์„๊นŒ์š”? โ†’ ์•„๋ž˜์˜ ์ฝ”๋ฉ˜ํŠธ๋“ค๊ณผ ๋ชฉ์ ์€ ์ด์–ด์ง‘๋‹ˆ๋‹ค!
@@ -0,0 +1,18 @@ +const DEFAULT_SEARCH_PARAMS = { + api_key: process.env.API_KEY, + language: 'ko-KR', +}; +const MOVIE_BASE_URL = 'https://api.themoviedb.org/3/movie'; + +export async function getPopularMovie(page) { + const param = new URLSearchParams({ + ...DEFAULT_SEARCH_PARAMS, + page, + }); + + const response = await fetch(`${MOVIE_BASE_URL}/popular?${param}`); + if (response.ok) { + return response.json(); + } + return { results: [] }; +}
JavaScript
์กฐ๊ธˆ ํŠน์ดํ•œ ๋ฐฉ์‹์ธ ๊ฒƒ ๊ฐ™๊ธดํ•œ๋ฐ์š”! ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๋กœ ๋ฌถ์–ด์„œ export ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”?
@@ -0,0 +1,12 @@ +import PageHandler from './PageHandler'; +import { getPopularMovie } from '../api/movie'; + +export async function getNextPopularMovieList() { + const { page, done } = PageHandler.next(); + const { results } = await getPopularMovie(page); + return { + page, + done, + nextMovieList: results, + }; +}
JavaScript
์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์€ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ํ•จ์ˆ˜๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•ด๋‘์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?!
@@ -0,0 +1,12 @@ +import PageHandler from './PageHandler'; +import { getPopularMovie } from '../api/movie'; + +export async function getNextPopularMovieList() { + const { page, done } = PageHandler.next(); + const { results } = await getPopularMovie(page); + return { + page, + done, + nextMovieList: results, + }; +}
JavaScript
ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์—์„œ UI๋ฅผ ์กฐ์ž‘ํ•ด์ฃผ๊ณ  ์žˆ๋„ค์š”! ์ฒ˜์Œ์—” `if (PageHandler.getCurrentPage() !== total_pages)` ์กฐ๊ฑด์ด UI ์™€ ๊ด€๋ จ์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ฐ๊ฐ ๋ชฉ์ ์ด ๋‹ค๋ฅด๋„ค์š”. ์—ฌ๊ธฐ์„œ ์ด ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ๋‘ ๊ฐ€์ง€๋ฅผ ๊ณ ๋ฏผํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! 1. `onClickMoreButton` ๋ผ๋Š” ์ด๋ฆ„์ด ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ๋“œ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ๋Š”๊ฐ€? 2. UI ์กฐ์ž‘๊ณผ ๋„๋ฉ”์ธ ๋กœ์ง์„ ํ•จ๊ป˜ ๋‘๋Š” ๊ฒŒ ๋งž์„๊นŒ?
@@ -0,0 +1,42 @@ +const MOVIE_TOTAL_PAGE_LIMIT = 500; + +const INITIAL_VALUE = { + page: 1, + totalPages: MOVIE_TOTAL_PAGE_LIMIT, +}; + +function PageEventHandler() { + let attr = { ...INITIAL_VALUE }; + + return { + next() { + if (!this.hasNextPage()) { + return { + page: attr.page, + done: true, + }; + } + attr.page = attr.page + 1; + + return { + page: attr.page, + done: !this.hasNextPage(), + }; + }, + getCurrentPage() { + return attr.page; + }, + setTotalPages(totalPages) { + if (totalPages > MOVIE_TOTAL_PAGE_LIMIT) { + return; + } + attr.totalPages = totalPages; + }, + hasNextPage() { + return attr.page < attr.totalPages; + }, + }; +} + +const PageHandler = PageEventHandler(); +export default PageHandler;
JavaScript
๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ๋“ค์— ๋Œ€ํ•œ ๊ตฌํ˜„๋“ค์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ์š”! ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋‚จ๊ฒจ ๋†“์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”?
@@ -0,0 +1,12 @@ +import PageHandler from './PageHandler'; +import { getPopularMovie } from '../api/movie'; + +export async function getNextPopularMovieList() { + const { page, done } = PageHandler.next(); + const { results } = await getPopularMovie(page); + return { + page, + done, + nextMovieList: results, + }; +}
JavaScript
์กฐ๊ธˆ ๋” ์˜๋„๋ฅผ ์ „๋‹ฌ๋“œ๋ฆฌ์ž๋ฉด! `onClickMoreButton`์€ ํด๋ฆญ + ํŠน์ •๋ฒ„ํŠผ + ํ•ธ๋“ค๋Ÿฌ๋ผ๋Š” ๊ฒƒ๋งŒ ๋“œ๋Ÿฌ๋‚˜์ง€, ์ด ํ•จ์ˆ˜๊ฐ€ ์‹ค์ œ๋กœ ์–ด๋–ค ํ–‰๋™์„ ํ•˜๋Š” ์ง€๋Š” ์ „ํ˜€ ์•Œ๋ ค ์ฃผ์ง€ ๋ชปํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,93 @@ +const context = describe; + +describe('์˜ํ™”๋ชฉ๋ก API ํ…Œ์ŠคํŠธ', () => { + context('์ธ๊ธฐ ์˜ํ™”๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด', () => { + it('20๊ฐœ์˜ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.', () => { + const baseUrl = 'https://api.themoviedb.org/3/movie/popular'; + const param = new URLSearchParams({ + api_key: Cypress.env('API_KEY'), + language: 'ko-KR', + page: 1, + }); + + cy.request('GET', `${baseUrl}?${param}`).as('popularMovies'); + + cy.get('@popularMovies').its('status').should('eq', 200); + cy.get('@popularMovies').its('body.results').should('have.length', 20); + }); + }); +}); + +describe('์˜ํ™”๋ชฉ๋ก e2e ํ…Œ์ŠคํŠธ', () => { + beforeEach(() => { + const baseUrl = 'https://api.themoviedb.org/3/movie/popular'; + const param = new URLSearchParams({ + api_key: Cypress.env('API_KEY'), + language: 'ko-KR', + page: 1, + }); + + cy.intercept( + { + method: 'GET', + url: `${baseUrl}?${param}`, + }, + { fixture: 'movie-popular.json' } + ).as('popularMovies'); + + cy.visit('http://localhost:8080'); + }); + + context('์˜ํ™” ๋ชฉ๋ก ์กฐํšŒ ์‹œ', () => { + it('800ms ๋™์•ˆ Skeleton UI๊ฐ€ ๋…ธ์ถœ๋˜์—ˆ๋‹ค๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.', () => { + cy.wait('@popularMovies'); + cy.get('.skeleton').should('have.length', 60); + cy.wait(800); + cy.get('.skeleton').should('have.length', 0); + }); + }); + + context('ํŽ˜์ด์ง€ ์ง„์ž… ์‹œ ์ธ๊ธฐ์˜ํ™” ๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด', () => { + it('20๊ฐœ์˜ .item-card๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.', () => { + cy.get('.item-card').should('have.length', 20); + }); + }); + + context('๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด', () => { + it('40๊ฐœ์˜ item-card ๋…ธ์ถœ๋œ๋‹ค.', () => { + cy.get('.btn').click(); + cy.get('.item-card').should('have.length', 40); + }); + }); +}); + +describe('์งง์€ ์˜ํ™”๋ชฉ๋ก e2e ํ…Œ์ŠคํŠธ', () => { + beforeEach(() => { + const baseUrl = 'https://api.themoviedb.org/3/movie/popular'; + const param = new URLSearchParams({ + api_key: Cypress.env('API_KEY'), + language: 'ko-KR', + page: 1, + }); + + cy.intercept( + { + method: 'GET', + url: `${baseUrl}?${param}`, + }, + { fixture: 'movie-popular-short.json' } + ).as('popularMovies'); + + cy.visit('http://localhost:8080'); + }); + + context('total_pages ๊ฐ€ 1์ธ ์ธ๊ธฐ์˜ํ™” ๋ชฉ๋ก 1ํŽ˜์ด์ง€๋ฅผ ์กฐํšŒํ•˜๋ฉด', () => { + it('18๊ฐœ์˜ .item-card๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.', () => { + cy.get('.item-card').should('have.length', 18); + }); + + it('๋” ๋ณด๊ธฐ ๋ฒ„ํŠผ์ด ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.', () => { + cy.contains('๋”๋ณด๊ธฐ').should('not.exist'); + }); + }); +});
JavaScript
์ธ๊ธฐ ์˜ํ™”๋ชฉ๋ก ๋ฐ์ดํ„ฐ๊ฐ€ 18๊ฐœ๋ฐ–์— ์—†์„ ๋•Œ 20๊ฐœ๋ฅผ ๊ทธ๋ฆฌ์ง€ ์•Š๊ณ  ๊ฐœ์ˆ˜๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ‘œ์‹œํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,18 @@ +const DEFAULT_SEARCH_PARAMS = { + api_key: process.env.API_KEY, + language: 'ko-KR', +}; +const MOVIE_BASE_URL = 'https://api.themoviedb.org/3/movie'; + +export async function getPopularMovie(page) { + const param = new URLSearchParams({ + ...DEFAULT_SEARCH_PARAMS, + page, + }); + + const response = await fetch(`${MOVIE_BASE_URL}/popular?${param}`); + if (response.ok) { + return response.json(); + } + return { results: [] }; +}
JavaScript
์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ๋ฌถ์–ด์„œ ๋‚ด๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์œ„์™€๊ฐ™์€ ์ข…์ข… ์‚ฌ์šฉํ•ด์™”์—ˆ๋Š”๋ฐ์š”! ์—ฌ๊ธฐ์„œ๋Š” ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋งŒ ๋‚ด๋ณด๋‚ด๊ธฐ์— ์ œ๊ฑฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค..! ํ˜น์‹œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ export ํ•ด์•ผํ•  ๋•Œ ์–ด๋–ค ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์‹œ๊ณ  ์„ ํ˜ธํ•˜์‹œ๋Š”์ง€ ์—ฌ์ญˆ์–ด๋ด๋„ ๋ ๊นŒ์š”?
@@ -0,0 +1,12 @@ +import PageHandler from './PageHandler'; +import { getPopularMovie } from '../api/movie'; + +export async function getNextPopularMovieList() { + const { page, done } = PageHandler.next(); + const { results } = await getPopularMovie(page); + return { + page, + done, + nextMovieList: results, + }; +}
JavaScript
figma๋ฅผ ๋ณด๊ณ  modal ์‚ฌ์šฉ๋ถ€๋ถ„์ด ์žˆ์–ด ๋ฏธ๋ฆฌ ์ž‘์„ฑํ•ด๋‘” ๊ฒƒ์ธ๋ฐ ๋ง์”€ํ•ด์ฃผ์‹  ๊ฒƒ์ฒ˜๋Ÿผ ์œ ์ง€๋ณด์ˆ˜์— ์–ด๋ ค์›€์„ ์ค„ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”..! ๋ช…์‹ฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ํ•ด๋‹น ๋ถ€๋ถ„์€ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™‡
@@ -0,0 +1,12 @@ +import PageHandler from './PageHandler'; +import { getPopularMovie } from '../api/movie'; + +export async function getNextPopularMovieList() { + const { page, done } = PageHandler.next(); + const { results } = await getPopularMovie(page); + return { + page, + done, + nextMovieList: results, + }; +}
JavaScript
`onClickMoreButton`์„ `getNextPopularMovieList`๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ์—ญํ• ์„ ๋ถ„๋ฆฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,42 @@ +const MOVIE_TOTAL_PAGE_LIMIT = 500; + +const INITIAL_VALUE = { + page: 1, + totalPages: MOVIE_TOTAL_PAGE_LIMIT, +}; + +function PageEventHandler() { + let attr = { ...INITIAL_VALUE }; + + return { + next() { + if (!this.hasNextPage()) { + return { + page: attr.page, + done: true, + }; + } + attr.page = attr.page + 1; + + return { + page: attr.page, + done: !this.hasNextPage(), + }; + }, + getCurrentPage() { + return attr.page; + }, + setTotalPages(totalPages) { + if (totalPages > MOVIE_TOTAL_PAGE_LIMIT) { + return; + } + attr.totalPages = totalPages; + }, + hasNextPage() { + return attr.page < attr.totalPages; + }, + }; +} + +const PageHandler = PageEventHandler(); +export default PageHandler;
JavaScript
์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค! ์ด์œ ๋Š” ์•ž์„œ ๋ง์”€๋“œ๋ฆฐ ์ด์œ ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค! ๊ผผ๊ผผํžˆ ๋ฆฌ๋ทฐํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค ๐Ÿ™‡
@@ -0,0 +1,12 @@ +import PageHandler from './PageHandler'; +import { getPopularMovie } from '../api/movie'; + +export async function getNextPopularMovieList() { + const { page, done } = PageHandler.next(); + const { results } = await getPopularMovie(page); + return { + page, + done, + nextMovieList: results, + }; +}
JavaScript
์Œ... ์ด ๋ถ€๋ถ„์€ ์ œ๊ฐ€ ์˜๋„ํ•œ ๋ฐ”์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ๋ณ€๊ฒฝ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ œ๊ฐ€ ์˜์‚ฌ ์ฝ”๋“œ๋ฅผ ์„ž์–ด๊ฐ€๋ฉฐ ์–ด๋А ์ •๋„ ๋ณ€๊ฒฝํ•ด๋ณผ๊ฒŒ์š”!
@@ -0,0 +1,18 @@ +export class LoadingHandler { + #loading; + constructor(initialValue) { + this.#loading = initialValue; + } + + start() { + this.#loading = true; + } + + end() { + this.#loading = false; + } + + isLoading() { + return this.#loading; + } +}
JavaScript
์‚ฌ์šฉ์ฒ˜๋ฅผ ๋ณด๋ฉด, ๋กœ๋”ฉ ์ƒํƒœ ์ž์ฒด๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ•œ ๋ฒˆ ๋ฐ–์— ์—†๊ณ , start(), end() ์‹คํ–‰๊ณผ ํ•จ๊ป˜ DOM ์กฐ์ž‘ ๋กœ์ง์ด ํ•จ๊ป˜ ์˜ค๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ์—ฌ๊ธฐ์„œ DOM ์กฐ์ž‘๋„ ๊ฐ™์ด ํ•ด์ฃผ๊ณ , ์ž…๋ ฅ์œผ๋กœ ์กฐ์ž‘ํ•  ์š”์†Œ๋ฅผ ๊ฐ™์ด ๋ฐ›์œผ๋ฉด ๋” ์‘์ง‘์„ฑ์ด ๋†’์€ ์ฝ”๋“œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,18 @@ +const DEFAULT_SEARCH_PARAMS = { + api_key: process.env.API_KEY, + language: 'ko-KR', +}; +const MOVIE_BASE_URL = 'https://api.themoviedb.org/3/movie'; + +export async function getPopularMovie(page) { + const param = new URLSearchParams({ + ...DEFAULT_SEARCH_PARAMS, + page, + }); + + const response = await fetch(`${MOVIE_BASE_URL}/popular?${param}`); + if (response.ok) { + return response.json(); + } + return { results: [] }; +}
JavaScript
์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ๋ฌถ์–ด์„œ ๋‚ด๋ณด๋‚ด๊ธฐ ์œ„ํ•จ์ด๋‹ค -> ์ปจ๋ฒค์…˜๋งŒ ๋งž๋‹ค๋ฉด ์–ด๋–ค ๋ฐฉํ–ฅ์ด๋˜ ์ƒ๊ด€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! ๋‹ค๋งŒ, ์ €๋Š” ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์ธ ์ƒํ™ฉ์—์„œ ํ•จ์ˆ˜๊ฐ€ ๋” ์ถ”๊ฐ€๋˜์ง€ ์•Š์•˜๋Š”๋ฐ, ๋” ์ถ”๊ฐ€๋  ์˜ˆ์ •์ธ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜์‹  ๊ฒƒ ๊ฐ™์•„์„œ ๋‚จ๊ธด ์ฝ”๋ฉ˜ํŠธ์ด๊ธดํ•ฉ๋‹ˆ๋‹ค! (๋ณธ๋ฌธ์˜ ์ฝ”๋ฉ˜ํŠธ์™€ ๋™์ผํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค ใ…Žใ…Ž)
@@ -0,0 +1,46 @@ +package lottodomain; + +import java.util.ArrayList; +import java.util.List; + +public class LottoGame { + private static final int PRICE_PER_LOTTO = 1000; + + private List<Lotto> allLotto; + private int numberOfAllLotto; + + public LottoGame(int inputMoney, List<List<Integer>> manualLottoNos) { + numberOfAllLotto = inputMoney / PRICE_PER_LOTTO; + allLotto = issueLottos(manualLottoNos); + } + + public LottoAnalyzer getLottoAnalyzer(WinningNos winningNos) { + return new LottoAnalyzer(allLotto, winningNos); + } + + public List<Lotto> getAllLotto() { + return allLotto; + } + + private List<Lotto> issueLottos(List<List<Integer>> manualLottoNos) { + List<Lotto> lottos = issueManualLottos(manualLottoNos); + lottos.addAll(issueAutoLottos(numberOfAllLotto - lottos.size())); + return lottos; + } + + private List<Lotto> issueManualLottos(List<List<Integer>> manualLottoNos) { + List<Lotto> ManualLottos = new ArrayList<>(); + for (int i = 0; i < manualLottoNos.size(); i++) { + ManualLottos.add(LottoGenerator.generateLottoWithNos(manualLottoNos.get(i))); + } + return ManualLottos; + } + + private List<Lotto> issueAutoLottos(int numberOfAutoLottos) { + List<Lotto> autoLottos = new ArrayList<>(); + for (int i = 0; i < numberOfAutoLottos; i++) { + autoLottos.add(LottoGenerator.generateAutoLotto()); + } + return autoLottos; + } +}
Java
๋ณ€์ˆ˜๋ช…์ด ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋„ค์š” ~
@@ -0,0 +1,25 @@ +package lottodomain; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class LottoGenerator { + private static final List<Integer> LOTTO_NO_POOLS = IntStream.rangeClosed(1, 45).boxed().collect(Collectors.toList()); + ; + + public static Lotto generateLottoWithNos(List<Integer> lottoNos) { + return new Lotto(lottoNos); + } + + public static Lotto generateAutoLotto() { + return generateLottoWithNos(getRandomNos()); + } + + + private static List<Integer> getRandomNos() { + Collections.shuffle(LOTTO_NO_POOLS); + return LOTTO_NO_POOLS.subList(0, 6); + } +}
Java
Lotto ์— factory method ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ƒ์„ฑํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ex> Lotto.ofAuto(), Lotto.ofManual("1,2,3,4,5,6)
@@ -0,0 +1,25 @@ +package lottodomain; + +public class LottoNo { + private Integer value; + + public LottoNo(int number) { + value = number; + } + + public Integer getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LottoNo lottoNo = (LottoNo) o; + return value == lottoNo.value; + } +}
Java
LottoNo ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ range ์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๊ฒ€์ฆํ•˜๋ฉด ์ข€ ๋” ์˜๋ฏธ ์žˆ๋Š” wrapping class ๊ฐ€ ๋˜์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,25 @@ +package lottodomain; + +public class LottoNo { + private Integer value; + + public LottoNo(int number) { + value = number; + } + + public Integer getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LottoNo lottoNo = (LottoNo) o; + return value == lottoNo.value; + } +}
Java
๋ฌธ์ œ : "๋ชจ๋“  ์›์‹œ๊ฐ’๊ณผ ๋ฌธ์ž์—ด์„ ํฌ์žฅํ•œ๋‹ค." ์›์น™์— ๋”ฐ๋ผ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋‹ค๋ณด๋‹ˆ ๋„ˆ๋ฌด ๋งŽ์€ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ , GC๊ฐ€ ๋˜์–ด ์„ฑ๋Šฅ์ƒ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ํŠนํžˆ ๋กœ๋˜ ๋ฒˆํ˜ธ ํ•˜๋‚˜๊นŒ์ง€ ๊ฐ์ฒด๋กœ ํฌ์žฅํ•  ๊ฒฝ์šฐ ์ƒ์„ฑ๋˜๋Š” ์ธ์Šคํ„ด์Šค์˜ ์ˆ˜๋Š” ์ƒ๋‹นํžˆ ๋Š˜์–ด๋‚˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? (ํ‚ค์›Œ๋“œ : static, map)
@@ -0,0 +1,25 @@ +package lottodomain; + +public class LottoNo { + private Integer value; + + public LottoNo(int number) { + value = number; + } + + public Integer getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + LottoNo lottoNo = (LottoNo) o; + return value == lottoNo.value; + } +}
Java
๋ถˆ๋ณ€์˜ ๊ฐ’์„ wrapping ํ•œ ํด๋ž˜์Šค์ด๋ฏ€๋กœ final int value; ๋กœ ์„ ์–ธํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,49 @@ +package lottoview; + +import lottodomain.Lotto; +import lottodomain.LottoAnalyzer; +import lottodomain.LottoNo; +import lottodomain.Rank; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OutputView { + public static void showAllLottos(List<Lotto> allLotto, int numberOfManualLottos) { + int numberOfAutoLottos = allLotto.size() - numberOfManualLottos; + System.out.printf("์ˆ˜๋™์œผ๋กœ %d์žฅ, ์ž๋™์œผ๋กœ %d์žฅ์„ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.\n", numberOfManualLottos, numberOfAutoLottos); + allLotto.stream().forEach(lotto -> showLotto(lotto)); + } + + public static void showLotto(Lotto lotto) { + List<LottoNo> lottoNos = lotto.getLottoNos(); + System.out.println(lottoNos.stream().map(LottoNo::getValue).map(number -> number.toString()) + .collect(Collectors.joining(", ", "[", "]"))); + } + + public static void showAnalysis(LottoAnalyzer lottoAnalyzer) { + showWRanks(lottoAnalyzer); + showEarningRate(lottoAnalyzer.calculateEarningRate()); + } + + public static void showRankCount(Rank rank, int count) { + System.out.printf("%d๊ฐœ ์ผ์น˜ (%d์›)- %d๊ฐœ\n", rank.getCountOfMatch(), rank.getWinningMoney(), count); + } + + public static void showEarningRate(double earningRate) { + System.out.printf("์ด ์ˆ˜์ต๋ฅ ์€ %.1f%%์ž…๋‹ˆ๋‹ค.\n", earningRate); + } + + private static void showWRanks(LottoAnalyzer lottoAnalyzer) { + System.out.println("๋‹น์ฒจ ํ†ต๊ณ„"); + System.out.println("---------"); + List<Rank> prize = Stream.of(Rank.values()).collect(Collectors.toList()); + prize.remove(Rank.MISS); + for (int i = prize.size() - 1; i >= 0; i--) { + int count = lottoAnalyzer.countRank(prize.get(i)); + showRankCount(prize.get(i), count); + } + } +}
Java
analyzer ๊ฐ€ outputview ์— ์™€์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š” ๊ฒŒ์ž„์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด๋Š” LottoResults(DTO) ํด๋ž˜์Šค์™€ Lotto list ๋ฅผ ๊ฐ์‹ธ๋Š” Lottos ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ outputview ์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์‹คํ–‰๋˜์ง€ ์•Š๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,63 @@ +package lottodomain; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class LottoAnalyzerTest { + private static final double DELTA = 1e-15; + + private List<Lotto> lottos; + private LottoAnalyzer lottoAnalyzer; + private WinningNos winningNos; + + @Before + public void setUp() throws Exception { + lottos = new ArrayList<>(); + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6))); // FIRST + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 7))); // SECOND + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 8))); // THIRD + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 7, 8))); // FOURTH + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 7, 8, 9))); // FIFTH + lottos.add(new Lotto(Arrays.asList(1, 2, 7, 8, 9, 10))); // MISS + winningNos = new WinningNos(Arrays.asList(1, 2, 3, 4, 5, 6), 7); + lottoAnalyzer = new LottoAnalyzer(lottos, winningNos); + } + + @Test + public void calculateRankOfLotto() { + List<Rank> ranks = Arrays.asList(Rank.values()); + for (int i = 0; i < lottos.size(); i++) { + assertEquals(ranks.get(i), lottoAnalyzer.calculateRankOfLotto(lottos.get(i), winningNos)); + } + } + + @Test + public void calculateEarnedMoney() { + assertEquals(2031555000, lottoAnalyzer.calculateEarnedMoney()); + } + + @Test + public void calculateEarningRate() { + assertEquals(33859250.0, lottoAnalyzer.calculateEarningRate(), DELTA); + } + + @Test + public void countRank() { + List<Rank> ranks = Arrays.asList(Rank.values()); + for (int i = 0; i < ranks.size(); i++) { + assertEquals(1, lottoAnalyzer.countRank(ranks.get(i))); + } + } + + @After + public void tearDown() throws Exception { + lottos = null; + } +} \ No newline at end of file
Java
์ข‹์€ ์œ ๋‹›ํ…Œ์ŠคํŠธ ์ž‘์„ฑ๋ฒ•์—๋Š” F.I.R.S.T ๋ผ๋Š” ์›์น™์ด ์žˆ๋Š”๋ฐ์š” https://coding-start.tistory.com/261 ์ฒจ๋ถ€ํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ~
@@ -0,0 +1,47 @@ +package lottodomain; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.*; + +public class LottoGameTest { + private LottoGame lottoGame; + List<List<Integer>> manualLottoNumbers; + + private void assertArrayEquals(List<Lotto> expectedLottos, List<Lotto> subList) { + for (int i = 0; i < expectedLottos.size(); i++) { + assertEquals(expectedLottos.get(i), subList.get(i)); + } + } + + @Before + public void setUp() throws Exception { + manualLottoNumbers = new ArrayList<>(); + manualLottoNumbers.add(Arrays.asList(1, 2, 3, 4, 5, 6)); + manualLottoNumbers.add(Arrays.asList(1, 4, 11, 15, 19, 23)); + manualLottoNumbers.add(Arrays.asList(4, 10, 17, 25, 34, 42)); + } + + @Test + public void setManualLottos() { + lottoGame = new LottoGame(10000, manualLottoNumbers); + assertEquals(10, lottoGame.getAllLotto().size()); + + List<Lotto> expectedLottos = manualLottoNumbers.stream().map(numbers -> LottoGenerator.generateLottoWithNos(numbers)) + .collect(Collectors.toList()); + assertArrayEquals(expectedLottos, lottoGame.getAllLotto().subList(0, 3)); + } + + @After + public void tearDown() throws Exception { + lottoGame = null; + manualLottoNumbers = null; + } +} \ No newline at end of file
Java
private method ๋Š” public ๋ณด๋‹ค ์•„๋ž˜์— ์œ„์น˜์‹œ์ผœ์ฃผ๋Š”๊ฒŒ ๊ฐ€๋…์„ฑ์— ์ข‹์Šต๋‹ˆ๋‹ค ~
@@ -0,0 +1,50 @@ +package lottodomain; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class WinningNosTest { + private WinningNos winningNos; + + @Before + public void setUp() throws Exception { + winningNos = new WinningNos(Arrays.asList(1, 2, 3, 4, 5, 6), 7); + } + + @Test + public void countMatchOf() { + List<Lotto> lottos = new ArrayList<>(); + lottos.add(new Lotto(Arrays.asList(8, 9, 10, 11, 12, 13))); + lottos.add(new Lotto(Arrays.asList(1, 8, 9, 10, 11, 12))); + lottos.add(new Lotto(Arrays.asList(1, 2, 9, 10, 11, 12))); + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 10, 11, 12))); + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 11, 12))); + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 12))); + lottos.add(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6))); + + for (int i = 0; i < lottos.size(); i++) { + assertEquals(i, winningNos.countMatchOf(lottos.get(i))); + } + } + + @Test + public void isBonus() { + Lotto trueLotto = new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6, 7)); + Lotto falseLotto = new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6, 8)); + + assertTrue(winningNos.isBonus(trueLotto)); + assertFalse(winningNos.isBonus(falseLotto)); + } + + @After + public void tearDown() throws Exception { + winningNos = null; + } +} \ No newline at end of file
Java
์ „์ฒด์ ์œผ๋กœ ์ฝ”๋“œ์— ๋น„ํ•ด ํ…Œ์ŠคํŠธ ์ชฝ์ด ์กฐ๊ธˆ ์•„์‰ฌ์›Œ์„œ ์‹œ๊ฐ„์ด ๋˜์‹ ๋‹ค๋ฉด TDD ๋ฐฉ๋ฒ•๋ก ์„ ํ†ตํ•ด ๋กœ๋˜๋‚˜ ๋ ˆ์ด์‹ฑ ๊ฒŒ์ž„์„ ๋‹ค์‹œ ๊ตฌํ˜„ํ•ด๋ณด๋Š” ๊ฒƒ๋„ ๋งŽ์ด ๋„์›€์ด ๋˜์‹ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -1,5 +1,98 @@ import React from 'react'; +import { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './Login.scss'; -export default function Login() { - return <div>Hello, Hyeonze!</div>; -} +const Login = () => { + const [idInput, setIdInput] = useState(''); + const [pwInput, setPwInput] = useState(''); + const [isValidatedUser, setIsValidatedUser] = useState(false); + + const navigate = useNavigate(); + const changeIdInput = e => { + const { value } = e.target; + setIdInput(value); + }; + + const changePwInput = e => { + const { value } = e.target; + setPwInput(value); + }; + + useEffect(() => { + setIsValidatedUser(idInput.indexOf('@') !== -1 && pwInput.length > 4); + }, [idInput, pwInput]); + + // ๋ฉ”์ธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๋กœ์ง + const goToMain = () => { + navigate('/main-hyeonze'); + }; + + // ํšŒ์›๊ฐ€์ž…์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const signup = () => { + // fetch('http://10.58.4.22:8000/users/signup', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'EMAIL_ALREADY_EXISTS') { + // console.log('Error : Duplicated'); + // } + // }); + // }; + + // ๋กœ๊ทธ์ธ์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const login = () => { + // fetch('http://10.58.4.22:8000/users/login', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'SUCCESS') { + // console.log('login SUCCESS'); + // } + // }); + // }; + + return ( + <div className="login"> + <div id="wrapper"> + <h1>westagram</h1> + <form className="login_form"> + <input + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + className="username" + onChange={changeIdInput} + /> + <input + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + className="userpassword" + onChange={changePwInput} + /> + <input + className={isValidatedUser ? 'activated' : ''} + disabled={!isValidatedUser} + type="button" + value="๋กœ๊ทธ์ธ" + onClick={goToMain} + /> + </form> + <p>๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</p> + </div> + </div> + ); +}; + +export default Login;
Unknown
์ด ๋‘ ์กฐ๊ฑด์„ &&๋กœ ๊ฑธ๋ฉด state๋ฅผ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๊ณ  validation์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? ์™œ ์œ ์ฆˆ์ดํŽ™ํŠธ๋ฅผ ์“ฐ์…จ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.
@@ -1,5 +1,98 @@ import React from 'react'; +import { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './Login.scss'; -export default function Login() { - return <div>Hello, Hyeonze!</div>; -} +const Login = () => { + const [idInput, setIdInput] = useState(''); + const [pwInput, setPwInput] = useState(''); + const [isValidatedUser, setIsValidatedUser] = useState(false); + + const navigate = useNavigate(); + const changeIdInput = e => { + const { value } = e.target; + setIdInput(value); + }; + + const changePwInput = e => { + const { value } = e.target; + setPwInput(value); + }; + + useEffect(() => { + setIsValidatedUser(idInput.indexOf('@') !== -1 && pwInput.length > 4); + }, [idInput, pwInput]); + + // ๋ฉ”์ธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๋กœ์ง + const goToMain = () => { + navigate('/main-hyeonze'); + }; + + // ํšŒ์›๊ฐ€์ž…์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const signup = () => { + // fetch('http://10.58.4.22:8000/users/signup', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'EMAIL_ALREADY_EXISTS') { + // console.log('Error : Duplicated'); + // } + // }); + // }; + + // ๋กœ๊ทธ์ธ์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const login = () => { + // fetch('http://10.58.4.22:8000/users/login', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'SUCCESS') { + // console.log('login SUCCESS'); + // } + // }); + // }; + + return ( + <div className="login"> + <div id="wrapper"> + <h1>westagram</h1> + <form className="login_form"> + <input + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + className="username" + onChange={changeIdInput} + /> + <input + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + className="userpassword" + onChange={changePwInput} + /> + <input + className={isValidatedUser ? 'activated' : ''} + disabled={!isValidatedUser} + type="button" + value="๋กœ๊ทธ์ธ" + onClick={goToMain} + /> + </form> + <p>๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</p> + </div> + </div> + ); +}; + +export default Login;
Unknown
id์™€ pw๋กœ ํ•œ๋ฒˆ์— validationํ•˜๊ณ  activateBtn์— True/false๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. isValidatedId๋‚˜ isValidatedPw๋Š” ํฌ๊ฒŒ ํ•„์š”ํ•˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์•„์š”. ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋žœ๋”๋ง๋„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,66 @@ +@use '../../../styles/mixin.scss' as mixin; +@use '../../../styles/color.scss' as clr; + +body { + background: clr.$bg-login; + + .login { + position: relative; + top: -80px; + + #wrapper { + @include mixin.flex($align: center); + position: relative; + flex-direction: column; + width: 700px; + height: 761px; + border: 1px solid clr.$clr-border; + + h1 { + margin-top: 90px; + margin-bottom: 130px; + font-size: 81px; + font-family: 'Lobster', cursive; + } + + .login_form { + input { + display: block; + width: 536px; + height: 76px; + margin-bottom: 12px; + padding-left: 10px; + background: #fafafa; + border: 1px solid clr.$clr-border; + border-radius: 3px; + font-size: 16px; + } + + input[type='button'] { + display: flex; + align-items: center; + justify-content: center; + width: 536px; + height: 60px; + margin-top: 16px; + background-color: clr.$clr-primary; + opacity: 0.5; + border: none; + border-radius: 4px; + font-size: 16px; + color: #fff; + + &.activated { + opacity: 1; + } + } + } + + p { + position: absolute; + bottom: 20px; + color: #043667; + } + } + } +}
Unknown
input์ด inline-block์ด๊ณ  ์ˆ˜์ง์œผ๋กœ ์ •๋ ฌํ•˜๋ ค๊ณ  display: flex๋ฅผ ์“ฐ์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ต‰์žฅํžˆ ์ข‹์€๋ฐ์š” ์ €๋Š” Input์„ display: blockํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์€ ๋ฐฉ๋ฒ•์ด์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,66 @@ +@use '../../../styles/mixin.scss' as mixin; +@use '../../../styles/color.scss' as clr; + +body { + background: clr.$bg-login; + + .login { + position: relative; + top: -80px; + + #wrapper { + @include mixin.flex($align: center); + position: relative; + flex-direction: column; + width: 700px; + height: 761px; + border: 1px solid clr.$clr-border; + + h1 { + margin-top: 90px; + margin-bottom: 130px; + font-size: 81px; + font-family: 'Lobster', cursive; + } + + .login_form { + input { + display: block; + width: 536px; + height: 76px; + margin-bottom: 12px; + padding-left: 10px; + background: #fafafa; + border: 1px solid clr.$clr-border; + border-radius: 3px; + font-size: 16px; + } + + input[type='button'] { + display: flex; + align-items: center; + justify-content: center; + width: 536px; + height: 60px; + margin-top: 16px; + background-color: clr.$clr-primary; + opacity: 0.5; + border: none; + border-radius: 4px; + font-size: 16px; + color: #fff; + + &.activated { + opacity: 1; + } + } + } + + p { + position: absolute; + bottom: 20px; + color: #043667; + } + } + } +}
Unknown
'๋กœ๊ทธ์ธ' ์ด๋ผ๋Š” ๊ธ€์ž ์„ผํ„ฐ์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด display: flex์ฃผ์‹  ๊ฒƒ ๊ฐ™์•„์š”. ์ข‹์€๋ฐฉ๋ฒ•์ด์ง€๋งŒ flex์™€ ๊ทธ ์†์„ฑ๋“ค์ด ์ข€ ๋–จ์–ด์ ธ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ฐ๊ด€๋˜๋Š” css์†์„ฑ์€ ๋ถ™์—ฌ ์ฃผ์‹œ๋Š” ๊ฒŒ ๋‚˜์ค‘์— ์œ ์ง€๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ ์ถ”์ฒœ ๋“œ๋ฆฝ๋‹ˆ๋‹ค!
@@ -0,0 +1,26 @@ +import React, { useState } from 'react'; +import { AiFillDelete, AiOutlineHeart, AiFillHeart } from 'react-icons/ai'; + +const Comment = ({ id, userId, value, time, handleDelete }) => { + const [isLiked, setIsLiked] = useState(false); + + const toggleLike = () => { + setIsLiked(!isLiked); + }; + + return ( + <span id={id} className="comment"> + <strong>{userId} </strong> + {value} + <small> {time}</small> + <AiFillDelete onClick={handleDelete} className="delete" /> + {isLiked ? ( + <AiFillHeart onClick={toggleLike} className="hearts liked" /> + ) : ( + <AiOutlineHeart onClick={toggleLike} className="hearts" /> + )} + </span> + ); +}; + +export default Comment;
Unknown
์ €๋ฒˆ ์„ธ์…˜์—์„œ ๋ฉ˜ํ† ๋‹˜์ด ํ•˜์‹  ๊ฒƒ์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋ช…์„ ์กฐ๊ธˆ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ค ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š”์ง€๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. commentVal์ด ๋Œ“๊ธ€ ์ „์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฐ์—ด์ด๊ณ  el์ด ๊ฐ ๋ฐฐ์—ด์˜ ์š”์†Œ์ด๋‹ˆ๊น commentVal๋Œ€์‹  allComments, el๋Œ€์‹  comment๋“ฑ์œผ๋กœ (์ด๊ฑด ์ €๋„ ์กฐ๊ธˆ ์• ๋งคํ•˜๋„ค์š” .. ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค) (๋ฐฐ์—ด์€ s๋ฅผ ๋ถ™์ธ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ํ•˜๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!)
@@ -0,0 +1,43 @@ +span.comment { + padding-left: 10px; + + small { + color: #a3a3a3; + } + + &:first-child { + padding: 10px; + font-weight: bold; + } + + svg { + &.delete { + display: none; + cursor: pointer; + } + + &.hearts { + color: #a3a3a3; + position: absolute; + right: 20px; + transition: color 1s; + cursor: pointer; + + path { + color: #a3a3a3; + } + } + + &.liked { + path { + color: #f00; + } + } + } + + &:hover svg { + &.delete { + display: inline-block; + } + } +}
Unknown
span์— ๋Œ€ํ•ด์„œ๋งŒ ์Šคํƒ€์ผ๋ง์„ ํ•ด์ฃผ์…จ๋Š”๋ฐ ์ด๋Ÿฌ๋ฉด ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์˜ span์—๋„ ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š์„๊นŒ ์กฐ์‹ฌ์Šค๋Ÿฝ๊ฒŒ ์ƒ๊ฐํ•ด ๋ด…๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ className์„ ์ฃผ๋Š” ๊ฒŒ ์กฐ๊ธˆ ๋” ์•ˆ์ „ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ์ถ”๊ฐ€์ ์œผ๋กœ svgํƒœ๊ทธ๋„ span ๋‚ด๋ถ€์— nestingํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. ``` svg { &.red {} &.blue {} } ```
@@ -0,0 +1,87 @@ +import React, { useState } from 'react'; +import { AiFillHeart, AiOutlineComment, AiOutlineUpload } from 'react-icons/ai'; +import { BiDotsHorizontalRounded } from 'react-icons/bi'; +import { GrBookmark } from 'react-icons/gr'; +import './Feeds.scss'; +import Comments from './Comment/Comments'; + +export default function Feeds({ userId, profileImg, img, comments }) { + const [commentVal, setCommentVal] = useState(comments); + let addedCommentVal = [...commentVal]; + const [currInputVal, setCurrInputVal] = useState(''); + + const handleInput = e => { + const { value } = e.target; + if (e.keyCode === 13) uploadComment(); + setCurrInputVal(value); + }; + + const uploadComment = e => { + e.preventDefault(); + if (!currInputVal) return; + addedCommentVal.push({ + id: addedCommentVal.length + 1, + userId: 'userid', + value: currInputVal, + time: '๋ฐฉ๊ธˆ์ „', + }); + setCommentVal(addedCommentVal); + setCurrInputVal(''); + }; + + const handleDelete = e => { + const { id } = e.target.parentNode.parentNode; + addedCommentVal = addedCommentVal.filter(el => el.id !== Number(id)); + setCommentVal(addedCommentVal); + }; + + return ( + <div className="feeds"> + <article> + <div className="top_menu"> + <span> + <img src={profileImg} alt="ํ”„๋กœํ•„" /> + {userId} + </span> + <span> + <BiDotsHorizontalRounded /> + </span> + </div> + <img src={img} alt="๋ฌดํ™”๊ณผ" /> + <div className="bottom_menu"> + <span> + <AiFillHeart className="" /> + </span> + <span> + <AiOutlineComment className="" /> + </span> + <span> + <AiOutlineUpload className="" /> + </span> + <span> + <GrBookmark className="" /> + </span> + </div> + <div className="comments"> + <span>4 Likes</span> + <Comments commentArr={commentVal} handleDelete={handleDelete} /> + </div> + <form className="add_comments"> + <input + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + className="input_upload" + onChange={handleInput} + value={currInputVal} + /> + <button + className={currInputVal ? 'activated' : ''} + onClick={uploadComment} + > + ๊ฒŒ์‹œ + </button> + </form> + </article> + </div> + ); +}
Unknown
๊ต‰์žฅํžˆ ์ž˜ ์งœ์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!! ๐Ÿ‘ ๊ทธ๋Ÿฐ๋ฐ ์ˆœ์„œ๊ฐ€ ์กฐ๊ธˆ ์• ๋งคํ•œ ๊ฒƒ ๊ฐ™์•„์š”. 17)์—์„œ uploadComment ํ˜ธ์ถœ์„ ํ•˜๊ณ  31)์—์„œ currInputVal( " " ) -> 18 ) ์—์„œ currInputVal( e.target.value ) ์ˆœ์œผ๋กœ ์ง„ํ–‰์ด ๋œ๋‹ค๋ฉด ๋Œ“๊ธ€์„ ์ถ”๊ฐ€ํ•˜๋”๋ผ๋„ input์€ ๋น„์›Œ์ง€์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,101 @@ +@use '../../../../styles/mixin.scss' as mixin; +@use '../../../../styles/color.scss' as clr; + +.feeds { + position: relative; + top: 77px; + width: 60%; + margin-bottom: 80px; + background: #fff; + + .top_menu { + @include mixin.flex($justify: space-between, $align: center); + height: 82px; + + span { + @include mixin.flex($align: center); + margin-left: 20px; + font-weight: bold; + + &:last-child { + margin-right: 22px; + color: #2a2a2a; + font-size: 35px; + } + + img { + width: 42px; + height: 42px; + border-radius: 50%; + margin-right: 20px; + } + } + } + + article { + img { + width: 100%; + height: 818px; + } + + .bottom_menu { + @include mixin.flex($align: center); + position: relative; + height: 62px; + + span > svg { + margin: 0 10px; + font-size: 32px; + color: #414141; + } + + span:last-child > svg { + position: absolute; + top: 15px; + right: 0; + } + + span:first-child > svg > path { + color: #eb4b59; + } + } + } + + .comments { + display: flex; + flex-direction: column; + + span { + padding-left: 10px; + } + } + + .add_comments { + position: absolute; + @include mixin.flex($justify: space-between, $align: center); + bottom: -62px; + width: 100%; + height: 62px; + border-top: 1px solid clr.$clr-border; + font-size: 14px; + + input { + width: 90%; + height: 50%; + margin-left: 15px; + border: none; + } + + button { + margin-right: 15px; + border: none; + color: clr.$clr-primary; + opacity: 0.5; + background: #fff; + + &.activated { + opacity: 1; + } + } + } +}
Unknown
Feeds.jsx์—์„œ .feeds์˜ ๋ฐ”๋กœ ์ž์‹์š”์†Œ๊ฐ€ article์ด๋ผ article์„ .feeds ๋ฐ”๋กœ ๋‹ค์Œ์— ์จ์ฃผ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  height์š”์†Œ๋Š” ์ž์‹์š”์†Œ๋“ค์˜ ๋†’์ด์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜๋‹ˆ๊น height: 100%๋ฅผ ์ฃผ์–ด๋„ feeds์˜ ๋†’์ด๊ฐ€ ๋ณ€ํ•˜์ง€๋Š” ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋Š”๋ฐ ํ˜น์‹œ ์„ค์ •ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ ๊ฐ€์š”?
@@ -1,5 +1,98 @@ import React from 'react'; +import { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './Login.scss'; -export default function Login() { - return <div>Hello, Hyeonze!</div>; -} +const Login = () => { + const [idInput, setIdInput] = useState(''); + const [pwInput, setPwInput] = useState(''); + const [isValidatedUser, setIsValidatedUser] = useState(false); + + const navigate = useNavigate(); + const changeIdInput = e => { + const { value } = e.target; + setIdInput(value); + }; + + const changePwInput = e => { + const { value } = e.target; + setPwInput(value); + }; + + useEffect(() => { + setIsValidatedUser(idInput.indexOf('@') !== -1 && pwInput.length > 4); + }, [idInput, pwInput]); + + // ๋ฉ”์ธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๋กœ์ง + const goToMain = () => { + navigate('/main-hyeonze'); + }; + + // ํšŒ์›๊ฐ€์ž…์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const signup = () => { + // fetch('http://10.58.4.22:8000/users/signup', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'EMAIL_ALREADY_EXISTS') { + // console.log('Error : Duplicated'); + // } + // }); + // }; + + // ๋กœ๊ทธ์ธ์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const login = () => { + // fetch('http://10.58.4.22:8000/users/login', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'SUCCESS') { + // console.log('login SUCCESS'); + // } + // }); + // }; + + return ( + <div className="login"> + <div id="wrapper"> + <h1>westagram</h1> + <form className="login_form"> + <input + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + className="username" + onChange={changeIdInput} + /> + <input + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + className="userpassword" + onChange={changePwInput} + /> + <input + className={isValidatedUser ? 'activated' : ''} + disabled={!isValidatedUser} + type="button" + value="๋กœ๊ทธ์ธ" + onClick={goToMain} + /> + </form> + <p>๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</p> + </div> + </div> + ); +}; + +export default Login;
Unknown
์ด ๋‘ ์กฐ๊ฑด์„ &&๋กœ ๊ฑธ๋ฉด state๋ฅผ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๊ณ  validation์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”? => ๋งž๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ˆ˜์ • ์ „ ์ฝ”๋“œ์—์„œ ์œ ํšจ์„ฑ๊ฒ€์‚ฌ๋ฅผ changeIdInput, changePwInput ๋‚ด์—์„œ ๊ฐ๊ฐ ํ•ด์คฌ์—ˆ๋Š”๋ฐ ํ˜ธํ™˜์„ฑ ๊ฒ€์‚ฌ์— ๋”œ๋ ˆ์ด๊ฐ€ ์ƒ๊ฒจ์„œ useEffect๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ํ•ด๊ฒฐ์ฑ…์ด๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์—์„œ changeIdInput, changePwInput ๋‚ด๋ถ€์˜ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋“ค์„ useEffect ๋‚ด๋ถ€๋กœ ์ž˜๋ผ๋‚ด์–ด ์˜ค๊ฒŒ ๋˜๋ฉด์„œ ๋กœ์ง์ด ๊ธธ์–ด์ง„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -1,5 +1,98 @@ import React from 'react'; +import { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './Login.scss'; -export default function Login() { - return <div>Hello, Hyeonze!</div>; -} +const Login = () => { + const [idInput, setIdInput] = useState(''); + const [pwInput, setPwInput] = useState(''); + const [isValidatedUser, setIsValidatedUser] = useState(false); + + const navigate = useNavigate(); + const changeIdInput = e => { + const { value } = e.target; + setIdInput(value); + }; + + const changePwInput = e => { + const { value } = e.target; + setPwInput(value); + }; + + useEffect(() => { + setIsValidatedUser(idInput.indexOf('@') !== -1 && pwInput.length > 4); + }, [idInput, pwInput]); + + // ๋ฉ”์ธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๋กœ์ง + const goToMain = () => { + navigate('/main-hyeonze'); + }; + + // ํšŒ์›๊ฐ€์ž…์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const signup = () => { + // fetch('http://10.58.4.22:8000/users/signup', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'EMAIL_ALREADY_EXISTS') { + // console.log('Error : Duplicated'); + // } + // }); + // }; + + // ๋กœ๊ทธ์ธ์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const login = () => { + // fetch('http://10.58.4.22:8000/users/login', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'SUCCESS') { + // console.log('login SUCCESS'); + // } + // }); + // }; + + return ( + <div className="login"> + <div id="wrapper"> + <h1>westagram</h1> + <form className="login_form"> + <input + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + className="username" + onChange={changeIdInput} + /> + <input + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + className="userpassword" + onChange={changePwInput} + /> + <input + className={isValidatedUser ? 'activated' : ''} + disabled={!isValidatedUser} + type="button" + value="๋กœ๊ทธ์ธ" + onClick={goToMain} + /> + </form> + <p>๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</p> + </div> + </div> + ); +}; + +export default Login;
Unknown
[๊ณต์‹๋ฌธ์„œ React๋กœ ์‚ฌ๊ณ ํ•˜๊ธฐ](https://ko.reactjs.org/docs/thinking-in-react.html#step-3-identify-the-minimal-but-complete-representation-of-ui-state) ๋ฅผ ๋ณด์‹œ๋ฉด ์–ด๋–ค ๊ฐ’๋“ค์ด state๊ฐ€ ๋˜์–ด์•ผํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ ์‚ดํŽด๋ณด๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” state๋กœ ์ ์ ˆํ•œ์ง€ ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์„ธ์š”. ๊ฐ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์•„๋ž˜์˜ ์„ธ ๊ฐ€์ง€ ์งˆ๋ฌธ์„ ํ†ตํ•ด ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค > 1. ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ props๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋ฉ๋‹ˆ๊นŒ? ๊ทธ๋Ÿฌ๋ฉด ํ™•์‹คํžˆ state๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. > > 2. ์‹œ๊ฐ„์ด ์ง€๋‚˜๋„ ๋ณ€ํ•˜์ง€ ์•Š๋‚˜์š”? ๊ทธ๋Ÿฌ๋ฉด ํ™•์‹คํžˆ state๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. > > 3. ์ปดํฌ๋„ŒํŠธ ์•ˆ์˜ ๋‹ค๋ฅธ state๋‚˜ props๋ฅผ ๊ฐ€์ง€๊ณ  ๊ณ„์‚ฐ ๊ฐ€๋Šฅํ•œ๊ฐ€์š”? ๊ทธ๋ ‡๋‹ค๋ฉด state๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. >
@@ -1,5 +1,98 @@ import React from 'react'; +import { useState, useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './Login.scss'; -export default function Login() { - return <div>Hello, Hyeonze!</div>; -} +const Login = () => { + const [idInput, setIdInput] = useState(''); + const [pwInput, setPwInput] = useState(''); + const [isValidatedUser, setIsValidatedUser] = useState(false); + + const navigate = useNavigate(); + const changeIdInput = e => { + const { value } = e.target; + setIdInput(value); + }; + + const changePwInput = e => { + const { value } = e.target; + setPwInput(value); + }; + + useEffect(() => { + setIsValidatedUser(idInput.indexOf('@') !== -1 && pwInput.length > 4); + }, [idInput, pwInput]); + + // ๋ฉ”์ธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๋กœ์ง + const goToMain = () => { + navigate('/main-hyeonze'); + }; + + // ํšŒ์›๊ฐ€์ž…์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const signup = () => { + // fetch('http://10.58.4.22:8000/users/signup', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'EMAIL_ALREADY_EXISTS') { + // console.log('Error : Duplicated'); + // } + // }); + // }; + + // ๋กœ๊ทธ์ธ์‹œ ์‚ฌ์šฉํ•  ๋กœ์ง + // const login = () => { + // fetch('http://10.58.4.22:8000/users/login', { + // method: 'POST', + // body: JSON.stringify({ + // email: idInput, + // password: pwInput, + // }), + // }) + // .then(response => response.json()) + // .then(result => { + // //console.log('๊ฒฐ๊ณผ: ', result.message) + // if (result.message === 'SUCCESS') { + // console.log('login SUCCESS'); + // } + // }); + // }; + + return ( + <div className="login"> + <div id="wrapper"> + <h1>westagram</h1> + <form className="login_form"> + <input + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + className="username" + onChange={changeIdInput} + /> + <input + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + className="userpassword" + onChange={changePwInput} + /> + <input + className={isValidatedUser ? 'activated' : ''} + disabled={!isValidatedUser} + type="button" + value="๋กœ๊ทธ์ธ" + onClick={goToMain} + /> + </form> + <p>๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</p> + </div> + </div> + ); +}; + +export default Login;
Unknown
๋ผ์ด๋ธŒ๋ฆฌ๋ทฐ ๋•Œ ๋ง์”€๋“œ๋ฆฐ ๊ฒƒ์ฒ˜๋Ÿผ ์กฐ๊ฑด์˜ falsy, truthyํ•œ ๊ฐ’์„ ์ด์šฉํ•ด ๋ฆฌํŒฉํ† ๋ง ํ•ด์ฃผ์„ธ์š”! :) ๋˜๋Š” state๋ฅผ ์ค„์ผ ๊ฒฝ์šฐ ๋ถˆํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”
@@ -0,0 +1,66 @@ +@use '../../../styles/mixin.scss' as mixin; +@use '../../../styles/color.scss' as clr; + +body { + background: clr.$bg-login; + + .login { + position: relative; + top: -80px; + + #wrapper { + @include mixin.flex($align: center); + position: relative; + flex-direction: column; + width: 700px; + height: 761px; + border: 1px solid clr.$clr-border; + + h1 { + margin-top: 90px; + margin-bottom: 130px; + font-size: 81px; + font-family: 'Lobster', cursive; + } + + .login_form { + input { + display: block; + width: 536px; + height: 76px; + margin-bottom: 12px; + padding-left: 10px; + background: #fafafa; + border: 1px solid clr.$clr-border; + border-radius: 3px; + font-size: 16px; + } + + input[type='button'] { + display: flex; + align-items: center; + justify-content: center; + width: 536px; + height: 60px; + margin-top: 16px; + background-color: clr.$clr-primary; + opacity: 0.5; + border: none; + border-radius: 4px; + font-size: 16px; + color: #fff; + + &.activated { + opacity: 1; + } + } + } + + p { + position: absolute; + bottom: 20px; + color: #043667; + } + } + } +}
Unknown
์œ„ ์ฝ”๋“œ๋Š” ์–ด๋””์— ์ž‘์„ฑ๋˜๋ฉด ์ข‹์„๊นŒ์š”?
@@ -0,0 +1,43 @@ +span.comment { + padding-left: 10px; + + small { + color: #a3a3a3; + } + + &:first-child { + padding: 10px; + font-weight: bold; + } + + svg { + &.delete { + display: none; + cursor: pointer; + } + + &.hearts { + color: #a3a3a3; + position: absolute; + right: 20px; + transition: color 1s; + cursor: pointer; + + path { + color: #a3a3a3; + } + } + + &.liked { + path { + color: #f00; + } + } + } + + &:hover svg { + &.delete { + display: inline-block; + } + } +}
Unknown
์ž์ฃผ ์‚ฌ์šฉ์ด ๋˜๋Š” ์ปฌ๋Ÿฌ๊ฐ’์ด๋„ค์š”! ์ด๋Ÿฐ ๊ฒฝ์šฐ sass variables ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋Š”๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”?