code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,33 @@ +import letters from "../mockData"; + +const sleep = n => new Promise(resolve => setTimeout(resolve, n)); + +export const getLetters = async () => { + await sleep(500); + return letters; +}; + +export const getLetterById = async id => { + await sleep(100); + return letters.find(letter => letter.id === id); +}; + +export const updateLetter = async (id, payload) => { + await sleep(100); + const { title, artist, imageUrl } = payload; + const songStory = payload.songStory; + + const foundLetter = letters.find(letter => letter.id === id); + + const updatedLetter = { + ...foundLetter, + song: { title, artist, imageUrl }, + songStory + }; + + const index = letters.indexOf(foundLetter); + letters.splice(index, 1); + letters.push(updatedLetter); + + return updatedLetter; +};
JavaScript
๋ฐฐ์—ด์—์„œ ์›ํ•˜๋Š” ์กฐ๊ฑด๊นŒ์ง€๋งŒ ์ฐพ๋Š” find method๋ฅผ ์‚ฌ์šฉํ•œ ๊ฑด ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค !
@@ -0,0 +1,24 @@ +import React from "react"; +import styled from "styled-components"; + +const Footer = () => { + return ( + <FooterBlock> + <p className="copyright">&copy; Museop Kim</p> + </FooterBlock> + ); +}; + +const FooterBlock = styled.div` + display: flex; + justify-content: center; + align-items: center; + width: 100%; + height: 8rem; + margin-top: 6rem; + background-color: #f5f5f7; + font-weight: 500; + box-shadow: 0px -10px 35px 5px #f1f1f1; +`; + +export default Footer;
Unknown
style components๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋‹ˆ๊นŒ `box-shadow: 0px -10px 35px 5px #f1f1f1;` ๋ถ€๋ถ„์˜ ์ƒ‰์ƒ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” styled-components ThemeProvider๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”. ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ „๋‹ฌ๋ฐ›์€ ์ƒ‰์ƒ์„ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋‹คํฌ๋ชจ๋“œ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ• ๋•Œ๋„ ์†์‰ฝ๊ฒŒ ์Šคํƒ€์ผ ๋ณ€๋™์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋„ˆ๋ฌด ๋งŽ์ด ์ง„ํ–‰๋œ ํ”„๋กœ์ ํŠธ๋Š” ์Šคํƒ€์ผ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š์œผ๋‹ˆ๊นŒ ์ข€ ์ž‘์€ ๋‹จ์œ„์ผ ๋•Œ ์‹œ๋„ํ•ด ๋ณด๋Š”๊ฒƒ๋„ ์ข‹์„๊ฑฐ ๊ฐ™์•„์š” ๐Ÿ˜‰
@@ -0,0 +1,47 @@ +import React from "react"; +import styled from "styled-components"; +import { Link } from "react-router-dom"; + +const Navigation = () => { + return ( + <NavigationBlock> + <Link to="/">์‹ ์ฒญ๊ณก</Link> + <Link to="/ranking">์‹ ์ฒญ๊ณก ์ˆœ์œ„</Link> + <Link to="/contents">์‹ ์ฒญ๊ณก ์œ ํŠœ๋ธŒ ์˜์ƒ</Link> + <Link to="/musicsheets">๋ฆฌ์–ผํ”ผ์•„๋…ธ ์•…๋ณด์ง‘</Link> + </NavigationBlock> + ); +}; + +const NavigationBlock = styled.div` + height: 100%; + display: flex; + align-items: center; + + & > * { + /* padding: 25px 5px 10px 5px; */ + box-sizing: border-box; + display: flex; + align-items: center; + margin: 0px 30px; + padding-top: 10px; + font-size: 1.2rem; + font-weight: 600; + text-decoration: none; + align-self: center; + color: inherit; + opacity: 0.5; + height: 100%; + /* border-bottom: 1px solid rgba(0, 0, 0, 0); */ + border-bottom: 3px solid rgba(250, 162, 193, 0); + transition: 0.3s; + + :hover { + opacity: 1; + /* color: #faa2c1; */ + border-bottom: 3px solid rgba(250, 162, 193, 1); + } + } +`; + +export default Navigation;
Unknown
ํฐํŠธ์™€ ๊ฐ™์ด ๋ฐ˜์‘ํ˜•์— ์˜ํ–ฅ์„ ๋ฐ›์•„์•ผ ํ•˜๋Š” ๊ธฐ์ค€๊ณผ ๊ทธ๋ ‡์ง€ ์•Š์€ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ px๊ณผ rem์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฑด๊ฐ€์š” ? ๊ทธ๋Ÿฐ ์˜๋„๋ผ๋ฉด ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,147 @@ +import React from "react"; +import styled from "styled-components"; + +const MAX_LENGTH = 100; + +const Letter = ({ + id, + user, + song, + songStory, + createdDateTime, + onReadLetter +}) => { + const { username, avatarUrl } = user; + const { title, artist, imageUrl } = song; + + return ( + <> + <LetterBlock onClick={() => onReadLetter(id)}> + <SongBlock> + <img src={imageUrl} alt="ALBUM IMAGE" className="album-image" /> + <div className="song-about"> + <span className="song-about__title">{title}</span> + <span className="song-about__artist">{artist}</span> + </div> + </SongBlock> + <SongStory> + {songStory.length > MAX_LENGTH + ? `${songStory.slice(0, MAX_LENGTH)} ...` + : songStory} + </SongStory> + <UserBlock> + <div className="created-time">{createdDateTime}</div> + <div className="user-about"> + <img src={avatarUrl} className="user-about__avatar" /> + <span className="user-about__name">{username}</span> + </div> + </UserBlock> + </LetterBlock> + </> + ); +}; + +const LetterBlock = styled.li` + width: 25rem; + height: 23rem; + margin: 1.2rem; + margin-bottom: 5rem; + box-sizing: border-box; + display: flex; + flex-direction: column; + padding: 0.7rem; + border: #ffdeeb; + border-radius: 0.5rem; + cursor: pointer; + transition: 0.3s; + box-shadow: 0 13px 27px -5px rgba(50, 50, 93, 0.2), + 0 8px 16px -8px rgba(0, 0, 0, 0.2), 0 -6px 16px -6px rgba(0, 0, 0, 0.025); + + &:hover { + box-shadow: 0px 25px 30px 3px rgba(0, 0, 0, 0.3); + } + + .album-image { + width: 10rem; + height: 10rem; + } +`; + +const SongBlock = styled.div` + display: flex; + justify-content: space-between; + + .album-image { + position: relative; + top: -2.5rem; + box-shadow: 3px 2px 5px 1px rgba(0, 0, 0, 0.3); + border-radius: 5px; + } + + .song-about { + display: flex; + flex-direction: column; + justify-content: center; + margin-right: auto; + margin-left: 2rem; + + .song-about__title { + overflow-x: hidden; + font-size: 1.4rem; + font-weight: 500; + color: #2c2c2c; + opacity: 0.9; + } + + .song-about__artist { + margin-top: 0.5rem; + font-size: 1.2rem; + opacity: 0.8; + } + } +`; + +const SongStory = styled.p` + width: 100%; + max-height: 8rem; + line-break: anywhere; + margin-top: -0.3rem; + font-size: 1.2rem; + line-height: 1.9rem; + opacity: 0.9; + padding: 0rem 0.5rem; +`; + +const UserBlock = styled.div` + display: flex; + justify-content: space-between; + align-items: center; + margin: auto 5px 3px 5px; + + .created-time { + font-size: 1.1rem; + margin-top: 0.8rem; + opacity: 0.7; + } + + .user-about { + display: flex; + align-items: center; + } + + .user-about__avatar { + max-width: 2.5rem; + max-height: 2.5rem; + border-radius: 50%; + box-shadow: 3px 2px 10px 1px rgba(0, 0, 0, 0.3); + } + + .user-about__name { + font-size: 1.1rem; + margin-top: 0.5rem; + margin-left: 0.5rem; + opacity: 0.8; + } +`; + +export default Letter;
Unknown
className ์ปจ๋ฒค์…˜ ์ค‘ "-"์™€ "_"๋Š” ์–ด๋–ค ๊ธฐ์ค€์ด ์žˆ์„๊นŒ์š” ? ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค ใ…Ž
@@ -0,0 +1,29 @@ +import React from "react"; +import styled from "styled-components"; +import LetterContainer from "../../containers/Letter/LetterContainer"; + +const LetterList = ({ letters }) => { + return ( + <LetterListBlock> + {letters.map(letter => ( + <LetterContainer + key={letter.id} + id={letter.id} + user={letter.user} + song={letter.song} + songStory={letter.songStory} + createdDateTime={letter.createdDateTime} + /> + ))} + </LetterListBlock> + ); +}; + +const LetterListBlock = styled.ul` + width: 100%; + display: flex; + flex-wrap: wrap; + margin-top: 4.5rem; +`; + +export default LetterList;
Unknown
๋ณดํ†ต Container๋Š” ์ข€ ๋” ํฐ ๋‹จ์œ„์—์„œ์˜ ์˜๋ฏธ์—์„œ ์‚ฌ์šฉ๋˜๊ณค ํ•˜๋Š”๊ฑฐ ๊ฐ™์•„์š”. ์—ฌ๋Ÿฌ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ๋งŽ์€ ์ด๋ฒคํŠธ ๋กœ์ง์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„ผํ„ฐ์˜ ๋А๋‚Œ์œผ๋กœ ๋„ค์ด๋ฐ์„ ํ•˜๊ณค ํ•˜๋Š”๋ฐ LetterList๋ฉด LetterItem๊ณผ ๊ฐ™์ด ๋‹จ์ผํ•˜๊ฒŒ ํ‘œํ˜„ํ•ด ์ฃผ๋Š”๊ฒƒ๋„ ์ข‹์„๊ฑฐ ๊ฐ™์•„์š” !
@@ -0,0 +1,30 @@ +import React, { useEffect, useState } from "react"; +import ModalTemplate from "../Template/Modal/ModalTemplate"; +import LetterDetailsSong from "./LetterDetailsSong"; +import LetterDetailsSongStory from "./LetterDetailsSongStory"; +import LetterModalTemplate from "../Template/LetterModal/LetterModalTemplate"; +import LetterDetailsUser from "./LetterDetailsUser"; +import LetterModalDiv from "../LetterModal/LetterModalContents/LetterModalDiv"; +import LetterModalHiddenButtonContainer from "../../containers/LetterModal/LetterModalHiddenButtonContainer"; +import LetterModalButtonContainer from "../../containers/LetterModal/LetterModalButtonContainer"; +import { useSelector } from "react-redux"; + +const LetterDetails = ({ letter }) => { + const { song, songStory, createdDateTime, user } = letter; + + return ( + <ModalTemplate> + <LetterModalTemplate> + <LetterModalHiddenButtonContainer /> + <LetterModalDiv> + <LetterDetailsSong song={song} /> + <LetterDetailsSongStory songStory={songStory} /> + <LetterDetailsUser user={user} createdDateTime={createdDateTime} /> + <LetterModalButtonContainer /> + </LetterModalDiv> + </LetterModalTemplate> + </ModalTemplate> + ); +}; + +export default LetterDetails;
Unknown
`const { song, songStory, createdDateTime, user } = letter;` ๋„ˆ๋ฌด ๋งŽ์€ ์–‘์˜ ๊ฐ์ฒด Props๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ด ๋ถ€๋ถ„ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๊ฐ๊ฐ ์ด๋ฆ„์„ ๊ฐ€์ง„ Props๋กœ ์ „๋‹ฌํ•ด ์ฃผ๋Š”๊ฒƒ๋„ ์ข‹์„๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ปดํฌ๋„ŒํŠธ์— ์ง์ ‘ ๋“ค์–ด์™€ ๋ณด์ง€ ์•Š์•„๋„ ์ „๋‹ฌ๋˜๋Š” Props ๋งŒ์œผ๋กœ ์—ญํ• ์„ ์œ ์ถ”ํ•ด ๋ณผ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์•„์š”.
@@ -0,0 +1,45 @@ +import React, { useEffect } from "react"; +import { MdMoreHoriz } from "react-icons/md"; +import styled, { css } from "styled-components"; +import LetterDetailsHiddenMenuButton from "./LetterDetailsHiddenMenuButton"; + +const LetterDetailsHiddenMenu = ({ + isMouseEnter, + isMenuOpen, + onToggle, + changeToEdit +}) => { + return ( + <ButtonBlock isMouseEnter={isMouseEnter} onClick={onToggle}> + <HiddenMenu /> + {isMenuOpen && ( + <LetterDetailsHiddenMenuButton changeToEdit={changeToEdit} /> + )} + </ButtonBlock> + ); +}; + +const ButtonBlock = styled.div` + ${props => + props.isMouseEnter + ? css` + visibility: visible; + opacity: 1; + ` + : css` + visibility: hidden; + opacity: 0; + `} + transition: 0.7s; +`; + +const HiddenMenu = styled(MdMoreHoriz)` + position: absolute; + bottom: 93%; + right: 3%; + cursor: pointer; + font-size: 2.1rem; + color: gray; +`; + +export default LetterDetailsHiddenMenu;
Unknown
visibility: hidden; , display : none ์˜ ์ฐจ์ด๋ฅผ ์•„์‹œ๋‚˜์š” ? ใ…Žใ…Ž ๋ชจ๋ฅด๊ณ  ๊ณ„์‹ ๋‹ค๋ฉด ํ•œ๋ฒˆ ์•Œ์•„๋ณด๋Š”๊ฒƒ๋„ ์ข‹์Šต๋‹ˆ๋‹ค ! ๋Œ€๋‹จํ•œ๊ฑด ์•„๋‹ˆ๊ณ  ๊ทธ๋ƒฅ DOM ํŠธ๋ฆฌ์™€ ๊ด€๋ จ๋œ ๋‚ด์šฉ์ด์—์š”.
@@ -0,0 +1,28 @@ +import React from "react"; +import LetterModalSong from "../LetterModal/LetterModalSong/LetterModalSong"; +import SongAbout from "../LetterModal/LetterModalSong/SongAbout"; +import SongArticle from "../LetterModal/LetterModalSong/SongArticle"; +import SongArticleItem from "../LetterModal/LetterModalSong/SongArticleItem"; +import SongArticleName from "../LetterModal/LetterModalSong/SongArticleName"; +import SongImage from "../LetterModal/LetterModalSong/SongImage"; + +const LetterDetailsSong = ({ song }) => { + const { title, artist, imageUrl } = song; + return ( + <LetterModalSong> + <SongImage imageUrl={imageUrl} /> + <SongAbout> + <SongArticle> + <SongArticleName articleName={"TITLE"} /> + <SongArticleItem articleName={"TITLE"} item={title} /> + </SongArticle> + <SongArticle> + <SongArticleName articleName={"ARTIST"} /> + <SongArticleItem articleName={"ARTIST"} item={artist} /> + </SongArticle> + </SongAbout> + </LetterModalSong> + ); +}; + +export default LetterDetailsSong;
Unknown
์ด ๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋ฐ˜๋ณต๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๋กœ ๋ณด์ด๋Š”๋ฐ ์ด๊ฑธ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•ด ๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š” ? <SongArticle> <SongArticleName articleName={"TITLE"} /> <SongArticleItem articleName={"TITLE"} item={title} /> </SongArticle>
@@ -0,0 +1,48 @@ +import React from "react"; +import styled from "styled-components"; +import { BiSearch } from "react-icons/bi"; +import useModal from "../../hooks/useModal"; +import SongSearchModal from "./SongSearchModal/SongSearchModal"; + +const LetterEditorSearchButton = () => { + const [isOpened, onOpenModal, onCloseModal] = useModal(); + + return ( + <> + <StyledButton onClick={onOpenModal}> + <SearchIcon /> + ์‹ ์ฒญ๊ณก ๊ฒ€์ƒ‰ + </StyledButton> + <SongSearchModal isOpened={isOpened} onCloseModal={onCloseModal} /> + </> + ); +}; + +const StyledButton = styled.div` + display: flex; + justify-content: center; + align-items: center; + position: absolute; + width: 11rem; + top: 14%; + right: 15%; + + padding: 0.35rem 0rem; + border-style: none; + border-radius: 0.5rem; + font-size: 1.2rem; + background-color: #f06595; + box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.15); + color: #fff; + font-weight: 500; + z-index: 1; + cursor: pointer; +`; + +const SearchIcon = styled(BiSearch)` + font-size: 1.35rem; + margin-right: 0.35rem; + opacity: 0.9; +`; + +export default LetterEditorSearchButton;
Unknown
modal๊ณผ ๊ด€๋ จ๋œ ๋™์ž‘์„ custom hook์œผ๋กœ ์‚ฌ์šฉํ•œ ๋ถ€๋ถ„ ์ข‹์•„ ๋ณด์ž…๋‹ˆ๋‹ค !
@@ -0,0 +1,108 @@ +import React from "react"; +import styled from "styled-components"; +import { BiSearch } from "react-icons/bi"; +import { useDispatch } from "react-redux"; +import { searchSong } from "../../../modules/song"; + +const SearchForm = ({ onCloseModal }) => { + const dispatch = useDispatch(); + + const onSearchSong = event => { + event.preventDefault(); + dispatch(searchSong()); + }; + + return ( + <Form> + <SearchInputWrap> + <SearchLabel>์•„ํ‹ฐ์ŠคํŠธ</SearchLabel> + <SearchInput type="text" name="artist" /> + </SearchInputWrap> + <SearchInputWrap> + <SearchLabel>์ œ๋ชฉ</SearchLabel> + <SearchInput type="text" name="title" /> + </SearchInputWrap> + <SearchButton type="submit" onClick={onSearchSong}> + <SearchIcon /> + </SearchButton> + </Form> + ); +}; + +const Form = styled.form` + box-sizing: border-box; + width: 95%; + display: flex; + justify-content: center; + margin: 0rem auto 3.5rem auto; +`; + +const SearchLabel = styled.span` + display: flex; + align-items: center; + position: absolute; + top: 0rem; + left: 0rem; + height: 2.37rem; + font-size: 1rem; + background-color: #727272; + padding: 0rem 0.7rem; + border-radius: 0.3rem 0rem 0rem 0.3rem; + color: white; + font-weight: 600; + box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1); +`; + +const SearchInput = styled.input` + border: none; + outline: none; + color: #868e96; + width: 100%; + margin: 0rem 0.5rem 0rem 5.5rem; + font-size: 1.2rem; + background-color: #fbfbfd; +`; + +const SearchInputWrap = styled.div` + display: flex; + align-items: center; + position: relative; + border: 1px solid #e9ecef; + border-radius: 1rem 0rem 0rem 1rem; + padding: 0.1rem 0rem; + background: #fbfbfd; + height: 2.1rem; + flex: 11; + + &:nth-child(2) { + & > ${SearchLabel} { + border-radius: 0.2rem; + margin-left: -0.25rem; + } + + & > ${SearchInput} { + margin-left: 3.8rem; + } + } +`; + +const SearchButton = styled.button` + display: flex; + position: relative; + justify-content: center; + align-items: center; + border: none; + outline: none; + cursor: pointer; + background-color: #e1999c; + border-radius: 0rem 0.5rem 0.5rem 0rem; + flex: 1; + box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1); +`; + +const SearchIcon = styled(BiSearch)` + color: #fff; + font-size: 1.4rem; +`; + +export default SearchForm;
Unknown
Form์ด ์ ์  ๋” ๋ณต์žกํ•œ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๋ฉด ๊ด€๋ฆฌ๊ฐ€ ์‰ฝ์ง€ ์•Š์€๋ฐ ๊ทธ๋Ÿด๋• https://react-hook-form.com/ ์œ„์™€ ๊ฐ™์€ ๋„๊ตฌ๋ฅผ ๋„์ž…ํ•ด ๋ณผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค ใ…Ž
@@ -0,0 +1,24 @@ +import React from "react"; +import styled from "styled-components"; + +const Footer = () => { + return ( + <FooterBlock> + <p className="copyright">&copy; Museop Kim</p> + </FooterBlock> + ); +}; + +const FooterBlock = styled.div` + display: flex; + justify-content: center; + align-items: center; + width: 100%; + height: 8rem; + margin-top: 6rem; + background-color: #f5f5f7; + font-weight: 500; + box-shadow: 0px -10px 35px 5px #f1f1f1; +`; + +export default Footer;
Unknown
์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉ ํ•˜๋“ฏ์ด ์Šคํƒ€์ผ๋„ ์—ฌ๋Ÿฌ ๊ณณ์— ์žฌ์‚ฌ์šฉ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋„ค์š”. `ThemeProvider`์— ๋Œ€ํ•ด ์ž˜ ๋ชฐ๋ž๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ์— ํ•œ๋ฒˆ ํ•™์Šต ํ•ด๋ณด๊ณ  ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ ์šฉ๊นŒ์ง€ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ์กฐ์–ธ ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,47 @@ +import React from "react"; +import styled from "styled-components"; +import { Link } from "react-router-dom"; + +const Navigation = () => { + return ( + <NavigationBlock> + <Link to="/">์‹ ์ฒญ๊ณก</Link> + <Link to="/ranking">์‹ ์ฒญ๊ณก ์ˆœ์œ„</Link> + <Link to="/contents">์‹ ์ฒญ๊ณก ์œ ํŠœ๋ธŒ ์˜์ƒ</Link> + <Link to="/musicsheets">๋ฆฌ์–ผํ”ผ์•„๋…ธ ์•…๋ณด์ง‘</Link> + </NavigationBlock> + ); +}; + +const NavigationBlock = styled.div` + height: 100%; + display: flex; + align-items: center; + + & > * { + /* padding: 25px 5px 10px 5px; */ + box-sizing: border-box; + display: flex; + align-items: center; + margin: 0px 30px; + padding-top: 10px; + font-size: 1.2rem; + font-weight: 600; + text-decoration: none; + align-self: center; + color: inherit; + opacity: 0.5; + height: 100%; + /* border-bottom: 1px solid rgba(0, 0, 0, 0); */ + border-bottom: 3px solid rgba(250, 162, 193, 0); + transition: 0.3s; + + :hover { + opacity: 1; + /* color: #faa2c1; */ + border-bottom: 3px solid rgba(250, 162, 193, 1); + } + } +`; + +export default Navigation;
Unknown
์ฒ˜์Œ์—๋Š” ๋ง์”€ ํ•˜์‹  ๊ฒƒ์ฒ˜๋Ÿผ px๊ณผ rem์„ ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉ ํ•˜๋‹ค๊ฐ€ ์ƒ๋Œ€ ๊ฐ’์ธ rem์œผ๋กœ ๋‹จ์œ„๋ฅผ ํ†ต์ผํ•˜๊ณ  ์ ์šฉ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ์ด๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ ์ฃผ์˜ํ•ด์•ผ ๋  ๊ณ ๋ ค์‚ฌํ•ญ ๊ฐ™์€๊ฒŒ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,147 @@ +import React from "react"; +import styled from "styled-components"; + +const MAX_LENGTH = 100; + +const Letter = ({ + id, + user, + song, + songStory, + createdDateTime, + onReadLetter +}) => { + const { username, avatarUrl } = user; + const { title, artist, imageUrl } = song; + + return ( + <> + <LetterBlock onClick={() => onReadLetter(id)}> + <SongBlock> + <img src={imageUrl} alt="ALBUM IMAGE" className="album-image" /> + <div className="song-about"> + <span className="song-about__title">{title}</span> + <span className="song-about__artist">{artist}</span> + </div> + </SongBlock> + <SongStory> + {songStory.length > MAX_LENGTH + ? `${songStory.slice(0, MAX_LENGTH)} ...` + : songStory} + </SongStory> + <UserBlock> + <div className="created-time">{createdDateTime}</div> + <div className="user-about"> + <img src={avatarUrl} className="user-about__avatar" /> + <span className="user-about__name">{username}</span> + </div> + </UserBlock> + </LetterBlock> + </> + ); +}; + +const LetterBlock = styled.li` + width: 25rem; + height: 23rem; + margin: 1.2rem; + margin-bottom: 5rem; + box-sizing: border-box; + display: flex; + flex-direction: column; + padding: 0.7rem; + border: #ffdeeb; + border-radius: 0.5rem; + cursor: pointer; + transition: 0.3s; + box-shadow: 0 13px 27px -5px rgba(50, 50, 93, 0.2), + 0 8px 16px -8px rgba(0, 0, 0, 0.2), 0 -6px 16px -6px rgba(0, 0, 0, 0.025); + + &:hover { + box-shadow: 0px 25px 30px 3px rgba(0, 0, 0, 0.3); + } + + .album-image { + width: 10rem; + height: 10rem; + } +`; + +const SongBlock = styled.div` + display: flex; + justify-content: space-between; + + .album-image { + position: relative; + top: -2.5rem; + box-shadow: 3px 2px 5px 1px rgba(0, 0, 0, 0.3); + border-radius: 5px; + } + + .song-about { + display: flex; + flex-direction: column; + justify-content: center; + margin-right: auto; + margin-left: 2rem; + + .song-about__title { + overflow-x: hidden; + font-size: 1.4rem; + font-weight: 500; + color: #2c2c2c; + opacity: 0.9; + } + + .song-about__artist { + margin-top: 0.5rem; + font-size: 1.2rem; + opacity: 0.8; + } + } +`; + +const SongStory = styled.p` + width: 100%; + max-height: 8rem; + line-break: anywhere; + margin-top: -0.3rem; + font-size: 1.2rem; + line-height: 1.9rem; + opacity: 0.9; + padding: 0rem 0.5rem; +`; + +const UserBlock = styled.div` + display: flex; + justify-content: space-between; + align-items: center; + margin: auto 5px 3px 5px; + + .created-time { + font-size: 1.1rem; + margin-top: 0.8rem; + opacity: 0.7; + } + + .user-about { + display: flex; + align-items: center; + } + + .user-about__avatar { + max-width: 2.5rem; + max-height: 2.5rem; + border-radius: 50%; + box-shadow: 3px 2px 10px 1px rgba(0, 0, 0, 0.3); + } + + .user-about__name { + font-size: 1.1rem; + margin-top: 0.5rem; + margin-left: 0.5rem; + opacity: 0.8; + } +`; + +export default Letter;
Unknown
์ปดํฌ๋„ŒํŠธ ์ด์™ธ์— className์„ ์ง์ ‘ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋Œ€๋ถ€๋ถ„ CSS ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์œผ๋กœ ์•Œ๋ ค์ง„ `BEM` ์„ ์ ์šฉ ํ–ˆ์Šต๋‹ˆ๋‹คใ…Žใ…Ž `-`๋Š” ๋‘ ๋‹จ์–ด๋ฅผ ์—ฐ๊ฒฐํ•ด์•ผ ํ•  ๋•Œ, `__`๋Š” ํŠน์ • ๋ธ”๋Ÿญ์„ ๊ตฌ์„ฑํ•˜๋Š” ํ•˜์œ„ ์š”์†Œ๋ฅผ ๊ตฌ๋ถ„ํ•  ๋•Œ ์‚ฌ์šฉ ํ–ˆ๋˜ ๋“ฏ ํ•˜๋„ค์š”. [CSS BEM](https://css-tricks.com/bem-101/) ์„ ์ฐธ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,29 @@ +import React from "react"; +import styled from "styled-components"; +import LetterContainer from "../../containers/Letter/LetterContainer"; + +const LetterList = ({ letters }) => { + return ( + <LetterListBlock> + {letters.map(letter => ( + <LetterContainer + key={letter.id} + id={letter.id} + user={letter.user} + song={letter.song} + songStory={letter.songStory} + createdDateTime={letter.createdDateTime} + /> + ))} + </LetterListBlock> + ); +}; + +const LetterListBlock = styled.ul` + width: 100%; + display: flex; + flex-wrap: wrap; + margin-top: 4.5rem; +`; + +export default LetterList;
Unknown
Container๋ฅผ ์–ธ์ œ ์‚ฌ์šฉํ•ด์•ผ ํ•  ์ง€ ๋ช…ํ™•ํ•œ ๊ธฐ์ค€์ด ์—†์—ˆ๋Š”๋ฐ, ๋ฆฌ๋ทฐ ํ•ด์ฃผ์‹  ๋‚ด์šฉ์„ ๋ณด๊ณ  ๊ณ ๋ฏผ ๋˜๋˜ ๊ฒƒ๋“ค์ด ๋งŽ์ด ๋œ์–ด์ง€๋Š” ๋А๋‚Œ์ด์—์š”. ๋‹ค์Œ ์ˆ˜์ • ๋•Œ ๋ฐ˜์˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ™‚
@@ -0,0 +1,30 @@ +import React, { useEffect, useState } from "react"; +import ModalTemplate from "../Template/Modal/ModalTemplate"; +import LetterDetailsSong from "./LetterDetailsSong"; +import LetterDetailsSongStory from "./LetterDetailsSongStory"; +import LetterModalTemplate from "../Template/LetterModal/LetterModalTemplate"; +import LetterDetailsUser from "./LetterDetailsUser"; +import LetterModalDiv from "../LetterModal/LetterModalContents/LetterModalDiv"; +import LetterModalHiddenButtonContainer from "../../containers/LetterModal/LetterModalHiddenButtonContainer"; +import LetterModalButtonContainer from "../../containers/LetterModal/LetterModalButtonContainer"; +import { useSelector } from "react-redux"; + +const LetterDetails = ({ letter }) => { + const { song, songStory, createdDateTime, user } = letter; + + return ( + <ModalTemplate> + <LetterModalTemplate> + <LetterModalHiddenButtonContainer /> + <LetterModalDiv> + <LetterDetailsSong song={song} /> + <LetterDetailsSongStory songStory={songStory} /> + <LetterDetailsUser user={user} createdDateTime={createdDateTime} /> + <LetterModalButtonContainer /> + </LetterModalDiv> + </LetterModalTemplate> + </ModalTemplate> + ); +}; + +export default LetterDetails;
Unknown
๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ์—ฌ๋Ÿฌ ์ธก๋ฉด์—์„œ ๋” ์ข‹์„๊ฑฐ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด์—ˆ๋Š”๋ฐ, ์•„๋‹Œ ๊ฒฝ์šฐ๋„ ์žˆ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š”! ์ˆ˜์ •ํ•  ๋•Œ ๋ฐ˜์˜ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,45 @@ +import React, { useEffect } from "react"; +import { MdMoreHoriz } from "react-icons/md"; +import styled, { css } from "styled-components"; +import LetterDetailsHiddenMenuButton from "./LetterDetailsHiddenMenuButton"; + +const LetterDetailsHiddenMenu = ({ + isMouseEnter, + isMenuOpen, + onToggle, + changeToEdit +}) => { + return ( + <ButtonBlock isMouseEnter={isMouseEnter} onClick={onToggle}> + <HiddenMenu /> + {isMenuOpen && ( + <LetterDetailsHiddenMenuButton changeToEdit={changeToEdit} /> + )} + </ButtonBlock> + ); +}; + +const ButtonBlock = styled.div` + ${props => + props.isMouseEnter + ? css` + visibility: visible; + opacity: 1; + ` + : css` + visibility: hidden; + opacity: 0; + `} + transition: 0.7s; +`; + +const HiddenMenu = styled(MdMoreHoriz)` + position: absolute; + bottom: 93%; + right: 3%; + cursor: pointer; + font-size: 2.1rem; + color: gray; +`; + +export default LetterDetailsHiddenMenu;
Unknown
`display: none`์€ DOM์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š” ๋ฐ˜๋ฉด `visibility: hidden` ์€ DOM์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ์•Œ๊ณ  ์žˆ์—ˆ์–ด์š”ใ…Žใ…Ž `transition` ์€ `display: none`์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š์•„์„œ `visibility` ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋๋Š”๋ฐ, ํ˜น์‹œ ์ปจ๋ฒค์…˜์ด ๋”ฐ๋กœ ์žˆ๋‹ค๊ฑฐ๋‚˜ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,28 @@ +import React from "react"; +import LetterModalSong from "../LetterModal/LetterModalSong/LetterModalSong"; +import SongAbout from "../LetterModal/LetterModalSong/SongAbout"; +import SongArticle from "../LetterModal/LetterModalSong/SongArticle"; +import SongArticleItem from "../LetterModal/LetterModalSong/SongArticleItem"; +import SongArticleName from "../LetterModal/LetterModalSong/SongArticleName"; +import SongImage from "../LetterModal/LetterModalSong/SongImage"; + +const LetterDetailsSong = ({ song }) => { + const { title, artist, imageUrl } = song; + return ( + <LetterModalSong> + <SongImage imageUrl={imageUrl} /> + <SongAbout> + <SongArticle> + <SongArticleName articleName={"TITLE"} /> + <SongArticleItem articleName={"TITLE"} item={title} /> + </SongArticle> + <SongArticle> + <SongArticleName articleName={"ARTIST"} /> + <SongArticleItem articleName={"ARTIST"} item={artist} /> + </SongArticle> + </SongAbout> + </LetterModalSong> + ); +}; + +export default LetterDetailsSong;
Unknown
์ปดํฌ๋„ŒํŠธ๋ฅผ ์™„์ „ํžˆ ๋‹ค ๋ถ„๋ฆฌ ํ•œ ๋’ค์— ํ•œ๋ฒˆ ๋” ์ •๋ฆฌ๋ฅผ ํ–ˆ์–ด์•ผ ํ•˜๋Š”๋ฐ ์ด๋Ÿฐ ์ค‘๋ณต์€ ๋ฌถ์–ด์„œ ์ค„์ด๋Š”๊ฒŒ ์ข‹์„๊ฑฐ ๊ฐ™์•„์š”. ๋ฐ˜์˜ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,108 @@ +import React from "react"; +import styled from "styled-components"; +import { BiSearch } from "react-icons/bi"; +import { useDispatch } from "react-redux"; +import { searchSong } from "../../../modules/song"; + +const SearchForm = ({ onCloseModal }) => { + const dispatch = useDispatch(); + + const onSearchSong = event => { + event.preventDefault(); + dispatch(searchSong()); + }; + + return ( + <Form> + <SearchInputWrap> + <SearchLabel>์•„ํ‹ฐ์ŠคํŠธ</SearchLabel> + <SearchInput type="text" name="artist" /> + </SearchInputWrap> + <SearchInputWrap> + <SearchLabel>์ œ๋ชฉ</SearchLabel> + <SearchInput type="text" name="title" /> + </SearchInputWrap> + <SearchButton type="submit" onClick={onSearchSong}> + <SearchIcon /> + </SearchButton> + </Form> + ); +}; + +const Form = styled.form` + box-sizing: border-box; + width: 95%; + display: flex; + justify-content: center; + margin: 0rem auto 3.5rem auto; +`; + +const SearchLabel = styled.span` + display: flex; + align-items: center; + position: absolute; + top: 0rem; + left: 0rem; + height: 2.37rem; + font-size: 1rem; + background-color: #727272; + padding: 0rem 0.7rem; + border-radius: 0.3rem 0rem 0rem 0.3rem; + color: white; + font-weight: 600; + box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1); +`; + +const SearchInput = styled.input` + border: none; + outline: none; + color: #868e96; + width: 100%; + margin: 0rem 0.5rem 0rem 5.5rem; + font-size: 1.2rem; + background-color: #fbfbfd; +`; + +const SearchInputWrap = styled.div` + display: flex; + align-items: center; + position: relative; + border: 1px solid #e9ecef; + border-radius: 1rem 0rem 0rem 1rem; + padding: 0.1rem 0rem; + background: #fbfbfd; + height: 2.1rem; + flex: 11; + + &:nth-child(2) { + & > ${SearchLabel} { + border-radius: 0.2rem; + margin-left: -0.25rem; + } + + & > ${SearchInput} { + margin-left: 3.8rem; + } + } +`; + +const SearchButton = styled.button` + display: flex; + position: relative; + justify-content: center; + align-items: center; + border: none; + outline: none; + cursor: pointer; + background-color: #e1999c; + border-radius: 0rem 0.5rem 0.5rem 0rem; + flex: 1; + box-shadow: 1px 0px 3px 1px rgba(0, 0, 0, 0.1); +`; + +const SearchIcon = styled(BiSearch)` + color: #fff; + font-size: 1.4rem; +`; + +export default SearchForm;
Unknown
์ง€๊ธˆ ์ •๋„์˜ ํผ ์‚ฌ์šฉ์—์„œ๋Š” ์ง์ ‘ ๊ตฌํ˜„๋„ ๋ฌด๋ฆฌ๊ฐ€ ์—†์—ˆ์ง€๋งŒ, ํผ์ด ๋ณต์žกํ•ด์ง€๋ฉด ํ™•์‹คํžˆ ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ์‰ฝ์ง€ ์•Š์„ ๋“ฏ ํ•˜๋”๋ผ๊ตฌ์š”. ๊ธฐ์–ต ํ•ด๋‘์—ˆ๋‹ค๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์ ์šฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค :) ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -1,9 +1,100 @@ -function Header(logo, searchBox) { +import Input from './Input'; +import Button from './Button'; +import { handleSearch } from '../services/handleSeach'; + +function Header({ logoSrc, logoAlt, onSearch }) { const headerElement = document.createElement('header'); - headerElement.classList.add('header'); - headerElement.appendChild(logo); - headerElement.appendChild(searchBox); + headerElement.innerHTML = ` + <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1> + <div class="search-box"></div> + `; + + let page = 1; + + const searchInput = Input({ + id: 'search-input', + type: 'text', + placeholder: '๊ฒ€์ƒ‰', + onKeyDown: async (event) => { + if (event.code === 'Enter') { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + await onSearch(event.target.value, page); + } + } + }); + + const searchButton = Button({ + classNames: ['search-button'], + name: '๊ฒ€์ƒ‰', + type: 'button', + onClick: async () => { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + const inputValue = document.getElementById('search-input').value; + await onSearch(inputValue, page); + } + }); + + const searchBox = headerElement.querySelector('.search-box'); + + searchBox.addEventListener('mouseenter', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.add('search-box-toggle'); + input.classList.add('input-toggle'); + logo.classList.add('logo-toggle'); + } + }); + + searchBox.addEventListener('mouseleave', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.remove('search-box-toggle'); + input.classList.remove('input-toggle'); + logo.classList.remove('logo-toggle'); + } + }); + + searchBox.appendChild(searchInput); + searchBox.appendChild(searchButton); return headerElement; }
JavaScript
์ €๋Š” ์ด๋ฒคํŠธ์˜ ์ด๋ฆ„์ด ์˜๋ฏธํ•˜๋Š”๊ฒŒ "ํ–‰์œ„์˜ ๋ฐœ์ƒ" ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”. ๊ฐ€๋ น, `onChange` ๋Š” ์‹ค์ œ๋กœ change๊ฐ€ ์ผ์–ด๋‚ฌ์„ ๋•Œ์ด๊ณ , `onKeyDown` ์€ ํ‚ค๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ ์ž…๋‹ˆ๋‹ค. `onSearch`๋Š” "๊ฒ€์ƒ‰์ด ๋˜์—ˆ์„ ๋•Œ ๋ฐœ์ƒ" ์ธ๋ฐ, ์ด ์ปดํฌ๋„ŒํŠธ์—์„œ ์‹ค์ œ๋กœ "๊ฒ€์ƒ‰"์ด๋ผ๋Š” ํ–‰์œ„๊ฐ€ ์ผ์–ด๋‚ฌ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ง€๊ธˆ์€ "๊ฒ€์ƒ‰"์ด๋ผ๋Š” ํ–‰์œ„๋ฅผ ๋ฐ–์—์„œ ์ฃผ์ž…ํ•ด์ฃผ๊ณ , ์ด ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ์ผ์–ด๋‚œ ์ผ์€ "์—”ํ„ฐํ‚ค๋ฅผ ๋ˆŒ๋ €์Œ" ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ด๋ฒคํŠธ๋Š” "๊ฒ€์ƒ‰ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒํ•จ"์„ ์ด์•ผ๊ธฐํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๊ฒ€์ƒ‰์ด๋ผ๋Š” ๊ธฐ๋Šฅ์€ ๋ฐ–์—์„œ ์ฃผ์ž…ํ•ด์ค„ ์ˆ˜ ๋ฐ–์— ์—†๋Š” ์ƒํ™ฉ์ธ๊ฑฐ์ฃ .
@@ -1,9 +1,100 @@ -function Header(logo, searchBox) { +import Input from './Input'; +import Button from './Button'; +import { handleSearch } from '../services/handleSeach'; + +function Header({ logoSrc, logoAlt, onSearch }) { const headerElement = document.createElement('header'); - headerElement.classList.add('header'); - headerElement.appendChild(logo); - headerElement.appendChild(searchBox); + headerElement.innerHTML = ` + <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1> + <div class="search-box"></div> + `; + + let page = 1; + + const searchInput = Input({ + id: 'search-input', + type: 'text', + placeholder: '๊ฒ€์ƒ‰', + onKeyDown: async (event) => { + if (event.code === 'Enter') { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + await onSearch(event.target.value, page); + } + } + }); + + const searchButton = Button({ + classNames: ['search-button'], + name: '๊ฒ€์ƒ‰', + type: 'button', + onClick: async () => { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + const inputValue = document.getElementById('search-input').value; + await onSearch(inputValue, page); + } + }); + + const searchBox = headerElement.querySelector('.search-box'); + + searchBox.addEventListener('mouseenter', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.add('search-box-toggle'); + input.classList.add('input-toggle'); + logo.classList.add('logo-toggle'); + } + }); + + searchBox.addEventListener('mouseleave', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.remove('search-box-toggle'); + input.classList.remove('input-toggle'); + logo.classList.remove('logo-toggle'); + } + }); + + searchBox.appendChild(searchInput); + searchBox.appendChild(searchButton); return headerElement; }
JavaScript
์ด ๋ถ€๋ถ„๋„ ๋™์ผํ•ฉ๋‹ˆ๋‹ค!
@@ -1,9 +1,100 @@ -function Header(logo, searchBox) { +import Input from './Input'; +import Button from './Button'; +import { handleSearch } from '../services/handleSeach'; + +function Header({ logoSrc, logoAlt, onSearch }) { const headerElement = document.createElement('header'); - headerElement.classList.add('header'); - headerElement.appendChild(logo); - headerElement.appendChild(searchBox); + headerElement.innerHTML = ` + <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1> + <div class="search-box"></div> + `; + + let page = 1; + + const searchInput = Input({ + id: 'search-input', + type: 'text', + placeholder: '๊ฒ€์ƒ‰', + onKeyDown: async (event) => { + if (event.code === 'Enter') { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + await onSearch(event.target.value, page); + } + } + }); + + const searchButton = Button({ + classNames: ['search-button'], + name: '๊ฒ€์ƒ‰', + type: 'button', + onClick: async () => { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + const inputValue = document.getElementById('search-input').value; + await onSearch(inputValue, page); + } + }); + + const searchBox = headerElement.querySelector('.search-box'); + + searchBox.addEventListener('mouseenter', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.add('search-box-toggle'); + input.classList.add('input-toggle'); + logo.classList.add('logo-toggle'); + } + }); + + searchBox.addEventListener('mouseleave', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.remove('search-box-toggle'); + input.classList.remove('input-toggle'); + logo.classList.remove('logo-toggle'); + } + }); + + searchBox.appendChild(searchInput); + searchBox.appendChild(searchButton); return headerElement; }
JavaScript
์—ฌ๊ธฐ์„œ ์“ฐ์ด๋Š” 390 ์ด๋ผ๋Š” ์ˆซ์ž๋Š” ์ƒ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ์–ด๋–จ๊นŒ์š”!?
@@ -1,9 +1,100 @@ -function Header(logo, searchBox) { +import Input from './Input'; +import Button from './Button'; +import { handleSearch } from '../services/handleSeach'; + +function Header({ logoSrc, logoAlt, onSearch }) { const headerElement = document.createElement('header'); - headerElement.classList.add('header'); - headerElement.appendChild(logo); - headerElement.appendChild(searchBox); + headerElement.innerHTML = ` + <h1><img src="${logoSrc}" alt="${logoAlt}" loading="lazy"></h1> + <div class="search-box"></div> + `; + + let page = 1; + + const searchInput = Input({ + id: 'search-input', + type: 'text', + placeholder: '๊ฒ€์ƒ‰', + onKeyDown: async (event) => { + if (event.code === 'Enter') { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + await onSearch(event.target.value, page); + } + } + }); + + const searchButton = Button({ + classNames: ['search-button'], + name: '๊ฒ€์ƒ‰', + type: 'button', + onClick: async () => { + document.getElementById('more-movies').remove(); + const sectionContainer = document.querySelector('.item-view'); + + sectionContainer.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleSearch(searchInput.value, page); + } + }) + ); + + const inputValue = document.getElementById('search-input').value; + await onSearch(inputValue, page); + } + }); + + const searchBox = headerElement.querySelector('.search-box'); + + searchBox.addEventListener('mouseenter', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.add('search-box-toggle'); + input.classList.add('input-toggle'); + logo.classList.add('logo-toggle'); + } + }); + + searchBox.addEventListener('mouseleave', () => { + const width = document.querySelector('body').getBoundingClientRect().width; + + if (width <= 390) { + const input = document.getElementById('search-input'); + const logo = document.querySelector('header h1'); + + searchBox.classList.remove('search-box-toggle'); + input.classList.remove('input-toggle'); + logo.classList.remove('logo-toggle'); + } + }); + + searchBox.appendChild(searchInput); + searchBox.appendChild(searchButton); return headerElement; }
JavaScript
์ž˜ ๋ณด๋‹ˆ๊นŒ ๋กœ์ง์ด ๊ฑฐ์˜ ์œ ์‚ฌํ•˜๋„ค์š”!
@@ -0,0 +1,33 @@ +import handleMovie from '../services/handleMovie'; +import Button from './Button'; + +function MainContent(initialPage) { + let page = initialPage; + const mainElement = document.createElement('main'); + mainElement.id = 'main'; + + mainElement.innerHTML = ` + <section class="item-view"> + <h2>์ง€๊ธˆ ์ธ๊ธฐ ์žˆ๋Š” ์˜ํ™”</h2> + <ul class="item-list"></ul> + </section> + `; + + const sectionElement = mainElement.querySelector('.item-view'); + sectionElement.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleMovie(page); + } + }) + ); + + return mainElement; +} + +export default MainContent;
JavaScript
```suggestion const mainElement = html(` <main id="main"> <section class="item-view"> <h2>์ง€๊ธˆ ์ธ๊ธฐ ์žˆ๋Š” ์˜ํ™”</h2> <ul class="item-list"></ul> </section> <main> `); ``` ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ฃผ์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”!?
@@ -0,0 +1,33 @@ +import handleMovie from '../services/handleMovie'; +import Button from './Button'; + +function MainContent(initialPage) { + let page = initialPage; + const mainElement = document.createElement('main'); + mainElement.id = 'main'; + + mainElement.innerHTML = ` + <section class="item-view"> + <h2>์ง€๊ธˆ ์ธ๊ธฐ ์žˆ๋Š” ์˜ํ™”</h2> + <ul class="item-list"></ul> + </section> + `; + + const sectionElement = mainElement.querySelector('.item-view'); + sectionElement.appendChild( + Button({ + classNames: ['btn', 'primary', 'full-width'], + type: 'button', + id: 'more-movies', + name: '๋” ๋ณด๊ธฐ', + onClick: () => { + page += 1; + handleMovie(page); + } + }) + ); + + return mainElement; +} + +export default MainContent;
JavaScript
์œ„์—์„œ๋Š” html template์œผ๋กœ ํ‘œํ˜„๋˜๊ณ  ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„๋˜๊ณ  ์žˆ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ ์ž…์žฅ์—์„œ๋Š” ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š์„๊นŒ ์‹ถ์–ด์š”!
@@ -0,0 +1,47 @@ +import { BASE_URL } from '../constants/api'; +import { APIError } from '../apis/error'; + +interface ApiClientType { + method: 'GET' | 'POST' | 'PUT' | 'DELETE'; + endpoint: string; + params?: Record<string, string>; + headers?: Record<string, string>; + body?: Record<string, any>; +} + +class ApiClient { + static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) { + const url = params + ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params }) + : `${BASE_URL}/${endpoint}`; + + const options: RequestInit = { + method, + headers: { + 'Content-Type': 'application/json', + ...headers + }, + ...(body && { body: JSON.stringify(body) }) + }; + + try { + const response = await fetch(url, options); + const data = await response.json(); + + if (!response.ok) { + throw new APIError(data.status_code, data.status_message); + } + return data; + } catch (error: any) { + throw new APIError(400, error.message || 'An unknown error occurred'); + } + } + + static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) { + const url = new URL(baseUrl); + url.search = new URLSearchParams(params).toString(); + return url.toString(); + } +} + +export default ApiClient;
TypeScript
์•„ํ•˜.. ์ด ํŒŒ์ผ ๋•Œ๋ฌธ์— tsconfig๊ฐ€ ์ƒ๊ธด๊ฑฐ์˜€๊ตฐ์š”
@@ -0,0 +1,47 @@ +import { BASE_URL } from '../constants/api'; +import { APIError } from '../apis/error'; + +interface ApiClientType { + method: 'GET' | 'POST' | 'PUT' | 'DELETE'; + endpoint: string; + params?: Record<string, string>; + headers?: Record<string, string>; + body?: Record<string, any>; +} + +class ApiClient { + static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) { + const url = params + ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params }) + : `${BASE_URL}/${endpoint}`; + + const options: RequestInit = { + method, + headers: { + 'Content-Type': 'application/json', + ...headers + }, + ...(body && { body: JSON.stringify(body) }) + }; + + try { + const response = await fetch(url, options); + const data = await response.json(); + + if (!response.ok) { + throw new APIError(data.status_code, data.status_message); + } + return data; + } catch (error: any) { + throw new APIError(400, error.message || 'An unknown error occurred'); + } + } + + static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) { + const url = new URL(baseUrl); + url.search = new URLSearchParams(params).toString(); + return url.toString(); + } +} + +export default ApiClient;
TypeScript
try ๊ตฌ๊ฐ„์—์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ -> catch๋กœ ์ง„์ž… -> ๋ฌด์กฐ๊ฑด status๋Š” 400์œผ๋กœ ๊ณ ์ •ํ•˜์—ฌ ๋‹ค์‹œ error throw ์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ ์˜๋ฏธํ•œ ์—๋Ÿฌ ํ”Œ๋กœ์šฐ์ผ๊นŒ์š”~?
@@ -0,0 +1,47 @@ +import { BASE_URL } from '../constants/api'; +import { APIError } from '../apis/error'; + +interface ApiClientType { + method: 'GET' | 'POST' | 'PUT' | 'DELETE'; + endpoint: string; + params?: Record<string, string>; + headers?: Record<string, string>; + body?: Record<string, any>; +} + +class ApiClient { + static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) { + const url = params + ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params }) + : `${BASE_URL}/${endpoint}`; + + const options: RequestInit = { + method, + headers: { + 'Content-Type': 'application/json', + ...headers + }, + ...(body && { body: JSON.stringify(body) }) + }; + + try { + const response = await fetch(url, options); + const data = await response.json(); + + if (!response.ok) { + throw new APIError(data.status_code, data.status_message); + } + return data; + } catch (error: any) { + throw new APIError(400, error.message || 'An unknown error occurred'); + } + } + + static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) { + const url = new URL(baseUrl); + url.search = new URLSearchParams(params).toString(); + return url.toString(); + } +} + +export default ApiClient;
TypeScript
๊ทธ๋Ÿฐ๋ฐ ์ด ํŒŒ์ผ์ด ์™œ domain ์— ํ•ด๋‹นํ•˜๋Š”๊ฑธ๊นŒ์š”~? domain์ด๋ผ๊ณ  ํ•˜๊ธฐ์—” ๋ง ๊ทธ๋Œ€๋กœ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ(client)๋ฅผ ํ‘œํ˜„ํ•œ๊ฑฐ๋ผ์„œ... ์ „ํ˜€ ์—ฐ๊ด€์ด ์—†์–ด๋ณด์—ฌ์š”!
@@ -0,0 +1,24 @@ +import getMovieList from '../apis/getMovieList'; +import ConfirmModal from '../components/modal/ConfirmModal'; +import { Modal } from '../components/modal/container/Modal'; +import { NOT_MORE_MOVIES_MESSAGE } from '../constants/message'; +import renderMovies from '../view/renderMovies'; +import renderSkeletonMovies from '../view/renderSkeletonMovies'; + +export default async function handleMovie(page) { + try { + renderSkeletonMovies({ loading: true }); + const movieList = await getMovieList(page); + renderSkeletonMovies({ loading: false }); + + if (movieList.results.length === 0) { + document.getElementById('more-movies').remove(); + new Modal(document.querySelector('body'), ConfirmModal(NOT_MORE_MOVIES_MESSAGE)); + } + + renderMovies({ movieList: movieList.results, resetHTML: false }); + } catch (error) { + renderSkeletonMovies({ loading: false }); + new Modal(document.querySelector('body'), ConfirmModal(error.message)); + } +}
JavaScript
api์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ ์ค‘์— message๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, CustomError๋ฅผ ๋งŒ๋“ค์–ด์„œ ์“ฐ๋Š” ์˜๋ฏธ๊ฐ€ ์žˆ๋Š”๊ฑด๊ฐ€ ์‹ถ์–ด์š”!
@@ -0,0 +1,47 @@ +import { BASE_URL } from '../constants/api'; +import { APIError } from '../apis/error'; + +interface ApiClientType { + method: 'GET' | 'POST' | 'PUT' | 'DELETE'; + endpoint: string; + params?: Record<string, string>; + headers?: Record<string, string>; + body?: Record<string, any>; +} + +class ApiClient { + static async request({ method, endpoint, params, headers = {}, body }: ApiClientType) { + const url = params + ? this.createUrlSearchParams({ baseUrl: `${BASE_URL}/${endpoint}`, params }) + : `${BASE_URL}/${endpoint}`; + + const options: RequestInit = { + method, + headers: { + 'Content-Type': 'application/json', + ...headers + }, + ...(body && { body: JSON.stringify(body) }) + }; + + try { + const response = await fetch(url, options); + const data = await response.json(); + + if (!response.ok) { + throw new APIError(data.status_code, data.status_message); + } + return data; + } catch (error: any) { + throw new APIError(400, error.message || 'An unknown error occurred'); + } + } + + static createUrlSearchParams({ baseUrl, params }: { baseUrl: string; params: Record<string, string> }) { + const url = new URL(baseUrl); + url.search = new URLSearchParams(params).toString(); + return url.toString(); + } +} + +export default ApiClient;
TypeScript
domain์ด apis๋ฅผ ์ฐธ์กฐํ•˜๋Š”๊ฑด ์–ด์ƒ‰ํ•ด๋ณด์—ฌ์š”!
@@ -1,2 +1 @@ export const BASE_URL = 'https://api.themoviedb.org'; -export const IMAGE_BASE_URL = 'https://image.tmdb.org/t/p/w500';
JavaScript
์ด๋Ÿฐ ์ •๋ณด๋Š” ์•„์˜ˆ API๋ฅผ ์š”์ฒญํ•˜๋Š” ์ชฝ์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1 @@ +export const NOT_MORE_MOVIES_MESSAGE = '๋”์ด์ƒ ์˜ํ™”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.';
JavaScript
์ด๋Ÿฐ ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋„ ๋ฉ”์„ธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ชฝ์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์–ด๋–จ๊นŒ์š”? ๋‹ค๋ฅธ ๊ตฌ๊ฐ„์—์„œ๋Š” ์•„์˜ˆ ์‚ฌ์šฉ๋  ๊ฒƒ ๊ฐ™์ง€ ์•Š์•„์„œ์š”!
@@ -0,0 +1,75 @@ +package bridge.controller; + +import bridge.BridgeRandomNumberGenerator; +import bridge.constants.GameValue; +import bridge.domain.Bridge; +import bridge.domain.BridgeGame; +import bridge.domain.BridgeMaker; +import bridge.view.handler.InputHandler; +import bridge.view.OutputView; + +public class BridgeController { + private final InputHandler inputHandler; + private final OutputView outputView; + private final BridgeGame bridgeGame; + private boolean status = true; + + public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) { + this.inputHandler = inputHandler; + this.outputView = outputView; + this.bridgeGame = bridgeGame; + } + + public void start() { + outputView.printStartMessage(); + Bridge bridge = createBridge(); + run(bridge); + } + + private Bridge createBridge() { + outputView.printBridgeSizeInputMessage(); + BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator()); + return inputHandler.createValidatedBridge(bridgeMaker); + } + + private void run(Bridge bridge) { + while (status) { + move(bridge); + if (isFinished()) { + finishGame(); + } + if (!isFinished()) { + askRetry(); + } + } + } + + private void move(Bridge bridge) { + for (String square : bridge.getBridge()) { + outputView.printMoveInputMessage(); + String moveCommand = inputHandler.readMoving(); + bridgeGame.move(square, moveCommand); + outputView.printMap(bridgeGame.getMoveResult()); + } + } + + private boolean isFinished() { + return bridgeGame.isSuccessful(); + } + + private void finishGame() { + outputView.printResult(bridgeGame); + status = false; + } + + private void askRetry() { + outputView.printRetryMessage(); + String retryCommand = inputHandler.readGameCommand(); + if (retryCommand.equals(GameValue.RETRY_COMMAND)) { + bridgeGame.retry(); + } + if (retryCommand.equals(GameValue.QUIT_COMMAND)) { + outputView.printResult(bridgeGame); + } + } +}
Java
๋‹ค๋ฆฌ๋ฅผ ๋Œ๋ฉด์„œ ์ด๋™์„ ๋ฐ›๊ณ  ์žˆ๋Š” ํ˜•ํƒœ๋ผ์„œ, ์ด๋™์ด ์‹คํŒจํ•ด๋„ ๋ฐ”๋กœ ์žฌ์‹œ๋„ ์—ฌ๋ถ€๋ฅผ ๋ฌป์ง€ ์•Š๊ณ , ๊ทธ๋ƒฅ ์ง„ํ–‰์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..!
@@ -0,0 +1,75 @@ +package bridge.controller; + +import bridge.BridgeRandomNumberGenerator; +import bridge.constants.GameValue; +import bridge.domain.Bridge; +import bridge.domain.BridgeGame; +import bridge.domain.BridgeMaker; +import bridge.view.handler.InputHandler; +import bridge.view.OutputView; + +public class BridgeController { + private final InputHandler inputHandler; + private final OutputView outputView; + private final BridgeGame bridgeGame; + private boolean status = true; + + public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) { + this.inputHandler = inputHandler; + this.outputView = outputView; + this.bridgeGame = bridgeGame; + } + + public void start() { + outputView.printStartMessage(); + Bridge bridge = createBridge(); + run(bridge); + } + + private Bridge createBridge() { + outputView.printBridgeSizeInputMessage(); + BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator()); + return inputHandler.createValidatedBridge(bridgeMaker); + } + + private void run(Bridge bridge) { + while (status) { + move(bridge); + if (isFinished()) { + finishGame(); + } + if (!isFinished()) { + askRetry(); + } + } + } + + private void move(Bridge bridge) { + for (String square : bridge.getBridge()) { + outputView.printMoveInputMessage(); + String moveCommand = inputHandler.readMoving(); + bridgeGame.move(square, moveCommand); + outputView.printMap(bridgeGame.getMoveResult()); + } + } + + private boolean isFinished() { + return bridgeGame.isSuccessful(); + } + + private void finishGame() { + outputView.printResult(bridgeGame); + status = false; + } + + private void askRetry() { + outputView.printRetryMessage(); + String retryCommand = inputHandler.readGameCommand(); + if (retryCommand.equals(GameValue.RETRY_COMMAND)) { + bridgeGame.retry(); + } + if (retryCommand.equals(GameValue.QUIT_COMMAND)) { + outputView.printResult(bridgeGame); + } + } +}
Java
์‹คํŒจํ•˜๊ณ  ์žฌ์‹œ๋„๋Š” ์ข…๋ฃŒ๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ๊ณ„์† ๋‹ค์Œ ์ด๋™ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..! ์‹คํŒจํ•˜๊ณ  ์—ฌ๊ธฐ๋กœ ๋“ค์–ด์™€์„œ status๋ฅผ false๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ถ€๋ถ„์ด ์—†์–ด์„œ์ธ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,75 @@ +package bridge.controller; + +import bridge.BridgeRandomNumberGenerator; +import bridge.constants.GameValue; +import bridge.domain.Bridge; +import bridge.domain.BridgeGame; +import bridge.domain.BridgeMaker; +import bridge.view.handler.InputHandler; +import bridge.view.OutputView; + +public class BridgeController { + private final InputHandler inputHandler; + private final OutputView outputView; + private final BridgeGame bridgeGame; + private boolean status = true; + + public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) { + this.inputHandler = inputHandler; + this.outputView = outputView; + this.bridgeGame = bridgeGame; + } + + public void start() { + outputView.printStartMessage(); + Bridge bridge = createBridge(); + run(bridge); + } + + private Bridge createBridge() { + outputView.printBridgeSizeInputMessage(); + BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator()); + return inputHandler.createValidatedBridge(bridgeMaker); + } + + private void run(Bridge bridge) { + while (status) { + move(bridge); + if (isFinished()) { + finishGame(); + } + if (!isFinished()) { + askRetry(); + } + } + } + + private void move(Bridge bridge) { + for (String square : bridge.getBridge()) { + outputView.printMoveInputMessage(); + String moveCommand = inputHandler.readMoving(); + bridgeGame.move(square, moveCommand); + outputView.printMap(bridgeGame.getMoveResult()); + } + } + + private boolean isFinished() { + return bridgeGame.isSuccessful(); + } + + private void finishGame() { + outputView.printResult(bridgeGame); + status = false; + } + + private void askRetry() { + outputView.printRetryMessage(); + String retryCommand = inputHandler.readGameCommand(); + if (retryCommand.equals(GameValue.RETRY_COMMAND)) { + bridgeGame.retry(); + } + if (retryCommand.equals(GameValue.QUIT_COMMAND)) { + outputView.printResult(bridgeGame); + } + } +}
Java
์ด๋™ํ–ˆ์„ ๋•Œ X๊ฐ€ ๋‚˜์˜ค๋ฉด ์žฌ์‹œ์ž‘์ด ๋‚˜์™€์•ผํ•˜๋Š”๋ฐ ์ด๊ฑฐ ์ œ๊ฐ€ ๋ฌธ์ œ๋ฅผ ์ž˜ ๋ชป์ดํ•ดํ–ˆ๋„ค์š” ใ…‹ใ…‹..
@@ -0,0 +1,75 @@ +package bridge.controller; + +import bridge.BridgeRandomNumberGenerator; +import bridge.constants.GameValue; +import bridge.domain.Bridge; +import bridge.domain.BridgeGame; +import bridge.domain.BridgeMaker; +import bridge.view.handler.InputHandler; +import bridge.view.OutputView; + +public class BridgeController { + private final InputHandler inputHandler; + private final OutputView outputView; + private final BridgeGame bridgeGame; + private boolean status = true; + + public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) { + this.inputHandler = inputHandler; + this.outputView = outputView; + this.bridgeGame = bridgeGame; + } + + public void start() { + outputView.printStartMessage(); + Bridge bridge = createBridge(); + run(bridge); + } + + private Bridge createBridge() { + outputView.printBridgeSizeInputMessage(); + BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator()); + return inputHandler.createValidatedBridge(bridgeMaker); + } + + private void run(Bridge bridge) { + while (status) { + move(bridge); + if (isFinished()) { + finishGame(); + } + if (!isFinished()) { + askRetry(); + } + } + } + + private void move(Bridge bridge) { + for (String square : bridge.getBridge()) { + outputView.printMoveInputMessage(); + String moveCommand = inputHandler.readMoving(); + bridgeGame.move(square, moveCommand); + outputView.printMap(bridgeGame.getMoveResult()); + } + } + + private boolean isFinished() { + return bridgeGame.isSuccessful(); + } + + private void finishGame() { + outputView.printResult(bridgeGame); + status = false; + } + + private void askRetry() { + outputView.printRetryMessage(); + String retryCommand = inputHandler.readGameCommand(); + if (retryCommand.equals(GameValue.RETRY_COMMAND)) { + bridgeGame.retry(); + } + if (retryCommand.equals(GameValue.QUIT_COMMAND)) { + outputView.printResult(bridgeGame); + } + } +}
Java
๊ทธ๋Ÿฌ๋„ค์š” ๋ถ„๋ฆฌํ•˜๊ธฐ์ „์—๋Š” ์ •์ƒ๋™์ž‘ํ•ด์„œ ๋ฆฌํŒฉํ† ๋ง๋‹จ๊ณ„์—์„œ ์‹ค์ˆ˜ํ–ˆ๋‚˜๋ด…๋‹ˆ๋‹ค.. ๋งˆ์ง€๋ง‰์— ๋‹ค์‹œ ํ•œ๋ฒˆ ๊ผผ๊ผผํžˆ ์ฒดํฌํ•ด์•ผ๊ฒ ๋„ค์š” ๐Ÿ˜จ
@@ -0,0 +1,14 @@ +package bridge.constants; + +public class GameValue { + + private GameValue() { + } + + public static final String MOVE_UP_COMMAND = "U"; + public static final String MOVE_DOWN_COMMAND = "D"; + public static final String RETRY_COMMAND = "R"; + public static final String QUIT_COMMAND = "Q"; + public static final String MOVE = "O"; + public static final String STOP = "X"; +}
Java
๊ด€๋ จ ์žˆ๋Š” ์ƒ์ˆ˜๋ผ๋ฆฌ Enum ์œผ๋กœ ๋ฌถ์–ด๋ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,17 @@ +package bridge.constants; + +import java.util.regex.Pattern; + +public enum RegexPattern { + ONLY_NUMBER(Pattern.compile("\\d+")); + + private final Pattern pattern; + + RegexPattern(Pattern pattern) { + this.pattern = pattern; + } + + public boolean matches(String value) { + return pattern.matcher(value).matches(); + } +}
Java
Pattern ์ž์ฒด๋ฅผ Enum ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ–ˆ๋Š”๋ฐ, ์ด๊ฑฐ ๊ฝค๋‚˜ ์žฌํ™œ์šฉ์„ฑ์ด ๋†’๊ฒ ๊ตฐ์š” ๐Ÿ‘
@@ -0,0 +1,75 @@ +package bridge.controller; + +import bridge.BridgeRandomNumberGenerator; +import bridge.constants.GameValue; +import bridge.domain.Bridge; +import bridge.domain.BridgeGame; +import bridge.domain.BridgeMaker; +import bridge.view.handler.InputHandler; +import bridge.view.OutputView; + +public class BridgeController { + private final InputHandler inputHandler; + private final OutputView outputView; + private final BridgeGame bridgeGame; + private boolean status = true; + + public BridgeController(InputHandler inputHandler, OutputView outputView, BridgeGame bridgeGame) { + this.inputHandler = inputHandler; + this.outputView = outputView; + this.bridgeGame = bridgeGame; + } + + public void start() { + outputView.printStartMessage(); + Bridge bridge = createBridge(); + run(bridge); + } + + private Bridge createBridge() { + outputView.printBridgeSizeInputMessage(); + BridgeMaker bridgeMaker = new BridgeMaker(new BridgeRandomNumberGenerator()); + return inputHandler.createValidatedBridge(bridgeMaker); + } + + private void run(Bridge bridge) { + while (status) { + move(bridge); + if (isFinished()) { + finishGame(); + } + if (!isFinished()) { + askRetry(); + } + } + } + + private void move(Bridge bridge) { + for (String square : bridge.getBridge()) { + outputView.printMoveInputMessage(); + String moveCommand = inputHandler.readMoving(); + bridgeGame.move(square, moveCommand); + outputView.printMap(bridgeGame.getMoveResult()); + } + } + + private boolean isFinished() { + return bridgeGame.isSuccessful(); + } + + private void finishGame() { + outputView.printResult(bridgeGame); + status = false; + } + + private void askRetry() { + outputView.printRetryMessage(); + String retryCommand = inputHandler.readGameCommand(); + if (retryCommand.equals(GameValue.RETRY_COMMAND)) { + bridgeGame.retry(); + } + if (retryCommand.equals(GameValue.QUIT_COMMAND)) { + outputView.printResult(bridgeGame); + } + } +}
Java
์ด ๋ถ€๋ถ„์€ ์ธ๋ดํŠธ๋ฅผ 1๋กœ ๋ฆฌํŒฉํ† ๋งํ•˜๋Š” ๊ณ ํ†ต์Šค๋Ÿฌ์šด(?) ๊ฒฝํ—˜์„ ํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ €๋„ ์ ˆ๋Œ€ ๋ชป ์ค„์ผ ๊ฒƒ ๊ฐ™๋˜ ์ธ๋ดํŠธ๋ฅผ ์ค„์ด๋ ค๊ณ  ์ด๋ฆฌ์ €๋ฆฌ ๋น„ํ‹€๋‹ค๋ณด๋‹ˆ(?) ๋‹ค์–‘ํ•œ ์ธ์‚ฌ์ดํŠธ๋“ค์ด ๋– ์˜ค๋ฅด๋”๋ผ๊ณ ์š”..!
@@ -0,0 +1,82 @@ +package bridge.domain; + +import bridge.constants.GameValue; + +/** + * ๋‹ค๋ฆฌ ๊ฑด๋„ˆ๊ธฐ ๊ฒŒ์ž„์„ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค + */ +public class BridgeGame { + + private int retryCount; + private MoveResult moveResult; + + public BridgeGame(MoveResult moveResult, int retryCount) { + this.moveResult = moveResult; + this.retryCount = retryCount; + } + + /** + * ์‚ฌ์šฉ์ž๊ฐ€ ์นธ์„ ์ด๋™ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ + */ + public void move(String square, String moveCommand) { + if (moveCommand.equals(GameValue.MOVE_UP_COMMAND)) { + moveUp(square, moveCommand); + } + if (moveCommand.equals(GameValue.MOVE_DOWN_COMMAND)) { + moveLower(square, moveCommand); + } + } + + private boolean isMoveUp(String square, String moveCommand) { + return square.equals(moveCommand); + } + + private void moveUp(String square, String moveCommand) { + if (isMoveUp(square, moveCommand)) { + moveResult.moveUp(GameValue.MOVE); + } + if (!isMoveUp(square, moveCommand)) { + moveResult.moveUp(GameValue.STOP); + } + } + + private boolean isMoveLower(String square, String moveCommand) { + return square.equals(moveCommand); + } + + private void moveLower(String square, String moveCommand) { + if (isMoveLower(square, moveCommand)) { + moveResult.moveLowwer(GameValue.MOVE); + } + if (!isMoveLower(square, moveCommand)) { + moveResult.moveLowwer(GameValue.STOP); + } + } + + public boolean isSuccessful() { + return moveResult.isSuccessful(); + } + + public String getGameResult() { + if (isSuccessful()) { + return "์„ฑ๊ณต"; + } + return "์‹คํŒจ"; + } + + /** + * ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒŒ์ž„์„ ๋‹ค์‹œ ์‹œ๋„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ + */ + public void retry() { + retryCount++; + moveResult = new MoveResult(); + } + + public int getRetryCount() { + return retryCount; + } + + public MoveResult getMoveResult() { + return moveResult; + } +}
Java
์—ฌ๊ธฐ early return ์ด ๋น ์ง„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,37 @@ +package bridge.domain; + +import java.util.ArrayList; +import java.util.List; + +public class MoveResult { + private final List<String> upperBridge; + private final List<String> lowerBridge; + + public MoveResult() { + this.upperBridge = new ArrayList<>(); + this.lowerBridge = new ArrayList<>(); + } + + public void moveUp(String moveResult) { + upperBridge.add(moveResult); + lowerBridge.add(" "); + } + + public void moveLowwer(String moveResult) { + upperBridge.add(" "); + lowerBridge.add(moveResult); + } + + public boolean isSuccessful() { + if (upperBridge.contains("X") || lowerBridge.contains("X")) { + return false; + } + return true; + } + + @Override + public String toString() { + return "[ " + String.join(" | ", upperBridge) + " ]\n" + + "[ " + String.join(" | ", lowerBridge) + " ]"; + } +}
Java
> ํ•ต์‹ฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ์™€ UI๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ๋ถ„๋ฆฌํ•ด ๊ตฌํ˜„ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์š”๊ตฌ์‚ฌํ•ญ์ด ์ง€์ผœ์ง€์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ”„๋ฆฌ์ฝ”์Šค ๊ธฐ๊ฐ„ ๋™์•ˆ ๊ฐ€์žฅ ๋งŽ์ด ์š”๊ตฌ๋œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฒ•์ด๋ผ, ์ด ๋ถ€๋ถ„์€ ๊ผญ ๋ฆฌํŒฉํ† ๋ง ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,56 @@ +package bridge.view; + +import bridge.domain.BridgeGame; +import bridge.domain.MoveResult; + +/** + * ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฒŒ์ž„ ์ง„ํ–‰ ์ƒํ™ฉ๊ณผ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. + */ +public class OutputView { + + public void printStartMessage() { + println("๋‹ค๋ฆฌ ๊ฑด๋„ˆ๊ธฐ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค."); + } + + public void printBridgeSizeInputMessage() { + printNewLine(); + println("๋‹ค๋ฆฌ์˜ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + } + + public void printMoveInputMessage() { + printNewLine(); + println("์ด๋™ํ•  ์นธ์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”. (์œ„: U, ์•„๋ž˜: D)"); + } + + /** + * ํ˜„์žฌ๊นŒ์ง€ ์ด๋™ํ•œ ๋‹ค๋ฆฌ์˜ ์ƒํƒœ๋ฅผ ์ •ํ•ด์ง„ ํ˜•์‹์— ๋งž์ถฐ ์ถœ๋ ฅํ•œ๋‹ค. + */ + public void printMap(MoveResult moveResult) { + println(moveResult.toString()); + } + + /** + * ๊ฒŒ์ž„์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์ •ํ•ด์ง„ ํ˜•์‹์— ๋งž์ถฐ ์ถœ๋ ฅํ•œ๋‹ค. + */ + public void printResult(BridgeGame bridgeGame) { + printNewLine(); + println("์ตœ์ข… ๊ฒŒ์ž„ ๊ฒฐ๊ณผ"); + printMap(bridgeGame.getMoveResult()); + printNewLine(); + System.out.printf("๊ฒŒ์ž„ ์„ฑ๊ณต ์—ฌ๋ถ€: %s\n", bridgeGame.getGameResult()); + System.out.printf("์ด ์‹œ๋„ํ•œ ํšŸ์ˆ˜: %d\n", bridgeGame.getRetryCount()); + } + + public void printRetryMessage() { + printNewLine(); + println("๊ฒŒ์ž„์„ ๋‹ค์‹œ ์‹œ๋„ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. (์žฌ์‹œ๋„: R, ์ข…๋ฃŒ: Q)"); + } + + private void println(String output) { + System.out.println(output); + } + + private void printNewLine() { + System.out.println(); + } +}
Java
ํ™•์‹คํžˆ ์ถœ๋ ฅ์šฉ ๋ฉ”์‹œ์ง€๊ฐ€ ์žฌํ™œ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†๋‹ค๋ณด๋‹ˆ, ์ด๋ ‡๊ฒŒ ํ•˜๋“œ ์ฝ”๋”ฉํ•ด๋„ ๊ฝค๋‚˜ ์ง๊ด€์ ์ด์ž–์•„? ๋ผ๋Š” ๋А๋‚Œ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,42 @@ +package bridge.view.handler; + +import bridge.domain.Bridge; +import bridge.domain.BridgeMaker; +import bridge.view.ErrorView; +import bridge.view.InputView; +import java.util.function.Supplier; + +public class InputHandler { + private final InputView inputView; + private final ErrorView errorView; + + public InputHandler(InputView inputView, ErrorView errorView) { + this.inputView = inputView; + this.errorView = errorView; + } + + public Bridge createValidatedBridge(BridgeMaker bridgeMaker) { + return receiveValidatedInput(() -> { + int bridgeSize = inputView.readBridgeSize(); + return Bridge.from(bridgeMaker.makeBridge(bridgeSize)); + }); + } + + public String readMoving() { + return receiveValidatedInput(inputView::readMoving); + } + + public String readGameCommand() { + return receiveValidatedInput(inputView::readGameCommand); + } + + private <T> T receiveValidatedInput(Supplier<T> inputView) { + while (true) { + try { + return inputView.get(); + } catch (IllegalArgumentException exception) { + errorView.printErrorMessage(exception.getMessage()); + } + } + } +}
Java
(๊ถ๊ธˆํ•ด์š”!) ๊ทธ๋™์•ˆ ํ˜„์ค€๋‹˜๊ณผ ๋ฆฌ๋ทฐํ•ด์˜ค๋ฉด์„œ ์ด๋Ÿฐ ์žฌ์‹œ๋„ ๋กœ์ง์„ while ๋กœ ์ฒ˜๋ฆฌํ• ๊นŒ, ์žฌ๊ท€๋กœ ์ฒ˜๋ฆฌํ• ๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์ €๋Š” ์žฌ๊ท€๋ฅผ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์žฌ๊ท€๋ฅผ ์„ ํƒํ•ด๋„ ํ˜ธ์ถœ ๋ށ์Šค๊ฐ€ ๊นŠ์–ด์ง€๋Š”๊ฒŒ ์•„๋‹ˆ๊ณ  ๋ށ์Šค๊ฐ€ 1๋กœ ๊ณ„์† ์œ ์ง€๋˜๋Š” ๊ฒƒ ๊ฐ™๋”๋ผ๊ณ ์š”. (์–ด์ฐจํ”ผ catch๋ฅผ ํ•˜๊ณ  ์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ์„ ํ•˜๋ฏ€๋กœ) ํ˜„์ค€๋‹˜์€ ์žฌ๊ท€ ๋Œ€์‹  while์„ ์„ ํƒํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค :)
@@ -0,0 +1,82 @@ +package bridge.domain; + +import bridge.constants.GameValue; + +/** + * ๋‹ค๋ฆฌ ๊ฑด๋„ˆ๊ธฐ ๊ฒŒ์ž„์„ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค + */ +public class BridgeGame { + + private int retryCount; + private MoveResult moveResult; + + public BridgeGame(MoveResult moveResult, int retryCount) { + this.moveResult = moveResult; + this.retryCount = retryCount; + } + + /** + * ์‚ฌ์šฉ์ž๊ฐ€ ์นธ์„ ์ด๋™ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ + */ + public void move(String square, String moveCommand) { + if (moveCommand.equals(GameValue.MOVE_UP_COMMAND)) { + moveUp(square, moveCommand); + } + if (moveCommand.equals(GameValue.MOVE_DOWN_COMMAND)) { + moveLower(square, moveCommand); + } + } + + private boolean isMoveUp(String square, String moveCommand) { + return square.equals(moveCommand); + } + + private void moveUp(String square, String moveCommand) { + if (isMoveUp(square, moveCommand)) { + moveResult.moveUp(GameValue.MOVE); + } + if (!isMoveUp(square, moveCommand)) { + moveResult.moveUp(GameValue.STOP); + } + } + + private boolean isMoveLower(String square, String moveCommand) { + return square.equals(moveCommand); + } + + private void moveLower(String square, String moveCommand) { + if (isMoveLower(square, moveCommand)) { + moveResult.moveLowwer(GameValue.MOVE); + } + if (!isMoveLower(square, moveCommand)) { + moveResult.moveLowwer(GameValue.STOP); + } + } + + public boolean isSuccessful() { + return moveResult.isSuccessful(); + } + + public String getGameResult() { + if (isSuccessful()) { + return "์„ฑ๊ณต"; + } + return "์‹คํŒจ"; + } + + /** + * ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒŒ์ž„์„ ๋‹ค์‹œ ์‹œ๋„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ + */ + public void retry() { + retryCount++; + moveResult = new MoveResult(); + } + + public int getRetryCount() { + return retryCount; + } + + public MoveResult getMoveResult() { + return moveResult; + } +}
Java
else๋ฌธ์ด ์•„๋‹ˆ๋ผ์„œ ์•„๋ฌด๋ž˜๋„ return์„ ์ถ”๊ฐ€ํ•ด ๋‹ค์Œ ์กฐ๊ฑด์„ ํ™•์ธํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š”๊ฒŒ ์ข‹๊ฒ ๋„ค์š” !
@@ -0,0 +1,37 @@ +package bridge.domain; + +import java.util.ArrayList; +import java.util.List; + +public class MoveResult { + private final List<String> upperBridge; + private final List<String> lowerBridge; + + public MoveResult() { + this.upperBridge = new ArrayList<>(); + this.lowerBridge = new ArrayList<>(); + } + + public void moveUp(String moveResult) { + upperBridge.add(moveResult); + lowerBridge.add(" "); + } + + public void moveLowwer(String moveResult) { + upperBridge.add(" "); + lowerBridge.add(moveResult); + } + + public boolean isSuccessful() { + if (upperBridge.contains("X") || lowerBridge.contains("X")) { + return false; + } + return true; + } + + @Override + public String toString() { + return "[ " + String.join(" | ", upperBridge) + " ]\n" + + "[ " + String.join(" | ", lowerBridge) + " ]"; + } +}
Java
ํ—› ๊ทธ ๋ถ€๋ถ„์„ ๋†“์ณ๋ฒ„๋ ธ๋„ค์š” .. ์ด๋ฒˆ 5๊ธฐ ์ตœ์ข…๊นŒ์ง€ ์น˜๋ฉด์„œ ๋А๋‚€๊ฑด๋ฐ 5์‹œ๊ฐ„์ด๋ผ๋Š” ์‹œ๊ฐ„์— ์–ฝ๋งค์ด์ง€ ์•Š๊ณ  ์ดˆ๋ฐ˜์— ๊ธฐ๋Šฅ ์š”๊ตฌ ์‚ฌํ•ญ์„ ๊ผผ๊ผผํžˆ ๊ฒ€ํ† ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ผˆ์ €๋ฆฌ๊ฒŒ ๋А๊ผˆ์Šต๋‹ˆ๋‹ค..
@@ -0,0 +1,42 @@ +package bridge.view.handler; + +import bridge.domain.Bridge; +import bridge.domain.BridgeMaker; +import bridge.view.ErrorView; +import bridge.view.InputView; +import java.util.function.Supplier; + +public class InputHandler { + private final InputView inputView; + private final ErrorView errorView; + + public InputHandler(InputView inputView, ErrorView errorView) { + this.inputView = inputView; + this.errorView = errorView; + } + + public Bridge createValidatedBridge(BridgeMaker bridgeMaker) { + return receiveValidatedInput(() -> { + int bridgeSize = inputView.readBridgeSize(); + return Bridge.from(bridgeMaker.makeBridge(bridgeSize)); + }); + } + + public String readMoving() { + return receiveValidatedInput(inputView::readMoving); + } + + public String readGameCommand() { + return receiveValidatedInput(inputView::readGameCommand); + } + + private <T> T receiveValidatedInput(Supplier<T> inputView) { + while (true) { + try { + return inputView.get(); + } catch (IllegalArgumentException exception) { + errorView.printErrorMessage(exception.getMessage()); + } + } + } +}
Java
์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๋•Œ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค๋Š” ์žฅ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ํƒ์ƒ‰ ๊นŠ์ด๊ฐ€ ๊นŠ์–ด ์งˆ ๋•Œ ์œ„ํ—˜ํ•  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์„ ํ˜ธ๋‹˜ ๋ง์”€๋Œ€๋กœ 1๋กœ ์œ ์ง€๋œ๋‹ค๋ฉด ์ด ์ ์€ ์•„๋ฌด๋ž˜๋„ ์ œ์™ธํ•ด์•ผํ• ๊ฒƒ ๊ฐ™๋„ค์š” ํ•˜์ง€๋งŒ while๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ๋„ ์žˆ๊ธฐ์— while๋ฌธ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ์‹ค์ œ ๋กœ๊ทธ์ธ ์ •์ฑ…์ฒ˜๋Ÿผ ์ž˜๋ชป๋œ ๊ฐ’์„ 3ํšŒ ๋˜๋Š” 5ํšŒ ์ž…๋ ฅ ํ–ˆ์„ ๊ฒฝ์šฐ ์ž…๋ ฅ์„ ์ข…๋ฃŒ ์‹œํ‚จ๋‹ค๋˜๊ฐ€ ๋“ฑ์˜ ์ƒํ™ฉ๋„ ์ƒ๊ฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,42 @@ +package bridge.view.handler; + +import bridge.domain.Bridge; +import bridge.domain.BridgeMaker; +import bridge.view.ErrorView; +import bridge.view.InputView; +import java.util.function.Supplier; + +public class InputHandler { + private final InputView inputView; + private final ErrorView errorView; + + public InputHandler(InputView inputView, ErrorView errorView) { + this.inputView = inputView; + this.errorView = errorView; + } + + public Bridge createValidatedBridge(BridgeMaker bridgeMaker) { + return receiveValidatedInput(() -> { + int bridgeSize = inputView.readBridgeSize(); + return Bridge.from(bridgeMaker.makeBridge(bridgeSize)); + }); + } + + public String readMoving() { + return receiveValidatedInput(inputView::readMoving); + } + + public String readGameCommand() { + return receiveValidatedInput(inputView::readGameCommand); + } + + private <T> T receiveValidatedInput(Supplier<T> inputView) { + while (true) { + try { + return inputView.get(); + } catch (IllegalArgumentException exception) { + errorView.printErrorMessage(exception.getMessage()); + } + } + } +}
Java
ํ™•์‹คํžˆ **3ํšŒ ๊นŒ์ง€๋งŒ ์žฌ์‹œ๋„๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค** ๋ผ๋Š” ๋กœ์ง์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” while ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”! ์ธ์‚ฌ์ดํŠธ ๋ฐ›๊ณ  ๊ฐ‘๋‹ˆ๋‹ค :D
@@ -1,4 +1,4 @@ -import { EstimationInputDTO, Estimation } from '#estimations/estimation.types.js'; +import { EstimationInputDTO, Estimation, IsActivate } from '#estimations/estimation.types.js'; import { IEstimationRepository } from '#estimations/interfaces/estimation.repository.interface.js'; import { PrismaService } from '#global/prisma.service.js'; import { FindOptions } from '#types/options.type.js'; @@ -88,4 +88,52 @@ export class EstimationRepository implements IEstimationRepository { take: pageSize, }); } + + // confirmedForId ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด์€ ์ด์‚ฌํ™•์ • ๋๋‚œ๊ฒƒ์ด๋‹ค..~! + async findReviewable(userId: string, moveInfoIds: string[], page: number, pageSize: number) { + const estimations = await this.prisma.estimation.findMany({ + where: { + confirmedForId: { in: moveInfoIds }, // ์™„๋ฃŒ๋œ ์ด์‚ฌ์™€ ์—ฐ๊ฒฐ๋œ ๊ฒฌ์ ์„ ์กฐํšŒ + }, + include: { + driver: true, // ๊ฒฌ์ ๊ณผ ๊ด€๋ จ๋œ ๋“œ๋ผ์ด๋ฒ„ ์ •๋ณด + moveInfo: true, // ๊ฒฌ์ ๊ณผ ๊ด€๋ จ๋œ ์ด์‚ฌ ์ •๋ณด + reviews: true, // ๊ฒฌ์ ์— ์ž‘์„ฑ๋œ ๋ฆฌ๋ทฐ๋“ค + }, + skip: (page - 1) * pageSize, + take: pageSize, + }); + + // ๋ฆฌ๋ทฐ๊ฐ€ ์—†๋Š” ๊ฒฌ์ ๋งŒ ํ•„ํ„ฐ๋ง + return estimations.filter(estimation => !estimation.reviews.some(review => review.ownerId === userId)); + } + + // ์œ ์ €์˜ ์ด์‚ฌ ์ •๋ณด ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ + async getUserMoveInfos(userId: string) { + return this.prisma.moveInfo.findMany({ + where: { ownerId: userId }, // ๋กœ๊ทธ์ธํ•œ ์œ ์ €์˜ ์ด์‚ฌ๋งŒ ์กฐํšŒํ•˜๊ธฐ + select: { id: true }, // ์ด์‚ฌ ID๋งŒ ์„ ํƒ + }); + } + + // ์ง€์ • ๊ฒฌ์  ์š”์ฒญ ์—ฌ๋ถ€ + async isDesignatedRequest(estimationId: string): Promise<IsActivate> { + const estimation = await this.prisma.estimation.findUnique({ + where: { id: estimationId }, + include: { + moveInfo: { + include: { + requests: { + where: { + status: 'APPLY', // + }, + }, + }, + }, + }, + }); + + // ์ง€์ •์š”์ฒญ์ด๋ฉด 'Active', ์ผ๋ฐ˜์š”์ฒญ์ด๋ฉด 'Inactive' + return estimation?.moveInfo.requests.length > 0 ? IsActivate.Active : IsActivate.Inactive; + } }
TypeScript
์•„๋งˆ๋„ javascript ๋ ˆ๋ฒจ์ด ์•„๋‹ˆ๋ผ prisma์—์„œ findMany ํ•ด์˜ค๋Š” ๋‹จ๊ณ„์—์„œ ์ด ์ž‘์—…์„ ํ•ด์„œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ํ•œ ๋ฒˆ ์ฐพ์•„๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -1,4 +1,4 @@ -import { EstimationInputDTO, Estimation } from '#estimations/estimation.types.js'; +import { EstimationInputDTO, Estimation, IsActivate } from '#estimations/estimation.types.js'; import { IEstimationRepository } from '#estimations/interfaces/estimation.repository.interface.js'; import { PrismaService } from '#global/prisma.service.js'; import { FindOptions } from '#types/options.type.js'; @@ -88,4 +88,52 @@ export class EstimationRepository implements IEstimationRepository { take: pageSize, }); } + + // confirmedForId ๊ฐ’์ด null์ด ์•„๋‹ˆ๋ฉด์€ ์ด์‚ฌํ™•์ • ๋๋‚œ๊ฒƒ์ด๋‹ค..~! + async findReviewable(userId: string, moveInfoIds: string[], page: number, pageSize: number) { + const estimations = await this.prisma.estimation.findMany({ + where: { + confirmedForId: { in: moveInfoIds }, // ์™„๋ฃŒ๋œ ์ด์‚ฌ์™€ ์—ฐ๊ฒฐ๋œ ๊ฒฌ์ ์„ ์กฐํšŒ + }, + include: { + driver: true, // ๊ฒฌ์ ๊ณผ ๊ด€๋ จ๋œ ๋“œ๋ผ์ด๋ฒ„ ์ •๋ณด + moveInfo: true, // ๊ฒฌ์ ๊ณผ ๊ด€๋ จ๋œ ์ด์‚ฌ ์ •๋ณด + reviews: true, // ๊ฒฌ์ ์— ์ž‘์„ฑ๋œ ๋ฆฌ๋ทฐ๋“ค + }, + skip: (page - 1) * pageSize, + take: pageSize, + }); + + // ๋ฆฌ๋ทฐ๊ฐ€ ์—†๋Š” ๊ฒฌ์ ๋งŒ ํ•„ํ„ฐ๋ง + return estimations.filter(estimation => !estimation.reviews.some(review => review.ownerId === userId)); + } + + // ์œ ์ €์˜ ์ด์‚ฌ ์ •๋ณด ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ + async getUserMoveInfos(userId: string) { + return this.prisma.moveInfo.findMany({ + where: { ownerId: userId }, // ๋กœ๊ทธ์ธํ•œ ์œ ์ €์˜ ์ด์‚ฌ๋งŒ ์กฐํšŒํ•˜๊ธฐ + select: { id: true }, // ์ด์‚ฌ ID๋งŒ ์„ ํƒ + }); + } + + // ์ง€์ • ๊ฒฌ์  ์š”์ฒญ ์—ฌ๋ถ€ + async isDesignatedRequest(estimationId: string): Promise<IsActivate> { + const estimation = await this.prisma.estimation.findUnique({ + where: { id: estimationId }, + include: { + moveInfo: { + include: { + requests: { + where: { + status: 'APPLY', // + }, + }, + }, + }, + }, + }); + + // ์ง€์ •์š”์ฒญ์ด๋ฉด 'Active', ์ผ๋ฐ˜์š”์ฒญ์ด๋ฉด 'Inactive' + return estimation?.moveInfo.requests.length > 0 ? IsActivate.Active : IsActivate.Inactive; + } }
TypeScript
๋„ค!! ์ฐพ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค !! ํƒœ์˜๋‹˜ ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!!!
@@ -0,0 +1,8 @@ +import "./css/reset.css"; +import "./css/common.css"; +import App from "./App"; + +addEventListener("DOMContentLoaded", async () => { + const app = new App(); + app.init(); +});
JavaScript
์š” ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ์•ฑ์„ ๊ตฌ๋™์‹œํ‚ค๋Š” Entry ํฌ์ธํŠธ๊ตฐ์š”! `controller`๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค? ๋ณด๋‹ค๋Š” `App`์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค๋Š” ๋А๋‚Œ์ด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,14 @@ +import { apiClient } from "./apiClient"; +import { BASE_URL } from "./constants"; + +export const fetchPopularMovies = async ({ page = 1 }) => { + const param = new URLSearchParams({ + api_key: process.env.TMDB_API_KEY, + language: "ko-KR", + page, + }); + + const response = await apiClient.get(`${BASE_URL}?${param}`); + + return response.results; +};
JavaScript
์˜ค ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,34 @@ +import { ApiError } from "./apiError"; + +export const apiClient = { + get: async (url, headers = {}) => { + return apiClient.request("GET", url, null, headers); + }, + + request: async (method, url, body = null, headers = {}) => { + const options = { + method, + headers: { + "Content-Type": "application/json", + ...headers, + }, + body: body && JSON.stringify(body), + }; + + try { + const response = await fetch(url, options); + + if (!response.ok) { + throw new ApiError(response.status); + } + + return await response.json(); + } catch (error) { + if (error instanceof ApiError) { + ApiError.handle(error); + } else { + throw error; + } + } + }, +};
JavaScript
`request()`์™€ `get()`์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ด๊ณ  ๋‚˜๋ˆˆ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -0,0 +1,31 @@ +import { ERROR_MSG } from "./constants"; + +export class ApiError extends Error { + constructor(message, details, status = -1) { + super(message); + this.details = details; + this.status = status; + } + + static handle(error) { + let errMsg; + let errDetails; + + switch (parseInt(error.message)) { + case 401: + errMsg = ERROR_MSG.INVALID_API_KEY.message; + errDetails = ERROR_MSG.INVALID_API_KEY.details; + break; + case 404: + errMsg = ERROR_MSG.INVALID_REQUEST.message; + errDetails = ERROR_MSG.INVALID_REQUEST.details; + break; + default: + errMsg = ERROR_MSG.DEFAULT.message; + errDetails = ERROR_MSG.DEFAULT.details; + break; + } + + throw new ApiError(errMsg, errDetails, error.status); + } +}
JavaScript
์˜ค ์ข‹์€ ์‹œ๋„์ž…๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,16 @@ +import { mainTitle, mainMoreButton, movieCardsList } from "./index"; + +export const mainSection = { + render() { + const element = document.createElement("section"); + element.classList.add("item-view"); + + const title = mainTitle.render(); + const itemList = movieCardsList.render(); + const moreButton = mainMoreButton.render(); + + element.append(title, itemList, moreButton); + + return element; + }, +};
JavaScript
[append()](https://developer.mozilla.org/en-US/docs/Web/API/Element/append)๋ฅผ ํ™œ์šฉํ•ด๋ณด์„ธ์š”!
@@ -0,0 +1,46 @@ +describe("API ํ…Œ์ŠคํŠธ", () => { + it("์˜ํ™” ๋ชฉ๋ก API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 20๊ฐœ์”ฉ ์˜ํ™”๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.", () => { + const baseUrl = "https://api.themoviedb.org/3/movie/popular"; + const param = new URLSearchParams({ + api_key: Cypress.env("TMDB_API_KEY"), + language: "ko-KR", + page: 1, + }); + + cy.request(`${baseUrl}?${param}`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.results).to.have.length(20); + }); + }); +}); + +describe("์˜ํ™” ๋ฆฌ๋ทฐ ์›น ํ…Œ์ŠคํŠธ", () => { + beforeEach(() => { + cy.visit("http://localhost:8080"); + }); + + it("์˜ํ™” ์นด๋“œ๋Š” ์ธ๋„ค์ผ, ๋ณ„์ , ์ œ๋ชฉ์„ ๊ฐ–๋Š”๋‹ค.", () => { + cy.get(".item-card").each(($el) => { + cy.wrap($el).within(() => { + cy.get(".item-thumbnail").should("be.visible"); + cy.get(".item-title").should("be.visible"); + cy.get(".item-score").should("be.visible"); + }); + }); + }); + + it("์˜ํ™” ๋ชฉ๋ก์€ ํ•œ ํŽ˜์ด์ง€์— 20๊ฐœ์”ฉ ๋‚˜์˜จ๋‹ค.", () => { + cy.get(".item-list li").should("have.length", 20); + }); + + it("๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 20๊ฐœ์˜ ์˜ํ™”๊ฐ€ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑ๋œ๋‹ค.", () => { + cy.get(".show-more").click(); + cy.get(".item-list li").should("have.length", 40); + }); + + it("์˜ํ™” ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋™์•ˆ ์Šค์ผˆ๋ ˆํ†ค UI๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.get(".skeleton-card").should("have.length", 20); + cy.wait(1000); + cy.get(".skeleton-card").should("have.length", 0); + }); +});
JavaScript
์˜คํ˜ธ? `exist`๋กœ ๊ฒ€์‚ฌํ•˜๋ฉด ๋ฌด์—‡์ด ๋‹ค๋ฅผ๊นŒ์š”?
@@ -0,0 +1,46 @@ +describe("API ํ…Œ์ŠคํŠธ", () => { + it("์˜ํ™” ๋ชฉ๋ก API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 20๊ฐœ์”ฉ ์˜ํ™”๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.", () => { + const baseUrl = "https://api.themoviedb.org/3/movie/popular"; + const param = new URLSearchParams({ + api_key: Cypress.env("TMDB_API_KEY"), + language: "ko-KR", + page: 1, + }); + + cy.request(`${baseUrl}?${param}`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.results).to.have.length(20); + }); + }); +}); + +describe("์˜ํ™” ๋ฆฌ๋ทฐ ์›น ํ…Œ์ŠคํŠธ", () => { + beforeEach(() => { + cy.visit("http://localhost:8080"); + }); + + it("์˜ํ™” ์นด๋“œ๋Š” ์ธ๋„ค์ผ, ๋ณ„์ , ์ œ๋ชฉ์„ ๊ฐ–๋Š”๋‹ค.", () => { + cy.get(".item-card").each(($el) => { + cy.wrap($el).within(() => { + cy.get(".item-thumbnail").should("be.visible"); + cy.get(".item-title").should("be.visible"); + cy.get(".item-score").should("be.visible"); + }); + }); + }); + + it("์˜ํ™” ๋ชฉ๋ก์€ ํ•œ ํŽ˜์ด์ง€์— 20๊ฐœ์”ฉ ๋‚˜์˜จ๋‹ค.", () => { + cy.get(".item-list li").should("have.length", 20); + }); + + it("๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 20๊ฐœ์˜ ์˜ํ™”๊ฐ€ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑ๋œ๋‹ค.", () => { + cy.get(".show-more").click(); + cy.get(".item-list li").should("have.length", 40); + }); + + it("์˜ํ™” ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋™์•ˆ ์Šค์ผˆ๋ ˆํ†ค UI๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.get(".skeleton-card").should("have.length", 20); + cy.wait(1000); + cy.get(".skeleton-card").should("have.length", 0); + }); +});
JavaScript
40์œผ๋กœ ๊ฒ€์‚ฌํ•˜๋Š” ์ด์œ ๋Š” ์ดˆ๊ธฐ ๋ฆฌ์ŠคํŠธ 20 + ์ถ”๊ฐ€ ๋”๋ณด๊ธฐ 20์ผ๊นŒ์š”?
@@ -0,0 +1,34 @@ +import { ApiError } from "./apiError"; + +export const apiClient = { + get: async (url, headers = {}) => { + return apiClient.request("GET", url, null, headers); + }, + + request: async (method, url, body = null, headers = {}) => { + const options = { + method, + headers: { + "Content-Type": "application/json", + ...headers, + }, + body: body && JSON.stringify(body), + }; + + try { + const response = await fetch(url, options); + + if (!response.ok) { + throw new ApiError(response.status); + } + + return await response.json(); + } catch (error) { + if (error instanceof ApiError) { + ApiError.handle(error); + } else { + throw error; + } + } + }, +};
JavaScript
`request()`๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” method๋ฅผ ํ†ตํ•ด API ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ด๊ณ , `get()`์€ ํ•ด๋‹น `request()` ํ•จ์ˆ˜์— `GET`์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ณผ์ œ์—์„œ GET ์š”์ฒญ๋งŒ ์กด์žฌํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜์ง„ ์•Š์•˜์ง€๋งŒ ์ถ”ํ›„์— ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ ์š”์ฒญ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,46 @@ +describe("API ํ…Œ์ŠคํŠธ", () => { + it("์˜ํ™” ๋ชฉ๋ก API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 20๊ฐœ์”ฉ ์˜ํ™”๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.", () => { + const baseUrl = "https://api.themoviedb.org/3/movie/popular"; + const param = new URLSearchParams({ + api_key: Cypress.env("TMDB_API_KEY"), + language: "ko-KR", + page: 1, + }); + + cy.request(`${baseUrl}?${param}`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.results).to.have.length(20); + }); + }); +}); + +describe("์˜ํ™” ๋ฆฌ๋ทฐ ์›น ํ…Œ์ŠคํŠธ", () => { + beforeEach(() => { + cy.visit("http://localhost:8080"); + }); + + it("์˜ํ™” ์นด๋“œ๋Š” ์ธ๋„ค์ผ, ๋ณ„์ , ์ œ๋ชฉ์„ ๊ฐ–๋Š”๋‹ค.", () => { + cy.get(".item-card").each(($el) => { + cy.wrap($el).within(() => { + cy.get(".item-thumbnail").should("be.visible"); + cy.get(".item-title").should("be.visible"); + cy.get(".item-score").should("be.visible"); + }); + }); + }); + + it("์˜ํ™” ๋ชฉ๋ก์€ ํ•œ ํŽ˜์ด์ง€์— 20๊ฐœ์”ฉ ๋‚˜์˜จ๋‹ค.", () => { + cy.get(".item-list li").should("have.length", 20); + }); + + it("๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 20๊ฐœ์˜ ์˜ํ™”๊ฐ€ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑ๋œ๋‹ค.", () => { + cy.get(".show-more").click(); + cy.get(".item-list li").should("have.length", 40); + }); + + it("์˜ํ™” ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋™์•ˆ ์Šค์ผˆ๋ ˆํ†ค UI๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.get(".skeleton-card").should("have.length", 20); + cy.wait(1000); + cy.get(".skeleton-card").should("have.length", 0); + }); +});
JavaScript
์ƒ์„ฑ๋œ `movieCard`์—๋Š” ์ธ๋„ค์ผ, ๋ณ„์ , ์ œ๋ชฉ element๊ฐ€ `์กด์žฌ`ํ•œ๋‹ค๋Š” ์˜๋ฏธ์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ, ํ•ด๋‹น element๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ๋ณด์—ฌ์ง€์ง€ ์•Š๊ฑฐ๋‚˜ ์–ด๋–ค css issue๊ฐ€ ์ƒ๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์— `be.visible`๋กœ ๋Œ€์ฒดํ•˜์˜€์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,46 @@ +describe("API ํ…Œ์ŠคํŠธ", () => { + it("์˜ํ™” ๋ชฉ๋ก API๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด 20๊ฐœ์”ฉ ์˜ํ™”๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.", () => { + const baseUrl = "https://api.themoviedb.org/3/movie/popular"; + const param = new URLSearchParams({ + api_key: Cypress.env("TMDB_API_KEY"), + language: "ko-KR", + page: 1, + }); + + cy.request(`${baseUrl}?${param}`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body.results).to.have.length(20); + }); + }); +}); + +describe("์˜ํ™” ๋ฆฌ๋ทฐ ์›น ํ…Œ์ŠคํŠธ", () => { + beforeEach(() => { + cy.visit("http://localhost:8080"); + }); + + it("์˜ํ™” ์นด๋“œ๋Š” ์ธ๋„ค์ผ, ๋ณ„์ , ์ œ๋ชฉ์„ ๊ฐ–๋Š”๋‹ค.", () => { + cy.get(".item-card").each(($el) => { + cy.wrap($el).within(() => { + cy.get(".item-thumbnail").should("be.visible"); + cy.get(".item-title").should("be.visible"); + cy.get(".item-score").should("be.visible"); + }); + }); + }); + + it("์˜ํ™” ๋ชฉ๋ก์€ ํ•œ ํŽ˜์ด์ง€์— 20๊ฐœ์”ฉ ๋‚˜์˜จ๋‹ค.", () => { + cy.get(".item-list li").should("have.length", 20); + }); + + it("๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด 20๊ฐœ์˜ ์˜ํ™”๊ฐ€ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑ๋œ๋‹ค.", () => { + cy.get(".show-more").click(); + cy.get(".item-list li").should("have.length", 40); + }); + + it("์˜ํ™” ๋ชฉ๋ก์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋™์•ˆ ์Šค์ผˆ๋ ˆํ†ค UI๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.get(".skeleton-card").should("have.length", 20); + cy.wait(1000); + cy.get(".skeleton-card").should("have.length", 0); + }); +});
JavaScript
๋„ค ๋งž์Šต๋‹ˆ๋‹ค! ์ด์ „ ํ…Œ์ŠคํŠธ์—์„œ 20๊ฐœ๊ฐ€ ๋ Œ๋”๋ง์ด ๋˜์—ˆ๊ณ  ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ํ›„์—, 20๊ฐœ๊ฐ€ ๋” ์ถ”๊ฐ€๋˜์–ด 40๊ฐœ๋ฅผ ๊ฒ€์‚ฌํ•˜๊ฒŒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กญ๊ฒŒ ๋ Œ๋”๋ง๋œ 20๊ฐœ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋” ์˜ฌ๋ฐ”๋ฅธ ๊ฒ€์‚ฌ์ผ๊นŒ์š”? +) ์ถ”๊ฐ€๋กœ, ๋‹จ์œ„ํ…Œ์ŠคํŠธ์—์„œ๋Š” ์ƒ˜ํ”Œ ๊ฐ’์„ ํ†ตํ•ด ๋„์ถœ๋˜๋Š” ๊ฐ’์„ ๊ฒ€์‚ฌํ•˜๋Š” ๊ณผ์ •์ด ํ…Œ์ŠคํŠธ์˜ ํ•ต์‹ฌ์ด์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ, ์ด๋ฒˆ์— e2e์—์„œ๋Š” ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐฉํ–ฅ๋ณด๋‹ค ๋ Œ๋”๋ง๋˜๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋‚˜ ์ƒ์„ฑ ์—ฌ๋ถ€๋ฅผ ์ค‘์‹ฌ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ณผ์ •์—์„œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋Š” ๊ฐ’ ์ค‘์‹ฌ, e2e๋Š” ์ „์ฒด์ ์ธ DOM ๋ Œ๋”๋ง ์ค‘์‹ฌ ์ด๋ ‡๊ฒŒ ์ž์—ฐ์Šค๋ ˆ ์ƒ๊ฐ์ด ์ด์–ด์ง€๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์ด๊ฒŒ ๋งž๋Š” ์ƒ๊ฐ์ผ๊นŒ ์˜๊ตฌ์‹ฌ์ด ๋“ค๊ธด ํ•ฉ๋‹ˆ๋‹ค,,!
@@ -0,0 +1,40 @@ +package org.sopt.androidseminar1 + +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.widget.Toast +import org.sopt.androidseminar1.databinding.SignInBinding + +class SignInActivity: AppCompatActivity() { + private lateinit var binding: SignInBinding // ๊ณ ์ •์ ์ธ Class ๊ฐ’์€ ์•„๋‹ˆ๋‹ค. + // activity_main -> ActivityMain + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์„ ๋ถˆ๋Ÿฌ์ฃผ๋Š” ์ •๋„ + setContentView(binding.root) // setContentView : xml์„ ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜ + + + // intent ๋ณ€๊ฒฝ + val goSignup: Intent = Intent(this, SignUpActivity::class.java) + val goHomeActivity: Intent = Intent(this, HomeActivity::class.java ) + + // ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ + binding.btnLogin.setOnClickListener { + if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){ + Toast.makeText(this, "๋กœ๊ทธ์ธ ์‹คํŒจ", Toast.LENGTH_SHORT).show() + } + else { + startActivity(goHomeActivity) + } + } + + // ํšŒ์›๊ฐ€์ž… + binding.btnSignup.setOnClickListener{ + startActivity(goSignup) + } + + setContentView(binding.root) + } +} \ No newline at end of file
Kotlin
์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ํƒ€์ž… ์ถ”๋ก  ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ด์ฃผ๊ธฐ๋•Œ๋ฌธ์— ```suggestion val goSignup = Intent(this, SignUpActivity::class.java) val goHomeActivity = Intent(this, HomeActivity::class.java ) ``` ์ด๋ ‡๊ฒŒ๋งŒ ์ž‘์„ฑํ•˜์…”๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹น~:)
@@ -0,0 +1,40 @@ +package org.sopt.androidseminar1 + +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.widget.Toast +import org.sopt.androidseminar1.databinding.SignInBinding + +class SignInActivity: AppCompatActivity() { + private lateinit var binding: SignInBinding // ๊ณ ์ •์ ์ธ Class ๊ฐ’์€ ์•„๋‹ˆ๋‹ค. + // activity_main -> ActivityMain + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์„ ๋ถˆ๋Ÿฌ์ฃผ๋Š” ์ •๋„ + setContentView(binding.root) // setContentView : xml์„ ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜ + + + // intent ๋ณ€๊ฒฝ + val goSignup: Intent = Intent(this, SignUpActivity::class.java) + val goHomeActivity: Intent = Intent(this, HomeActivity::class.java ) + + // ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ + binding.btnLogin.setOnClickListener { + if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){ + Toast.makeText(this, "๋กœ๊ทธ์ธ ์‹คํŒจ", Toast.LENGTH_SHORT).show() + } + else { + startActivity(goHomeActivity) + } + } + + // ํšŒ์›๊ฐ€์ž… + binding.btnSignup.setOnClickListener{ + startActivity(goSignup) + } + + setContentView(binding.root) + } +} \ No newline at end of file
Kotlin
์—ฌ๊ธฐ์„œ toString()์˜ ๊ฒฝ์šฐ ๋นผ์ฃผ์…”๋„ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค~!!
@@ -0,0 +1,40 @@ +package org.sopt.androidseminar1 + +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.widget.Toast +import org.sopt.androidseminar1.databinding.SignInBinding + +class SignInActivity: AppCompatActivity() { + private lateinit var binding: SignInBinding // ๊ณ ์ •์ ์ธ Class ๊ฐ’์€ ์•„๋‹ˆ๋‹ค. + // activity_main -> ActivityMain + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์„ ๋ถˆ๋Ÿฌ์ฃผ๋Š” ์ •๋„ + setContentView(binding.root) // setContentView : xml์„ ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜ + + + // intent ๋ณ€๊ฒฝ + val goSignup: Intent = Intent(this, SignUpActivity::class.java) + val goHomeActivity: Intent = Intent(this, HomeActivity::class.java ) + + // ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ + binding.btnLogin.setOnClickListener { + if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){ + Toast.makeText(this, "๋กœ๊ทธ์ธ ์‹คํŒจ", Toast.LENGTH_SHORT).show() + } + else { + startActivity(goHomeActivity) + } + } + + // ํšŒ์›๊ฐ€์ž… + binding.btnSignup.setOnClickListener{ + startActivity(goSignup) + } + + setContentView(binding.root) + } +} \ No newline at end of file
Kotlin
ํ˜„์žฌ setContentView ํ•จ์ˆ˜๊ฐ€ ๋‘๋ฒˆ ํ˜ธ์ถœ๋˜๊ณ  ์žˆ๋Š”๋ฐ ํ•˜๋‚˜๋Š” ์ง€์›Œ์ฃผ์…”๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์•„์š”~!!
@@ -0,0 +1,40 @@ +package org.sopt.androidseminar1 + +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.widget.Toast +import org.sopt.androidseminar1.databinding.SignInBinding + +class SignInActivity: AppCompatActivity() { + private lateinit var binding: SignInBinding // ๊ณ ์ •์ ์ธ Class ๊ฐ’์€ ์•„๋‹ˆ๋‹ค. + // activity_main -> ActivityMain + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = SignInBinding.inflate(layoutInflater) // layoutInflater : xml์„ ๋ถˆ๋Ÿฌ์ฃผ๋Š” ์ •๋„ + setContentView(binding.root) // setContentView : xml์„ ๊ทธ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜ + + + // intent ๋ณ€๊ฒฝ + val goSignup: Intent = Intent(this, SignUpActivity::class.java) + val goHomeActivity: Intent = Intent(this, HomeActivity::class.java ) + + // ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ + binding.btnLogin.setOnClickListener { + if (binding.etId.text.toString().isEmpty() || binding.etPw.text.toString().isEmpty()){ + Toast.makeText(this, "๋กœ๊ทธ์ธ ์‹คํŒจ", Toast.LENGTH_SHORT).show() + } + else { + startActivity(goHomeActivity) + } + } + + // ํšŒ์›๊ฐ€์ž… + binding.btnSignup.setOnClickListener{ + startActivity(goSignup) + } + + setContentView(binding.root) + } +} \ No newline at end of file
Kotlin
๋ชจ๋“  ๋กœ์ง์„ ๋‹ค onCreate์— ๋„ฃ๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ€๋…์„ฑ, ์žฌ์‚ฌ์šฉ์„ฑ ์ธก๋ฉด์„ ๊ณ ๋ คํ•ด์„œ ํ•จ์ˆ˜ํ™”๋ฅผ ํ•ด์ฃผ์‹œ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”~!!
@@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<shape xmlns:android="http://schemas.android.com/apk/res/android"> + + <corners android:radius="5dip"/> + + <stroke android:width="2dp" + android:color="#F658A6"/> + <solid android:color="#F658A6"/> + <corners + android:topLeftRadius="12dp" + android:topRightRadius="12dp" + android:bottomLeftRadius="12dp" + android:bottomRightRadius="12dp"/> +</shape> \ No newline at end of file
Unknown
dip๋Š” dp์™€ ๊ฐ™์€ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค! ์ˆ˜์น˜๋ฅผ dp๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”~!!!
@@ -0,0 +1,126 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".HomeActivity"> + + <TextView + android:id="@+id/textView10" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="70dp" + android:text="@string/sopthub" + android:textColor="#F658A6" + android:textSize="40sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + + <TextView + android:id="@+id/textView11" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="20dp" + android:layout_marginTop="40dp" + android:text="@string/kimdaeho" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.059" + app:layout_constraintStart_toEndOf="@+id/textView15" + app:layout_constraintTop_toBottomOf="@+id/imageView2" /> + + <TextView + android:id="@+id/textView12" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/old" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.193" + app:layout_constraintStart_toEndOf="@+id/textView16" + app:layout_constraintTop_toTopOf="@+id/textView16" /> + + <TextView + android:id="@+id/textView14" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="60dp" + android:text="@string/introduce" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.453" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/textView13" /> + + <TextView + android:id="@+id/textView13" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/entj" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.154" + app:layout_constraintStart_toEndOf="@+id/textView17" + app:layout_constraintTop_toTopOf="@+id/textView17" /> + + <ImageView + android:id="@+id/imageView2" + android:layout_width="119dp" + android:layout_height="147dp" + android:layout_marginTop="30dp" + app:layout_constraintEnd_toEndOf="@+id/textView10" + app:layout_constraintHorizontal_bias="0.491" + app:layout_constraintStart_toStartOf="@+id/textView10" + app:layout_constraintTop_toBottomOf="@+id/textView10" + app:srcCompat="@drawable/kimdaeho" /> + + <TextView + android:id="@+id/textView15" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="150dp" + android:text="@string/name" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="@+id/textView11" /> + + <TextView + android:id="@+id/textView16" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="40dp" + android:text="@string/age" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintStart_toStartOf="@+id/textView15" + app:layout_constraintTop_toBottomOf="@+id/textView15" /> + + <TextView + android:id="@+id/textView17" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="40dp" + android:text="@string/mbti" + android:textColor="@color/black" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintStart_toStartOf="@+id/textView16" + app:layout_constraintTop_toBottomOf="@+id/textView16" /> + + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file
Unknown
View ์ปดํฌ๋„ŒํŠธ์˜ id ๊ฐ’์˜ ๋„ค์ด๋ฐ์€ ์ข€ ๋” ๊ธฐ๋Šฅ์— ๋งž๊ฒŒ~~ ๊ตฌ์ฒด์ ์œผ๋กœ ๋„ค์ด๋ฐ ํ•˜๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”~!!
@@ -0,0 +1,134 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".SignInActivity"> + + <!-- + text size -> sp + hardcoded text -> @values ํด๋”๋กœ text ์˜ฎ๊ธฐ๊ธฐ + @values ์—๋‹ค๊ฐ€ color ์˜ฎ๊ธฐ๊ธฐ + amulator ์— action bar ์ œ๊ฑฐ + ์Šคํ…Œ์ด๋”์Šค๋ฐ” ์ƒ‰๊น” ๋ณ€๊ฒฝํ•ด์•ผํ•จ -> @themas--> + <TextView + android:id="@+id/textView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="70dp" + android:text="@string/sopthub" + android:textColor="@color/pink" + android:textSize="40sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + <TextView + android:id="@+id/textView2" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="40dp" + android:layout_marginTop="40dp" + android:text="@string/id" + android:textColor="@color/black" + android:textSize="20sp" + android:textStyle="bold" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/textView4" /> + + <EditText + android:id="@+id/et_id" + android:layout_width="0dp" + android:layout_height="50dp" + android:layout_marginTop="8dp" + android:layout_marginEnd="40dp" + android:background="@drawable/round_blank_edge" + android:ems="10" + android:inputType="textPersonName" + android:paddingStart="15dp" + android:hint="@string/input_id" + android:textColor="@color/gray" + android:textSize="15sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.0" + app:layout_constraintStart_toStartOf="@+id/textView2" + app:layout_constraintTop_toBottomOf="@+id/textView2" /> + + <TextView + android:id="@+id/textView3" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="20dp" + android:text="@string/password" + android:textColor="@color/black" + android:textSize="20sp" + android:textStyle="bold" + app:layout_constraintStart_toStartOf="@+id/et_id" + app:layout_constraintTop_toBottomOf="@+id/et_id" /> + + <EditText + android:id="@+id/et_pw" + android:layout_width="0dp" + android:layout_height="50dp" + android:layout_marginTop="10dp" + android:layout_marginEnd="40dp" + android:background="@drawable/round_blank_edge" + android:ems="10" + android:inputType="textPassword" + android:minHeight="48dp" + android:paddingStart="15dp" + android:hint="@string/input_password" + android:textColor="@color/gray" + android:textSize="15sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="@+id/textView3" + app:layout_constraintTop_toBottomOf="@+id/textView3" /> + + <Button + android:id="@+id/btn_login" + android:layout_width="0dp" + android:layout_height="50dp" + android:layout_marginBottom="120dp" + android:background="@drawable/round_full_edge" + android:text="@string/login" + android:textColor="@color/white" + android:textSize="15sp" + android:textStyle="bold" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="@+id/et_pw" + app:layout_constraintHorizontal_bias="1.0" + app:layout_constraintStart_toStartOf="@+id/et_pw" + app:layout_constraintTop_toBottomOf="@+id/et_pw" /> + + + <Button + android:id="@+id/btn_signup" + android:layout_width="0dp" + android:layout_height="50dp" + android:background="@color/white" + android:text="@string/signup" + android:textColor="@color/gray" + android:textSize="15sp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="@+id/btn_login" + app:layout_constraintStart_toStartOf="@+id/btn_login" + app:layout_constraintTop_toBottomOf="@+id/btn_login" + app:layout_constraintVertical_bias="0.0" /> + + <TextView + android:id="@+id/textView4" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="10dp" + android:text="@string/login" + android:textColor="@color/black" + android:textSize="25sp" + android:textStyle="bold" + app:layout_constraintEnd_toEndOf="@+id/textView" + app:layout_constraintHorizontal_bias="0.486" + app:layout_constraintStart_toStartOf="@+id/textView" + app:layout_constraintTop_toBottomOf="@+id/textView" /> + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file
Unknown
๊ณ ์ • dp๋ฅผ ๋†’์ด๋‚˜ ๋„ˆ๋น„๊ฐ’์œผ๋กœ ์ค„ ๊ฒฝ์šฐ ํœด๋Œ€ํฐ ๊ธฐ์ข…์— ๋”ฐ๋ผ ๋‚ด์šฉ์ด ์ž˜๋ฆด์ˆ˜๋„์žˆ๊ณ , ๋ชจ์–‘์ด ์ด์ƒํ•˜๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค~ ํ˜น์‹œ ๊ณ ์ • dp๋ฅผ ๋†’์ด ๊ฐ’์œผ๋กœ ์ฃผ์‹  ํŠน๋ณ„ํ•œ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”~๐Ÿ‘€๐Ÿ‘€
@@ -3,8 +3,10 @@ import { ParsedUrlQuery } from "querystring"; import { GetServerSideProps } from "next"; import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; +import { getReviewStat, getReviews } from "@/lib/api/ReviewService"; import { getTrainerInfo } from "@/lib/api/trainerService"; import { getFavorite } from "@/lib/api/userService"; +import { ReviewResult } from "@/types/reviews"; import FindTrainerCard from "@/components/Cards/FindTrainerCard"; import { HorizontalLine } from "@/components/Common/Line"; import Loading from "@/components/Common/Loading"; @@ -38,6 +40,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => { export default function DetailTrainer({ trainerId }: { trainerId: string | null }) { const [currentPage, setCurrentPage] = useState<number>(1); + const pageSize = 3; const { data, isLoading, isError } = useQuery( ["trainer-detail", trainerId], () => getTrainerInfo(trainerId as string), @@ -46,22 +49,42 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null }, ); + const { data: reviewStat } = useQuery(["review-stat"], () => getReviewStat(trainerId as string), { + enabled: !!trainerId, + cacheTime: 5 * 60 * 1000, + staleTime: 5 * 60 * 1000, + }); + const { + data: reviewList, + isLoading: isReviewLoading, + isError: isReviewError, + } = useQuery<ReviewResult>( + ["reviews", currentPage], + () => getReviews(trainerId as string, { page: currentPage, limit: pageSize }), + { + enabled: !!trainerId, + keepPreviousData: true, + }, + ); + const { data: favoriteInfo, isLoading: isFavoriteLoading, isError: isFavoriteError, } = useQuery(["favorite"], () => getFavorite(trainerId as string), { enabled: !!trainerId }); - if (isError || isFavoriteError) return <div>error!!</div>; + if (isError || isReviewError || isFavoriteError) return <div>error!!</div>; const trainerInfo = data?.profile; + const reviews = reviewList?.reviews || []; + const totalCount = reviewList?.totalCount || 0; return ( <div className={clsx( "relative flex flex-col justify-between gap-16 m-auto mt-[2.4rem] mb-16 px-8", "pc:flex-row pc:gap-0 pc:mt-[5.6rem] pc:max-w-[144rem]", - "tablet:max-w-[74.4rem] mobile:max-w-[37.5rem]", + "tablet:max-w-[74.4rem] mobile:max-w-auto", )} > <div className={"flex flex-col gap-[2.4rem] w-full pc:gap-16 pc:pr-[10rem]"}> @@ -73,8 +96,12 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null <HorizontalLine width="100%" /> <TrainerInfo profile={trainerInfo} /> {/* ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ api ์—ฐ๊ฒฐํ•ด์•ผํ•จ */} - <TrainerReview reviewList={trainerInfo} /> - <Pagination currentPage={currentPage} totalPages={5} onPageChange={setCurrentPage} /> + <TrainerReview reviewList={reviews} reviewStat={reviewStat} totalCount={totalCount} /> + <Pagination + currentPage={currentPage} + totalPages={Math.ceil(totalCount / pageSize)} + onPageChange={setCurrentPage} + /> </div> <div className="flex flex-col gap-[2.4rem] pc:gap-16"> <TrainerControl profile={trainerInfo} /> @@ -83,7 +110,7 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null <ShareSNS label="๋‚˜๋งŒ ์•Œ๊ธฐ์—” ์•„์‰ฌ์šด ๊ฐ•์‚ฌ๋‹˜์ธ๊ฐ€์š”?" trainerInfo={trainerInfo} /> </div> </div> - {isLoading || (isFavoriteLoading && <Loading />)} + {(isLoading || isReviewLoading || isFavoriteLoading) && <Loading />} </div> ); }
Unknown
```js useQuery(["review-stat", trainerId], () => getReviewStat(trainerId as string), { enabled: !!trainerId, cacheTime: 5 * 60 * 1000, staleTime: 5 * 60 * 1000 }); ``` ๋กœ ํ•ด์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์•„๋ณด์—ฌ์š”.
@@ -3,8 +3,10 @@ import { ParsedUrlQuery } from "querystring"; import { GetServerSideProps } from "next"; import { useState } from "react"; import { useQuery } from "@tanstack/react-query"; +import { getReviewStat, getReviews } from "@/lib/api/ReviewService"; import { getTrainerInfo } from "@/lib/api/trainerService"; import { getFavorite } from "@/lib/api/userService"; +import { ReviewResult } from "@/types/reviews"; import FindTrainerCard from "@/components/Cards/FindTrainerCard"; import { HorizontalLine } from "@/components/Common/Line"; import Loading from "@/components/Common/Loading"; @@ -38,6 +40,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => { export default function DetailTrainer({ trainerId }: { trainerId: string | null }) { const [currentPage, setCurrentPage] = useState<number>(1); + const pageSize = 3; const { data, isLoading, isError } = useQuery( ["trainer-detail", trainerId], () => getTrainerInfo(trainerId as string), @@ -46,22 +49,42 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null }, ); + const { data: reviewStat } = useQuery(["review-stat"], () => getReviewStat(trainerId as string), { + enabled: !!trainerId, + cacheTime: 5 * 60 * 1000, + staleTime: 5 * 60 * 1000, + }); + const { + data: reviewList, + isLoading: isReviewLoading, + isError: isReviewError, + } = useQuery<ReviewResult>( + ["reviews", currentPage], + () => getReviews(trainerId as string, { page: currentPage, limit: pageSize }), + { + enabled: !!trainerId, + keepPreviousData: true, + }, + ); + const { data: favoriteInfo, isLoading: isFavoriteLoading, isError: isFavoriteError, } = useQuery(["favorite"], () => getFavorite(trainerId as string), { enabled: !!trainerId }); - if (isError || isFavoriteError) return <div>error!!</div>; + if (isError || isReviewError || isFavoriteError) return <div>error!!</div>; const trainerInfo = data?.profile; + const reviews = reviewList?.reviews || []; + const totalCount = reviewList?.totalCount || 0; return ( <div className={clsx( "relative flex flex-col justify-between gap-16 m-auto mt-[2.4rem] mb-16 px-8", "pc:flex-row pc:gap-0 pc:mt-[5.6rem] pc:max-w-[144rem]", - "tablet:max-w-[74.4rem] mobile:max-w-[37.5rem]", + "tablet:max-w-[74.4rem] mobile:max-w-auto", )} > <div className={"flex flex-col gap-[2.4rem] w-full pc:gap-16 pc:pr-[10rem]"}> @@ -73,8 +96,12 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null <HorizontalLine width="100%" /> <TrainerInfo profile={trainerInfo} /> {/* ๋ฆฌ๋ทฐ ๋ฆฌ์ŠคํŠธ api ์—ฐ๊ฒฐํ•ด์•ผํ•จ */} - <TrainerReview reviewList={trainerInfo} /> - <Pagination currentPage={currentPage} totalPages={5} onPageChange={setCurrentPage} /> + <TrainerReview reviewList={reviews} reviewStat={reviewStat} totalCount={totalCount} /> + <Pagination + currentPage={currentPage} + totalPages={Math.ceil(totalCount / pageSize)} + onPageChange={setCurrentPage} + /> </div> <div className="flex flex-col gap-[2.4rem] pc:gap-16"> <TrainerControl profile={trainerInfo} /> @@ -83,7 +110,7 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null <ShareSNS label="๋‚˜๋งŒ ์•Œ๊ธฐ์—” ์•„์‰ฌ์šด ๊ฐ•์‚ฌ๋‹˜์ธ๊ฐ€์š”?" trainerInfo={trainerInfo} /> </div> </div> - {isLoading || (isFavoriteLoading && <Loading />)} + {(isLoading || isReviewLoading || isFavoriteLoading) && <Loading />} </div> ); }
Unknown
๋„ต ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -1,11 +1,29 @@ import { useState } from "react"; +import Header from "./components/Header/Header"; +import Product from "./pages/Product"; +import GlobalStyles from "./styles/Global.style"; +import Toast from "./components/common/Toast/Toast"; +import { QuantityContext } from "./store/QuantityContext"; +import { createPortal } from "react-dom"; function App() { - const [count, setCount] = useState(0); - + const [quantity, setQuantity] = useState(0); + const [showToast, setShowToast] = useState(false); + const [toastMessage, setToastMessage] = useState(""); + const handleError = (message: string) => { + setToastMessage(message); + setShowToast(true); + setTimeout(() => setShowToast(false), 3000); + }; return ( <> - <h1>React Shopping Products</h1> + <GlobalStyles /> + {showToast && + createPortal(<Toast message={toastMessage} />, document.body)} + <QuantityContext.Provider value={{ quantity, setQuantity }}> + <Header /> + <Product onError={handleError} /> + </QuantityContext.Provider> </> ); }
Unknown
์—ฌ๊ธฐ ์ด `showToast` ์ƒํƒœ๋ฅผ ํ† ์ŠคํŠธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ฑ…์ž„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ์‹ถ์—ˆ๋Š”๋ฐ, ๊ทธ๋ ‡๋‹ค๋ฉด createPortal์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜๋‚˜์š”..? createPortal์„ ์‚ฌ์šฉํ•ด๋ณด์ง€ ์•Š์•„์„œ ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ๋ฏธ๋ฆฌ ์—ด์–ด๋‘˜ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ทธ์ชฝ์œผ๋กœ ์ƒํƒœ๋ฅผ ์˜ฎ๊ฒจ๋ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,52 @@ +import { generateBasicToken } from "../util/auth"; +import { CART_ITEMS_ENDPOINT } from "./config"; + +const USER_PASSWORD = import.meta.env.VITE_USER_PASSWORD; +const USER_ID = import.meta.env.VITE_USER_ID; +const token = generateBasicToken(USER_ID, USER_PASSWORD); + +export async function requestFetchCartItemList() { + const response = await fetch(`${CART_ITEMS_ENDPOINT}?size=2000`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: token, + }, + }); + + if (!response.ok) { + throw new Error("Failed to fetch cart item list"); + } + + const data = await response.json(); + return data; +} + +export async function requestAddCartItem(productId: number, quantity: number) { + const response = await fetch(`${CART_ITEMS_ENDPOINT}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: token, + }, + body: JSON.stringify({ productId, quantity }), + }); + + if (!response.ok) { + throw new Error("Failed to add cart item"); + } +} + +export async function requestDeleteCartItem(cartItemId: number | undefined) { + const response = await fetch(`${CART_ITEMS_ENDPOINT}/${cartItemId}`, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: token, + }, + }); + + if (!response.ok) { + throw new Error("Failed to delete cart item"); + } +}
TypeScript
ํ•„์ˆ˜๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋ฐ˜๋ณต๋˜๋Š” fetch ๋กœ์ง๋“ค์€ ์ถ”์ƒํ™”ํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!ใ…Žใ…Ž
@@ -0,0 +1,52 @@ +import { generateBasicToken } from "../util/auth"; +import { CART_ITEMS_ENDPOINT } from "./config"; + +const USER_PASSWORD = import.meta.env.VITE_USER_PASSWORD; +const USER_ID = import.meta.env.VITE_USER_ID; +const token = generateBasicToken(USER_ID, USER_PASSWORD); + +export async function requestFetchCartItemList() { + const response = await fetch(`${CART_ITEMS_ENDPOINT}?size=2000`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: token, + }, + }); + + if (!response.ok) { + throw new Error("Failed to fetch cart item list"); + } + + const data = await response.json(); + return data; +} + +export async function requestAddCartItem(productId: number, quantity: number) { + const response = await fetch(`${CART_ITEMS_ENDPOINT}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: token, + }, + body: JSON.stringify({ productId, quantity }), + }); + + if (!response.ok) { + throw new Error("Failed to add cart item"); + } +} + +export async function requestDeleteCartItem(cartItemId: number | undefined) { + const response = await fetch(`${CART_ITEMS_ENDPOINT}/${cartItemId}`, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: token, + }, + }); + + if (!response.ok) { + throw new Error("Failed to delete cart item"); + } +}
TypeScript
chat GPTํ•œํ…Œ '์ด ์ฝ”๋“œ ์ถ”์ƒํ™” ํ•ด๋ด'ํ•˜๋ฉด ๊ธฐ๊ฐ€๋ง‰ํžˆ๊ฒŒ ํ•ด์ฃผ๋”๋ผ๊ตฌ์š”
@@ -0,0 +1,10 @@ +import { generateBasicToken } from "../util/auth"; + +const API_URL = import.meta.env.VITE_API_URL; + +export const PRODUCTS_ENDPOINT = `${API_URL}/products`; +export const CART_ITEMS_ENDPOINT = `${API_URL}/cart-items`; + +export const USER_PASSWORD = import.meta.env.VITE_USER_PASSWORD; +export const USER_ID = import.meta.env.VITE_USER_ID; +export const token = generateBasicToken(USER_ID, USER_PASSWORD);
TypeScript
api์š”์ฒญ์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋“ค์ด ์ž˜ ๋ชจ์—ฌ์žˆ์–ด ๊ด€๋ฆฌํ•˜๊ธฐ ํŽธํ•  ๊ฒƒ ๊ฐ™๋„ค์š”! ๐Ÿ‘
@@ -0,0 +1,87 @@ +import { useContext, useEffect, useRef } from "react"; +import { QuantityContext } from "../../store/QuantityContext"; +import useProductList from "../../hooks/useProductList"; +import useCartItemList from "../../hooks/useCartItemList"; +import ProductItem from "../ProductItem/ProductItem"; +import * as S from "./ProductItemList.style"; +import { Category } from "../../interfaces/Product"; +import { Sorting } from "../../interfaces/Sorting"; +import useIntersectionObserver from "../../hooks/useIntersectionObserver"; +import Spinner from "../common/Spinner/Spinner"; + +interface ProductItemListProp { + category: Category; + sortOption: Sorting; + onError: (error: string) => void; +} + +function ProductItemList({ + category, + sortOption, + onError, +}: ProductItemListProp) { + const { + productList, + productListError, + productListLoading, + page, + fetchNextPage, + isLastPage, + setPage, + } = useProductList({ + category, + sortOption, + }); + const { cartItemList, isInCart, toggleCartItem, cartItemListError } = + useCartItemList(); + const target = useRef(null); + const [observe, unobserve] = useIntersectionObserver(() => { + fetchNextPage(); + }); + + useEffect(() => { + setPage(0); + }, [category, sortOption]); + + useEffect(() => { + if (page === -1 || target.current === null) return; + observe(target.current); + + const N = productList.length; + + if (0 === N || isLastPage) { + unobserve(target.current); + } + }, [productList, page, observe, unobserve]); + const quantityContext = useContext(QuantityContext); + const setQuantity = quantityContext ? quantityContext.setQuantity : () => {}; + setQuantity(cartItemList.length); + + if (productListError) { + onError("์ƒํ’ˆ ๋ชฉ๋ก ์กฐํšŒ ์‹คํŒจ"); + } + if (cartItemListError) { + onError("์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์กฐํšŒ ์‹คํŒจ"); + } + + return ( + <> + <S.ProductList> + {productList.map((product, idx) => { + return ( + <ProductItem + key={`${idx}_${product.id}`} + product={product} + isInCart={isInCart(product.id)} + toggleCartItem={() => toggleCartItem(product)} + /> + ); + })} + </S.ProductList> + <div ref={target} style={{ height: "1px" }}></div> + {productListLoading && <Spinner />} + </> + ); +} + +export default ProductItemList;
Unknown
ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ด€๋ จ ๋กœ์ง๋„ ์ปค์Šคํ…€ ํ›…์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,87 @@ +import { useContext, useEffect, useRef } from "react"; +import { QuantityContext } from "../../store/QuantityContext"; +import useProductList from "../../hooks/useProductList"; +import useCartItemList from "../../hooks/useCartItemList"; +import ProductItem from "../ProductItem/ProductItem"; +import * as S from "./ProductItemList.style"; +import { Category } from "../../interfaces/Product"; +import { Sorting } from "../../interfaces/Sorting"; +import useIntersectionObserver from "../../hooks/useIntersectionObserver"; +import Spinner from "../common/Spinner/Spinner"; + +interface ProductItemListProp { + category: Category; + sortOption: Sorting; + onError: (error: string) => void; +} + +function ProductItemList({ + category, + sortOption, + onError, +}: ProductItemListProp) { + const { + productList, + productListError, + productListLoading, + page, + fetchNextPage, + isLastPage, + setPage, + } = useProductList({ + category, + sortOption, + }); + const { cartItemList, isInCart, toggleCartItem, cartItemListError } = + useCartItemList(); + const target = useRef(null); + const [observe, unobserve] = useIntersectionObserver(() => { + fetchNextPage(); + }); + + useEffect(() => { + setPage(0); + }, [category, sortOption]); + + useEffect(() => { + if (page === -1 || target.current === null) return; + observe(target.current); + + const N = productList.length; + + if (0 === N || isLastPage) { + unobserve(target.current); + } + }, [productList, page, observe, unobserve]); + const quantityContext = useContext(QuantityContext); + const setQuantity = quantityContext ? quantityContext.setQuantity : () => {}; + setQuantity(cartItemList.length); + + if (productListError) { + onError("์ƒํ’ˆ ๋ชฉ๋ก ์กฐํšŒ ์‹คํŒจ"); + } + if (cartItemListError) { + onError("์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ชฉ๋ก ์กฐํšŒ ์‹คํŒจ"); + } + + return ( + <> + <S.ProductList> + {productList.map((product, idx) => { + return ( + <ProductItem + key={`${idx}_${product.id}`} + product={product} + isInCart={isInCart(product.id)} + toggleCartItem={() => toggleCartItem(product)} + /> + ); + })} + </S.ProductList> + <div ref={target} style={{ height: "1px" }}></div> + {productListLoading && <Spinner />} + </> + ); +} + +export default ProductItemList;
Unknown
์•„๋ฌด๋ž˜๋„ ์ด๋ฒˆ ๋ฏธ์…˜์—์„œ ๊ฐ€์žฅ ๋ฉ”์ธ์ด ๋˜๋Š”(?) ์ปดํฌ๋„ŒํŠธ์ธ ๊ฒƒ ๊ฐ™์•„์š”. ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ UI ๋กœ์ง์ด ์ข€ ๋” ์ž˜ ๋ถ„๋ฆฌ๋˜๋ฉด ๋”์šฑ ์ฝ๊ธฐ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,5 @@ +package model; + +public interface State { + int execute(int length); +}
Java
ํ™•์žฅ ๊ฐ€๋Šฅ์„ฑ์„ ์ƒ๊ฐํ•˜์—ฌ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋„ค์š”!! ๊ฐ ๋‹ค๋ฅธ ์ƒํƒœ๋งˆ๋‹ค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ Car์—์„œ ๊ฐˆ์•„๋ผ์šฐ๋Š” ๊ฒƒ๋„ ์‹ ์„ ํ•ฉ๋‹ˆ๋‹ค!! State ํŒจํ„ด ์ข‹์€ ๊ฒƒ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,13 @@ +package function; + +import java.util.Random; + +public class RandomNumberMaker { + + private static final Random random = new Random(); + private static final int RANDOM_NUMBER_RANGE = 10; + + public static int getRandomNumber() { + return random.nextInt(RANDOM_NUMBER_RANGE); + } +}
Java
getRandomNumber ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด Random๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!! ๊ทธ๋ฆฌ๊ณ  Random๊ฐ’์˜boundary๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
number๋ฅผ Car๋ฅผ ์‹๋ณ„ํ•ด์ฃผ๋Š” ๋ฉค๋ฒ„๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”!! ์ ‘๊ทผ์ง€์ •์ž์™€ final ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹ค๊นŒ์š”?
@@ -1,21 +1,18 @@ package view; -import controller.RacingGame; import model.Car; -import java.util.Collections; import java.util.List; public class ResultView { - public static void printTrialResult(List<Car> cars){ - for (Car car : cars){ - System.out.print(car.getName() + " : " + String.join("", Collections.nCopies(car.getPosition(), "*")) + "\n"); + private static final String RESULT_SYMBOL = "-"; + + public static void printAllCarsState(long id, int len) { + System.out.println("car" + id); + for (int i = 0; i < len; i++) { + System.out.print(RESULT_SYMBOL); } System.out.println(); } - - public static void printFinalWinner(RacingGame racingGame, List<Car> cars){ - System.out.print(String.join(",", racingGame.searchWinners(cars)) + "๊ฐ€ ์ตœ์ข… ์šฐ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค."); - } } \ No newline at end of file
Java
ํ˜„์žฌ ์ถœ๋ ฅํ•˜๋Š” ์šฉ๋„๋กœ ์ง€๊ธˆ --- ์ด๋ ‡๊ฒŒ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์ง€๋งŒ, ๋‚˜์ค‘์—๋Š” ๋‹ค๋ฅธ ๊ธฐํ˜ธ๋กœ ๋ฐ”๋€”์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!! ์ด ๊ธฐํ˜ธ๋“ค ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ InputView์™€ ResultView์˜ ์ž…์ถœ๋ ฅ ๋ฉ”์„ธ์ง€, ๋งค์ง๋„˜๋ฒ„๋“ค์„ ์ƒ์ˆ˜ํ™” ํ•ด์ฃผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,38 @@ +package controller; + +import function.RandomNumberMaker; +import model.Car; +import model.State; +import model.Movable; +import model.UnMovable; + +import java.util.List; + +public class CarController { + + private static final int MOVE_CONDITION = 4; + + public void registerCars(List<Car> cars, long len) { + for (int i = 0; i < len; i++) { + cars.add(new Car(i)); + } + } + + public void moveCar(List<Car> cars) { + for (Car car : cars) { + car.changeState(checkMoveOrStop(car)); + car.move(); + } + } + + private State checkMoveOrStop(Car car) { + if (getRandomNumber() >= MOVE_CONDITION) { + return new Movable(); + } + return new UnMovable(); + } + + private int getRandomNumber() { + return RandomNumberMaker.getRandomNumber(); + } +}
Java
์ „์ฒด์ ์œผ๋กœ Car๋“ค์„ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ์ œ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” Controller์˜ ์—ญํ• ๊ณผ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” Controller์˜ ์—ญํ• ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์š”์ฒญ๊ฐ’์„ ๋ฐ›์•„, ์š”์ฒญ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ณ  ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์„œ View์—๊ฒŒ ๋ณด๋‚ด์ฃผ๋Š” ์—ญํ• ์ด๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” CarController๋ณด๋‹ค Cars๊ฐ™์€ ์ด๋ฆ„์ด ๋” ์–ด์šธ๋ฆฐ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋Š”๋ฐ ํ˜„์šฐ๋‹˜์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹ค๊นŒ์š”?? ์ด ์ž๋ฃŒ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! https://tecoble.techcourse.co.kr/post/2020-05-08-First-Class-Collection/
@@ -0,0 +1,47 @@ +package engine; + +import controller.CarController; +import model.Car; +import view.InputView; +import view.ResultView; + +import java.util.ArrayList; +import java.util.List; + +public class RacingGame { + + private List<Car> cars; + private CarController carController; + private InputView inputView; + + public RacingGame() { + cars = new ArrayList<>(); + carController = new CarController(); + inputView = new InputView(); + } + + public void run() { + long carNum = inputView.inputTotalCarNum(); + int trial = inputView.inputTotalTrial(); + + register(carNum); + executeGame(trial); + printAll(cars); + } + + private void executeGame(int trial) { + for (int i = 0; i < trial; i++) { + carController.moveCar(cars); + } + } + + private void register(long carNum) { + carController.registerCars(cars, carNum); + } + + private void printAll(List<Car> cars) { + for (Car car : cars) { + ResultView.printAllCarsState(car.getCarNum(), car.moveCount()); + } + } +}
Java
InputView๋Š” ์ธ์Šคํ„ด์Šค๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ResultView๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•œ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•˜๋„ค์š”!! ์ €๋„ View๋ฅผ ๊ตฌํ˜„์„ ํ•  ๋•Œ, ๊ณ ๋ฏผ์ด ๋งŽ์ด ๋˜๋Š”๋ฐ์š”! View ๊ฐ™์€ ๊ฒฝ์šฐ์— ์ƒํƒœ๊ฐ€ ์—†์–ด์„œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„์„ ํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์ง€๋งŒ, ์œ ํ‹ธ์„ฑ ํด๋ž˜์Šค์™€๋Š” ์ƒ‰๊น”์ด ์กฐ๊ธˆ ๋‹ค๋ฅธ ๋А๋‚Œ๋„ ์žˆ์–ด์„œ์š”!! ์ •๋‹ต์€ ์—†๊ฒ ์ง€๋งŒ, ์ €๋Š” ์ด๋ฒˆ์— View ๊ตฌํ˜„์„ ํ•  ๋•Œ ์‹ฑ๊ธ€ํ„ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด๋ณด์•˜์–ด์š”!! ํ˜„์šฐ๋‹˜์ด InputView์™€ OutputView๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•œ์ ์ด ๊ถ๊ธˆํ•˜๋„ค์š”!
@@ -0,0 +1,38 @@ +package controller; + +import function.RandomNumberMaker; +import model.Car; +import model.State; +import model.Movable; +import model.UnMovable; + +import java.util.List; + +public class CarController { + + private static final int MOVE_CONDITION = 4; + + public void registerCars(List<Car> cars, long len) { + for (int i = 0; i < len; i++) { + cars.add(new Car(i)); + } + } + + public void moveCar(List<Car> cars) { + for (Car car : cars) { + car.changeState(checkMoveOrStop(car)); + car.move(); + } + } + + private State checkMoveOrStop(Car car) { + if (getRandomNumber() >= MOVE_CONDITION) { + return new Movable(); + } + return new UnMovable(); + } + + private int getRandomNumber() { + return RandomNumberMaker.getRandomNumber(); + } +}
Java
๋žœ๋ค๊ฐ’ ๋•Œ๋ฌธ์— checkMoveOrStop ๋ฉ”์„œ๋“œ๋ฅผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ ์–ด๋ ค์› ์„ ๊ฑฐ์˜ˆ์š”!! https://tecoble.techcourse.co.kr/post/2020-05-07-appropriate_method_for_test_by_parameter/ https://tecoble.techcourse.co.kr/post/2020-05-17-appropriate_method_for_test_by_interface/ ์ž๋ฃŒ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!! ํ•˜์ง€๋งŒ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ๋ฌด์กฐ๊ฑด ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค!! ์˜์‹ฌํ•˜๋ฉด์„œ ๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!! ํ˜„์šฐ๋‹˜์€ ์ƒ๊ฐ์ด ๋‹ค๋ฅด์‹ค ์ˆ˜ ๋„ ์žˆ๊ตฌ์š”!
@@ -0,0 +1,13 @@ +package function; + +import java.util.Random; + +public class RandomNumberMaker { + + private static final Random random = new Random(); + private static final int RANDOM_NUMBER_RANGE = 10; + + public static int getRandomNumber() { + return random.nextInt(RANDOM_NUMBER_RANGE); + } +}
Java
1. getRandomNumber ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด Random๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!! ๋™๊ฑด๋‹˜ ๋ง์”€๋Œ€๋กœ getRandomNumber๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋งˆ๋‹ค Random ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ญ๋‹ˆ๋‹ค Random๊ฐ์ฒด๋ฅผ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋ฉด ๋ฆฌ์†Œ์Šค ๋ถ€๋ถ„์—์„œ ๋‚ญ๋น„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!! :) 2. Random๊ฐ’์˜ boundary๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?? ์ œ๊ฐ€ ์ƒ๊ฐํ•˜๊ธฐ์—๋Š” ๋‚˜์ค‘์— 10์—์„œ ๋‹ค๋ฅธ ์ˆซ์ž๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ์€ "๋ณ€๊ฒฝ ์‚ฌํ•ญ"์„ ์ƒ๊ฐํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์ฐจ๋ฅผ ์›€์ง์ด๊ฒŒ ํ•˜๋Š” "CarController"์—์„œ ์ˆ˜์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,13 @@ +package function; + +import java.util.Random; + +public class RandomNumberMaker { + + private static final Random random = new Random(); + private static final int RANDOM_NUMBER_RANGE = 10; + + public static int getRandomNumber() { + return random.nextInt(RANDOM_NUMBER_RANGE); + } +}
Java
```java private static Random random = new Random(); private static final int RANDOM_BOUNDARY = 10; public static int getRandomNumber() { return random.nextInt(RANDOM_BOUNDARY); } ``` ์ด๋ผ๊ณ  ํ•ด์ค„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ์š”!! ๋žœ๋ค ๊ฐ’์„ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, ๋žœ๋ค๊ฐ’์˜ ๋ฒ”์œ„๋„ ๋žœ๋ค๊ฐ’์ด ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ณณ์—์„œ ์•Œ์•„์•ผ ํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ๋„ค์š”!
@@ -0,0 +1,13 @@ +package function; + +import java.util.Random; + +public class RandomNumberMaker { + + private static final Random random = new Random(); + private static final int RANDOM_NUMBER_RANGE = 10; + + public static int getRandomNumber() { + return random.nextInt(RANDOM_NUMBER_RANGE); + } +}
Java
"๋žœ๋ค๊ฐ’์˜ ๋ฒ”์œ„๋„ ๋žœ๋ค๊ฐ’์ด ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ณณ์—์„œ ์•Œ์•„์•ผ ํ•˜์ง€ ์•Š์„๊นŒ ์‹ถ๋„ค์š”" ์ด ๋ถ€๋ถ„์ด ๋งž๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋‹ˆ CarController๊ฐ€ ๋žœ๋ค๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ •ํ•œ๋‹ค๋ฉด ๊ฒฐ๊ตญ Random๊ฐ’์„ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ณณ์— ์˜์กดํ•˜๊ฒŒ ๋˜์–ด ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ง€๊ฒ ๋„ค์š”!! ๊ฒฐํ•ฉ๋„๋ฅผ ์ค„์ด๋Š” ๋ฐฉ๋ฒ• ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,5 @@ +package model; + +public interface State { + int execute(int length); +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :)
@@ -1,16 +1,54 @@ package view; +import java.io.InputStream; +import java.util.InputMismatchException; import java.util.Scanner; -public class InputView{ +public class InputView { - public static String[] inputCarName(Scanner scanner){ - System.out.println("๊ฒฝ์ฃผํ•  ์ฐจ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”. (์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"); - return scanner.nextLine().split(","); + Scanner sc; + private static final String CARS_COUNT = "์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€ ์ธ๊ฐ€์š”?"; + private static final String PUT_NATURAL_NUMBER = "์Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + private static final String TRIAL_COUNT = "์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ ์ธ๊ฐ€์š”?"; + private static final String DO_NOT_PUT_STRING = "๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + + public InputView() { + sc = new Scanner(System.in); + } + + public long inputTotalCarNum() { + System.out.println(CARS_COUNT); + long carCount = 0L; + try { + carCount = Long.parseLong(sc.nextLine()); + if (carCount <= 0) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException exception) { + System.out.println(PUT_NATURAL_NUMBER); + return inputTotalCarNum(); + } catch (InputMismatchException exception) { + System.out.println(DO_NOT_PUT_STRING); + return inputTotalCarNum(); + } + return carCount; } - public static int inputNumberOfTrials(Scanner scanner){ - System.out.println("์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"); - return scanner.nextInt(); + public int inputTotalTrial() { + System.out.println(TRIAL_COUNT); + int trial = 0; + try { + trial = Integer.parseInt(sc.nextLine()); + if (trial <= 0) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException exception) { + System.out.println(PUT_NATURAL_NUMBER); + return inputTotalTrial(); + } catch (InputMismatchException exception) { + System.out.println(DO_NOT_PUT_STRING); + return inputTotalTrial(); + } + return trial; } -} \ No newline at end of file +}
Java
์Œ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋์„ ๊ฒฝ์šฐ์— ์˜ˆ์™ธ๊ฐ€ ๋˜์ ธ์ง€๋„๋ก ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์…จ๋„ค์š”! ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ ๋ง๊ณ ๋„ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’์ด ์ž…๋ ฅ ๋์„ ๊ฒฝ์šฐ๋„ ์˜ˆ์™ธ๊ฐ€ ๋˜์ ธ์ง€๋„๋ก ๋ช…์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -1,16 +1,54 @@ package view; +import java.io.InputStream; +import java.util.InputMismatchException; import java.util.Scanner; -public class InputView{ +public class InputView { - public static String[] inputCarName(Scanner scanner){ - System.out.println("๊ฒฝ์ฃผํ•  ์ฐจ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”. (์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"); - return scanner.nextLine().split(","); + Scanner sc; + private static final String CARS_COUNT = "์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€ ์ธ๊ฐ€์š”?"; + private static final String PUT_NATURAL_NUMBER = "์Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + private static final String TRIAL_COUNT = "์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ ์ธ๊ฐ€์š”?"; + private static final String DO_NOT_PUT_STRING = "๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + + public InputView() { + sc = new Scanner(System.in); + } + + public long inputTotalCarNum() { + System.out.println(CARS_COUNT); + long carCount = 0L; + try { + carCount = Long.parseLong(sc.nextLine()); + if (carCount <= 0) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException exception) { + System.out.println(PUT_NATURAL_NUMBER); + return inputTotalCarNum(); + } catch (InputMismatchException exception) { + System.out.println(DO_NOT_PUT_STRING); + return inputTotalCarNum(); + } + return carCount; } - public static int inputNumberOfTrials(Scanner scanner){ - System.out.println("์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"); - return scanner.nextInt(); + public int inputTotalTrial() { + System.out.println(TRIAL_COUNT); + int trial = 0; + try { + trial = Integer.parseInt(sc.nextLine()); + if (trial <= 0) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException exception) { + System.out.println(PUT_NATURAL_NUMBER); + return inputTotalTrial(); + } catch (InputMismatchException exception) { + System.out.println(DO_NOT_PUT_STRING); + return inputTotalTrial(); + } + return trial; } -} \ No newline at end of file +}
Java
์ €๋„ ๋™๊ฑด๋‹˜๊ป˜ ๋ฐฐ์šด๊ฑด๋ฐ if๋ฌธ ๋ฐ”๋””๊ฐ€ ํ•œ ๋ฌธ์žฅ์ด๋”๋ผ๋„ ์ค‘๊ด„ํ˜ธ {}๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋Š”๊ฒƒ์ด ๊ด€๋ก€๋ผ๊ณ  ํ•˜๋„ค์š”!
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
์ ‘๊ทผ์ œํ•œ์ž๋Š” ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ์ข์€ ๋ฒ”์œ„๋กœ ์„ ์–ธํ•ด์ฃผ๋Š”๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. number, length, state ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” private๋กœ ์„ ์–ธํ•ด์ฃผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,38 @@ +package controller; + +import function.RandomNumberMaker; +import model.Car; +import model.State; +import model.Movable; +import model.UnMovable; + +import java.util.List; + +public class CarController { + + private static final int MOVE_CONDITION = 4; + + public void registerCars(List<Car> cars, long len) { + for (int i = 0; i < len; i++) { + cars.add(new Car(i)); + } + } + + public void moveCar(List<Car> cars) { + for (Car car : cars) { + car.changeState(checkMoveOrStop(car)); + car.move(); + } + } + + private State checkMoveOrStop(Car car) { + if (getRandomNumber() >= MOVE_CONDITION) { + return new Movable(); + } + return new UnMovable(); + } + + private int getRandomNumber() { + return RandomNumberMaker.getRandomNumber(); + } +}
Java
RANDOM_NUMBER_RANGE ๋Š” ์–ด๋–จ๊นŒ์š”?
@@ -1,66 +0,0 @@ -package controller; - -import model.Car; -import util.RandomGenerator; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class RacingGame { - - public static final int CRITICAL_POINT = 5; - public static final int MAX_NAME_LENGTH = 5; - - public void oneTrial(List<Car> cars){ - for (Car car : cars){ - check(car); - } - } - - public void check(Car car){ - if (RandomGenerator.randInt(10) > CRITICAL_POINT){ - car.move(); - } - } - - public List<String> searchWinners(List<Car> cars){ - List<String> winnerNames = new ArrayList<>(); - int maxPosition = searchMaxPosition(cars); - - for (Car car : cars){ - checkWinner(car, winnerNames, maxPosition); - } - return winnerNames; - } - - public int searchMaxPosition(List<Car> cars){ - List<Integer> carPosition = new ArrayList<>(); - - for (Car car : cars){ - carPosition.add(car.getPosition()); - } - return Collections.max(carPosition); - } - - public void checkWinner(Car car, List<String> winnerNames, int maxPosition){ - if (car.getPosition() == maxPosition){ - winnerNames.add(car.getName()); - } - } - - public static List<Car> generateCars(String[] carNames) throws StringOutOfBoundsException{ - List<Car> cars = new ArrayList<>(); - for (String carName : carNames){ - checkNameLength(carName, cars); - } - return cars; - } - - public static void checkNameLength(String carName, List<Car> cars) throws StringOutOfBoundsException { - if(carName.length() > MAX_NAME_LENGTH){ - throw new StringOutOfBoundsException(); - } - cars.add(new Car(carName)); - } -}
Java
๋ฉ”์„œ๋“œ๋ช…์€ ํ–‰์œ„๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋™์‚ฌ๋กœ ์‹œ์ž‘ํ•˜๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -1,66 +0,0 @@ -package controller; - -import model.Car; -import util.RandomGenerator; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class RacingGame { - - public static final int CRITICAL_POINT = 5; - public static final int MAX_NAME_LENGTH = 5; - - public void oneTrial(List<Car> cars){ - for (Car car : cars){ - check(car); - } - } - - public void check(Car car){ - if (RandomGenerator.randInt(10) > CRITICAL_POINT){ - car.move(); - } - } - - public List<String> searchWinners(List<Car> cars){ - List<String> winnerNames = new ArrayList<>(); - int maxPosition = searchMaxPosition(cars); - - for (Car car : cars){ - checkWinner(car, winnerNames, maxPosition); - } - return winnerNames; - } - - public int searchMaxPosition(List<Car> cars){ - List<Integer> carPosition = new ArrayList<>(); - - for (Car car : cars){ - carPosition.add(car.getPosition()); - } - return Collections.max(carPosition); - } - - public void checkWinner(Car car, List<String> winnerNames, int maxPosition){ - if (car.getPosition() == maxPosition){ - winnerNames.add(car.getName()); - } - } - - public static List<Car> generateCars(String[] carNames) throws StringOutOfBoundsException{ - List<Car> cars = new ArrayList<>(); - for (String carName : carNames){ - checkNameLength(carName, cars); - } - return cars; - } - - public static void checkNameLength(String carName, List<Car> cars) throws StringOutOfBoundsException { - if(carName.length() > MAX_NAME_LENGTH){ - throw new StringOutOfBoundsException(); - } - cars.add(new Car(carName)); - } -}
Java
check ๋Š” ๋ฌด์—‡์„ ์ฒดํฌํ•œ๋‹ค๋Š”๊ฑด์ง€ ์•Œ๊ธฐ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™์•„์š”~ ๋‚ด๋ถ€์—์„œ๋งŒ ํ™œ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋ฉด private ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๊ณ  ๋ฉ”์„œ๋“œ๋ช…์„ ์ข€ ๋” ํ’€์–ด์จ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,8 @@ +package model; + +public class Movable implements State { + @Override + public int execute(int length) { + return length + 1; + } +}
Java
~able ์€ interface ์˜ ์ ‘๋ฏธ์‚ฌ๋กœ ํ™œ์šฉ๋˜๋Š”๋ฐ ๊ตฌํ˜„์ฒด์˜ ๋„ค์ด๋ฐ์ธ ๊ฒฝ์šฐ์—๋Š” ์ด ํƒ€์ž…์ด ๋ฌด์—‡์ด ๊ฐ€๋Šฅํ•œ์ง€๋ณด๋‹ค๋Š” ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ๊ตฌ์ฒด์ ์ธ ๋‹จ์–ด๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”? ์˜ˆ๋ฅผ ๋“ค์–ด Forward ๋‚˜ Stop ์ฒ˜๋Ÿผ์š”
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
์ž๋™์ฐจ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜ ๊ฐ™์€๋ฐ position ์€ ์–ด๋–จ๊นŒ์š”?
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
์˜๋ฏธ๊ฐ€ ๋ถˆ๋ถ„๋ช…ํ•œ number ๋ณด๋‹ค๋Š” ์‹๋ณ„์ด ๊ฐ€๋Šฅํ•œ ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” id ๋„ ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ~
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
Num ๋„ ์ค„์ž„๋ง์ด๋‹ˆ number, no ๋กœ ํ†ต์ผํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ๊ทธ๋ฆฌ๊ณ  getter ๋Š” get{field๋ช…} ์ด๋ผ๋Š” java bean ์ปจ๋ฒค์…˜์ด ์žˆ์œผ๋‹ˆ ๋”ฐ๋ฅด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
count ๋ฅผ ์ด๋™์‹œํ‚จ๋‹ค ๋ผ๋Š” ๋œป์˜ ๋ฉ”์„œ๋“œ๋ช…์ธ ๊ฒƒ ๊ฐ™์•„์š”~ getLength ๋Š” ์–ด๋–จ๊นŒ์š”?
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
cmd ์˜ ์˜๋ฏธ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”? command? ์Šคํ„ฐ๋”” ๊ทœ์น™์€ ์ถ•์•ฝํ˜• ๋‹จ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ง์ž~ ์ž…๋‹ˆ๋‹ค
@@ -2,22 +2,28 @@ public class Car{ - private String name; - private int position = 0; + long number; + int length; + State state; - public Car(String name){ - this.name = name; + public Car(long number) { + this.number = number; + length = 0; } - public String getName() { - return name; + public int moveCount() { + return length; } - public int getPosition(){ - return position; + public long getCarNum() { + return number; } - public void move(){ - position++; + public void changeState(State cmd) { + state = cmd; + } + + public void move() { + length = state.execute(length); } } \ No newline at end of file
Java
state ์˜ ๊ธฐ๋ณธ๊ฐ’์ด ์„ธํŒ…๋˜์–ด์žˆ์ง€ ์•Š์€๋ฐ, ์ด ํด๋ž˜์Šค๊ฐ€ state ํŒจํ„ด์„ ์ ์šฉํ•œ ๊ฒƒ์ธ์ง€ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ์€ car.move() ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๊ฒฝ์šฐ์— NPE ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„๊นŒ์š”~?
@@ -0,0 +1,38 @@ +package controller; + +import function.RandomNumberMaker; +import model.Car; +import model.State; +import model.Movable; +import model.UnMovable; + +import java.util.List; + +public class CarController { + + private static final int MOVE_CONDITION = 4; + + public void registerCars(List<Car> cars, long len) { + for (int i = 0; i < len; i++) { + cars.add(new Car(i)); + } + } + + public void moveCar(List<Car> cars) { + for (Car car : cars) { + car.changeState(checkMoveOrStop(car)); + car.move(); + } + } + + private State checkMoveOrStop(Car car) { + if (getRandomNumber() >= MOVE_CONDITION) { + return new Movable(); + } + return new UnMovable(); + } + + private int getRandomNumber() { + return RandomNumberMaker.getRandomNumber(); + } +}
Java
์ƒํƒœ ํŒจํ„ด์„ ํ™œ์šฉํ•˜์‹ ๊ฒŒ ์‹ ์„ ํ•˜๋„ค์š”~ ๋‹ค๋งŒ ์ „์ง„๊ณผ ์ •์ง€ ๋ฟ์ด๊ณ  ์ƒํƒœ์˜ ์ „์ด๊ฐ€ ์—†์„ ๋ฟ๋”๋Ÿฌ ์กฐ๊ฑด๋ฌธ์ด ๋ณต์žกํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ ์šฉํ•˜๊ธฐ์—” ๊ณผํ•˜์ง€ ์•Š๋‚˜๋ผ๋Š” ๊ณ ๋ฏผ๋„ ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. move(int condition) ๋ฉ”์„œ๋“œ์—์„œ ๋ถ„๊ธฐ์ณ์„œ ์ „์ง„, ์ •์ง€๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ๊ณผ, ์›€์ง์ž„ ๊ด€๋ จ ์ƒํƒœ๋ฅผ ์ •์˜ํ•˜๊ณ , CarController ์—์„œ ๋ถ„๊ธฐ์ณ์„œ condition ์ฒดํฌํ•˜๊ณ  ์›€์ง์ž„ ๊ด€๋ จ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋„๋ก ๊ตฌํ˜„ํ•œ ๋‹ค์Œ, Car.move ์—์„œ ๋˜ ๋ถ„๊ธฐ๋ฅผ ํ†ตํ•ด ์ƒํƒœ๊ฐ’์„ ์ฒดํฌํ•ด์„œ ํ–‰์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ(์„ ๊ฐœ์„ ํ•œ ๊ฒƒ์ด ์ƒํƒœ ํŒจํ„ด ์ ์šฉ๋ฒ„์ „์ธ๋ฐ) ๋ฌด์—‡์ด ๋” ๋ณต์žกํ•˜๊ณ  ์‹ฌํ”Œํ•œ์ง€ ๊ณ ๋ฏผํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. ๊ฐ„๋‹จํ•˜๊ฒŒ ํ–‰์œ„๋กœ ํ’€ ๊ฒƒ์„ ์ƒํƒœ๋กœ ๋งŒ๋“ค์–ด์„œ ํŒจํ„ด์˜ ์ ์šฉ์ด ์ฝ”๋“œ์˜ ๋ชฉ์ ์ด ๋œ๊ฑด ์•„๋‹Œ์ง€๋„ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ~ ํ•™์Šต ๋ชฉ์ ์œผ๋กœ๋Š” ์ข‹์€ ์‹œ๋„์ธ ๊ฒƒ ๊ฐ™์•„์š”~
@@ -0,0 +1,5 @@ +package model; + +public interface State { + int execute(int length); +}
Java
state ๋ฅผ ์ ์šฉํ•  ๋‹ค๋ฅธ ์ผ€์ด์Šค๋ฅผ ์ƒ๊ฐํ•ด์„œ ์ข€ ๋” ๊ตฌ์ฒด์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค๋ช…์„ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? MoveState?
@@ -1,16 +1,54 @@ package view; +import java.io.InputStream; +import java.util.InputMismatchException; import java.util.Scanner; -public class InputView{ +public class InputView { - public static String[] inputCarName(Scanner scanner){ - System.out.println("๊ฒฝ์ฃผํ•  ์ฐจ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”. (์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"); - return scanner.nextLine().split(","); + Scanner sc; + private static final String CARS_COUNT = "์ž๋™์ฐจ ๋Œ€์ˆ˜๋Š” ๋ช‡ ๋Œ€ ์ธ๊ฐ€์š”?"; + private static final String PUT_NATURAL_NUMBER = "์Œ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + private static final String TRIAL_COUNT = "์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ ํšŒ ์ธ๊ฐ€์š”?"; + private static final String DO_NOT_PUT_STRING = "๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"; + + public InputView() { + sc = new Scanner(System.in); + } + + public long inputTotalCarNum() { + System.out.println(CARS_COUNT); + long carCount = 0L; + try { + carCount = Long.parseLong(sc.nextLine()); + if (carCount <= 0) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException exception) { + System.out.println(PUT_NATURAL_NUMBER); + return inputTotalCarNum(); + } catch (InputMismatchException exception) { + System.out.println(DO_NOT_PUT_STRING); + return inputTotalCarNum(); + } + return carCount; } - public static int inputNumberOfTrials(Scanner scanner){ - System.out.println("์‹œ๋„ํ•  ํšŸ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"); - return scanner.nextInt(); + public int inputTotalTrial() { + System.out.println(TRIAL_COUNT); + int trial = 0; + try { + trial = Integer.parseInt(sc.nextLine()); + if (trial <= 0) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException exception) { + System.out.println(PUT_NATURAL_NUMBER); + return inputTotalTrial(); + } catch (InputMismatchException exception) { + System.out.println(DO_NOT_PUT_STRING); + return inputTotalTrial(); + } + return trial; } -} \ No newline at end of file +}
Java
์ฝ”๋“œ์˜ ํฌ๋งทํŒ…๋„ ์ปจ๋ฒค์…˜์˜ ์ผ์ข…์ž…๋‹ˆ๋‹ค. intellij ์˜ ์ฝ”๋“œ ํฌ๋งทํŒ… ๋‹จ์ถ•ํ‚ค๋ฅผ ํ™œ์šฉํ•ด์„œ ์ •๋ ฌํ•ด๋ณด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”?