code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,23 @@ +package christmas.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class MenuTest { + @DisplayName("์ฃผ์–ด์ง„ ์ด๋ฆ„์„ ํ† ๋Œ€๋กœ ๋ฉ”๋‰ด๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.") + @Test + void decideMenu() { + String inputTapas = "ํƒ€ํŒŒ์Šค"; + String inputSeafoodPasta = "ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€"; + String inputRedWine = "๋ ˆ๋“œ์™€์ธ"; + Menu inputTapasResult = Menu.decideMenu(inputTapas); + Menu inputSeafoodPastaResult = Menu.decideMenu(inputSeafoodPasta); + Menu inputRedWineResult = Menu.decideMenu(inputRedWine); + + assertThat(inputTapasResult).isEqualTo(Menu.TAPAS); + assertThat(inputSeafoodPastaResult).isEqualTo(Menu.SEAFOOD_PASTA); + assertThat(inputRedWineResult).isEqualTo(Menu.RED_WINE); + } +} \ No newline at end of file
Java
๋งž์•„์š” ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ€์žฅ ์•„์‰ฌ์šด ๋ถ€๋ถ„์ธ๋ฐ์š”,, ๊ตฌํ˜„ํ•˜๊ณ  ๋ฆฌํŒฉํ„ฐ๋ง์„ ํ•˜๋„ ๋งŽ์ด ํ•ด์„œ ์‹œ๊ฐ„์ด ๋งค์šฐ ์ด‰๋ฐ•ํ–ˆ๋‹ค๋Š”,, ใ… ใ… 
@@ -0,0 +1,39 @@ +package christmas.domain; + +import java.util.Arrays; +import java.util.List; + +public enum MenuType { + APPETIZER("์• ํ”ผํƒ€์ด์ €", Arrays.asList(Menu.MUSHROOM_SOUP, Menu.TAPAS, Menu.CAESAR_SALAD)), + MAIN("๋ฉ”์ธ", Arrays.asList(Menu.T_BONE_STAKE, Menu.BARBECUE_LIP, Menu.SEAFOOD_PASTA, Menu.CHRISTMAS_PASTA)), + DESSERT("๋””์ €ํŠธ", Arrays.asList(Menu.CHOCOLATE_CAKE, Menu.ICE_CREAM)), + BEVERAGE("์Œ๋ฃŒ", Arrays.asList(Menu.ZERO_COKE, Menu.RED_WINE, Menu.CHAMPAGNE)); + + private final String name; + private final List<Menu> menus; + + MenuType(String name, List<Menu> menus) { + this.name = name; + this.menus = menus; + } + + public static MenuType decideMenuType(Menu menu) { + MenuType[] menuTypes = values(); + + for (MenuType menuType : menuTypes) { + if (menuType.getMenus().contains(menu)) { + return menuType; + } + } + + return null; + } + + public String getName() { + return name; + } + + public List<Menu> getMenus() { + return menus; + } +} \ No newline at end of file
Java
์ €ํ•œํ…Œ ๋‚จ๊ฒจ์ฃผ์…จ๋˜ ์ฝ”๋“œ ๋ฆฌ๋ทฐ์—์„œ ์–ธ๊ธ‰ํ•˜์‹  ํŒจํ„ด์ด๋„ค์š”! ์ข‹์€ ๊ณต๋ถ€๊ฐ€ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,21 @@ +package christmas.domain; + +public enum OrderResultType { + ORDER_MENU("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + TOTAL_ORDER_PRICE("<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIFT_MENU("<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT_STATISTICS("<ํ˜œํƒ ๋‚ด์—ญ>"), + TOTAL_BENEFIT_AMOUNT("<์ดํ˜œํƒ ๊ธˆ์•ก>"), + ESTIMATED_PAYMENT("<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + EVENT_BADGE("<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private final String name; + + OrderResultType(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}
Java
๐Ÿค” `OrderResultType`์˜ ์ œ๋ชฉ์€ ๊ทธ๋ ‡๋‹ค ์ณ๋„, < > ๊ฐ™์€ character ๋“ค์€ View์˜ ์ฑ…์ž„์— ๊ฐ€๊นŒ์›Œ ๋ณด์—ฌ์š”. View์—์„œ`"<%s>"` ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๊ณ  formatting์„ ์ด์šฉํ•ด ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋” ๋ณ€๊ฒฝ์— ๊ฐ•ํ•œ ์„ค๊ณ„ ์•„๋‹๊นŒ์š”?
@@ -0,0 +1,45 @@ +package christmas.domain; + +public class OrderedMenu { + public static final String ORDER_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final int AMOUNT_MINIMUM_QUANTITY = 1; + + private final String menuName; + private final int quantity; + + public OrderedMenu(String menuName, int quantity) { + validateOrderNameExistInMenu(menuName); + validateOrderAmountQuantity(quantity); + + this.menuName = menuName; + this.quantity = quantity; + } + + private void validateOrderNameExistInMenu(String name) { + if (Menu.decideMenu(name) == Menu.NONE) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateOrderAmountQuantity(int amount) { + if (amount < AMOUNT_MINIMUM_QUANTITY) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isBeverage() { + return MenuType.decideMenuType(Menu.decideMenu(menuName)) == MenuType.BEVERAGE; + } + + public int calculatePrice() { + return Menu.decideMenu(menuName).getPrice() * quantity; + } + + public String getMenuName() { + return menuName; + } + + public int getQuantity() { + return quantity; + } +} \ No newline at end of file
Java
`Menu` ์— ์†ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฒ€์ฆ์ด ๋˜์—ˆ๋‹ค๋ฉด, field๋ฅผ `String`์ด ์•„๋‹Œ `Menu`๋กœ ์ €์žฅํ•˜๋Š”๊ฒŒ ๋” ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์•„์š”. enum ์‚ฌ์šฉ์˜ ์˜๋ฏธ๊ฐ€ ์•ฝ๊ฐ„ ํ‡ด์ƒ‰๋˜์ง€ ์•Š์•˜๋‚˜ ์‹ถ๋„ค์š”!
@@ -0,0 +1,52 @@ +package christmas.ui; + +import static christmas.domain.Date.DATE_RE_READ_REQUEST_MESSAGE; +import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE; + +import java.util.ArrayList; +import java.util.List; + +public class Converter { + private static final String ORDER_DELIMITER = ","; + private static final String NAME_AND_AMOUNT_DELIMITER = "-"; + + public static int convertDateNumeric(String Input) { + try { + return Integer.parseInt(Input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public static List<String> separateOrder(String order) { + return new ArrayList<>(List.of(order.split(ORDER_DELIMITER))); + } + + public static List<String> separateNameAndAmount(String orderedMenu) { + validateAppropriateDelimiter(orderedMenu); + List<String> separatedNameAndAmount = new ArrayList<>(List.of(orderedMenu.split(NAME_AND_AMOUNT_DELIMITER))); + validateAppropriateForm(separatedNameAndAmount); + + return separatedNameAndAmount; + } + + private static void validateAppropriateDelimiter(String orderedMenu) { + if (!orderedMenu.contains(NAME_AND_AMOUNT_DELIMITER)) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private static void validateAppropriateForm(List<String> separatedNameAndAmount) { + if (separatedNameAndAmount.size() != 2) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public static int convertAmountNumeric(String input) { + try { + return Integer.parseInt(input); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } +} \ No newline at end of file
Java
String์„ ์˜ค๋ธŒ์ ํŠธ๋กœ ๋ฐ”๊พธ๋Š” ํด๋ž˜์Šค๋Š” `Parser`๋ผ๊ณ  ์ด๋ฆ„ ์ง“๋Š” ๊ฒƒ์ด ๋” convention์— ๊ฐ€๊นŒ์šธ ๊ฒƒ ๊ฐ™์€๋ฐ, ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,63 @@ +package christmas.ui; + +import camp.nextstep.edu.missionutils.Console; +import christmas.domain.Date; +import christmas.domain.Order; +import christmas.domain.OrderedMenu; +import java.util.ArrayList; +import java.util.List; + +public class InputView { + private static final String OPENING_MESSAGE = "์•ˆ๋…•ํ•˜์„ธ์š”! ์šฐํ…Œ์ฝ” ์‹๋‹น 12์›” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค."; + private static final String DATE_REQUEST_MESSAGE = "12์›” ์ค‘ ์‹๋‹น ์˜ˆ์ƒ ๋ฐฉ๋ฌธ ๋‚ ์งœ๋Š” ์–ธ์ œ์ธ๊ฐ€์š”? (์ˆซ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”!)"; + private static final String ORDER_REQUEST_MESSAGE + = "์ฃผ๋ฌธํ•˜์‹ค ๋ฉ”๋‰ด๋ฅผ ๋ฉ”๋‰ด์™€ ๊ฐœ์ˆ˜๋ฅผ ์•Œ๋ ค ์ฃผ์„ธ์š”. (e.g. ํ•ด์‚ฐ๋ฌผํŒŒ์Šคํƒ€-2,๋ ˆ๋“œ์™€์ธ-1,์ดˆ์ฝ”์ผ€์ดํฌ-1)"; + + public static void notifyProgramStarted() { + System.out.println(OPENING_MESSAGE); + } + + public static Date inputDate() { + try { + return readDate(); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e); + return inputDate(); + } + } + + private static Date readDate() { + System.out.println(DATE_REQUEST_MESSAGE); + + String input = Console.readLine(); + int dayNumber = Converter.convertDateNumeric(input); + + return new Date(dayNumber); + } + + public static Order inputOrder() { + try { + return new Order(readOrder()); + } catch (IllegalArgumentException e) { + OutputView.printErrorMessage(e); + return inputOrder(); + } + } + + private static List<OrderedMenu> readOrder() { + System.out.println(ORDER_REQUEST_MESSAGE); + + String input = Console.readLine(); + List<String> separatedOrder = Converter.separateOrder(input); + List<OrderedMenu> order = new ArrayList<>(); + + for (String orderedMenu : separatedOrder) { + List<String> separatedNameAndAmount = Converter.separateNameAndAmount(orderedMenu); + String name = separatedNameAndAmount.get(0); + int amount = Converter.convertAmountNumeric(separatedNameAndAmount.get(1)); + order.add(new OrderedMenu(name, amount)); + } + + return order; + } +} \ No newline at end of file
Java
Supplier์˜ ํ˜•ํƒœ๋กœ handling ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์–ด์š”. inputDate์™€ inputOrder์˜ ํ˜•ํƒœ๊ฐ€ ๊ต‰์žฅํžˆ ๋‹ฎ์•„์žˆ์–ด์„œ, ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ ์šฉ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์œผ๋‹ˆ ์ด ์ž๋ฃŒ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๊ธธ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. https://hwan33.tistory.com/17
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
Early return์„ ์‚ฌ์šฉํ•˜์…จ๋Š”๋ฐ, ๊ตณ์ด ๋ฒ”์œ„๋ฅผ max, min์„ ์ด์šฉํ•ด ์ผ์ผ์ด ์„ค๋ช…ํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”. ๊ผญ maximum_amount๊ฐ€ ํ•„์š”ํ–ˆ์„๊นŒ์š”? (๊ทธ๋ฆฌ๊ณ  ๋ณ€์ˆ˜๋ช…์„ maximum์ด๋ผ๊ณ  ๋ณด๊ธฐ์—” ํ•ด๋‹น ๋ถ€๋ถ„์„ ํฌํ•จํ•˜์ง€ ์•Š์•„์„œ ํ˜ผ๋™์ด ์žˆ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฐจ๋ผ๋ฆฌ `STAR_BADGE_MAXIMUM_AMOUNT = 9999`์™€ ๊ฐ™์€ ๋ฐฉ์‹์ด์—ˆ๋‹ค๋ฉด...) ```suggestion if (totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) { return STAR_BADGE; } if (totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) { return TREE_BADGE; } if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { return SANTA_BADGE; } return NON_BADGE; ```
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
์ถ”๊ฐ€) NON_BADGE๋„ ๋ณ€์ˆ˜๋ช… ํ†ต์ผ์„ฑ์„ ์œ„ํ•ด NONE_BADGE๋กœ ์ž‘์„ฑํ•˜์…จ๋‹ค๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,39 @@ +package christmas.domain; + +import java.util.Arrays; +import java.util.List; + +public enum MenuType { + APPETIZER("์• ํ”ผํƒ€์ด์ €", Arrays.asList(Menu.MUSHROOM_SOUP, Menu.TAPAS, Menu.CAESAR_SALAD)), + MAIN("๋ฉ”์ธ", Arrays.asList(Menu.T_BONE_STAKE, Menu.BARBECUE_LIP, Menu.SEAFOOD_PASTA, Menu.CHRISTMAS_PASTA)), + DESSERT("๋””์ €ํŠธ", Arrays.asList(Menu.CHOCOLATE_CAKE, Menu.ICE_CREAM)), + BEVERAGE("์Œ๋ฃŒ", Arrays.asList(Menu.ZERO_COKE, Menu.RED_WINE, Menu.CHAMPAGNE)); + + private final String name; + private final List<Menu> menus; + + MenuType(String name, List<Menu> menus) { + this.name = name; + this.menus = menus; + } + + public static MenuType decideMenuType(Menu menu) { + MenuType[] menuTypes = values(); + + for (MenuType menuType : menuTypes) { + if (menuType.getMenus().contains(menu)) { + return menuType; + } + } + + return null; + } + + public String getName() { + return name; + } + + public List<Menu> getMenus() { + return menus; + } +} \ No newline at end of file
Java
๋งž์•„์š” ๋‹ค์‹œ ๋ฌผ์–ด๋ณด์…จ์–ด์„œ ๋Œ“๊ธ€์— ์ •๋ณด๋ฅผ ์–ป์€ ๋งํฌ ๋‚จ๊ฒจ๋“œ๋ ธ์—ˆ๋Š”๋ฐ์š” ๋ชป ๋ณด์‹  ๊ฒƒ ๊ฐ™์•„ ๋‹ค์‹œ ๋‚จ๊ฒจ๋“œ๋ ค์š”..!! https://techblog.woowahan.com/2527/ @Uniaut
@@ -0,0 +1,21 @@ +package christmas.domain; + +public enum OrderResultType { + ORDER_MENU("<์ฃผ๋ฌธ ๋ฉ”๋‰ด>"), + TOTAL_ORDER_PRICE("<ํ• ์ธ ์ „ ์ด์ฃผ๋ฌธ ๊ธˆ์•ก>"), + GIFT_MENU("<์ฆ์ • ๋ฉ”๋‰ด>"), + BENEFIT_STATISTICS("<ํ˜œํƒ ๋‚ด์—ญ>"), + TOTAL_BENEFIT_AMOUNT("<์ดํ˜œํƒ ๊ธˆ์•ก>"), + ESTIMATED_PAYMENT("<ํ• ์ธ ํ›„ ์˜ˆ์ƒ ๊ฒฐ์ œ ๊ธˆ์•ก>"), + EVENT_BADGE("<12์›” ์ด๋ฒคํŠธ ๋ฐฐ์ง€>"); + + private final String name; + + OrderResultType(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}
Java
๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ธ๊ฒƒ ๊ฐ™๋„ค์š”!! ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,45 @@ +package christmas.domain; + +public class OrderedMenu { + public static final String ORDER_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final int AMOUNT_MINIMUM_QUANTITY = 1; + + private final String menuName; + private final int quantity; + + public OrderedMenu(String menuName, int quantity) { + validateOrderNameExistInMenu(menuName); + validateOrderAmountQuantity(quantity); + + this.menuName = menuName; + this.quantity = quantity; + } + + private void validateOrderNameExistInMenu(String name) { + if (Menu.decideMenu(name) == Menu.NONE) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateOrderAmountQuantity(int amount) { + if (amount < AMOUNT_MINIMUM_QUANTITY) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isBeverage() { + return MenuType.decideMenuType(Menu.decideMenu(menuName)) == MenuType.BEVERAGE; + } + + public int calculatePrice() { + return Menu.decideMenu(menuName).getPrice() * quantity; + } + + public String getMenuName() { + return menuName; + } + + public int getQuantity() { + return quantity; + } +} \ No newline at end of file
Java
๋งž๋„ค์š”..! ์ข‹์€ ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!! Menu์—ด๊ฑฐ์—์„œ ๋ฉ”๋‰ด์— ์†ํ•˜์ง€ ์•Š๋Š” ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด "์—†์Œ"์„ ๋งŒ๋“ค์—ˆ์—ˆ๋Š”๋ฐ, ๋งŒ์•ฝ null๋กœ ์ฒ˜๋ฆฌํ–ˆ์—ˆ๋‹ค๋ฉด Menu์— ์†ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์•„์ง ๊ฒ€์ฆ์ด ์•ˆ๋˜์—ˆ๋˜ ๊ฑฐ๋‹ˆ๊นŒ ์ง€๊ธˆ์ฒ˜๋Ÿผ String์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ์š”? ์ œ๊ฐ€ ์ดํ•ดํ•œ ๊ฒƒ์ด ๋งž๋Š”์ง€ ๊ถ๊ธˆํ•ด์š” @Uniaut
@@ -0,0 +1,58 @@ +package christmas.domain; + +import java.time.DayOfWeek; +import java.time.LocalDate; + +public class Date { + public static final String DATE_RE_READ_REQUEST_MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ ๋‚ ์งœ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private static final LocalDate CHRISTMAS_DAY = LocalDate.of(2023, 12, 25); + private static final int YEAR = 2023; + private static final int MONTH = 12; + private static final int DATE_MIN_NUMBER = 1; + private static final int DATE_MAX_NUMBER = 31; + + private final int dayNumber; + + public Date(int dayNumber) { + validateRange(dayNumber); + this.dayNumber = dayNumber; + } + + private void validateRange(int dayNumber) { + if ((dayNumber < DATE_MIN_NUMBER) || (dayNumber > DATE_MAX_NUMBER)) { + throw new IllegalArgumentException(DATE_RE_READ_REQUEST_MESSAGE); + } + } + + public boolean isDDayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + + return localDate.isBefore(CHRISTMAS_DAY.plusDays(1)); + } + + public boolean isWeekdayDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.MONDAY) || day.equals(DayOfWeek.TUESDAY) || day.equals(DayOfWeek.WEDNESDAY) + || day.equals(DayOfWeek.THURSDAY) || day.equals(DayOfWeek.SUNDAY)); + } + + public boolean isWeekendDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.FRIDAY) || day.equals(DayOfWeek.SATURDAY)); + } + + public boolean isSpecialDiscountActive() { + LocalDate localDate = LocalDate.of(YEAR, MONTH, dayNumber); + DayOfWeek day = localDate.getDayOfWeek(); + + return (day.equals(DayOfWeek.SUNDAY) || localDate.equals(CHRISTMAS_DAY)); + } + + public int getDayNumber() { + return dayNumber; + } +} \ No newline at end of file
Java
@JaeHongDev ์ €๋„ ์ƒˆ๋กญ๊ฒŒ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค..!
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
๋งž์•„์š”!! ์ €๋„ ๊ฐœ์ธ์ ์œผ๋กœ ์ด ๋ฉ”์„œ๋“œ์˜ ๋กœ์ง์„ ๋งŒ๋“ค์—ˆ๋˜ ๋ฐฉ์‹์ด ์•„์‰ฌ์› ๋Š”๋ฐ์š” ,, ๋ง์”€ํ•ด ์ฃผ์‹  ๋ฐฉ๋ฒ•์ด ํ›จ์”ฌ ๊น”๋”ํ•œ ๊ฒƒ ๊ฐ™์•„์š” ๋Œ€์‹  if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { return SANTA_BADGE; } if (totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) { return TREE_BADGE; } if (totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) { return STAR_BADGE; } return NON_BADGE; ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฐํƒ€ ๋ฐฐ์ง€๊ฐ€ ์ œ์ผ ์œ„๋กœ ๊ฐ€์•ผ 30000์›์ผ ๋•Œ ๋ณ„ ๋ฐฐ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜์ง€ ์•Š๊ณ  ์‚ฐํƒ€ ๋ฐฐ์ง€๋ฅผ ๋ฆฌํ„ด ํ•  ๊ฒƒ ๊ฐ™์•„์š” ์ œ๊ฐ€ ์ดํ•ดํ•œ๊ฒŒ ๋งž๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค @Uniaut Maximum์ด๋ผ๋Š” ํ‘œํ˜„์€ ํ•ด๋‹น ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ํ‘œํ˜„์ด๋ž€ ๊ฒƒ์„ ๋†“์ณค๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”!! ๋‚ ์นด๋กœ์šด ์ง€์  ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
if๋ฌธ ์ „๊นŒ์ง€๊ฐ€ calculateTotalBenefitAmount๋ž‘ ๊ฐ™์•„์„œ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ ์„ ํ•„์š”์—†์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,85 @@ +package christmas.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class EventResult { + private static final String NON_BADGE = "์—†์Œ"; + private static final String STAR_BADGE = "๋ณ„"; + private static final String TREE_BADGE = "ํŠธ๋ฆฌ"; + private static final String SANTA_BADGE = "์‚ฐํƒ€"; + private static final int STAR_BADGE_MINIMUM_AMOUNT = 5000; + private static final int STAR_BADGE_MAXIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MINIMUM_AMOUNT = 10000; + private static final int TREE_BADGE_MAXIMUM_AMOUNT = 20000; + private static final int SANTA_BADGE_MINIMUM_AMOUNT = 20000; + + private final Map<BenefitType, Integer> allBenefit; + + public EventResult(Map<BenefitType, Integer> allBenefit) { + this.allBenefit = allBenefit; + } + + public int calculateTotalBenefitAmount() { + int totalBenefitAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalBenefitAmount += allBenefit.get(benefitType); + } + + return totalBenefitAmount; + } + + public int calculateTotalDiscountAmount() { + int totalDiscountAmount = 0; + List<BenefitType> benefitTypes = List.of(BenefitType.values()); + + for (BenefitType benefitType : benefitTypes) { + totalDiscountAmount += allBenefit.get(benefitType); + } + if (allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice()) { + totalDiscountAmount -= Menu.CHAMPAGNE.getPrice(); + } + + return totalDiscountAmount; + } + + public boolean isReceivedGiftBenefit() { + return allBenefit.get(BenefitType.GIFT) == Menu.CHAMPAGNE.getPrice(); + } + + public List<BenefitType> checkWhichBenefitExist() { + List<BenefitType> existingBenefit = new ArrayList<>(); + BenefitType[] benefitTypes = BenefitType.values(); + + for (BenefitType benefitType : benefitTypes) { + if (allBenefit.get(benefitType) != 0) { + existingBenefit.add(benefitType); + } + } + + return existingBenefit; + } + + public String decideEventBadge() { + int totalBenefitAmount = calculateTotalBenefitAmount(); + + if ((totalBenefitAmount >= STAR_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < STAR_BADGE_MAXIMUM_AMOUNT)) { + return STAR_BADGE; + } + if ((totalBenefitAmount >= TREE_BADGE_MINIMUM_AMOUNT) && (totalBenefitAmount < TREE_BADGE_MAXIMUM_AMOUNT)) { + return TREE_BADGE; + } + if ((totalBenefitAmount >= SANTA_BADGE_MINIMUM_AMOUNT)) { + return SANTA_BADGE; + } + return NON_BADGE; + } + + public Map<BenefitType, Integer> getAllBenefit() { + return Collections.unmodifiableMap(allBenefit); + } +} \ No newline at end of file
Java
์ด if๋ฌธ์˜ ์กฐ๊ฑด๋„ isReceivedGiftBenefit ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ฒ ๋„ค์š”!
@@ -0,0 +1,76 @@ +package christmas.domain; + +import static christmas.domain.OrderedMenu.ORDER_RE_READ_REQUEST_MESSAGE; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Order { + private static final int MAXIMUM_MENU_QUANTITY = 20; + + private final List<OrderedMenu> order; + + public Order(List<OrderedMenu> order) { + validateOrderedMenuNamesNotAllBeverage(order); + validateOrderedMenuNamesNotDuplicate(order); + validateTotalOrderedMenuQuantity(order); + + this.order = order; + } + + private void validateOrderedMenuNamesNotAllBeverage(List<OrderedMenu> order) { + List<Boolean> beverageChecker = new ArrayList<>(); + + for (OrderedMenu orderedMenu : order) { + if (orderedMenu.isBeverage()) { + beverageChecker.add(true); + } + if (!orderedMenu.isBeverage()) { + beverageChecker.add(false); + } + } + + if (!beverageChecker.contains(false)) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateOrderedMenuNamesNotDuplicate(List<OrderedMenu> order) { + Set<String> duplicateChecker = new HashSet<>(); + + for (OrderedMenu orderedMenu : order) { + duplicateChecker.add(orderedMenu.getMenuName()); + } + if (duplicateChecker.size() != order.size()) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + private void validateTotalOrderedMenuQuantity(List<OrderedMenu> order) { + int totalQuantity = 0; + + for (OrderedMenu orderedMenu : order) { + totalQuantity += orderedMenu.getQuantity(); + } + if (totalQuantity > MAXIMUM_MENU_QUANTITY) { + throw new IllegalArgumentException(ORDER_RE_READ_REQUEST_MESSAGE); + } + } + + public int calculateTotalOrderPrice() { + int totalOrderPrice = 0; + + for (OrderedMenu orderedMenu : order) { + totalOrderPrice += orderedMenu.calculatePrice(); + } + + return totalOrderPrice; + } + + public List<OrderedMenu> getOrder() { + return Collections.unmodifiableList(order); + } +} \ No newline at end of file
Java
๋ฉ”์„œ๋“œ๋ช…์ด ๊ธธ์–ด๋„ ํ™•์‹คํžˆ ์ด๋ฆ„๋งŒ ๋ณด๊ณ ๋„ ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,21 @@ +package nextstep.subway; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ControllerExceptionHandler { + + @ExceptionHandler(DataIntegrityViolationException.class) + public ResponseEntity handleIllegalArgsException(DataIntegrityViolationException e) { + return ResponseEntity.badRequest().build(); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgumentException(IllegalArgumentException illegalArgumentException){ + String message = illegalArgumentException.getMessage(); + return ResponseEntity.badRequest().body(message); + } +}
Java
@RestControllerAdvice ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ํ•œ๊ตฐ๋ฐ์„œ ์ฒ˜๋ฆฌํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒŒํ•ด์ค€ ๋ถ€๋ถ„ ์ข‹์Šต๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,21 @@ +package nextstep.subway; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ControllerExceptionHandler { + + @ExceptionHandler(DataIntegrityViolationException.class) + public ResponseEntity handleIllegalArgsException(DataIntegrityViolationException e) { + return ResponseEntity.badRequest().build(); + } + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgumentException(IllegalArgumentException illegalArgumentException){ + String message = illegalArgumentException.getMessage(); + return ResponseEntity.badRequest().body(message); + } +}
Java
์ด ๋ถ€๋ถ„์€ ์กฐ๊ธˆ ๊ถ๊ธˆํ•˜์—ฌ ์—ฌ์ญค๋ด…๋‹ˆ๋‹ค! DataIntegrityViolationException ๋Š” ํ˜„์žฌ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ์–ธ์ œ, ์–ด๋–ค ์ผ€์ด์Šค์—์„œ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋‚˜์š”...?
@@ -0,0 +1,38 @@ +package nextstep.subway.line.domain; + +import java.util.ArrayList; +import java.util.List; +import nextstep.subway.station.domain.Station; +import org.jgrapht.GraphPath; +import org.jgrapht.alg.shortestpath.DijkstraShortestPath; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; + +public class Lines { + + List<Line> lines = new ArrayList<>(); + + public Lines(List<Line> lines) { + this.lines = lines; + } + + public GraphPath minPath(Station sourceStation, Station targetStation) { + + validatePath(sourceStation, targetStation); + WeightedMultigraph<Station, DefaultWeightedEdge> graph = new WeightedMultigraph(DefaultWeightedEdge.class); + + for (Line line : lines) { + line.addPathInfo(graph); + } + + DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(graph); + + return dijkstraShortestPath.getPath(sourceStation, targetStation); + } + + private void validatePath(Station sourceStation, Station targetStation){ + if(sourceStation.equals(targetStation)){ + throw new IllegalArgumentException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค."); + } + } +}
Java
์ ‘๊ทผ์ œํ•œ์ž๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,38 @@ +package nextstep.subway.line.domain; + +import java.util.ArrayList; +import java.util.List; +import nextstep.subway.station.domain.Station; +import org.jgrapht.GraphPath; +import org.jgrapht.alg.shortestpath.DijkstraShortestPath; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; + +public class Lines { + + List<Line> lines = new ArrayList<>(); + + public Lines(List<Line> lines) { + this.lines = lines; + } + + public GraphPath minPath(Station sourceStation, Station targetStation) { + + validatePath(sourceStation, targetStation); + WeightedMultigraph<Station, DefaultWeightedEdge> graph = new WeightedMultigraph(DefaultWeightedEdge.class); + + for (Line line : lines) { + line.addPathInfo(graph); + } + + DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(graph); + + return dijkstraShortestPath.getPath(sourceStation, targetStation); + } + + private void validatePath(Station sourceStation, Station targetStation){ + if(sourceStation.equals(targetStation)){ + throw new IllegalArgumentException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ๊ณผ ๋„์ฐฉ์—ญ์ด ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค."); + } + } +}
Java
๋™์‚ฌ๊ตฌ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ํ‘œํ˜„ํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”...? ๊ทธ๋ฆฌ๊ณ  ํ˜น์‹œ ์ œ๋„ˆ๋ฆญ์„ ์•ˆ์“ฐ์‹œ๋Š” ์ด์œ ๊ฐ€ ์žˆ์œผ์‹ค๊นŒ์š”...?
@@ -6,9 +6,11 @@ import javax.persistence.CascadeType; import javax.persistence.Embeddable; import javax.persistence.OneToMany; -import nextstep.subway.SectionsNotAddedException; -import nextstep.subway.SectionsNotRemovedException; +import nextstep.subway.line.exception.SectionsNotAddedException; +import nextstep.subway.line.exception.SectionsNotRemovedException; import nextstep.subway.station.domain.Station; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; @Embeddable public class Sections { @@ -34,41 +36,36 @@ public void requestAddSection(Section section) { sections.add(section); return; } - checkExistsStation(section, section.getUpStation(), section.getDownStation()); + checkExistsStation(section); } - private void checkExistsStation(Section section, Station upStation, Station downStation) { - boolean upMatchesLineUp = stationExistsInUp(upStation); - boolean downMatchsLineDown = stationExistsInDown(downStation); - boolean matchesAtFront = stationExistsInUp(downStation); - boolean matchesAtBack = stationExistsInDown(upStation); - if (!upMatchesLineUp && !matchesAtFront && !matchesAtBack && !downMatchsLineDown) { + private void checkExistsStation(Section section) { + boolean upMatchesLineUp = stationExistsInUp(section.getUpStation()); + boolean downMatchesLineDown = stationExistsInDown(section.getDownStation()); + boolean matchesAtEnd = stationExistsAtEnd(section); + + if (!upMatchesLineUp && !downMatchesLineDown && !matchesAtEnd) { throw new SectionsNotAddedException("๊ตฌ๊ฐ„ ์ค‘ ์–ด๋– ํ•œ ์—ญ๋„ ํ˜„์žฌ ๊ตฌ๊ฐ„์— ์—†์Šต๋‹ˆ๋‹ค."); } - if (upMatchesLineUp && downMatchsLineDown) { + if (upMatchesLineUp && downMatchesLineDown) { throw new SectionsNotAddedException("์ด๋ฏธ ๋“ฑ๋ก๋œ ๊ตฌ๊ฐ„ ์ž…๋‹ˆ๋‹ค."); } - addSectionToMatchStation(section, upStation, downStation, upMatchesLineUp, downMatchsLineDown, matchesAtFront, - matchesAtBack); - + addSectionToMatchStation(section, upMatchesLineUp, downMatchesLineDown); } - private void addSectionToMatchStation(Section section, Station upStation, Station downStation, - boolean upMatchesLineUp, boolean downMatchsLineDown, boolean matchesAtFront, - boolean matchesAtBack) { + private void addSectionToMatchStation(Section section, boolean upMatchesLineUp, boolean downMatchsLineDown) { + Station upStation = section.getUpStation(); + Station downStation = section.getDownStation(); if (upMatchesLineUp) { - addUpStationExists(section, upStation, downStation, section.getDistance()); - return; + updateUpStationMatch(section, upStation, downStation, section.getDistance()); } if (downMatchsLineDown) { - addDownStationExists(section, upStation, downStation, section.getDistance()); - return; + updateDownStatioMatch(section, upStation, downStation, section.getDistance()); } - if (matchesAtFront || matchesAtBack) { - sections.add(section); - } + //์–‘์ชฝ ๋์— ๋”ํ•ด์งˆ ๊ฒฝ์šฐ + sections.add(section); } private boolean stationExistsInUp(Station station) { @@ -79,33 +76,40 @@ private boolean stationExistsInDown(Station station) { return sections.stream().anyMatch(section -> section.getDownStation().equals(station)); } + private boolean stationExistsAtEnd(Section section) { + return sections.stream().anyMatch( + sectionIterate -> sectionIterate.getDownStation().equals(section.getUpStation()) + || sectionIterate.getUpStation().equals(section.getDownStation())); + } - private void addUpStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateUpStationMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> upStation.equals(sectionIterate.getUpStation())).findFirst() .ifPresent(sectionIterate -> sectionIterate.updateUpStation(downStation, distance)); - sections.add(section); } - private void addDownStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateDownStatioMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> downStation.equals(sectionIterate.getDownStation())).findFirst() .ifPresent(it -> it.updateDownStation(upStation, distance)); - sections.add(section); } + public void removeLineStation(Station station, Line line) { if (sections.size() <= ONE) { throw new SectionsNotRemovedException("ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ํ•˜๋‚˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค."); } - Optional<Section> upLineStation = sections.stream().filter(it -> it.getUpStation() == station).findFirst(); - Optional<Section> downLineStation = sections.stream().filter(it -> it.getDownStation() == station).findFirst(); + Optional<Section> upLineStation = sections.stream().filter(section -> section.getUpStation().equals(station)) + .findFirst(); + Optional<Section> downLineStation = sections.stream() + .filter(section -> section.getDownStation().equals(station)).findFirst(); connectFrontBackSection(line, upLineStation, downLineStation); - upLineStation.ifPresent(it -> sections.remove(it)); - downLineStation.ifPresent(it -> sections.remove(it)); + upLineStation.ifPresent(section -> sections.remove(section)); + downLineStation.ifPresent(section -> sections.remove(section)); } - private void connectFrontBackSection(Line line, Optional<Section> upLineStation, Optional<Section> downLineStation) { + private void connectFrontBackSection(Line line, Optional<Section> upLineStation, + Optional<Section> downLineStation) { if (upLineStation.isPresent() && downLineStation.isPresent()) { Station newUpStation = downLineStation.get().getUpStation(); Station newDownStation = upLineStation.get().getDownStation(); @@ -114,4 +118,11 @@ private void connectFrontBackSection(Line line, Optional<Section> upLineStation, sections.add(new Section(line, newUpStation, newDownStation, newDistance)); } } + + public void addGraphEdge(WeightedMultigraph<Station, DefaultWeightedEdge> graph) { + for (Section section : sections) { + section.addGraphEdge(graph); + } + } + } \ No newline at end of file
Java
์ด๋ถ€๋ถ„๋„ Section์— ๋ฉ”์‹œ์ง€๋ฅผ ๋˜์ ธ์„œ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -6,9 +6,11 @@ import javax.persistence.CascadeType; import javax.persistence.Embeddable; import javax.persistence.OneToMany; -import nextstep.subway.SectionsNotAddedException; -import nextstep.subway.SectionsNotRemovedException; +import nextstep.subway.line.exception.SectionsNotAddedException; +import nextstep.subway.line.exception.SectionsNotRemovedException; import nextstep.subway.station.domain.Station; +import org.jgrapht.graph.DefaultWeightedEdge; +import org.jgrapht.graph.WeightedMultigraph; @Embeddable public class Sections { @@ -34,41 +36,36 @@ public void requestAddSection(Section section) { sections.add(section); return; } - checkExistsStation(section, section.getUpStation(), section.getDownStation()); + checkExistsStation(section); } - private void checkExistsStation(Section section, Station upStation, Station downStation) { - boolean upMatchesLineUp = stationExistsInUp(upStation); - boolean downMatchsLineDown = stationExistsInDown(downStation); - boolean matchesAtFront = stationExistsInUp(downStation); - boolean matchesAtBack = stationExistsInDown(upStation); - if (!upMatchesLineUp && !matchesAtFront && !matchesAtBack && !downMatchsLineDown) { + private void checkExistsStation(Section section) { + boolean upMatchesLineUp = stationExistsInUp(section.getUpStation()); + boolean downMatchesLineDown = stationExistsInDown(section.getDownStation()); + boolean matchesAtEnd = stationExistsAtEnd(section); + + if (!upMatchesLineUp && !downMatchesLineDown && !matchesAtEnd) { throw new SectionsNotAddedException("๊ตฌ๊ฐ„ ์ค‘ ์–ด๋– ํ•œ ์—ญ๋„ ํ˜„์žฌ ๊ตฌ๊ฐ„์— ์—†์Šต๋‹ˆ๋‹ค."); } - if (upMatchesLineUp && downMatchsLineDown) { + if (upMatchesLineUp && downMatchesLineDown) { throw new SectionsNotAddedException("์ด๋ฏธ ๋“ฑ๋ก๋œ ๊ตฌ๊ฐ„ ์ž…๋‹ˆ๋‹ค."); } - addSectionToMatchStation(section, upStation, downStation, upMatchesLineUp, downMatchsLineDown, matchesAtFront, - matchesAtBack); - + addSectionToMatchStation(section, upMatchesLineUp, downMatchesLineDown); } - private void addSectionToMatchStation(Section section, Station upStation, Station downStation, - boolean upMatchesLineUp, boolean downMatchsLineDown, boolean matchesAtFront, - boolean matchesAtBack) { + private void addSectionToMatchStation(Section section, boolean upMatchesLineUp, boolean downMatchsLineDown) { + Station upStation = section.getUpStation(); + Station downStation = section.getDownStation(); if (upMatchesLineUp) { - addUpStationExists(section, upStation, downStation, section.getDistance()); - return; + updateUpStationMatch(section, upStation, downStation, section.getDistance()); } if (downMatchsLineDown) { - addDownStationExists(section, upStation, downStation, section.getDistance()); - return; + updateDownStatioMatch(section, upStation, downStation, section.getDistance()); } - if (matchesAtFront || matchesAtBack) { - sections.add(section); - } + //์–‘์ชฝ ๋์— ๋”ํ•ด์งˆ ๊ฒฝ์šฐ + sections.add(section); } private boolean stationExistsInUp(Station station) { @@ -79,33 +76,40 @@ private boolean stationExistsInDown(Station station) { return sections.stream().anyMatch(section -> section.getDownStation().equals(station)); } + private boolean stationExistsAtEnd(Section section) { + return sections.stream().anyMatch( + sectionIterate -> sectionIterate.getDownStation().equals(section.getUpStation()) + || sectionIterate.getUpStation().equals(section.getDownStation())); + } - private void addUpStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateUpStationMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> upStation.equals(sectionIterate.getUpStation())).findFirst() .ifPresent(sectionIterate -> sectionIterate.updateUpStation(downStation, distance)); - sections.add(section); } - private void addDownStationExists(Section section, Station upStation, Station downStation, int distance) { + private void updateDownStatioMatch(Section section, Station upStation, Station downStation, int distance) { sections.stream().filter(sectionIterate -> downStation.equals(sectionIterate.getDownStation())).findFirst() .ifPresent(it -> it.updateDownStation(upStation, distance)); - sections.add(section); } + public void removeLineStation(Station station, Line line) { if (sections.size() <= ONE) { throw new SectionsNotRemovedException("ํ˜„์žฌ ๊ตฌ๊ฐ„์ด ํ•˜๋‚˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค."); } - Optional<Section> upLineStation = sections.stream().filter(it -> it.getUpStation() == station).findFirst(); - Optional<Section> downLineStation = sections.stream().filter(it -> it.getDownStation() == station).findFirst(); + Optional<Section> upLineStation = sections.stream().filter(section -> section.getUpStation().equals(station)) + .findFirst(); + Optional<Section> downLineStation = sections.stream() + .filter(section -> section.getDownStation().equals(station)).findFirst(); connectFrontBackSection(line, upLineStation, downLineStation); - upLineStation.ifPresent(it -> sections.remove(it)); - downLineStation.ifPresent(it -> sections.remove(it)); + upLineStation.ifPresent(section -> sections.remove(section)); + downLineStation.ifPresent(section -> sections.remove(section)); } - private void connectFrontBackSection(Line line, Optional<Section> upLineStation, Optional<Section> downLineStation) { + private void connectFrontBackSection(Line line, Optional<Section> upLineStation, + Optional<Section> downLineStation) { if (upLineStation.isPresent() && downLineStation.isPresent()) { Station newUpStation = downLineStation.get().getUpStation(); Station newDownStation = upLineStation.get().getDownStation(); @@ -114,4 +118,11 @@ private void connectFrontBackSection(Line line, Optional<Section> upLineStation, sections.add(new Section(line, newUpStation, newDownStation, newDistance)); } } + + public void addGraphEdge(WeightedMultigraph<Station, DefaultWeightedEdge> graph) { + for (Section section : sections) { + section.addGraphEdge(graph); + } + } + } \ No newline at end of file
Java
์—ฌ๊ธฐ๊นŒ์ง€ ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ ์ข‹๋„ค์š” ใ…Žใ…Ž ๐Ÿ‘
@@ -0,0 +1,38 @@ +package nextstep.subway.path.application; + +import nextstep.subway.station.exception.StationNotFoundException; +import nextstep.subway.line.domain.LineRepository; +import nextstep.subway.line.domain.Lines; +import nextstep.subway.path.dto.PathResponse; +import nextstep.subway.station.domain.Station; +import nextstep.subway.station.domain.StationRepository; +import org.jgrapht.GraphPath; +import org.springframework.stereotype.Service; + +@Service +public class PathService { + + private final StationRepository stationRepository; + private final LineRepository lineRepository; + + public PathService(StationRepository stationRepository, LineRepository lineRepository) { + this.stationRepository = stationRepository; + this.lineRepository = lineRepository; + } + + public PathResponse minPath(Long sourceStationId, Long targetStationId) { + System.out.println("PathService.minPath"); + + Station sourceStation = stationRepository.findById(sourceStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + Station targetStation = stationRepository.findById(targetStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ๋„์ฐฉ์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + + Lines lines = new Lines(lineRepository.findAll()); + GraphPath graphPath = lines.minPath(sourceStation, targetStation); + + PathResponse pathResponse = new PathResponse(graphPath.getVertexList(), (int) graphPath.getWeight()); + + return pathResponse; + } +}
Java
์—ฌ๊ธฐ์— ํ˜น์‹œ System ํ•จ์ˆ˜๋ฅผ ์“ฐ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”...? Sysytem ํ•จ์ˆ˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŽ์ด ๋จน์–ด์„œ ์ตœ๋Œ€ํ•œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,38 @@ +package nextstep.subway.path.application; + +import nextstep.subway.station.exception.StationNotFoundException; +import nextstep.subway.line.domain.LineRepository; +import nextstep.subway.line.domain.Lines; +import nextstep.subway.path.dto.PathResponse; +import nextstep.subway.station.domain.Station; +import nextstep.subway.station.domain.StationRepository; +import org.jgrapht.GraphPath; +import org.springframework.stereotype.Service; + +@Service +public class PathService { + + private final StationRepository stationRepository; + private final LineRepository lineRepository; + + public PathService(StationRepository stationRepository, LineRepository lineRepository) { + this.stationRepository = stationRepository; + this.lineRepository = lineRepository; + } + + public PathResponse minPath(Long sourceStationId, Long targetStationId) { + System.out.println("PathService.minPath"); + + Station sourceStation = stationRepository.findById(sourceStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + Station targetStation = stationRepository.findById(targetStationId) + .orElseThrow(() -> new StationNotFoundException("๊ฒฝ๋กœ ๋„์ฐฉ์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + + Lines lines = new Lines(lineRepository.findAll()); + GraphPath graphPath = lines.minPath(sourceStation, targetStation); + + PathResponse pathResponse = new PathResponse(graphPath.getVertexList(), (int) graphPath.getWeight()); + + return pathResponse; + } +}
Java
Repository ์กฐํšŒํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ readOnly ์˜ต์…˜์„ ์ฃผ๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”....?
@@ -1,9 +1,111 @@ package nextstep.subway.path; +import static nextstep.subway.line.acceptance.LineAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ; +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.Map; import nextstep.subway.AcceptanceTest; +import nextstep.subway.line.acceptance.LineSectionAcceptanceTest; +import nextstep.subway.line.dto.LineRequest; +import nextstep.subway.line.dto.LineResponse; +import nextstep.subway.station.StationAcceptanceTest; +import nextstep.subway.station.dto.StationResponse; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; @DisplayName("์ง€ํ•˜์ฒ  ๊ฒฝ๋กœ ์กฐํšŒ") public class PathAcceptanceTest extends AcceptanceTest { + + private LineResponse ์‹ ๋ถ„๋‹น์„ ; + private LineResponse ์ดํ˜ธ์„ ; + private LineResponse ์‚ผํ˜ธ์„ ; + private StationResponse ๊ฐ•๋‚จ์—ญ; + private StationResponse ์–‘์žฌ์—ญ; + private StationResponse ๊ต๋Œ€์—ญ; + private StationResponse ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ; + + /** + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ + * | | + * *3ํ˜ธ์„ * *์‹ ๋ถ„๋‹น์„ * + * | | + * ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + */ + @BeforeEach + public void setUp() { + super.setUp(); + + ๊ฐ•๋‚จ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ฐ•๋‚จ์—ญ").as(StationResponse.class); + ์–‘์žฌ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("์–‘์žฌ์—ญ").as(StationResponse.class); + ๊ต๋Œ€์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ต๋Œ€์—ญ").as(StationResponse.class); + ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ").as(StationResponse.class); + + ์‹ ๋ถ„๋‹น์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‹ ๋ถ„๋‹น์„ ", "bg-red-600", ๊ฐ•๋‚จ์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 10)).as( + LineResponse.class); + ์ดํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์ดํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ๊ฐ•๋‚จ์—ญ.getId(), 10)).as(LineResponse.class); + ์‚ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‚ผํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 5)).as(LineResponse.class); + + ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(์‚ผํ˜ธ์„ , ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, 3); + } + + private ExtractableResponse<Response> ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(LineResponse lineResponse, StationResponse upStation, + StationResponse downStation, + int distance) { + return LineSectionAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก_์š”์ฒญ(lineResponse, + upStation, downStation, distance); + } + + @Test + @DisplayName("๊ต๋Œ€์—ญ-์–‘์žฌ์—ญ ์ตœ๋‹จ๊ฒฝ๋กœ ์กฐํšŒํ•˜๊ธฐ (๊ต๋Œ€์—ญ-๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ-์–‘์žฌ์—ญ)") + public void findMinPath() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ์–‘์žฌ์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ, 5, "๊ต๋Œ€์—ญ","๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ", "์–‘์žฌ์—ญ"); + } + + + @Test + @DisplayName("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ ๋„์ฐฉ์—ญ ์ผ์น˜ ์กฐํšŒ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathSourceTargetSame() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ๊ต๋Œ€์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + @Test + @DisplayName("๊ฒฝ๋กœ ์กฐํšŒ์‹œ ๋ฏธ์กด์žฌ ์—ญ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathNotExsistStation() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(new StationResponse(), new StationResponse()); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(ExtractableResponse<Response> response) { + assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(ExtractableResponse<Response> response,int distance, String... stationNames) { + assertThat(response.jsonPath().getInt("distance")).isEqualTo(distance); + assertThat(response.jsonPath().getList("stations.name")).containsExactly(stationNames); + } + + private ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(StationResponse source, StationResponse target) { + Map<String, Long> param = new HashMap<>(); + param.put("source", source.getId()); + param.put("target", target.getId()); + + return RestAssured + .given().log().all() + .queryParams(param) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when().get("/paths") + .then().log().all() + .extract(); + } + }
Java
์ธ์ˆ˜ํ…Œ์ŠคํŠธ ์ข‹์Šต๋‹ˆ๋‹ค ๐Ÿ‘ ๋‹ค๋งŒ ์ด๋ฒˆ์— ์ƒˆ๋กœ๋งŒ๋“œ์‹  Lines์— ๋Œ€ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ, PathService์— ๋Œ€ํ•œ ๋‹จ์œ„ํ…Œ์ŠคํŠธ๋„ ์ถ”๊ฐ€๋˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! Lines์˜ ๊ฒฝ์šฐ ์‹ค์ œ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ํ•ด๋‹น ์‹ค์ œ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , PathService์˜ ๊ฒฝ์šฐ๋Š” ์™ธ๋ถ€ ์˜์กด์„ฑ์„ Mocking ํ•œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๊ณต๋ถ€๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -1,9 +1,111 @@ package nextstep.subway.path; +import static nextstep.subway.line.acceptance.LineAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ; +import static org.assertj.core.api.Assertions.assertThat; + +import io.restassured.RestAssured; +import io.restassured.response.ExtractableResponse; +import io.restassured.response.Response; +import java.util.HashMap; +import java.util.Map; import nextstep.subway.AcceptanceTest; +import nextstep.subway.line.acceptance.LineSectionAcceptanceTest; +import nextstep.subway.line.dto.LineRequest; +import nextstep.subway.line.dto.LineResponse; +import nextstep.subway.station.StationAcceptanceTest; +import nextstep.subway.station.dto.StationResponse; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; @DisplayName("์ง€ํ•˜์ฒ  ๊ฒฝ๋กœ ์กฐํšŒ") public class PathAcceptanceTest extends AcceptanceTest { + + private LineResponse ์‹ ๋ถ„๋‹น์„ ; + private LineResponse ์ดํ˜ธ์„ ; + private LineResponse ์‚ผํ˜ธ์„ ; + private StationResponse ๊ฐ•๋‚จ์—ญ; + private StationResponse ์–‘์žฌ์—ญ; + private StationResponse ๊ต๋Œ€์—ญ; + private StationResponse ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ; + + /** + * ๊ต๋Œ€์—ญ --- *2ํ˜ธ์„ * --- ๊ฐ•๋‚จ์—ญ + * | | + * *3ํ˜ธ์„ * *์‹ ๋ถ„๋‹น์„ * + * | | + * ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ --- *3ํ˜ธ์„ * --- ์–‘์žฌ + */ + @BeforeEach + public void setUp() { + super.setUp(); + + ๊ฐ•๋‚จ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ฐ•๋‚จ์—ญ").as(StationResponse.class); + ์–‘์žฌ์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("์–‘์žฌ์—ญ").as(StationResponse.class); + ๊ต๋Œ€์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๊ต๋Œ€์—ญ").as(StationResponse.class); + ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ = StationAcceptanceTest.์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ("๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ").as(StationResponse.class); + + ์‹ ๋ถ„๋‹น์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‹ ๋ถ„๋‹น์„ ", "bg-red-600", ๊ฐ•๋‚จ์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 10)).as( + LineResponse.class); + ์ดํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์ดํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ๊ฐ•๋‚จ์—ญ.getId(), 10)).as(LineResponse.class); + ์‚ผํ˜ธ์„  = ์ง€ํ•˜์ฒ _๋…ธ์„ _๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(new LineRequest("์‚ผํ˜ธ์„ ", "bg-red-600", ๊ต๋Œ€์—ญ.getId(), ์–‘์žฌ์—ญ.getId(), 5)).as(LineResponse.class); + + ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(์‚ผํ˜ธ์„ , ๊ต๋Œ€์—ญ, ๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ, 3); + } + + private ExtractableResponse<Response> ์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก๋˜์–ด_์žˆ์Œ(LineResponse lineResponse, StationResponse upStation, + StationResponse downStation, + int distance) { + return LineSectionAcceptanceTest.์ง€ํ•˜์ฒ _๋…ธ์„ ์—_์ง€ํ•˜์ฒ ์—ญ_๋“ฑ๋ก_์š”์ฒญ(lineResponse, + upStation, downStation, distance); + } + + @Test + @DisplayName("๊ต๋Œ€์—ญ-์–‘์žฌ์—ญ ์ตœ๋‹จ๊ฒฝ๋กœ ์กฐํšŒํ•˜๊ธฐ (๊ต๋Œ€์—ญ-๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ-์–‘์žฌ์—ญ)") + public void findMinPath() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ์–‘์žฌ์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ, 5, "๊ต๋Œ€์—ญ","๋‚จ๋ถ€ํ„ฐ๋ฏธ๋„์—ญ", "์–‘์žฌ์—ญ"); + } + + + @Test + @DisplayName("๊ฒฝ๋กœ ์‹œ์ž‘์—ญ ๋„์ฐฉ์—ญ ์ผ์น˜ ์กฐํšŒ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathSourceTargetSame() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(๊ต๋Œ€์—ญ, ๊ต๋Œ€์—ญ); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + @Test + @DisplayName("๊ฒฝ๋กœ ์กฐํšŒ์‹œ ๋ฏธ์กด์žฌ ์—ญ ์—๋Ÿฌ ํ™•์ธ") + public void findMinPathNotExsistStation() { + ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ = ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(new StationResponse(), new StationResponse()); + ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญ๊ฒฐ๊ณผ); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_์š”์ฒญ๊ฒฐ๊ณผ_์—๋Ÿฌ(ExtractableResponse<Response> response) { + assertThat(response.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + private void ์ตœ๋‹จ_๊ฒฝ๋กœ_๋ชฉ๋ก_๊ฒ€์ฆ(ExtractableResponse<Response> response,int distance, String... stationNames) { + assertThat(response.jsonPath().getInt("distance")).isEqualTo(distance); + assertThat(response.jsonPath().getList("stations.name")).containsExactly(stationNames); + } + + private ExtractableResponse<Response> ์ตœ๋‹จ๊ฒฝ๋กœ์กฐํšŒ_์š”์ฒญํ•˜๊ธฐ(StationResponse source, StationResponse target) { + Map<String, Long> param = new HashMap<>(); + param.put("source", source.getId()); + param.put("target", target.getId()); + + return RestAssured + .given().log().all() + .queryParams(param) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .when().get("/paths") + .then().log().all() + .extract(); + } + }
Java
์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์—ญ์— ๋Œ€ํ•œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์กฐํšŒ์˜ ๊ฒฝ์šฐ์—๋„ ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”...?
@@ -55,6 +55,7 @@ captures/ .idea/caches # Keystore files +keystore.properties # Uncomment the following line if you do not want to check your keystore files in. #*.jks @@ -100,3 +101,6 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk +app/src/main/java/kr/co/connect/boostcamp/livewhere/ui/bookmark/ +app/src/main/java/kr/co/connect/boostcamp/livewhere/ui/search/ +app/src/main/res/layout-v26/
Unknown
#82 ์ž‘์—…์ด ์–ด๋–ค๊ฑด์ง€ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ์—†์–ด์„œ ์™œ ์ถ”๊ฐ€๋˜์—ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ 1) ๋ณด์•ˆ๊ณผ ๊ด€๋ จ๋˜์–ด ๊ณต์œ ๋˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ํŒŒ์ผ 2) ๊ฐœ์ธ๋ณ„ ํ™˜๊ฒฝ์„ค์ • ํŒŒ์ผ 3) ์บ์‹œ ํŒŒ์ผ 4) ์“ฐ๋ ˆ๊ธฐ ํŒŒ์ผ ๋“ฑ์˜ ์‚ฌ์œ ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด .gitignore์— ๋“ค์–ด๊ฐ€๋Š”๊ฒŒ ๋ถ€์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํด๋”๋ฅผ git์—์„œ ์ œ๊ฑฐํ•˜๋ ค๋‹ค ํŽธ๋ฒ•์„ ์“ด๊ฒŒ ์•„๋‹๊นŒ ์ถ”์ธก์ด ๋˜๋Š”๋ฐ ์˜ฌ๋ฐ”๋ฅด์ง€ ๋ชปํ•œ ๋ฐฉ๋ฒ•์ธ๊ฑฐ ๊ฐ™์•„์š”.
@@ -0,0 +1,68 @@ +# ์„œ์šธ ์‚ด์ด <img width="85" alt="kakaotalk_20190212_204520833" src="https://user-images.githubusercontent.com/22374750/52834626-1efe2080-3126-11e9-92c3-66ac08c1f2c9.png"> +![Seoul42 Service](https://img.shields.io/badge/service-android-green.svg) +![Seoul42 Pattern](https://img.shields.io/badge/pattern-MVVM-blue.svg) +[![Seoul42 DI](https://img.shields.io/badge/DI-koin-blue.svg)](https://insert-koin.io) +[![Seoul42_UI](https://img.shields.io/badge/UI-zeplin-blue.svg)](https://app.zeplin.io/project/5c4db2597a8bebbfe8be9d39/dashboard) + +[![Seoul42_UI](https://img.shields.io/badge/library-RxJava2-blue.svg)](https://github.com/amitshekhariitbhu/RxJava2-Android-Samples) +[![Seoul42_UI](https://img.shields.io/badge/library-Room-blue.svg)](https://developer.android.com/topic/libraries/architecture/room.html) +[![Seoul42_UI](https://img.shields.io/badge/library-LiveData-blue.svg)](https://developer.android.com/topic/libraries/architecture/livedata.html) + +booscamp3_CํŒ€์˜ ์„œ์šธ์‚ด์ด repository์ž…๋‹ˆ๋‹ค. ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋Š” MVVM๊ธฐ๋ฐ˜์˜ Androidํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค. +์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•ด์„œ koin์ด ์‚ฌ์šฉ๋˜์—ˆ๊ณ , RxJava, Room, LiveData, MotionLayout๋“ฑ ๊พธ์ค€ํ•œ ํ•™์Šต์— ์˜ํ•ด์„œ ์ง„ํ–‰๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. +<hr/> + +#### ํŒ€์› ์†Œ๊ฐœ : ๋ฌธ๋ณ‘ํ•™, ์œ ์ง€์›, ์ตœ์ค€์˜(๋งํฌ ์ถ”๊ฐ€ ์˜ˆ์ •) + +์„œ์šธ์‚ด์ด๋Š” ์„œ์šธ์‚ด์ด๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ์ฃผํƒ์— ๊ด€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ž…๋‹ˆ๋‹ค. + +<p>ํ˜„์žฌ ์•„ํŒŒํŠธ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์กด์žฌํ•˜์ง€๋งŒ, ์ƒ๋Œ€์ ์œผ๋กœ ์ฃผํƒ์— ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. +๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ๋ถ€์กฑํ•œ ์ •๋ณด๋ฅผ ์„œ์šธ์‚ด์ด๋ผ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ†ตํ•ด์„œ ์ฃผํƒ์— ๊ด€๋ จ๋œ ์ „/์›”์„ธ, ์ฃผ๋ณ€ ์ง€์—ญ ์ •๋ณด, ์ฃผํƒ ์ •๋ณด๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.</p> +<hr/> + + +#### ํ”„๋กœํ† ํƒ€์ž… UI: [![Seoul42_UI](https://img.shields.io/badge/UI-zeplin-blue.svg)](https://app.zeplin.io/project/5c4db2597a8bebbfe8be9d39/dashboard) +![image](https://user-images.githubusercontent.com/22374750/52126227-17dd0a00-2672-11e9-9678-2cf1e2aeb6fc.png) + +ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋Š” ํ˜‘์—…ํˆด์„ Zeplin์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ตฌ์ฒด์ ์ธ ๋‚ด์šฉ์€ ํ•ด๋‹น ๋ฑƒ์ง€๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.<br> +์ €ํฌ๋Š” Zeplin์„ ํ†ตํ•ด์„œ Style ๊ฐ€์ด๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๊ณ , ๊ฐ์ข… xml์˜ ๊ฐ€์ด๋“œ ๊ธฐ์ค€์„ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. + +#### ํŒจํ‚ค์ง€ ์†Œ๊ฐœ: +1. **api**: Retrofit์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ interface๋ฅผ ๋ชจ์•„๋‘๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. +2. **di**: Koin์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด์„ฑ ์ฃผ์ž…์„ ํ–ˆ๊ณ , ํ•ด๋‹น ํŒจํ‚ค์ง€์— di ๊ด€๋ จ ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. +3. **firebase**: Firebase์— ๊ด€๋ จํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ํด๋ž˜์Šค๋ฅผ ๋ชจ์•„๋‘๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. +4. **model**: ์ฃผ์†Œ, ๋ถ๋งˆํฌ, ์ง‘์ •๋ณด, ๊ฐ€๊ฒฉ๋“ฑ์— ๊ด€๋ จํ•œ ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๋ฅผ ๋ชจ์•„๋‘๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. +5. **repository**: ViewModel๊ณผ Model์‚ฌ์ด์— repositoryํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ข… ๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” ํด๋ž˜์Šค๋ฅผ ๋ชจ์•„๋‘” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. +6. **ui**: View์— ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋“ค์„ ๋ชจ์•„๋‘๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. +7. **util**: ํ”„๋กœ์ ํŠธ๋‚ด์—์„œ ๊ฐ์ข… ์œ ํ‹ธ๋ฆฌํ‹ฐ์„ฑ ํด๋ž˜์Šค๋ฅผ ๋ชจ์•„๋‘๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. + +### [๊ธฐํš์•ˆ](https://drive.google.com/file/d/1ui4lvMc81kCAki4UVtxirsg0szLEbqD2/view?usp=sharing) +### [์ผ์ •๊ด€๋ฆฌ](https://docs.google.com/spreadsheets/d/1nQlae8ONeO42Rk9Pr0tZFxilmCsfuNRmOOc4p7cFlYY/edit?usp=sharing) +### [UI๊ธฐํš](https://drive.google.com/file/d/13BLtMr3i-YnhjuDIkYmLRVolUX6OQnIu/view?usp=sharing) +### [๊ธฐ๋Šฅ๋ช…์„ธ์„œ](https://docs.google.com/spreadsheets/d/1Y4Xpb8lSP5qQ53e1NPewsZMmYud5io1H1SQxxZZOmY4/edit?usp=sharing) + +### [์„œ๋ฒ„](https://github.com/seoul42/seoul42-server) +<hr/> + +## 1์ฃผ์ฐจ ์‚ฐ์ถœ๋ฌผ +[DOCS](https://github.com/boostcampth/boostcamp3_C/tree/dev/docs) ๋ฌธ์„œ ์ž‘์—… (๊ธฐํš์„œ, ๊ธฐ๋Šฅ์ •์˜์„œ, ํ”„๋กœ์ ํŠธ ์ผ์ •, Api๋ช…์„ธ์„œ) + +Zeplin[![Seoul42_UI](https://img.shields.io/badge/UI-zeplin-blue.svg)](https://app.zeplin.io/project/5c4db2597a8bebbfe8be9d39/dashboard) +![image](https://user-images.githubusercontent.com/22374750/52126227-17dd0a00-2672-11e9-9678-2cf1e2aeb6fc.png) + +## 2์ฃผ์ฐจ ์‚ฐ์ถœ๋ฌผ + +Seoul42 API [![Seoul42_API](https://img.shields.io/badge/API-SwaggerHub-blue.svg)](https://app.zeplin.io/project/5c4db2597a8bebbfe8be9d39/dashboard) +![image](https://user-images.githubusercontent.com/22374750/52177071-3f151200-27ff-11e9-8b58-ebc916b21c75.png) + +### 2์ฃผ์ฐจ ํšŒ๊ณ ๋ก +![kakaotalk_20190214_181143100](https://user-images.githubusercontent.com/22374750/52834631-26bdc500-3126-11e9-9b4e-1c2eb4ff5895.jpg) +![kakaotalk_20190214_181143373](https://user-images.githubusercontent.com/22374750/52834632-26bdc500-3126-11e9-8f68-95c67e153998.jpg) +์ค€์˜๋‹˜ ํŒŒ์ผ ์žฌ์—…๋กœ๋“œ ํ•„์š” + +## 3์ฃผ์ฐจ ์‚ฐ์ถœ๋ฌผ + +### 3์ฃผ์ฐจ ํšŒ๊ณ ๋ก +![kakaotalk_20190214_181144018](https://user-images.githubusercontent.com/22374750/52834634-27565b80-3126-11e9-98fa-45f600706af6.jpg) +![kakaotalk_20190214_181144329](https://user-images.githubusercontent.com/22374750/52834636-27565b80-3126-11e9-8b02-ccb4046a729f.jpg) +![kakaotalk_20190214_181144629](https://user-images.githubusercontent.com/22374750/52834637-27565b80-3126-11e9-8a4c-f851f327d334.jpg)
Unknown
์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฏธ์ง€์™€ ๋งํฌ๋ฅผ ์ž˜ ๋„ฃ์–ด์ฃผ์…”์„œ ์–ด๋–ค ํ”„๋กœ์ ํŠธ์ธ์ง€ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ข‹๋„ค์š” :+1:
@@ -1,21 +1,54 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="kr.co.connect.boostcamp.livewhere"> - + xmlns:tools="http://schemas.android.com/tools" package="kr.co.connect.boostcamp.livewhere"> + <uses-permission android:name="android.permission.INTERNET"/> + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> + <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> + <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application - android:allowBackup="true" - android:icon="@mipmap/ic_launcher" + android:name=".LiveApplication" + android:allowBackup="false" + android:icon="@drawable/icon_logo" + android:roundIcon="@drawable/icon_logo" android:label="@string/app_name" - android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - <activity android:name=".MainActivity"> + + <uses-library android:name="org.apache.http.legacy" + android:required="false"/> + + <meta-data + android:name="com.google.android.geo.API_KEY" + android:value="@string/key_street_view"/> + <activity android:name=".ui.main.SplashActivity" + android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> + <activity android:name=".ui.login.LoginActivity"> + + </activity> + <activity android:name=".ui.main.HomeActivity"> + </activity> + <activity android:name=".ui.detail.DetailActivity" + android:windowSoftInputMode="adjustResize"> + + </activity> + <activity android:name=".ui.map.MapActivity" + android:screenOrientation="portrait"> + <intent-filter> + <action android:name="android.intent.action.VIEW"/> + + <category android:name="android.intent.category.LAUNCHER"/> + </intent-filter> + </activity> + <activity android:name=".ui.map.StreetMapActivity" + android:screenOrientation="portrait"> + </activity> + </application> </manifest> \ No newline at end of file
Unknown
`android:screenOrientation` ์†์„ฑ์ด ์ผ๋ถ€ Activity์—๋งŒ ์ ์šฉ๋˜์–ด ์žˆ๋Š”๋ฐ ์˜๋„ํ•˜์‹ ๊ฒŒ ์•„๋‹ˆ๋ผ ๋น ํŠธ๋ฆฌ์‹ ๊ฑฐ ๊ฐ™๋„ค์š”
@@ -0,0 +1,45 @@ +package kr.co.connect.boostcamp.livewhere + +import android.content.Context +import androidx.multidex.MultiDex +import androidx.multidex.MultiDexApplication +import com.bumptech.glide.Glide +import com.crashlytics.android.Crashlytics +import com.facebook.stetho.Stetho +import com.naver.maps.map.NaverMapSdk +import io.fabric.sdk.android.Fabric +import kr.co.connect.boostcamp.livewhere.di.appModules +import org.koin.android.ext.android.startKoin + + +// FIXME MultiDex ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•  ์—ฌ์ง€๊ฐ€ ์ถฉ๋ถ„ํ•˜๊ธฐ๋•Œ๋ฌธ์— gradle๊ณผ Applicationํด๋ž˜์Šค์—์„œ Multidex ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•ด์ฃผ์„ธ์š” +class LiveApplication : MultiDexApplication() { + override fun onCreate() { + super.onCreate() + //debug๋ณ€์ˆ˜๋ฅผ ๋†“๊ณ  debug ์ธ์ง€ ์บ์น˜ํ•จ. + if (BuildConfig.isDebug) { + Stetho.initializeWithDefaults(this) + } + startKoin(this, appModules) + //Firebase Crashlytics + Fabric.with(this, Crashlytics()) + NaverMapSdk.getInstance(this).client = NaverMapSdk.NaverCloudPlatformClient(BuildConfig.NaverClientId) + } + + //application class์—์„œ MultiDex ์‚ฌ์šฉ + override fun attachBaseContext(base: Context?) { + super.attachBaseContext(base) + MultiDex.install(this) + } + + + override fun onLowMemory() { + super.onLowMemory() + Glide.get(this).clearMemory() + } + + override fun onTrimMemory(level: Int) { + super.onTrimMemory(level) + Glide.get(this).trimMemory(level) + } +} \ No newline at end of file
Kotlin
https://github.com/boostcamp-3rd/android_teamC/pull/8/files#diff-51a0b488f963eb0be6c6599bf5df497313877cf5bdff3950807373912ac1cdc9R21 ์ด๋ฏธ multidex ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ ์ฃผ์„์ด ๋‚จ์•„์žˆ๋Š”๊ฑด ์ฝ”๋“œ๋ฅผ ์ฝ๋Š”๋ฐ ์–ด๋ ค์›€์„ ์ฃผ๋‹ˆ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”.
@@ -0,0 +1,45 @@ +package kr.co.connect.boostcamp.livewhere + +import android.content.Context +import androidx.multidex.MultiDex +import androidx.multidex.MultiDexApplication +import com.bumptech.glide.Glide +import com.crashlytics.android.Crashlytics +import com.facebook.stetho.Stetho +import com.naver.maps.map.NaverMapSdk +import io.fabric.sdk.android.Fabric +import kr.co.connect.boostcamp.livewhere.di.appModules +import org.koin.android.ext.android.startKoin + + +// FIXME MultiDex ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒํ•  ์—ฌ์ง€๊ฐ€ ์ถฉ๋ถ„ํ•˜๊ธฐ๋•Œ๋ฌธ์— gradle๊ณผ Applicationํด๋ž˜์Šค์—์„œ Multidex ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•ด์ฃผ์„ธ์š” +class LiveApplication : MultiDexApplication() { + override fun onCreate() { + super.onCreate() + //debug๋ณ€์ˆ˜๋ฅผ ๋†“๊ณ  debug ์ธ์ง€ ์บ์น˜ํ•จ. + if (BuildConfig.isDebug) { + Stetho.initializeWithDefaults(this) + } + startKoin(this, appModules) + //Firebase Crashlytics + Fabric.with(this, Crashlytics()) + NaverMapSdk.getInstance(this).client = NaverMapSdk.NaverCloudPlatformClient(BuildConfig.NaverClientId) + } + + //application class์—์„œ MultiDex ์‚ฌ์šฉ + override fun attachBaseContext(base: Context?) { + super.attachBaseContext(base) + MultiDex.install(this) + } + + + override fun onLowMemory() { + super.onLowMemory() + Glide.get(this).clearMemory() + } + + override fun onTrimMemory(level: Int) { + super.onTrimMemory(level) + Glide.get(this).trimMemory(level) + } +} \ No newline at end of file
Kotlin
๋™์ž‘์ด ์ž๋ช…ํ•œ ์ฝ”๋“œ๋ผ ์ฃผ์„์ด ๋ถˆํ•„์š”ํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +package kr.co.connect.boostcamp.livewhere.api + +import io.reactivex.Single +import kr.co.connect.boostcamp.livewhere.model.HouseResponse +import kr.co.connect.boostcamp.livewhere.model.PlaceResponse +import retrofit2.Response +import retrofit2.http.GET +import retrofit2.http.POST +import retrofit2.http.Query + +interface Api { + @GET("house/search/info") + fun getHouseDetail( + @Query("address") address: String + ): Single<Response<List<Any>>> + + @GET("house/search/infos") + fun getDetail( + @Query("address") address: String + ): Single<Response<HouseResponse>> + + @GET("place/search/infos") + fun getPlace( + @Query("lat") lat: String, + @Query("lng") lng: String, + @Query("radius") radius: String, + @Query("category") category: String + ): Single<Response<PlaceResponse>> + + @GET("house/search/find/infos") + fun getDetailWithAddress( + @Query("address") address: String + ): Single<Response<HouseResponse>> + + @POST("") + fun postReview( + @Query("nickname") nickname: String, @Query("id") id: String, @Query("contents") contents: String + ): Single<Response<Any>> + +} \ No newline at end of file
Kotlin
์–ด๋– ํ•œ ์ž์›๋„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์ง€ ์•Š๋‹ค๋ฉด RESTful ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅด์ง€ ๋ชปํ•œ ์„ค๊ณ„์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,20 @@ +package kr.co.connect.boostcamp.livewhere.api + +import io.reactivex.Observable +import kr.co.connect.boostcamp.livewhere.BuildConfig +import kr.co.connect.boostcamp.livewhere.model.TmapResponse +import retrofit2.Response +import retrofit2.http.GET +import retrofit2.http.Query + +interface TmapApi { + @GET("tmap/pois") + fun getAddress( + @Query("searchKeyword") searchKeyword: String, + @Query("appKey") appKey: String = BuildConfig.TmapApiKey, + @Query("count") count: String = "10", + @Query("areaLLCode") code: String = "11" + ): Observable<Response<TmapResponse>> + + +} \ No newline at end of file
Kotlin
`10`, `11`์€ ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ์ •ํ•ด์ง„ default ๊ฐ’์ธ์ง€ ์•Œ ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค. ์ƒ์ˆ˜๋กœ ๋นผ๋‚ด๊ณ  ๋ณ€์ˆ˜๋ช…์— ์˜๋ฏธ๋ฅผ ๋‹ด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,20 @@ +package kr.co.connect.boostcamp.livewhere.api + +import io.reactivex.Observable +import kr.co.connect.boostcamp.livewhere.BuildConfig +import kr.co.connect.boostcamp.livewhere.model.TmapResponse +import retrofit2.Response +import retrofit2.http.GET +import retrofit2.http.Query + +interface TmapApi { + @GET("tmap/pois") + fun getAddress( + @Query("searchKeyword") searchKeyword: String, + @Query("appKey") appKey: String = BuildConfig.TmapApiKey, + @Query("count") count: String = "10", + @Query("areaLLCode") code: String = "11" + ): Observable<Response<TmapResponse>> + + +} \ No newline at end of file
Kotlin
Retrofit์œผ๋กœ ๊ตฌํ˜„๋˜๋Š”๋ฐ Observable์ผ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. Http๋‹ˆ๊นŒ์š”.
@@ -0,0 +1,19 @@ +package kr.co.connect.boostcamp.livewhere.api + +import io.reactivex.Single +import kr.co.connect.boostcamp.livewhere.BuildConfig +import kr.co.connect.boostcamp.livewhere.model.ReverseGeo +import retrofit2.Response +import retrofit2.http.GET +import retrofit2.http.Headers +import retrofit2.http.Query + +interface ReverseGeoApi { + @Headers("Authorization: KakaoAK " + BuildConfig.KakaoServiceKey) + @GET("geo/coord2address.json") + fun getAddress( + @Query("y") latitude: String, + @Query("x") longitude: String, + @Query("input_coord") inputCoord: String + ): Single<Response<ReverseGeo>> +} \ No newline at end of file
Kotlin
gps ์ขŒํ‘œ ์ •๋ณด์ธ๊ฑธ๋กœ ๋ณด์ด๋Š”๋ฐ String์ด ๋งž๋‚˜์š”?
@@ -0,0 +1,12 @@ +package kr.co.connect.boostcamp.livewhere.data.entity + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "recent_search") +data class RecentSearchEntity( + @PrimaryKey var text: String, + @ColumnInfo(name = "Longitude") var longitude: String, + @ColumnInfo(name = "Latitude") var latitude: String +) \ No newline at end of file
Kotlin
๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๋“ค์€ ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” snake case๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ†ต์ผ์ด ํ•„์š”ํ•ด๋ณด์—ฌ์š”
@@ -0,0 +1,13 @@ +package kr.co.connect.boostcamp.livewhere.di + + +val appModules = arrayListOf( + apiModule + , loginModule + , sharedModule + , databaseModule + , homeModule + , mapModule + , reverseGeoApiModule + , detailModule +)
Kotlin
๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค๊ณผ ์ „ํ˜€ ๋‹ค๋ฅธ code format์ธ๋ฐ ํ†ต์ผ์ด ํ•„์š”ํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,24 @@ +package kr.co.connect.boostcamp.livewhere.di + +import androidx.room.Room +import kr.co.connect.boostcamp.livewhere.data.database.AppDataBase +import org.koin.android.ext.koin.androidContext +import org.koin.dsl.module.module + +const val DATABASE_NAME = "mdatabase.db" + +val databaseModule = module { + single("databaseModule") { + Room.databaseBuilder(androidContext(), AppDataBase::class.java, DATABASE_NAME) + .fallbackToDestructiveMigration() + .build() + } + + single("bookmarkDAO") { + get<AppDataBase>().bookmarkDao() + } + + single("recentSearchDAO") { + get<AppDataBase>().recentSearchDao() + } +} \ No newline at end of file
Kotlin
์ด ๋ชจ๋“ˆ์— ์ •์˜๋œ 3๊ฐ€์ง€ injection๋“ค์€ named inject๊ฐ€ ํ•„์š”ํ•œ๊ฐ€์š”? ๋ถˆํ•„์š”ํ•ด๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,22 @@ +package codesquad.dto.question; + +import lombok.*; + +@AllArgsConstructor +@Getter +@Setter +@Builder +public class QuestionRequestDto { + private String writer; + private String title; + private String contents; + + @Override + public String toString() { + return "QuestionDto{" + + "writer='" + writer + '\'' + + ", title='" + title + '\'' + + ", contents='" + contents + '\'' + + '}'; + } +}
Java
๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -1,22 +1,36 @@ package codesquad; -import codesquad.repository.ArrayUserRepository; +import codesquad.repository.QuestionRepositoryImpl; +import codesquad.repository.UserRepositoryImpl; +import codesquad.repository.QuestionRepository; import codesquad.repository.UserRepository; +import codesquad.service.QuestionService; +import codesquad.service.QuestionServiceImpl; import codesquad.service.UserService; import codesquad.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { - + @Bean public UserService userService() { return new UserServiceImpl(userRepository()); } @Bean public UserRepository userRepository() { - return new ArrayUserRepository(); + return new UserRepositoryImpl(); + } + + @Bean + public QuestionService questionService() { + return new QuestionServiceImpl(questionRepository()); + } + + @Bean + public QuestionRepository questionRepository() { + return new QuestionRepositoryImpl(); } }
Java
์ปจํŠธ๋กค๋Ÿฌ๋Š” @Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•ด ๋นˆ์„ ๋“ฑ๋กํ•˜๊ณ  ๊ณ„์‹œ๋Š”๋ฐ, Service์™€ Repository๋Š” Config ํŒŒ์ผ์—์„œ ๋นˆ์„ ๋”ฐ๋กœ ๋“ฑ๋กํ•ด์ฃผ๊ณ  ๊ณ„์‹œ๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -7,6 +7,8 @@ public interface UserRepository { void register(UserEntity userEntity); + void update(String userId, UserEntity userEntity); + UserEntity findById(String userId); List<UserEntity> findAll();
Java
update()๋Š” dto๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋Š”๋ฐ, register()๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
์—”ํ‹ฐํ‹ฐ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์œผ๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
Entity, Model, Dto, VO์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,74 @@ +package codesquad.controller; + +import codesquad.AppConfig; +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; +import codesquad.mapper.QuestionMapper; +import codesquad.service.QuestionService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.stream.Collectors; + +@Controller +public class QuestionController { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + QuestionService questionService = applicationContext.getBean("questionService", QuestionService.class); + + @GetMapping("/questions/post") + public String getQuestionForm() { + return "qna/form"; + } + + @PostMapping("/questions") + public String postQuestion(QuestionRequestDto questionRequestDto) { + QuestionEntity questionEntity = QuestionMapper.dtoToEntity(questionRequestDto); + questionService.postQuestion(questionEntity); + return "redirect:/questions"; + } + + @GetMapping("/questions") + public String getQuestionList(Model model) { + List<QuestionResponseDto> questions = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questions); + + return "qna/list"; + } + + @GetMapping("/questions/{id}") + public String getQuestionShow(@PathVariable String id, Model model) { + QuestionResponseDto question = new QuestionResponseDto(questionService.findQuestion(id)); + + model.addAttribute("writer", question.getWriter()); + model.addAttribute("title", question.getTitle()); + model.addAttribute("contents", question.getContents()); + model.addAttribute("date", question.getDate()); + + return "qna/show"; + } + + @GetMapping("/") + public String getHome(Model model) { + List<QuestionEntity> questionEntityList = questionService.findQuestions(); + if (questionEntityList.size() == 0) { + return "qna/list"; + } + + List<QuestionResponseDto> questionResponseDtoList = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questionResponseDtoList); + return "qna/list"; + } +} \ No newline at end of file
Java
applicationcontext๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ๋นˆ์„ ๊ฐ€์ ธ์˜ค์…”์„œ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -1,18 +1,17 @@ package codesquad.controller; import codesquad.AppConfig; -import codesquad.dto.UserDto; +import codesquad.dto.user.UserDto; +import codesquad.dto.user.UserUpdateRequestDto; import codesquad.entity.UserEntity; +import codesquad.mapper.UserMapper; import codesquad.service.UserService; import org.modelmapper.ModelMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.stream.Collectors; @@ -39,7 +38,6 @@ public String registerUser(UserDto userDto) { @GetMapping() public String getUserList(Model model) { - System.out.println("userService.findUsers().get(0) = " + userService.findUsers().get(0)); List<UserDto> users = userService.findUsers().stream() .map(userEntity -> modelMapper.map(userEntity, UserDto.class)).collect(Collectors.toList()); model.addAttribute("users", users); @@ -57,4 +55,15 @@ public String getUserProfile(@PathVariable String userId, Model model) { return "user/profile"; } + + @GetMapping("/{userId}/form") + public String getProfileEditForm() { + return "user/update_form"; + } + + @PutMapping("/{userId}/update") + public String updateUserProfile(@PathVariable("userId") String userId, UserUpdateRequestDto requestDto) { + userService.updateUser(userId, UserMapper.dtoToEntity(requestDto)); + return "redirect:/users"; + } }
Java
์—…๋ฐ์ดํŠธ์—๋Š” ๋ณดํ†ต PUT๊ณผ PATCH๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๋‘˜์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -1,22 +1,36 @@ package codesquad; -import codesquad.repository.ArrayUserRepository; +import codesquad.repository.QuestionRepositoryImpl; +import codesquad.repository.UserRepositoryImpl; +import codesquad.repository.QuestionRepository; import codesquad.repository.UserRepository; +import codesquad.service.QuestionService; +import codesquad.service.QuestionServiceImpl; import codesquad.service.UserService; import codesquad.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { - + @Bean public UserService userService() { return new UserServiceImpl(userRepository()); } @Bean public UserRepository userRepository() { - return new ArrayUserRepository(); + return new UserRepositoryImpl(); + } + + @Bean + public QuestionService questionService() { + return new QuestionServiceImpl(questionRepository()); + } + + @Bean + public QuestionRepository questionRepository() { + return new QuestionRepositoryImpl(); } }
Java
์šฐ์„  ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜์—ฌ ์ฝ”๋“œ/๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•ด Service์™€ Repository๋Š” ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ง์ ‘ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๋‹ค Repository๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ผ ๋•Œ, Config์— ๋“ฑ๋ก๋œ Repository Bean๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ Controller์˜ ์—ญํ• ์„ ์ƒ๊ฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ๋Š” View์™€ Service๋ฅผ ์ด์–ด์ฃผ๋Š” ์—ญํ• ๋งŒ ํ•˜๊ณ  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Repository๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋ณ€๊ฒฝ๋˜์–ด๋„ Controller๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ Config์—์„œ ๋“ฑ๋กํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ref: https://inf.run/SZEa -> ๋ง ๋‹ค๋“ฌ๋Š”๋ฐ ๋„์›€ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค:)
@@ -0,0 +1,74 @@ +package codesquad.controller; + +import codesquad.AppConfig; +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; +import codesquad.mapper.QuestionMapper; +import codesquad.service.QuestionService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.stream.Collectors; + +@Controller +public class QuestionController { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + QuestionService questionService = applicationContext.getBean("questionService", QuestionService.class); + + @GetMapping("/questions/post") + public String getQuestionForm() { + return "qna/form"; + } + + @PostMapping("/questions") + public String postQuestion(QuestionRequestDto questionRequestDto) { + QuestionEntity questionEntity = QuestionMapper.dtoToEntity(questionRequestDto); + questionService.postQuestion(questionEntity); + return "redirect:/questions"; + } + + @GetMapping("/questions") + public String getQuestionList(Model model) { + List<QuestionResponseDto> questions = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questions); + + return "qna/list"; + } + + @GetMapping("/questions/{id}") + public String getQuestionShow(@PathVariable String id, Model model) { + QuestionResponseDto question = new QuestionResponseDto(questionService.findQuestion(id)); + + model.addAttribute("writer", question.getWriter()); + model.addAttribute("title", question.getTitle()); + model.addAttribute("contents", question.getContents()); + model.addAttribute("date", question.getDate()); + + return "qna/show"; + } + + @GetMapping("/") + public String getHome(Model model) { + List<QuestionEntity> questionEntityList = questionService.findQuestions(); + if (questionEntityList.size() == 0) { + return "qna/list"; + } + + List<QuestionResponseDto> questionResponseDtoList = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questionResponseDtoList); + return "qna/list"; + } +} \ No newline at end of file
Java
AppConfig์—์„œ @ComponentScan ๊ฐ™์€ Spring Bean์„ ์ž๋™์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๊ธฐ์— ์ง์ ‘ ๋นˆ์„ ๊ฐ€์ ธ์™”์Šต๋‹ˆ๋‹ค! (์‚ฌ์‹ค ์งˆ๋ฌธ ์˜๋„๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ถ€์—ฐ ์„ค๋ช… ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!)
@@ -1,18 +1,17 @@ package codesquad.controller; import codesquad.AppConfig; -import codesquad.dto.UserDto; +import codesquad.dto.user.UserDto; +import codesquad.dto.user.UserUpdateRequestDto; import codesquad.entity.UserEntity; +import codesquad.mapper.UserMapper; import codesquad.service.UserService; import org.modelmapper.ModelMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.stream.Collectors; @@ -39,7 +38,6 @@ public String registerUser(UserDto userDto) { @GetMapping() public String getUserList(Model model) { - System.out.println("userService.findUsers().get(0) = " + userService.findUsers().get(0)); List<UserDto> users = userService.findUsers().stream() .map(userEntity -> modelMapper.map(userEntity, UserDto.class)).collect(Collectors.toList()); model.addAttribute("users", users); @@ -57,4 +55,15 @@ public String getUserProfile(@PathVariable String userId, Model model) { return "user/profile"; } + + @GetMapping("/{userId}/form") + public String getProfileEditForm() { + return "user/update_form"; + } + + @PutMapping("/{userId}/update") + public String updateUserProfile(@PathVariable("userId") String userId, UserUpdateRequestDto requestDto) { + userService.updateUser(userId, UserMapper.dtoToEntity(requestDto)); + return "redirect:/users"; + } }
Java
`PUT`์€ ์ž์› ์ „์ฒด๋ฅผ ๊ต์ฒดํ•˜์—ฌ, ์ž์›์˜ ๋ชจ๋“  ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•˜๊ณ  `PATCH`๋Š” ์ž์›์˜ ๋ถ€๋ถ„๋งŒ ๊ต์ฒดํ•˜์—ฌ, ์ž์›์˜ ์ผ๋ถ€ ํ•„๋“œ๋งŒ ์žˆ์–ด๋„ ๋ฉ๋‹ˆ๋‹ค. GOOD ``` PUT /userId/1 { "userId" : "mywnajsldkf", "age" : 24 } GET /userId/1 { "userId" : "mywnajsldkf", "age" : 24 } ``` BAD ``` PUT /userId/1 { "userId" : "mywnajsldkf", } GET /userId/1 { "userId" : "mywnajsldkf", "age" : null } ``` GOOD ``` PATCH /userId/1 { "userId" : "mywnajsldkf", } GET /userId/1 { "userId" : "mywnajsldkf", "age" : 24 } ```
@@ -7,6 +7,8 @@ public interface UserRepository { void register(UserEntity userEntity); + void update(String userId, UserEntity userEntity); + UserEntity findById(String userId); List<UserEntity> findAll();
Java
dto <-> entity ์‚ฌ์ด์˜ ๋งตํ•‘์„ ์–ด๋””์„œ ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ์š”. DTO๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋ ˆ์ด์–ด์— ์ข…์†๋˜์–ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋งตํ•‘์— ๊ด€๋ จ๋œ ๊ฒƒ์€ ๋ชจ๋‘ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋‘๋ ค๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. update()์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ํ”ผ๋“œ๋ฐฑ์„ ๋ณด๊ณ  ๋‹ค์‹œ ๋ณด๋‹ˆ, ์ƒˆ๋กœ์šด userEntity๋ฅผ ์ƒ์„ฑํ•˜๋Š” register์™€ ๋‹ค๋ฅด๊ฒŒ ๊ธฐ์กด์— ์žˆ๋Š” userEntity๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค๋ณด๋‹ˆ ๊ฐ’์„ updateํ•˜๋Š” ๋ฐฉํ–ฅ์— ์ง‘์ค‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ DTO -> Entity๋„ ์ƒˆ๋กญ๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ์ƒ๊ฐ์— ๊ฑฐ์น˜์ง€ ์•Š์•˜๋„ค์š”.๐Ÿ˜… ๋‹ค์‹œ๋ณด๋‹ˆ register() ์ฒ˜๋Ÿผ controller์—์„œ entity๋กœ ๋ฐ”๊ฟ”์„œ ๋„˜๊ฒจ์ง€๊ณ  entity์— ์žˆ๋Š” ๊ฐ’๋“ค์„ repository์—์„œ ๊บผ๋‚ด์–ด ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€๋ฐ @saint6839 ๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋– ์„ธ์š”? +) dto <-> entity๋Š” ์–ด๋””์—์— ๊ด€ํ•œ ๋ถ€๋ถ„์€... ์ด์ „ ๋ฆฌ๋ทฐ์—์„œ๋„ ์งˆ๋ฌธ๋“œ๋ฆฌ๊ธฐ๋„ ํ–ˆ๊ณ , ๋‹ค์–‘ํ•œ [ํ† ๋ก ๊ธ€ 1](https://okky.kr/articles/708060), [ํ† ๋ก ๊ธ€ 2](https://tecoble.techcourse.co.kr/post/2021-04-25-dto-layer-scope/) ๋“ฑ์„ ์ฐพ์•„๋ณด๊ธฐ๋„ ํ–ˆ๋Š”๋ฐ์š”~ ์Šค์Šค๋กœ ๋‚ด๋ฆฐ ์ž‘์—… ๋ฐฉํ–ฅ์„ฑ์€ **repository๊นŒ์ง€ ๋“ค์–ด๊ฐ€์ง€ ๋ง ๊ฒƒ, ํ”„๋กœ์ ํŠธ ๋‚ด์— ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ๊ฒƒ, (๋ฌด์—‡๋ณด๋‹ค) ๊ฐ™์ด ํ•˜๋Š” ์‚ฌ๋žŒ์ด๋ž‘ ๋งž์ถœ ๊ฒƒ** ์ž…๋‹ˆ๋‹ค.๐Ÿ™‚ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ƒ์—ฝ๋‹˜์˜ ์ƒ๊ฐ๋„ ๋“ค์–ด๋ณด๊ณ  ์‹ถ๋„ค์š”~
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
> ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์œผ๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋„ค! ํ•œ๋ฒˆ ๊ณ ๋ฏผํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~ ํ˜น์‹œ ์ƒ์—ฝ๋‹˜์ด ๋ง์”€ํ•˜์‹  ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ถ€๋ถ„์€ view(controller)์™€ ์ฃผ๊ณ ๋ฐ›๋Š”๋‹ค๋Š” ๋ง์”€์ด์‹ค๊นŒ์š”?
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
> Entity, Model, Dto, VO์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” ์กฐ์–ธ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•„์ง VO๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ์™€๋‹ฟ์ง€ ์•Š๋Š”๋ฐ์š”~ DTO์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๋Š” ์ƒํ™ฉ์—๋งŒ ์“ด๋‹ค๋Š” ๊ฒƒ์ด ์•„๋‹Œ, VO์˜ ์ œ์•ฝ์‚ฌํ•ญ(๋ถˆ๋ณ€์„ฑ, ๋™๋“ฑ์„ฑ, ์ž๊ฐ€ ์œ ํšจ์„ฑ)์ด ์žˆ๊ธฐ์— ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ VO ๊ทธ ์ž์ฒด๊ฐ€ ์ œ์•ฝ์‚ฌํ•ญ์ด ๋˜๊ฑฐ๋‚˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฏธ๋ฆฌ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋“ฑ VO์˜ ํŠน์ง•์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ ์ด ์žˆ๊ธฐ์— ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ดํ•ดํ•ด๋„ ๋ ๊นŒ์š”? ์•„๋ž˜๋ถ€ํ„ฐ๋Š” ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. # Entity - DB์˜ ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋Š” ๊ฐ์ฒด๋กœ, `id`๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ Entity๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. - ๋กœ์ง์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ```java public class User { private Long id; private String name; private String email; @Builder public User(String name, String email) { this.name = name; this.email = email; } public User update(String name, String email) { this.name = name; this.email = email; return this; } } ``` # DTO - Data Transfer Object, ๋ง ๊ทธ๋Œ€๋กœ ๊ณ„์ธต(Layer: Service, Controller, Repository)๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. - ๋กœ์ง์„ ๊ฐ–์ง€ ์•Š๊ณ , getter/setter ๋ฉ”์†Œ๋“œ๋งŒ ๊ฐ–์Šต๋‹ˆ๋‹ค. ```java class UserDto { private int name; private int email; public UserDto(int name, int email){ this.name = name; this.email = email; } public int getName() { return name; } public int setName(int name) { this.name = name; } ... } ``` # VO Value Object์˜ ์•ฝ์ž๋กœ ๊ฐ’ ๊ทธ ์ž์ฒด์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์‹œ, ์—ผ๋‘ํ•ด์•ผํ•  ๋ถ€๋ถ„์€ **1. equals & hash code ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. 2. ์ˆ˜์ •์ž(setter)๊ฐ€ ์—†๋Š” ๋ถˆ๋ณ€ ๊ฐ์ฒด์—ฌ์•ผ ํ•œ๋‹ค.** ์ž…๋‹ˆ๋‹ค. 1๋ฒˆ์— ๋Œ€ํ•ด ์ข€ ๋” ์ด์•ผ๊ธฐํ•˜์ž๋ฉด ๋™๋“ฑ์„ฑ์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด๊ฐ€ ์‹ค์ œ ๋‹ค๋ฅธ ๊ฐ์ฒด์ด๋”๋ผ๋„, ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด(๋‚˜์˜ ์•„์ดํฐ 13๋ฏธ๋‹ˆ, ๋„ˆ์˜ ํœด๋Œ€ํฐ 13๋ฏธ๋‹ˆ ์„œ๋กœ ๋‹ค๋ฅด์ง€๋งŒ ๊ฐ™์Œ) ๊ฐ™๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. 2๋ฒˆ์€ setter๊ฐ€ ์—†์–ด์„œ ๊ฐ’ ๋ณ€๊ฒฝ ๊ฑฑ์ •์ด ์—†์–ด DB์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์™€ ๋ฐ์ดํ„ฐ๋ฅผ VO์— ๋‹ด์œผ๋ฉด ํ•ญ์ƒ VO์˜ ๊ฐ’์„ ์›๋ณธ์œผ๋กœ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. +) ์ž˜ ์ •๋ฆฌ๋œ ๊ธ€๋„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค. * https://tecoble.techcourse.co.kr/post/2020-06-11-value-object/ * https://hudi.blog/value-object/ # Model Model์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. DB์— ๊ฐ’์„ ๋„ฃ๊ฑฐ๋‚˜ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ค‘๊ฐ„์— ์—ฐ์‚ฐ์ด ์ด๋ฃจ์–ด์งˆ ๋•Œ ํ•„๋“œ๋“ค์„ ๋ชจ๋ธ์— ๋‹ด์•„ ๊ด€๋ฆฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -1,22 +1,36 @@ package codesquad; -import codesquad.repository.ArrayUserRepository; +import codesquad.repository.QuestionRepositoryImpl; +import codesquad.repository.UserRepositoryImpl; +import codesquad.repository.QuestionRepository; import codesquad.repository.UserRepository; +import codesquad.service.QuestionService; +import codesquad.service.QuestionServiceImpl; import codesquad.service.UserService; import codesquad.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { - + @Bean public UserService userService() { return new UserServiceImpl(userRepository()); } @Bean public UserRepository userRepository() { - return new ArrayUserRepository(); + return new UserRepositoryImpl(); + } + + @Bean + public QuestionService questionService() { + return new QuestionServiceImpl(questionRepository()); + } + + @Bean + public QuestionRepository questionRepository() { + return new QuestionRepositoryImpl(); } }
Java
์˜์กด์„ฑ ์ฃผ์ž…์€ ์Šคํ”„๋ง์ด ์•Œ์•„์„œ ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค ~ ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ DI ๋ฐฉ์‹์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,74 @@ +package codesquad.controller; + +import codesquad.AppConfig; +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; +import codesquad.mapper.QuestionMapper; +import codesquad.service.QuestionService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.stream.Collectors; + +@Controller +public class QuestionController { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + QuestionService questionService = applicationContext.getBean("questionService", QuestionService.class); + + @GetMapping("/questions/post") + public String getQuestionForm() { + return "qna/form"; + } + + @PostMapping("/questions") + public String postQuestion(QuestionRequestDto questionRequestDto) { + QuestionEntity questionEntity = QuestionMapper.dtoToEntity(questionRequestDto); + questionService.postQuestion(questionEntity); + return "redirect:/questions"; + } + + @GetMapping("/questions") + public String getQuestionList(Model model) { + List<QuestionResponseDto> questions = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questions); + + return "qna/list"; + } + + @GetMapping("/questions/{id}") + public String getQuestionShow(@PathVariable String id, Model model) { + QuestionResponseDto question = new QuestionResponseDto(questionService.findQuestion(id)); + + model.addAttribute("writer", question.getWriter()); + model.addAttribute("title", question.getTitle()); + model.addAttribute("contents", question.getContents()); + model.addAttribute("date", question.getDate()); + + return "qna/show"; + } + + @GetMapping("/") + public String getHome(Model model) { + List<QuestionEntity> questionEntityList = questionService.findQuestions(); + if (questionEntityList.size() == 0) { + return "qna/list"; + } + + List<QuestionResponseDto> questionResponseDtoList = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questionResponseDtoList); + return "qna/list"; + } +} \ No newline at end of file
Java
์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ฒ€์ƒ‰ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,45 @@ +package codesquad.mapper; + +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class QuestionMapper { + + public static QuestionEntity dtoToEntity(QuestionRequestDto questionRequestDto) { + if (questionRequestDto == null){ + return null; + } + + QuestionEntity.QuestionEntityBuilder questionEntity = QuestionEntity.builder(); + + questionEntity.writer(questionRequestDto.getWriter()); + questionEntity.title(questionRequestDto.getTitle()); + questionEntity.contents(questionRequestDto.getContents()); + + return questionEntity.build(); + } + + public static QuestionResponseDto entityToDto(QuestionEntity questionEntity) { + if (questionEntity == null) { + return null; + } + + QuestionResponseDto.QuestionResponseDtoBuilder questionResponseDto = QuestionResponseDto.builder(); + + questionResponseDto.writer(questionEntity.getWriter()); + questionResponseDto.title(questionEntity.getTitle()); + questionResponseDto.contents(questionEntity.getContents()); + questionResponseDto.date(localTimeToString(questionEntity.getCreatedDate())); + + return questionResponseDto.build(); + } + + public static String localTimeToString(LocalDateTime localDateTime) { + String stringLocalTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); + return stringLocalTime; + } +}
Java
๋ณ€์ˆ˜๋Š” private ์œผ๋กœ ์„ ์–ธํ•˜๊ณ  setter ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผํ•˜๋Š”๊ฒŒ ์ž๋ฐ”์˜ ์ผ๋ฐ˜์ ์ธ ์›์น™์ž…๋‹ˆ๋‹ค ~
@@ -0,0 +1,24 @@ +package codesquad.entity; + +import lombok.*; + +@Setter +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class QuestionEntity extends BaseTimeEntity { + + private String writer; + private String title; + private String contents; + + @Override + public String toString() { + return "QuestionEntity{" + + "writer='" + writer + '\'' + + ", title='" + title + '\'' + + ", contents='" + contents + '\'' + + '}'; + } +}
Java
Entity ๋ผ๋Š” postfix ๋ฅผ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ~
@@ -1,22 +1,36 @@ package codesquad; -import codesquad.repository.ArrayUserRepository; +import codesquad.repository.QuestionRepositoryImpl; +import codesquad.repository.UserRepositoryImpl; +import codesquad.repository.QuestionRepository; import codesquad.repository.UserRepository; +import codesquad.service.QuestionService; +import codesquad.service.QuestionServiceImpl; import codesquad.service.UserService; import codesquad.service.UserServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { - + @Bean public UserService userService() { return new UserServiceImpl(userRepository()); } @Bean public UserRepository userRepository() { - return new ArrayUserRepository(); + return new UserRepositoryImpl(); + } + + @Bean + public QuestionService questionService() { + return new QuestionServiceImpl(questionRepository()); + } + + @Bean + public QuestionRepository questionRepository() { + return new QuestionRepositoryImpl(); } }
Java
์ œ์–ด์˜ ์—ญ์ „(IoC)๊ฐ€ ๋ฌด์—‡์ธ์ง€๋„ ํ•œ ๋ฒˆ ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
> > ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์œผ๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! > > ๋„ค! ํ•œ๋ฒˆ ๊ณ ๋ฏผํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~ ํ˜น์‹œ ์ƒ์—ฝ๋‹˜์ด ๋ง์”€ํ•˜์‹  ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ถ€๋ถ„์€ view(controller)์™€ ์ฃผ๊ณ ๋ฐ›๋Š”๋‹ค๋Š” ๋ง์”€์ด์‹ค๊นŒ์š”? ์ปจํŠธ๋กค๋Ÿฌ ๋ฟ ์•„๋‹ˆ๋ผ ๋ ˆ์ด์–ด๋“œ ์•„ํ‚คํ…์ณ์—์„œ ๊ฐ ๊ณ„์ธต๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค!
@@ -7,6 +7,8 @@ public interface UserRepository { void register(UserEntity userEntity); + void update(String userId, UserEntity userEntity); + UserEntity findById(String userId); List<UserEntity> findAll();
Java
> dto <-> entity ์‚ฌ์ด์˜ ๋งตํ•‘์„ ์–ด๋””์„œ ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ์š”. DTO๋Š” ์ปจํŠธ๋กค๋Ÿฌ ๋ ˆ์ด์–ด์— ์ข…์†๋˜์–ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ๋งตํ•‘์— ๊ด€๋ จ๋œ ๊ฒƒ์€ ๋ชจ๋‘ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋‘๋ ค๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค. > > update()์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ํ”ผ๋“œ๋ฐฑ์„ ๋ณด๊ณ  ๋‹ค์‹œ ๋ณด๋‹ˆ, ์ƒˆ๋กœ์šด userEntity๋ฅผ ์ƒ์„ฑํ•˜๋Š” register์™€ ๋‹ค๋ฅด๊ฒŒ ๊ธฐ์กด์— ์žˆ๋Š” userEntity๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค๋ณด๋‹ˆ ๊ฐ’์„ updateํ•˜๋Š” ๋ฐฉํ–ฅ์— ์ง‘์ค‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ DTO -> Entity๋„ ์ƒˆ๋กญ๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ์ƒ๊ฐ์— ๊ฑฐ์น˜์ง€ ์•Š์•˜๋„ค์š”.๐Ÿ˜… > > ๋‹ค์‹œ๋ณด๋‹ˆ register() ์ฒ˜๋Ÿผ controller์—์„œ entity๋กœ ๋ฐ”๊ฟ”์„œ ๋„˜๊ฒจ์ง€๊ณ  entity์— ์žˆ๋Š” ๊ฐ’๋“ค์„ repository์—์„œ ๊บผ๋‚ด์–ด ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€๋ฐ @saint6839 ๋‹˜์˜ ์ƒ๊ฐ์€ ์–ด๋– ์„ธ์š”? > > +) dto <-> entity๋Š” ์–ด๋””์—์— ๊ด€ํ•œ ๋ถ€๋ถ„์€... ์ด์ „ ๋ฆฌ๋ทฐ์—์„œ๋„ ์งˆ๋ฌธ๋“œ๋ฆฌ๊ธฐ๋„ ํ–ˆ๊ณ , ๋‹ค์–‘ํ•œ [ํ† ๋ก ๊ธ€ 1](https://okky.kr/articles/708060), [ํ† ๋ก ๊ธ€ 2](https://tecoble.techcourse.co.kr/post/2021-04-25-dto-layer-scope/) ๋“ฑ์„ ์ฐพ์•„๋ณด๊ธฐ๋„ ํ–ˆ๋Š”๋ฐ์š”~ ์Šค์Šค๋กœ ๋‚ด๋ฆฐ ์ž‘์—… ๋ฐฉํ–ฅ์„ฑ์€ **repository๊นŒ์ง€ ๋“ค์–ด๊ฐ€์ง€ ๋ง ๊ฒƒ, ํ”„๋กœ์ ํŠธ ๋‚ด์— ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ๊ฒƒ, (๋ฌด์—‡๋ณด๋‹ค) ๊ฐ™์ด ํ•˜๋Š” ์‚ฌ๋žŒ์ด๋ž‘ ๋งž์ถœ ๊ฒƒ** ์ž…๋‹ˆ๋‹ค.๐Ÿ™‚ ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ƒ์—ฝ๋‹˜์˜ ์ƒ๊ฐ๋„ ๋“ค์–ด๋ณด๊ณ  ์‹ถ๋„ค์š”~ ๊ฐœ์ธ์ ์œผ๋กœ๋Š” Entity <-> Dto ๋ณ€ํ™˜ ์ฑ…์ž„์„ ์„œ๋น„์Šค ๊ณ„์ธต์— ๋ถ€์—ฌํ•˜๋Š”๊ฒƒ์„ ์„ ํ˜ธํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ ๊ณ„์ธต์˜ ๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ์€ ๋น„์ฆˆ๋‹ˆ์Šค ์šฉ์–ด๋ฅผ, ์„œ๋น„์Šค ๊ณ„์ธต์˜ ๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ์€ ๊ฐœ๋ฐœ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜์ƒ์„ ๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค(์•„๋งˆ๋„ ๊น€์˜ํ•œ๋‹˜). ๊ทธ๋งŒํผ ์ปจํŠธ๋กค๋Ÿฌ ๊ณ„์ธต์€ ๊ฐœ๋ฐœ์ ์ธ ์š”์†Œ๋ณด๋‹ค๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์ฑ…์ž„์ด ์ง‘์ค‘๋˜์–ด์•ผ ํ•˜๋Š” ๊ณ„์ธต์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ  ๋•Œ๋ฌธ์— ์ €๋Š” ์„œ๋น„์Šค ๊ณ„์ธต์—์„œ Entity์™€ Dto ๋ณ€ํ™˜์„ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ์ •๋‹ต์€ ์—†๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ ๋ณธ์ธ์ด ์„ ํ˜ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋ฉด, ๊ทธ ๊ทผ๊ฑฐ์— ๋Œ€ํ•ด์„œ ์ƒ๊ฐํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”(๋‹จ์ˆœํžˆ ๋Œ€๋ถ€๋ถ„ ์‚ฌ๋žŒ๋“ค์ด ๊ทธ๋ ‡๋‹ค๊ณ  ํ•ด์„œ๊ฐ€ ์•„๋‹Œ..)
@@ -7,6 +7,8 @@ public interface UserRepository { void register(UserEntity userEntity); + void update(String userId, UserEntity userEntity); + UserEntity findById(String userId); List<UserEntity> findAll();
Java
@saint6839 ์ €๋Š” service(application or usecase), domain ์€ ์™ธ๋ถ€์˜ ๊ฒƒ์œผ๋กœ๋ถ€ํ„ฐ ๋…๋ฆฝ์ ์ด๊ณ  ์ตœ๋Œ€ํ•œ ์ˆœ์ˆ˜ํ•œ ์‘๋‹ต์„ ๋ณด๋‚ด์•ผ ์‚ฌ์šฉ์„ฑ์ด ์ข‹์•„์ง„๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ service ๋ ˆ์ด์–ด์—์„œ๋Š” ์™ธ๋ถ€ ์š”๊ตฌ์‚ฌํ•ญ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ธ dto ๋ณ€ํ™˜์„ ์ง€์–‘ํ•˜๋„๋ก ๊ฐœ๋ฐœํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ ์„œ๋น„์Šค์—์„œ ๊ฐœ๋ฐœํ•˜๋‹ค๋ณด๋ฉด 1controller - 1service ๊ฐ€ ์•„๋‹ˆ๋ผ n๊ฐœ์˜ controller ์ผ€์ด์Šค์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ชฉ์ ์œผ๋กœ 1service ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ผ€์ด์Šค๋„ ์žˆ์œผ๋‹ˆ ๊ฐ์ž๊ฐ€ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ, dto ๊ฐ€ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜์–ด์„œ ๋šฑ๋šฑํ•ด์ง€๊ณ  ๋ฌด์—‡์ด ์–ด๋–ค ์ผ€์ด์Šค์— ๊ผญ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์ธ์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ๋„ ์–ด๋ ค์›Œ์ง‘๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„œ๋กœ์˜ ๋ณ€๊ฒฝ์— ์˜ํ–ฅ๋„ ๋งŽ์ด ๋ฐ›๊ตฌ์š”. ๊ทธ๋ ‡๋‹ค๊ณ  entity ๋ฅผ ์ˆœ์ˆ˜ํ•˜๊ฒŒ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋ฉด OSIV ๊ณ ๋ฏผ์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ข€ ๋ณต์žกํ•œ ์ฝ”๋“œ์—์„œ๋Š” Service layer DTO ๋ฅผ ๋”ฐ๋กœ ์ •์˜ํ•ด์„œ ๊ทธ๊ฒƒ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ณตํ†ต์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ณ , controller ์—์„œ๋Š” service dto ๋ฅผ ๋ฐ›์•„์„œ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋“ค์„ ๋‹ค์‹œ controller layer dto ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ๋„ˆ๋ฌด ํด๋ž˜์Šค๊ฐ€ ๋งŽ์•„์ง€๊ณ  ์˜คํžˆ๋ ค ๋ณต์žกํ•ด์ง„๋‹ค๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์–ด์„œ ์ด๋Ÿฐ ํ† ์ดํ”Œ์ ์—์„œ๋Š” ์ถ”์ฒœํ•˜์ง€ ์•Š์ง€๋งŒ ์ด๋Ÿฐ ๋ถ€๋ถ„๋„ ์žˆ๊ตฌ๋‚˜ ํ•˜๊ณ  ๋„˜๊ธฐ์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ๊ณต์œ ๋“œ๋ฆฝ๋‹ˆ๋‹ค ~ ์ด ์ผ€์ด์Šค์—์„œ๋Š” ์ž‘์€ ๊ทœ๋ชจ์ด๋‹ˆ ๊ทธ๋ƒฅ service layer ์—์„œ dto ๋ณ€ํ™˜์„ ํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค ~
@@ -0,0 +1,30 @@ +package codesquad.service; + +import codesquad.entity.QuestionEntity; +import codesquad.repository.QuestionRepository; + +import java.util.List; + +public class QuestionServiceImpl implements QuestionService { + private final QuestionRepository questionRepository; + + public QuestionServiceImpl(QuestionRepository questionRepository) { + this.questionRepository = questionRepository; + } + + @Override + public void postQuestion(QuestionEntity questionEntity) { + questionRepository.post(questionEntity); + questionRepository.setBaseTime(questionEntity); + } + + @Override + public List<QuestionEntity> findQuestions() { + return questionRepository.findAll(); + } + + @Override + public QuestionEntity findQuestion(String id) { + return questionRepository.findAll().get(Integer.parseInt(id) - 1); + } +}
Java
์•„์ง 1๋‹จ๊ณ„๋ผ์„œ ์ด๋Ÿฐ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ฝ”๋ฉ˜ํŠธ๋Š” ์ข€ ๋” ์นœ์ ˆํžˆ ์„ค๋ช…ํ•ด์ฃผ์–ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,24 @@ +package codesquad.entity; + +import lombok.*; + +@Setter +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class QuestionEntity extends BaseTimeEntity { + + private String writer; + private String title; + private String contents; + + @Override + public String toString() { + return "QuestionEntity{" + + "writer='" + writer + '\'' + + ", title='" + title + '\'' + + ", contents='" + contents + '\'' + + '}'; + } +}
Java
@hyukjin-lee class๋ช… ๋ง์”€ํ•˜์‹ค๊นŒ์š”??
@@ -0,0 +1,74 @@ +package codesquad.controller; + +import codesquad.AppConfig; +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; +import codesquad.mapper.QuestionMapper; +import codesquad.service.QuestionService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +import java.util.stream.Collectors; + +@Controller +public class QuestionController { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + QuestionService questionService = applicationContext.getBean("questionService", QuestionService.class); + + @GetMapping("/questions/post") + public String getQuestionForm() { + return "qna/form"; + } + + @PostMapping("/questions") + public String postQuestion(QuestionRequestDto questionRequestDto) { + QuestionEntity questionEntity = QuestionMapper.dtoToEntity(questionRequestDto); + questionService.postQuestion(questionEntity); + return "redirect:/questions"; + } + + @GetMapping("/questions") + public String getQuestionList(Model model) { + List<QuestionResponseDto> questions = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questions); + + return "qna/list"; + } + + @GetMapping("/questions/{id}") + public String getQuestionShow(@PathVariable String id, Model model) { + QuestionResponseDto question = new QuestionResponseDto(questionService.findQuestion(id)); + + model.addAttribute("writer", question.getWriter()); + model.addAttribute("title", question.getTitle()); + model.addAttribute("contents", question.getContents()); + model.addAttribute("date", question.getDate()); + + return "qna/show"; + } + + @GetMapping("/") + public String getHome(Model model) { + List<QuestionEntity> questionEntityList = questionService.findQuestions(); + if (questionEntityList.size() == 0) { + return "qna/list"; + } + + List<QuestionResponseDto> questionResponseDtoList = questionService.findQuestions().stream() + .map(questionEntity -> QuestionMapper.entityToDto(questionEntity)) + .collect(Collectors.toList()); + + model.addAttribute("questions", questionResponseDtoList); + return "qna/list"; + } +} \ No newline at end of file
Java
> applicationcontext๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ๋นˆ์„ ๊ฐ€์ ธ์˜ค์…”์„œ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! questionService๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. **AppConfig** ```java @Bean public QuestionService questionService() { return new QuestionServiceImpl(questionRepository()); } @Bean public QuestionRepository questionRepository() { return new QuestionRepositoryImpl(); } ``` QuestionServiceImpl ```java public QuestionServiceImpl(QuestionRepository questionRepository) { this.questionRepository = questionRepository; } ``` ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ•œ ๊ฒฝ์šฐ, `getBean()`์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์–ป์–ด์™€์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,45 @@ +package codesquad.mapper; + +import codesquad.dto.question.QuestionRequestDto; +import codesquad.dto.question.QuestionResponseDto; +import codesquad.entity.QuestionEntity; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class QuestionMapper { + + public static QuestionEntity dtoToEntity(QuestionRequestDto questionRequestDto) { + if (questionRequestDto == null){ + return null; + } + + QuestionEntity.QuestionEntityBuilder questionEntity = QuestionEntity.builder(); + + questionEntity.writer(questionRequestDto.getWriter()); + questionEntity.title(questionRequestDto.getTitle()); + questionEntity.contents(questionRequestDto.getContents()); + + return questionEntity.build(); + } + + public static QuestionResponseDto entityToDto(QuestionEntity questionEntity) { + if (questionEntity == null) { + return null; + } + + QuestionResponseDto.QuestionResponseDtoBuilder questionResponseDto = QuestionResponseDto.builder(); + + questionResponseDto.writer(questionEntity.getWriter()); + questionResponseDto.title(questionEntity.getTitle()); + questionResponseDto.contents(questionEntity.getContents()); + questionResponseDto.date(localTimeToString(questionEntity.getCreatedDate())); + + return questionResponseDto.build(); + } + + public static String localTimeToString(LocalDateTime localDateTime) { + String stringLocalTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); + return stringLocalTime; + } +}
Java
@hyukjin-lee ๋„ต!!! ๊ธฐ์–ตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์„ ์—ด์–ด๋‘๋Š” setter ์‚ฌ์šฉ์„ ์ง€์–‘ํ•˜๊ณ , ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์„ค์ •ํ•˜๊ณ  ์‹ถ์–ด Builder ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค~
@@ -1,158 +1,89 @@ package codesquad.qua; -import codesquad.answer.Answer; -import codesquad.answer.AnswerRepository; -import codesquad.user.User; -import codesquad.utils.SessionUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; -import java.util.NoSuchElementException; @Controller +@RequiredArgsConstructor +@Slf4j public class QuestionController { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - @Autowired - QuestionRepository questionRepository; - - @Autowired - AnswerRepository answerRepository; + private final QuestionService questionService; @GetMapping("/questions/form") public String createForm(HttpSession session) { - User user = SessionUtil.getUserBySession(session); - - if (user == null) { + if (!questionService.setCreateForm(session)) { return "/login"; } return "qna/form"; } @PostMapping("/questions") - public String create(Question question, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - - if (user == null) { + public String createQuestion(QuestionDto questionDto, HttpSession session) { + if (!questionService.create(questionDto, session)) { return "/login"; } - question.setWriter(user); - questionRepository.save(question); - logger.info("user : {}", question.getWriter().getName()); return "redirect:/"; } @GetMapping("/") - public String list(Model model) { - model.addAttribute("question", questionRepository.findAll()); + public String showQuestionList(Model model) { + model.addAttribute("question", questionService.list()); return "qna/list"; } @GetMapping("/questions/{id}") - public String qnaInfo(Model model, @PathVariable("id") Long id) { - Question question = findQuestionById(id); - model.addAttribute("question", question); - model.addAttribute("count", countAnswers(question)); - + public String showQuestion(Model model, @PathVariable("id") Long id) { + model.addAttribute("question", questionService.findQuestionById(id)); + model.addAttribute("count", questionService.countAnswers(id)); return "qna/show"; } - @Transactional @PutMapping("/questions/{id}") - public String update(Question changedQuestion, @PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + public String updateQuestion(QuestionDto changedQuestion, @PathVariable("id") Long id, HttpSession session) { + try { + questionService.update(changedQuestion, id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; } - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); - - savedQuestion.update(changedQuestion); - return "redirect:/"; } @GetMapping("/questions/{id}/updateForm") public String updateForm(Model model, @PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + try { + questionService.setUpdateForm(id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; } - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); - - model.addAttribute("question", savedQuestion); + model.addAttribute("question", questionService.findQuestionById(id)); return "qna/updateForm"; } @DeleteMapping("/questions/{id}") - @Transactional - public String remove(@PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); + public String removeQuestion(@PathVariable("id") Long id, HttpSession session) { - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + try { + questionService.remove(id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; - } - - if (!canDeleteQuestion(savedQuestion, user)) { + } catch (QuestionDeleteException deleteException) { return "qna/delete_failed"; } - savedQuestion.changeDeleteFlag(); - - for (Answer answer : savedQuestion.getAnswers()) { - answer.changeDeletedFlag(); - } - return "redirect:/questions/" + id; } - - private Question findQuestionById(Long id) { - return questionRepository.findById(id) - .orElseThrow(NoSuchElementException::new); - } - - private boolean isQuestionMatchUser(User loginUser, Question question) { - return question.equalsWriter(loginUser); - } - - private boolean canDeleteQuestion(Question deletedQuestion, User user) { - if (deletedQuestion.hasAnswers()) { - for (Answer answer : deletedQuestion.getAnswers()) { - if (!answer.equalsWriter(user)) { - return false; - } - } - } - return true; - } - - private int countAnswers(Question question) { - int count = 0; - for (Answer answer : question.getAnswers()) { - if (!answer.isDeletedFlag()) { - count++; - } - } - return count; - } } \ No newline at end of file
Java
Question์— ๋Œ€ํ•œ Exception์„ customizing ํ•œ ์ด์œ ์ž…๋‹ˆ๋‹ค QuestionEditException์€ ํ•ด๋‹น ์งˆ๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ญ์ œํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, QuestionDeleteException์€ ๋‹ค๋ฅธ User์˜ ๋‹ต๊ธ€์ด ์กด์žฌํ•˜์—ฌ ์‚ญ์ œํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค ์ฒ˜์Œ์—๋Š” boolean์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ ค๋‹ค๊ฐ€ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ฒŒ ๋˜์–ด(=๋‘ ๊ฐ€์ง€ view) exception์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค
@@ -1,10 +1,15 @@ package codesquad.user; +import lombok.Getter; +import lombok.NoArgsConstructor; + import javax.persistence.*; import java.util.Objects; @Entity +@NoArgsConstructor +@Getter public class User { @Id @@ -20,43 +25,14 @@ public class User { private String email; - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getPassword() { - return password; + User(SignUpUserDto signUpUserDto) { + userId = signUpUserDto.getUserId(); + password = signUpUserDto.getPassword(); + name = signUpUserDto.getName(); + email = signUpUserDto.getEmail(); } - public void setPassword(String password) { - this.password = password; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public Long getId() { - return id; - } - - public void update(User user) { + public void update(SignUpUserDto user) { if (equalsPassword(user.getPassword())) { name = user.getName(); email = user.getEmail(); @@ -67,6 +43,10 @@ public boolean equalsPassword(String password) { return this.password.equals(password); } + public boolean equalsPassword(LoginUserDto loginUserDto) { + return password.equals(loginUserDto.getPassword()); + } + public boolean equalsId(Long id) { return this.id.equals(id); } @@ -76,15 +56,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; - return Objects.equals(getId(), user.getId()) && - Objects.equals(getUserId(), user.getUserId()) && - Objects.equals(getPassword(), user.getPassword()) && - Objects.equals(getName(), user.getName()) && - Objects.equals(getEmail(), user.getEmail()); + return Objects.equals(getId(), user.getId()); } @Override public int hashCode() { - return Objects.hash(getId(), getUserId(), getPassword(), getName(), getEmail()); + return Objects.hash(getId()); } }
Java
์ €๋ฒˆ์— ๋ง์”€ํ•ด์ฃผ์‹  equals์— ๋Œ€ํ•œ ๋ฆฌ๋ทฐ ์ƒ๊ฐํ•ด๋ดค์Šต๋‹ˆ๋‹ค ๋ง์”€๋Œ€๋กœ ๊ด€๊ณ„ํ˜• DB์—์„œ๋Š” "์‹๋ณ„์ž(id)" ๊ฐ€ ๊ณ ์œ ํ•œ ๊ฐ’์œผ๋กœ, ํ•ด๋‹น ์‹๋ณ„์ž๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ์ธ์‹ํ•˜๋Š”๋ฐ equals๋ฅผ overridingํ•œ๋‹ค๋ฉด ๋ชจ๋“  ํ•„๋“œ์˜ ๊ฐ’์ด ๋™์ผํ•ด์•ผ ๊ฐ™์€ ๊ฐ์ฒด๋ผ๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค equals๋ฅผ ๊ตณ์ด overridingํ•  ํ•„์š”์—†๋Š” ์ƒํ™ฉ์—์„œ overriding์„ ํ•œ๋‹ค๋ฉด HashSet์ด๋‚˜ HashMap์— ์˜ํ–ฅ์„ ์ฃผ์–ด ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค ๋”ฐ๋ผ์„œ User๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” "id"๋กœ ์ถฉ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— equals๋ฅผ overridingํ•˜์ง€ ์•Š์•„๋„๋œ๋‹ค ๋ผ๋Š” ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค
@@ -0,0 +1,126 @@ +package codesquad.qua; + +import codesquad.answer.Answer; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.NoSuchElementException; + +@Slf4j +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public boolean setCreateForm(HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + return user != null; + } + + public boolean create(QuestionDto questionDto, HttpSession session) { + log.info("์ž‘์„ฑ์ž = {}", questionDto.getWriter()); + log.info("์ œ๋ชฉ = {}", questionDto.getTitle()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return false; + } + + questionRepository.save(new Question(questionDto, user)); + + return true; + } + + public List<Question> list() { + return questionRepository.findAll(); + } + + public void setUpdateForm(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + } + + @Transactional + public void update(QuestionDto changedQuestion, long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question content: {}", changedQuestion.getContents()); + log.info("question title: {}", changedQuestion.getTitle()); + + savedQuestion.update(changedQuestion); + } + + @Transactional + public void remove(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + if (!canDeleteQuestion(savedQuestion, user)) { + throw new QuestionDeleteException(); + } + + savedQuestion.deleteQuestion(); + } + + public long countAnswers(long questionId) { + Long countAnswer = questionRepository.countNotDeletedAnswers(questionId); + + if (countAnswer == null) { + return 0L; + } + + return countAnswer; + } + + public Question findQuestionById(Long id) { + return questionRepository.findById(id) + .orElseThrow(NoSuchElementException::new); + } + + private boolean canDeleteQuestion(Question deletedQuestion, User user) { + if (deletedQuestion.hasAnswers()) { + for (Answer answer : deletedQuestion.getAnswers()) { + if (!answer.equalsWriter(user)) { + return false; + } + } + } + return true; + } + + private boolean isQuestionMatchUser(User loginUser, Question question) { + log.info("match User? = {}", question.equalsWriter(loginUser)); + return question.equalsWriter(loginUser); + } +}
Java
canDeleteQuestion์€ ์งˆ๋ฌธ์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค - ๋‹ต๊ธ€์ด ์—†์„ ๊ฒฝ์šฐ ์‚ญ์ œ ๊ฐ€๋Šฅ - ๋‹ต๊ธ€์ด ์žˆ์œผ๋‚˜ ์ž‘์„ฑ์ž ๋ชจ๋‘ ์งˆ๋ฌธ ์ž‘์„ฑ์ž์™€ ๊ฐ™์„ ๊ฒฝ์šฐ ์‚ญ์ œ ๊ฐ€๋Šฅ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋Š” Question์ด ์‚ญ์ œ๋  ์ˆ˜ ์žˆ์–ด?๋ผ๊ณ  ๋ฌป๊ณ , Question์ด ์‚ญ์ œ๋  ์ˆ˜ ์žˆ์–ด/์—†์–ด๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค ๊ทธ๋ž˜์„œ ์ €๋Š” ์ด ๋ถ€๋ถ„์ด Question์—๊ฒŒ ๋ฌผ์–ด๋ณด๊ณ  Question์ด ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด Question ํด๋ž˜์Šค์— ์žˆ์–ด์•ผํ• ๊นŒ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋…ผ๋ฆฌ๋ผ๋ฉด ์•„๋ž˜ isQuestionMatchUser(์งˆ๋ฌธ ์ž‘์„ฑ์ž์™€ ๋กœ๊ทธ์ธ ๋œ User๊ฐ€ ๊ฐ™์€์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฉ”์„œ๋“œ)๋„ Question์— ์žˆ์–ด์•ผํ•˜์ง€ ์•Š์„๊นŒ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์ง€๋งŒ, ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด Question ํด๋ž˜์Šค๊ฐ€ ๋„ˆ๋ฌด ๋ฌด๊ฑฐ์›Œ์ง€์ง€ ์•Š์„๊นŒ๋ผ๋Š” ์ƒ๊ฐ๋„ ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค ๋ฆฌ๋ทฐ์–ด๋‹˜์€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋Š”์ง€ ๊ถ๊ธˆํ•ด์„œ comment ๋‚จ๊ฒจ๋ด…๋‹ˆ๋‹ค!
@@ -0,0 +1,62 @@ +package codesquad.answer; + +import codesquad.qua.Question; +import codesquad.qua.QuestionRepository; +import codesquad.response.Result; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.NoSuchElementException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class AnswerService { + + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public Result<ResponseAnswerDto> create(long questionId, RequestAnswerDto requestAnswerDto, HttpSession session) { + log.info("comment = {}", requestAnswerDto.getComment()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Question question = questionRepository.findById(questionId) + .orElseThrow(NoSuchElementException::new); + + Answer answer = new Answer(question, user, requestAnswerDto.getComment()); + answer.addQuestion(question); + answerRepository.save(answer); + + return Result.ok(answer.toResponseAnswerDto()); + } + + @Transactional + public Result<ResponseAnswerDto> remove(long questionId, long answerId, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Answer answer = answerRepository.findQuestionFetchJoinById(answerId) + .orElseThrow(NoSuchElementException::new); + + if (!answer.equalsWriter(user)) { + return Result.fail("๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋‹ต๊ธ€์€ ์‚ญ์ œ ๋ชปํ•ด์š”"); + } + + answer.changeDeletedFlag(); + + return Result.ok(answer.toResponseAnswerDto()); + } +} \ No newline at end of file
Java
Answer์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋‹ด๋‹นํ•˜๋Š” AnswerService์—์„œ Question๊ด€๋ จ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ QuestionRepository๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, QuestionService๋ฅผ ํ†ตํ•ด ์ฐธ์กฐํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฑฐ ๊ฐ™์€๋ฐ ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,62 @@ +package codesquad.answer; + +import codesquad.qua.Question; +import codesquad.qua.QuestionRepository; +import codesquad.response.Result; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.NoSuchElementException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class AnswerService { + + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public Result<ResponseAnswerDto> create(long questionId, RequestAnswerDto requestAnswerDto, HttpSession session) { + log.info("comment = {}", requestAnswerDto.getComment()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Question question = questionRepository.findById(questionId) + .orElseThrow(NoSuchElementException::new); + + Answer answer = new Answer(question, user, requestAnswerDto.getComment()); + answer.addQuestion(question); + answerRepository.save(answer); + + return Result.ok(answer.toResponseAnswerDto()); + } + + @Transactional + public Result<ResponseAnswerDto> remove(long questionId, long answerId, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Answer answer = answerRepository.findQuestionFetchJoinById(answerId) + .orElseThrow(NoSuchElementException::new); + + if (!answer.equalsWriter(user)) { + return Result.fail("๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋‹ต๊ธ€์€ ์‚ญ์ œ ๋ชปํ•ด์š”"); + } + + answer.changeDeletedFlag(); + + return Result.ok(answer.toResponseAnswerDto()); + } +} \ No newline at end of file
Java
Answer๋ฅผ db์—์„œ ์กฐํšŒํ•˜๋Š” ๋กœ์ง ๋‹ค์Œ ์„ธ์…˜ ์œ ์ € ์ฒดํฌ๊ฐ€ ์ด๋ค„์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์„ ํ•  ์ ์ด ์žˆ์–ด๋ณด์ด๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?
@@ -0,0 +1,27 @@ +package codesquad.answer; + +import codesquad.response.Result; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpSession; + +@RestController +@RequiredArgsConstructor +@Slf4j +public class ApiAnswerController { + + private final AnswerService answerService; + + @PostMapping("/questions/{question-id}/answers") + public Result<ResponseAnswerDto> create(@PathVariable("question-id") Long questionId, @RequestBody RequestAnswerDto requestAnswerDto, HttpSession session) { + return answerService.create(questionId, requestAnswerDto, session); + } + + @DeleteMapping("/questions/{question-id}/answers/{answer-id}") + public Result<ResponseAnswerDto> remove(@PathVariable("question-id") Long questionId, + @PathVariable("answer-id") Long answerId, HttpSession session) { + return answerService.remove(questionId, answerId, session); + } +}
Java
์ด ์š”์ฒญ์˜ ์‘๋‹ต ์ƒํƒœ์ฝ”๋“œ๋Š” ์–ด๋–ป๊ฒŒ ๋‚˜๊ฐˆ๊นŒ์š”?
@@ -0,0 +1,126 @@ +package codesquad.qua; + +import codesquad.answer.Answer; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.NoSuchElementException; + +@Slf4j +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public boolean setCreateForm(HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + return user != null; + } + + public boolean create(QuestionDto questionDto, HttpSession session) { + log.info("์ž‘์„ฑ์ž = {}", questionDto.getWriter()); + log.info("์ œ๋ชฉ = {}", questionDto.getTitle()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return false; + } + + questionRepository.save(new Question(questionDto, user)); + + return true; + } + + public List<Question> list() { + return questionRepository.findAll(); + } + + public void setUpdateForm(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + } + + @Transactional + public void update(QuestionDto changedQuestion, long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question content: {}", changedQuestion.getContents()); + log.info("question title: {}", changedQuestion.getTitle()); + + savedQuestion.update(changedQuestion); + } + + @Transactional + public void remove(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + if (!canDeleteQuestion(savedQuestion, user)) { + throw new QuestionDeleteException(); + } + + savedQuestion.deleteQuestion(); + } + + public long countAnswers(long questionId) { + Long countAnswer = questionRepository.countNotDeletedAnswers(questionId); + + if (countAnswer == null) { + return 0L; + } + + return countAnswer; + } + + public Question findQuestionById(Long id) { + return questionRepository.findById(id) + .orElseThrow(NoSuchElementException::new); + } + + private boolean canDeleteQuestion(Question deletedQuestion, User user) { + if (deletedQuestion.hasAnswers()) { + for (Answer answer : deletedQuestion.getAnswers()) { + if (!answer.equalsWriter(user)) { + return false; + } + } + } + return true; + } + + private boolean isQuestionMatchUser(User loginUser, Question question) { + log.info("match User? = {}", question.equalsWriter(loginUser)); + return question.equalsWriter(loginUser); + } +}
Java
1. Service๊ฐ์ฒด๊ฐ€ HttpSession๋ฅผ ์•Œ์•„์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ์š”? 2. ํ˜„์žฌ ์š”์ฒญ์— ๋Œ€ํ•œ ์„ธ์…˜ ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋ณด์ด๋Š”๋ฐ, ํ˜„์žฌ ๋ฉ”์†Œ๋“œ๋ช…์ด ์‹ค์ œ ์—ญํ• ์„ ์ถฉ๋ถ„ํžˆ ์„ค๋ช…ํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,126 @@ +package codesquad.qua; + +import codesquad.answer.Answer; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.NoSuchElementException; + +@Slf4j +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public boolean setCreateForm(HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + return user != null; + } + + public boolean create(QuestionDto questionDto, HttpSession session) { + log.info("์ž‘์„ฑ์ž = {}", questionDto.getWriter()); + log.info("์ œ๋ชฉ = {}", questionDto.getTitle()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return false; + } + + questionRepository.save(new Question(questionDto, user)); + + return true; + } + + public List<Question> list() { + return questionRepository.findAll(); + } + + public void setUpdateForm(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + } + + @Transactional + public void update(QuestionDto changedQuestion, long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question content: {}", changedQuestion.getContents()); + log.info("question title: {}", changedQuestion.getTitle()); + + savedQuestion.update(changedQuestion); + } + + @Transactional + public void remove(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + if (!canDeleteQuestion(savedQuestion, user)) { + throw new QuestionDeleteException(); + } + + savedQuestion.deleteQuestion(); + } + + public long countAnswers(long questionId) { + Long countAnswer = questionRepository.countNotDeletedAnswers(questionId); + + if (countAnswer == null) { + return 0L; + } + + return countAnswer; + } + + public Question findQuestionById(Long id) { + return questionRepository.findById(id) + .orElseThrow(NoSuchElementException::new); + } + + private boolean canDeleteQuestion(Question deletedQuestion, User user) { + if (deletedQuestion.hasAnswers()) { + for (Answer answer : deletedQuestion.getAnswers()) { + if (!answer.equalsWriter(user)) { + return false; + } + } + } + return true; + } + + private boolean isQuestionMatchUser(User loginUser, Question question) { + log.info("match User? = {}", question.equalsWriter(loginUser)); + return question.equalsWriter(loginUser); + } +}
Java
์„ธ์…˜์— ์œ ์ € ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜์•ผ๋งŒํ•˜๋Š” ๋ชจ๋“  ๋ฉ”์†Œ๋“œ์—์„œ ํ•ด๋‹น ๋กœ์ง์ด ์ž‘์„ฑ๋˜๊ณ  ์žˆ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ค‘๋ณต์„ ์ค„์ผ ์ˆ˜ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,126 @@ +package codesquad.qua; + +import codesquad.answer.Answer; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.NoSuchElementException; + +@Slf4j +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public boolean setCreateForm(HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + return user != null; + } + + public boolean create(QuestionDto questionDto, HttpSession session) { + log.info("์ž‘์„ฑ์ž = {}", questionDto.getWriter()); + log.info("์ œ๋ชฉ = {}", questionDto.getTitle()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return false; + } + + questionRepository.save(new Question(questionDto, user)); + + return true; + } + + public List<Question> list() { + return questionRepository.findAll(); + } + + public void setUpdateForm(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + } + + @Transactional + public void update(QuestionDto changedQuestion, long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question content: {}", changedQuestion.getContents()); + log.info("question title: {}", changedQuestion.getTitle()); + + savedQuestion.update(changedQuestion); + } + + @Transactional + public void remove(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + if (!canDeleteQuestion(savedQuestion, user)) { + throw new QuestionDeleteException(); + } + + savedQuestion.deleteQuestion(); + } + + public long countAnswers(long questionId) { + Long countAnswer = questionRepository.countNotDeletedAnswers(questionId); + + if (countAnswer == null) { + return 0L; + } + + return countAnswer; + } + + public Question findQuestionById(Long id) { + return questionRepository.findById(id) + .orElseThrow(NoSuchElementException::new); + } + + private boolean canDeleteQuestion(Question deletedQuestion, User user) { + if (deletedQuestion.hasAnswers()) { + for (Answer answer : deletedQuestion.getAnswers()) { + if (!answer.equalsWriter(user)) { + return false; + } + } + } + return true; + } + + private boolean isQuestionMatchUser(User loginUser, Question question) { + log.info("match User? = {}", question.equalsWriter(loginUser)); + return question.equalsWriter(loginUser); + } +}
Java
๋ฉ”์†Œ๋“œ๋ช…์„ ๋ณด๊ณ  Question์— ๋Œ€ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ์ฒด์—์„œ Form๊นŒ์ง€ ๊ด€์—ฌํ•˜๋Š” ๊ฑด๊ฐ€๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,126 @@ +package codesquad.qua; + +import codesquad.answer.Answer; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.NoSuchElementException; + +@Slf4j +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public boolean setCreateForm(HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + return user != null; + } + + public boolean create(QuestionDto questionDto, HttpSession session) { + log.info("์ž‘์„ฑ์ž = {}", questionDto.getWriter()); + log.info("์ œ๋ชฉ = {}", questionDto.getTitle()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return false; + } + + questionRepository.save(new Question(questionDto, user)); + + return true; + } + + public List<Question> list() { + return questionRepository.findAll(); + } + + public void setUpdateForm(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + } + + @Transactional + public void update(QuestionDto changedQuestion, long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question content: {}", changedQuestion.getContents()); + log.info("question title: {}", changedQuestion.getTitle()); + + savedQuestion.update(changedQuestion); + } + + @Transactional + public void remove(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + if (!canDeleteQuestion(savedQuestion, user)) { + throw new QuestionDeleteException(); + } + + savedQuestion.deleteQuestion(); + } + + public long countAnswers(long questionId) { + Long countAnswer = questionRepository.countNotDeletedAnswers(questionId); + + if (countAnswer == null) { + return 0L; + } + + return countAnswer; + } + + public Question findQuestionById(Long id) { + return questionRepository.findById(id) + .orElseThrow(NoSuchElementException::new); + } + + private boolean canDeleteQuestion(Question deletedQuestion, User user) { + if (deletedQuestion.hasAnswers()) { + for (Answer answer : deletedQuestion.getAnswers()) { + if (!answer.equalsWriter(user)) { + return false; + } + } + } + return true; + } + + private boolean isQuestionMatchUser(User loginUser, Question question) { + log.info("match User? = {}", question.equalsWriter(loginUser)); + return question.equalsWriter(loginUser); + } +}
Java
์ €๋Š” ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ฑ ์ธก๋ฉด์—์„œ ์ƒ๊ฐํ•ด๋ดค์„ ๋•Œ ์ด ๋กœ์ง์ด ํ•ด๋‹น Service์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฑฐ๋ผ๋ฉด Question์ด ๋ฌด๊ฑฐ์›Œ์ง€๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด์„œ Service์—์„œ ๋‹ด๋‹นํ•˜๋„๋ก ๊ตฌํ˜„ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด ์ด Service๋ฅผ ๋ฒ—์–ด๋‚œ ์˜์—ญ์—์„œ๋„ ์‚ฌ์šฉ๋˜๋Š” ๋กœ์ง์ด๋ผ๋ฉด Question์—์„œ ๋‹ด๋‹นํ•˜๋Š” ๊ฒƒ์ด ์ค‘๋ณต์„ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์ด์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,62 @@ +package codesquad.answer; + +import codesquad.qua.Question; +import codesquad.qua.QuestionRepository; +import codesquad.response.Result; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.NoSuchElementException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class AnswerService { + + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public Result<ResponseAnswerDto> create(long questionId, RequestAnswerDto requestAnswerDto, HttpSession session) { + log.info("comment = {}", requestAnswerDto.getComment()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Question question = questionRepository.findById(questionId) + .orElseThrow(NoSuchElementException::new); + + Answer answer = new Answer(question, user, requestAnswerDto.getComment()); + answer.addQuestion(question); + answerRepository.save(answer); + + return Result.ok(answer.toResponseAnswerDto()); + } + + @Transactional + public Result<ResponseAnswerDto> remove(long questionId, long answerId, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Answer answer = answerRepository.findQuestionFetchJoinById(answerId) + .orElseThrow(NoSuchElementException::new); + + if (!answer.equalsWriter(user)) { + return Result.fail("๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋‹ต๊ธ€์€ ์‚ญ์ œ ๋ชปํ•ด์š”"); + } + + answer.changeDeletedFlag(); + + return Result.ok(answer.toResponseAnswerDto()); + } +} \ No newline at end of file
Java
ResponseAnswerDto๋ฅผ Result๋กœ ํ•œ๋ฒˆ ๋” ๊ฐ์‹ธ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์…จ๋Š”๋ฐ ์–ด๋–ค ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -1,158 +1,89 @@ package codesquad.qua; -import codesquad.answer.Answer; -import codesquad.answer.AnswerRepository; -import codesquad.user.User; -import codesquad.utils.SessionUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; -import java.util.NoSuchElementException; @Controller +@RequiredArgsConstructor +@Slf4j public class QuestionController { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - @Autowired - QuestionRepository questionRepository; - - @Autowired - AnswerRepository answerRepository; + private final QuestionService questionService; @GetMapping("/questions/form") public String createForm(HttpSession session) { - User user = SessionUtil.getUserBySession(session); - - if (user == null) { + if (!questionService.setCreateForm(session)) { return "/login"; } return "qna/form"; } @PostMapping("/questions") - public String create(Question question, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - - if (user == null) { + public String createQuestion(QuestionDto questionDto, HttpSession session) { + if (!questionService.create(questionDto, session)) { return "/login"; } - question.setWriter(user); - questionRepository.save(question); - logger.info("user : {}", question.getWriter().getName()); return "redirect:/"; } @GetMapping("/") - public String list(Model model) { - model.addAttribute("question", questionRepository.findAll()); + public String showQuestionList(Model model) { + model.addAttribute("question", questionService.list()); return "qna/list"; } @GetMapping("/questions/{id}") - public String qnaInfo(Model model, @PathVariable("id") Long id) { - Question question = findQuestionById(id); - model.addAttribute("question", question); - model.addAttribute("count", countAnswers(question)); - + public String showQuestion(Model model, @PathVariable("id") Long id) { + model.addAttribute("question", questionService.findQuestionById(id)); + model.addAttribute("count", questionService.countAnswers(id)); return "qna/show"; } - @Transactional @PutMapping("/questions/{id}") - public String update(Question changedQuestion, @PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + public String updateQuestion(QuestionDto changedQuestion, @PathVariable("id") Long id, HttpSession session) { + try { + questionService.update(changedQuestion, id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; } - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); - - savedQuestion.update(changedQuestion); - return "redirect:/"; } @GetMapping("/questions/{id}/updateForm") public String updateForm(Model model, @PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + try { + questionService.setUpdateForm(id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; } - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); - - model.addAttribute("question", savedQuestion); + model.addAttribute("question", questionService.findQuestionById(id)); return "qna/updateForm"; } @DeleteMapping("/questions/{id}") - @Transactional - public String remove(@PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); + public String removeQuestion(@PathVariable("id") Long id, HttpSession session) { - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + try { + questionService.remove(id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; - } - - if (!canDeleteQuestion(savedQuestion, user)) { + } catch (QuestionDeleteException deleteException) { return "qna/delete_failed"; } - savedQuestion.changeDeleteFlag(); - - for (Answer answer : savedQuestion.getAnswers()) { - answer.changeDeletedFlag(); - } - return "redirect:/questions/" + id; } - - private Question findQuestionById(Long id) { - return questionRepository.findById(id) - .orElseThrow(NoSuchElementException::new); - } - - private boolean isQuestionMatchUser(User loginUser, Question question) { - return question.equalsWriter(loginUser); - } - - private boolean canDeleteQuestion(Question deletedQuestion, User user) { - if (deletedQuestion.hasAnswers()) { - for (Answer answer : deletedQuestion.getAnswers()) { - if (!answer.equalsWriter(user)) { - return false; - } - } - } - return true; - } - - private int countAnswers(Question question) { - int count = 0; - for (Answer answer : question.getAnswers()) { - if (!answer.isDeletedFlag()) { - count++; - } - } - return count; - } } \ No newline at end of file
Java
์ด๋ฏธ ๋ณด์…จ์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฑฐ ๊ฐ™์•„ ๊ณต์œ ๋“œ๋ฆฝ๋‹ˆ๋‹ค! https://tecoble.techcourse.co.kr/post/2020-08-17-custom-exception/
@@ -1,158 +1,89 @@ package codesquad.qua; -import codesquad.answer.Answer; -import codesquad.answer.AnswerRepository; -import codesquad.user.User; -import codesquad.utils.SessionUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; -import java.util.NoSuchElementException; @Controller +@RequiredArgsConstructor +@Slf4j public class QuestionController { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - @Autowired - QuestionRepository questionRepository; - - @Autowired - AnswerRepository answerRepository; + private final QuestionService questionService; @GetMapping("/questions/form") public String createForm(HttpSession session) { - User user = SessionUtil.getUserBySession(session); - - if (user == null) { + if (!questionService.setCreateForm(session)) { return "/login"; } return "qna/form"; } @PostMapping("/questions") - public String create(Question question, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - - if (user == null) { + public String createQuestion(QuestionDto questionDto, HttpSession session) { + if (!questionService.create(questionDto, session)) { return "/login"; } - question.setWriter(user); - questionRepository.save(question); - logger.info("user : {}", question.getWriter().getName()); return "redirect:/"; } @GetMapping("/") - public String list(Model model) { - model.addAttribute("question", questionRepository.findAll()); + public String showQuestionList(Model model) { + model.addAttribute("question", questionService.list()); return "qna/list"; } @GetMapping("/questions/{id}") - public String qnaInfo(Model model, @PathVariable("id") Long id) { - Question question = findQuestionById(id); - model.addAttribute("question", question); - model.addAttribute("count", countAnswers(question)); - + public String showQuestion(Model model, @PathVariable("id") Long id) { + model.addAttribute("question", questionService.findQuestionById(id)); + model.addAttribute("count", questionService.countAnswers(id)); return "qna/show"; } - @Transactional @PutMapping("/questions/{id}") - public String update(Question changedQuestion, @PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + public String updateQuestion(QuestionDto changedQuestion, @PathVariable("id") Long id, HttpSession session) { + try { + questionService.update(changedQuestion, id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; } - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); - - savedQuestion.update(changedQuestion); - return "redirect:/"; } @GetMapping("/questions/{id}/updateForm") public String updateForm(Model model, @PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + try { + questionService.setUpdateForm(id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; } - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); - - model.addAttribute("question", savedQuestion); + model.addAttribute("question", questionService.findQuestionById(id)); return "qna/updateForm"; } @DeleteMapping("/questions/{id}") - @Transactional - public String remove(@PathVariable("id") Long id, HttpSession session) { - User user = SessionUtil.getUserBySession(session); - Question savedQuestion = findQuestionById(id); - - logger.info("user: {}", user.getName()); - logger.info("question: {}", savedQuestion.getWriter()); + public String removeQuestion(@PathVariable("id") Long id, HttpSession session) { - if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + try { + questionService.remove(id, session); + } catch (QuestionEditException showException) { return "qna/show_failed"; - } - - if (!canDeleteQuestion(savedQuestion, user)) { + } catch (QuestionDeleteException deleteException) { return "qna/delete_failed"; } - savedQuestion.changeDeleteFlag(); - - for (Answer answer : savedQuestion.getAnswers()) { - answer.changeDeletedFlag(); - } - return "redirect:/questions/" + id; } - - private Question findQuestionById(Long id) { - return questionRepository.findById(id) - .orElseThrow(NoSuchElementException::new); - } - - private boolean isQuestionMatchUser(User loginUser, Question question) { - return question.equalsWriter(loginUser); - } - - private boolean canDeleteQuestion(Question deletedQuestion, User user) { - if (deletedQuestion.hasAnswers()) { - for (Answer answer : deletedQuestion.getAnswers()) { - if (!answer.equalsWriter(user)) { - return false; - } - } - } - return true; - } - - private int countAnswers(Question question) { - int count = 0; - for (Answer answer : question.getAnswers()) { - if (!answer.isDeletedFlag()) { - count++; - } - } - return count; - } } \ No newline at end of file
Java
์˜ค ๋งž์Šต๋‹ˆ๋‹ค ์ €๋„ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ์˜ฌ๋ ค์ฃผ์‹  ๋งํฌ๋ฅผ ๋ดค์Šต๋‹ˆ๋‹ค. ๋งํฌ์— ๋Œ€ํ•ด์„œ ๋ณด๋ฉด ๊ตณ์ด ๋‚˜๋ˆŒ ํ•„์š” ์—†์ด, IllegalArgumentException, IllegalstateException์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ทฐ๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋•Œ๋Š” ๋ช…์‹œ์ ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š”๊ฒŒ ๋‚ซ์ง€ ์•Š์„๊นŒ ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,126 @@ +package codesquad.qua; + +import codesquad.answer.Answer; +import codesquad.exception.QuestionDeleteException; +import codesquad.exception.QuestionEditException; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.NoSuchElementException; + +@Slf4j +@RequiredArgsConstructor +@Service +public class QuestionService { + + private final QuestionRepository questionRepository; + + public boolean setCreateForm(HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + return user != null; + } + + public boolean create(QuestionDto questionDto, HttpSession session) { + log.info("์ž‘์„ฑ์ž = {}", questionDto.getWriter()); + log.info("์ œ๋ชฉ = {}", questionDto.getTitle()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return false; + } + + questionRepository.save(new Question(questionDto, user)); + + return true; + } + + public List<Question> list() { + return questionRepository.findAll(); + } + + public void setUpdateForm(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + } + + @Transactional + public void update(QuestionDto changedQuestion, long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + log.info("user: {}", user.getName()); + log.info("question content: {}", changedQuestion.getContents()); + log.info("question title: {}", changedQuestion.getTitle()); + + savedQuestion.update(changedQuestion); + } + + @Transactional + public void remove(long id, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + Question savedQuestion = findQuestionById(id); + + log.info("user: {}", user.getName()); + log.info("question: {}", savedQuestion.getWriter()); + + if (user == null || !isQuestionMatchUser(user, savedQuestion)) { + throw new QuestionEditException(); + } + + if (!canDeleteQuestion(savedQuestion, user)) { + throw new QuestionDeleteException(); + } + + savedQuestion.deleteQuestion(); + } + + public long countAnswers(long questionId) { + Long countAnswer = questionRepository.countNotDeletedAnswers(questionId); + + if (countAnswer == null) { + return 0L; + } + + return countAnswer; + } + + public Question findQuestionById(Long id) { + return questionRepository.findById(id) + .orElseThrow(NoSuchElementException::new); + } + + private boolean canDeleteQuestion(Question deletedQuestion, User user) { + if (deletedQuestion.hasAnswers()) { + for (Answer answer : deletedQuestion.getAnswers()) { + if (!answer.equalsWriter(user)) { + return false; + } + } + } + return true; + } + + private boolean isQuestionMatchUser(User loginUser, Question question) { + log.info("match User? = {}", question.equalsWriter(loginUser)); + return question.equalsWriter(loginUser); + } +}
Java
updateํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์กฐ๊ฑด์ด ํ•„์š”ํ•œ๋ฐ ์ด ์กฐ๊ฑด์„ ๊ฒ€์ฆํ•˜๋Š” ๊ฒƒ๋„ Question์— ๋Œ€ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค๋กœ์ง์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ๋ฉ”์„œ๋“œ๋ช…์„ ์ˆ˜์ •ํ•˜๋Š”๊ฒŒ ์ข‹์•„๋ณด์ผ ๊ฒƒ ๊ฐ™์€ ๋ง์”€์ด์‹ค๊นŒ์š”~~??
@@ -0,0 +1,62 @@ +package codesquad.answer; + +import codesquad.qua.Question; +import codesquad.qua.QuestionRepository; +import codesquad.response.Result; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.NoSuchElementException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class AnswerService { + + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public Result<ResponseAnswerDto> create(long questionId, RequestAnswerDto requestAnswerDto, HttpSession session) { + log.info("comment = {}", requestAnswerDto.getComment()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Question question = questionRepository.findById(questionId) + .orElseThrow(NoSuchElementException::new); + + Answer answer = new Answer(question, user, requestAnswerDto.getComment()); + answer.addQuestion(question); + answerRepository.save(answer); + + return Result.ok(answer.toResponseAnswerDto()); + } + + @Transactional + public Result<ResponseAnswerDto> remove(long questionId, long answerId, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Answer answer = answerRepository.findQuestionFetchJoinById(answerId) + .orElseThrow(NoSuchElementException::new); + + if (!answer.equalsWriter(user)) { + return Result.fail("๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋‹ต๊ธ€์€ ์‚ญ์ œ ๋ชปํ•ด์š”"); + } + + answer.changeDeletedFlag(); + + return Result.ok(answer.toResponseAnswerDto()); + } +} \ No newline at end of file
Java
์˜ค,, ์ด ๋ถ€๋ถ„์€ DB ์ ‘๊ทผ์€ ๋ฌด๊ฑฐ์šด ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ธ์…˜์„ ์ฒดํฌํ•˜๊ณ  ๋‚˜์„œ DB ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,62 @@ +package codesquad.answer; + +import codesquad.qua.Question; +import codesquad.qua.QuestionRepository; +import codesquad.response.Result; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.NoSuchElementException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class AnswerService { + + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public Result<ResponseAnswerDto> create(long questionId, RequestAnswerDto requestAnswerDto, HttpSession session) { + log.info("comment = {}", requestAnswerDto.getComment()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Question question = questionRepository.findById(questionId) + .orElseThrow(NoSuchElementException::new); + + Answer answer = new Answer(question, user, requestAnswerDto.getComment()); + answer.addQuestion(question); + answerRepository.save(answer); + + return Result.ok(answer.toResponseAnswerDto()); + } + + @Transactional + public Result<ResponseAnswerDto> remove(long questionId, long answerId, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Answer answer = answerRepository.findQuestionFetchJoinById(answerId) + .orElseThrow(NoSuchElementException::new); + + if (!answer.equalsWriter(user)) { + return Result.fail("๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋‹ต๊ธ€์€ ์‚ญ์ œ ๋ชปํ•ด์š”"); + } + + answer.changeDeletedFlag(); + + return Result.ok(answer.toResponseAnswerDto()); + } +} \ No newline at end of file
Java
์ด ๋ถ€๋ถ„๋งŒ ๊ทธ๋Ÿฐ๊ฒŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๊ณณ๋„ ๊ทธ๋ ‡๊ฒŒ ์ž‘์„ฑ๋˜์–ด์žˆ๋„ค์š”!!
@@ -0,0 +1,62 @@ +package codesquad.answer; + +import codesquad.qua.Question; +import codesquad.qua.QuestionRepository; +import codesquad.response.Result; +import codesquad.user.User; +import codesquad.utils.SessionUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.servlet.http.HttpSession; +import java.util.NoSuchElementException; + +@Service +@Slf4j +@RequiredArgsConstructor +public class AnswerService { + + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public Result<ResponseAnswerDto> create(long questionId, RequestAnswerDto requestAnswerDto, HttpSession session) { + log.info("comment = {}", requestAnswerDto.getComment()); + + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Question question = questionRepository.findById(questionId) + .orElseThrow(NoSuchElementException::new); + + Answer answer = new Answer(question, user, requestAnswerDto.getComment()); + answer.addQuestion(question); + answerRepository.save(answer); + + return Result.ok(answer.toResponseAnswerDto()); + } + + @Transactional + public Result<ResponseAnswerDto> remove(long questionId, long answerId, HttpSession session) { + User user = SessionUtil.getUserBySession(session); + + if (user == null) { + return Result.fail("๋กœ๊ทธ์ธ ํ•˜์„ธ์š”"); + } + + Answer answer = answerRepository.findQuestionFetchJoinById(answerId) + .orElseThrow(NoSuchElementException::new); + + if (!answer.equalsWriter(user)) { + return Result.fail("๋‹ค๋ฅธ ์‚ฌ๋žŒ ๋‹ต๊ธ€์€ ์‚ญ์ œ ๋ชปํ•ด์š”"); + } + + answer.changeDeletedFlag(); + + return Result.ok(answer.toResponseAnswerDto()); + } +} \ No newline at end of file
Java
๋ฏธ์…˜ ๋ช…์„ธ์—์„œ Result๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ๊ณ  ํ•ด์„œ ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,27 @@ +package codesquad.answer; + +import codesquad.response.Result; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpSession; + +@RestController +@RequiredArgsConstructor +@Slf4j +public class ApiAnswerController { + + private final AnswerService answerService; + + @PostMapping("/questions/{question-id}/answers") + public Result<ResponseAnswerDto> create(@PathVariable("question-id") Long questionId, @RequestBody RequestAnswerDto requestAnswerDto, HttpSession session) { + return answerService.create(questionId, requestAnswerDto, session); + } + + @DeleteMapping("/questions/{question-id}/answers/{answer-id}") + public Result<ResponseAnswerDto> remove(@PathVariable("question-id") Long questionId, + @PathVariable("answer-id") Long answerId, HttpSession session) { + return answerService.remove(questionId, answerId, session); + } +}
Java
create๋ฉ”์„œ๋“œ๋Š” ์„ฑ๊ณต, ์‹คํŒจ ์—ฌ๋ถ€์—†์ด Result ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 200 OK๊ฐ€ ๋‚˜๊ฐˆ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,13 @@ +export class NetworkError extends Error { + constructor(message) { + super(message); + this.name = "NetworkError"; + } +} + +export class HttpError extends Error { + constructor(message) { + super(message); + this.name = "HttpError"; + } +}
JavaScript
์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์ฝ”๋“œ๊ฐ€ ๋‚จ์•„์žˆ๋„ค์š”, ํ˜น์‹œ ์–ด๋–ค ์˜๋„๋กœ ๋งŒ๋“œ์…จ์„๊นŒ์š”?!
@@ -0,0 +1,39 @@ +function formatUrl(config) { + const { href, query } = config; + const searchParams = new URLSearchParams(query); + + return `${href}?${searchParams.toString()}`; +} + +function formatHeaders(headersRecord) { + const headers = new Headers(); + + headers.append("content-type", "application/json"); + Object.entries(headersRecord).forEach(([key, value]) => { + headers.append(key, value); + }); + return headers; +} + +export async function createAPIRequest(request) { + const response = await fetch( + formatUrl({ + href: request.url, + query: request.query ?? {}, + }), + { + method: request.method || "GET", + headers: formatHeaders(request.headers), + body: request.body, + }, + ).catch((error) => { + throw new Error("Network error: ", error); + }); + + if (!response.ok) { + throw new Error("Http error: ", response.status); + } + + const data = !response.body ? null : await response.json(); + return data; +}
JavaScript
[MDN Error() ์ƒ์„ฑ์ž](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) ๋ฌธ์„œ ์ฒจ๋ถ€๋“œ๋ฆฝ๋‹ˆ๋‹ค! error ๊ฐ์ฒด๋Š” ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ `message`๋ฅผ ๋ฐ›์œผ๋ฉฐ, ๋‘๋ฒˆ์งธ ์„ธ๋ฒˆ์งธ ์ธ์ž๊ฐ€ ์žˆ๊ธดํ•˜์ง€๋งŒ, ๋น„ํ‘œ์ค€์ž…๋‹ˆ๋‹ค! ์‹ค์ œ๋กœ ์—๋Ÿฌ๋ฅผ ์ผ์œผ์ผœ๋ณด๋ฉด alert์—์„œ `Http error: ` , `Network error: ` ํ•ด๋‹น ๋ฌธ์ž์—ด๋งŒ ๋…ธ์ถœ๋˜๋Š” ๊ฑธ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์„๊ฑฐ์—์š”!
@@ -0,0 +1,39 @@ +function formatUrl(config) { + const { href, query } = config; + const searchParams = new URLSearchParams(query); + + return `${href}?${searchParams.toString()}`; +} + +function formatHeaders(headersRecord) { + const headers = new Headers(); + + headers.append("content-type", "application/json"); + Object.entries(headersRecord).forEach(([key, value]) => { + headers.append(key, value); + }); + return headers; +} + +export async function createAPIRequest(request) { + const response = await fetch( + formatUrl({ + href: request.url, + query: request.query ?? {}, + }), + { + method: request.method || "GET", + headers: formatHeaders(request.headers), + body: request.body, + }, + ).catch((error) => { + throw new Error("Network error: ", error); + }); + + if (!response.ok) { + throw new Error("Http error: ", response.status); + } + + const data = !response.body ? null : await response.json(); + return data; +}
JavaScript
์ด ํŒŒ์ผ์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ๋А๋‚Œ์€ `formatUrl`, `formatHeaders` ํ•จ์ˆ˜์˜ ํ•„์š”์„ฑ์„ ๋ชจ๋ฅด๊ฒ ๋‹ค.. ์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค! `createAPIRequest` ์ฒ˜๋Ÿผ ์ „์ฒด์ ์ธ ์š”์ฒญ์— ๋Œ€ํ•œ ๊ณตํ†ต ํ•จ์ˆ˜๋Š” ์žˆ์œผ๋ฉด ๋‹น์—ฐํžˆ ์ข‹๋‹ค! ์ด๊ธดํ•œ๋ฐ ํ˜•ํƒœ์— ๋Œ€ํ•ด์„œ๋Š” ์กฐ๊ธˆ ์•„์‰ฌ์šด ๋ถ€๋ถ„์ด ์žˆ๋‹ค๊ณ  ๋А๊ปด์ง‘๋‹ˆ๋‹ค! ์ฝ”๋ฉ˜ํŠธ๋กœ ํ•˜๋‚˜ ํ•˜๋‚˜ ์ž‘์„ฑํ•ด๋ณผ๊ฒŒ์š”!
@@ -0,0 +1,39 @@ +function formatUrl(config) { + const { href, query } = config; + const searchParams = new URLSearchParams(query); + + return `${href}?${searchParams.toString()}`; +} + +function formatHeaders(headersRecord) { + const headers = new Headers(); + + headers.append("content-type", "application/json"); + Object.entries(headersRecord).forEach(([key, value]) => { + headers.append(key, value); + }); + return headers; +} + +export async function createAPIRequest(request) { + const response = await fetch( + formatUrl({ + href: request.url, + query: request.query ?? {}, + }), + { + method: request.method || "GET", + headers: formatHeaders(request.headers), + body: request.body, + }, + ).catch((error) => { + throw new Error("Network error: ", error); + }); + + if (!response.ok) { + throw new Error("Http error: ", response.status); + } + + const data = !response.body ? null : await response.json(); + return data; +}
JavaScript
`headers.append("content-type", "application/json")`; ์‚ฌ์‹ค์ƒ ์—ญํ• ์€ ๋ชจ๋“  ์š”์ฒญ์— ํ•ด๋‹น ํ—ค๋”๋ฅผ ๋”ํ•ด์ฃผ๋Š” ์—ญํ•  ๋ฐ–์— ์—†๋Š”๋ฐ `createAPIRequest`์˜ headers์—์„œ ํ•ด์ฃผ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ๋„ค์š”! ```js headers: { ...request.headers 'content-type': "application/json" } ```
@@ -0,0 +1,39 @@ +function formatUrl(config) { + const { href, query } = config; + const searchParams = new URLSearchParams(query); + + return `${href}?${searchParams.toString()}`; +} + +function formatHeaders(headersRecord) { + const headers = new Headers(); + + headers.append("content-type", "application/json"); + Object.entries(headersRecord).forEach(([key, value]) => { + headers.append(key, value); + }); + return headers; +} + +export async function createAPIRequest(request) { + const response = await fetch( + formatUrl({ + href: request.url, + query: request.query ?? {}, + }), + { + method: request.method || "GET", + headers: formatHeaders(request.headers), + body: request.body, + }, + ).catch((error) => { + throw new Error("Network error: ", error); + }); + + if (!response.ok) { + throw new Error("Http error: ", response.status); + } + + const data = !response.body ? null : await response.json(); + return data; +}
JavaScript
๊ฐ€์žฅ ๋จผ์ € searchParams์˜ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„ ์—†์ด URL์— `?`๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  `createAPIRequest`๋ณด๋‹ค ์‹ค์ œ ์š”์ฒญํ•˜๋Š” ๊ณณ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–จ๊นŒ ์‹ถ๊ธดํ•ฉ๋‹ˆ๋‹ค! ์–ด๋–ค ์˜๋ฏธ๋ƒ๋ฉด์š”! fetch API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋“œ๋Ÿฌ๋‚ด๊ณ , ์‚ฌ์šฉ์ฒ˜์—์„œ ์ด๋ฅผ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉด ๋” ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค. `fetchPopularMovies`๋Š” fetch API์— ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ํ˜•ํƒœ์˜ `createAPIRequest`์— ์•Œ๋งž์€ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค. `createAPIRequest`๋Š” ์ด ์ธ์ž๋ฅผ ๊ฐ€๊ณตํ•ด์„œ fetch API์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ์„ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”! ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ `createAPIRequest`์˜ ๋™์ž‘๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ชจ๋ฅด๋”๋ผ๋„ fetch API์˜ request ์ •๋ณด์— ๋งž๋Š” ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด, ํ•„์š”ํ•œ ์ถ”๊ฐ€์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์„œ ์š”์ฒญ์„ ํ•ด์ค€๋‹ค! ๊ฐ€ ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”~ ๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ, fetch๋Š” ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ URL์„ ๋ฐ›์œผ๋‹ˆ, createAPIRequest์—์„œ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด, createAPIRequest๋Š” ์ธ์ž๋กœ fetch์˜ request ์ •๋ณด์™€ ๋™์ผํ•œ ํ˜•ํƒœ์˜ ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์—†๊ฒŒ ๋˜๊ณ , ์‚ฌ์šฉํ•˜๋Š” ์ž…์žฅ์—์„œ ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ ํ•™์Šต์ด ํ•„์š”ํ•˜๊ฒŒ๋  ๊ฒƒ ๊ฐ™์•„์š”~
@@ -0,0 +1,39 @@ +function formatUrl(config) { + const { href, query } = config; + const searchParams = new URLSearchParams(query); + + return `${href}?${searchParams.toString()}`; +} + +function formatHeaders(headersRecord) { + const headers = new Headers(); + + headers.append("content-type", "application/json"); + Object.entries(headersRecord).forEach(([key, value]) => { + headers.append(key, value); + }); + return headers; +} + +export async function createAPIRequest(request) { + const response = await fetch( + formatUrl({ + href: request.url, + query: request.query ?? {}, + }), + { + method: request.method || "GET", + headers: formatHeaders(request.headers), + body: request.body, + }, + ).catch((error) => { + throw new Error("Network error: ", error); + }); + + if (!response.ok) { + throw new Error("Http error: ", response.status); + } + + const data = !response.body ? null : await response.json(); + return data; +}
JavaScript
์ฝ”๋ฉ˜ํŠธ๊ฐ€ ๋งŽ์ด ๊ธธ์–ด์กŒ๋„ค์š”.. ใ…Žใ…Žใ…Žใ…Ž ์š”์•ฝํ•˜์ž๋ฉด, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋™๋ฃŒ๊ฐ€ ๋”ฐ๋กœ ๋œฏ์–ด๋ณด์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜์ž..! ์ž…๋‹ˆ๋‹ค
@@ -0,0 +1,39 @@ +function formatUrl(config) { + const { href, query } = config; + const searchParams = new URLSearchParams(query); + + return `${href}?${searchParams.toString()}`; +} + +function formatHeaders(headersRecord) { + const headers = new Headers(); + + headers.append("content-type", "application/json"); + Object.entries(headersRecord).forEach(([key, value]) => { + headers.append(key, value); + }); + return headers; +} + +export async function createAPIRequest(request) { + const response = await fetch( + formatUrl({ + href: request.url, + query: request.query ?? {}, + }), + { + method: request.method || "GET", + headers: formatHeaders(request.headers), + body: request.body, + }, + ).catch((error) => { + throw new Error("Network error: ", error); + }); + + if (!response.ok) { + throw new Error("Http error: ", response.status); + } + + const data = !response.body ? null : await response.json(); + return data; +}
JavaScript
`data` ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”ํ•  ์—†์ด ๋ฐ”๋กœ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋„ค์š”~
@@ -0,0 +1,37 @@ +import "./components/layout/app-header.js"; +import "./components/movie/movie-card.js"; +import "./components/movie/movie-list.js"; +import "./components/movie/movie-container.js"; + +import { html } from "./utils/template.js"; + +class App extends HTMLElement { + constructor() { + super(); + this.attachShadow({ mode: "open" }); + } + + connectedCallback() { + this.render(); + } + + render() { + this.shadowRoot.innerHTML = this.template(); + } + + template() { + return html` + <style> + * { + padding-bottom: 48px; + } + </style> + <app-header></app-header> + <main> + <movie-container></movie-container> + </main> + `; + } +} + +customElements.define("movie-app", App);
JavaScript
์ œ๊ฑฐ ํ•ด๋„ ๊ดœ์ฐฎ์€ ์Šคํƒ€์ผ ๊ฐ™์€๋ฐ ์–ด๋–ค ์˜๋ฏธ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,2 @@ +export const html = String.raw; +export const css = String.raw;
JavaScript
๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ์žˆ๋„ค์š”!
@@ -0,0 +1,58 @@ +import endedPopularMovies from "../fixtures/ended-popular-movies.json"; + +describe("์˜ํ™” ๋ชฉ๋ก", () => { + beforeEach(() => { + const url = "http://localhost:8080"; + cy.visit(url); + + cy.get("movie-app") + .as("movieApp") + .shadow() + .find("movie-container") + .as("movieContainer") + .shadow() + .find("movie-list") + .as("movieList") + .shadow() + .find("movie-card") + .as("movieCard"); + }); + + it("์ตœ์ดˆ ๋กœ๋“œ์‹œ 20๊ฐœ์˜ ์˜ํ™” ์นด๋“œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.get("@movieCard").should("have.length", 20); + }); + + it("๋”๋ณด๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ 20๊ฐœ์˜ ์˜ํ™”๋ฅผ ์ถ”๊ฐ€๋กœ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.get("@movieContainer").shadow().find(".show-more").as("showMoreButton"); + + cy.get("@movieCard").should("have.length", 20); + cy.get("@showMoreButton").contains("๋”๋ณด๊ธฐ").click(); + cy.get("@movieCard").should("have.length", 40); + }); + + it("๋กœ๋”ฉ์ค‘์ผ ๋•Œ๋Š” ์Šค์ผˆ๋ ˆํ†ค ์นด๋“œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.", () => { + cy.intercept("GET", "**/popular*", (req) => { + req.on("response", (res) => { + res.setDelay(1000); // Delay the response to simulate loading + }); + }).as("getPopularMoviesDelayed"); + + cy.reload(); + cy.get("@movieCard").shadow().find(".skeleton").should("exist"); + cy.wait("@getPopularMoviesDelayed"); + + cy.get("@movieCard").should("have.length", 20); + }); + + it("์˜ํ™” ๋ชฉ๋ก์„ ๋”์ด์ƒ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์œผ๋ฉด '๋”๋ณด๊ธฐ' ๋ฒ„ํŠผ์ด ์‚ฌ๋ผ์ง„๋‹ค.", () => { + cy.intercept("GET", "**/popular*", endedPopularMovies).as( + "getEndedPopularMovies", + ); + cy.get("@movieContainer").shadow().find(".show-more").as("showMoreButton"); + cy.get("@showMoreButton").should("exist"); + cy.get("@showMoreButton").click(); + + cy.wait("@getEndedPopularMovies"); + cy.get("@movieContainer").shadow().find("show-more").should("not.exist"); + }); +});
JavaScript
ํ…Œ์ŠคํŠธ๊ฐ€ ๋ชจ๋‘ ํ•ดํ”ผ์ผ€์ด์Šค๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์ž‘์„ฑ๋œ ๊ฒƒ ๊ฐ™์•„์š”! ๋„คํŠธ์›Œํฌ ์—๋Ÿฌ๋‚˜ HTTP ์—๋Ÿฌ์— ๊ด€ํ•œ ์ƒํ™ฉ์— ์ ์ ˆํ•˜๊ฒŒ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€๋„ ํ™•์ธํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,143 @@ +import StarFilled from "../../../images/star_filled.png"; +import { html } from "../../utils/template.js"; + +const BASE_URL = "https://image.tmdb.org/t/p/w220_and_h330_face"; + +class MovieCard extends HTMLElement { + static get observedAttributes() { + return ["posterPath", "title", "voteAverage", "isLoading"]; + } + + constructor() { + super(); + this.attachShadow({ mode: "open" }); + } + + get posterPath() { + return this.getAttribute("posterPath"); + } + + get title() { + return this.getAttribute("title"); + } + + get voteAverage() { + return this.getAttribute("voteAverage"); + } + + get isLoading() { + return this.getAttribute("isLoading") === "true"; + } + + attributeChangedCallback(name, oldValue, newValue) { + if (oldValue !== newValue) { + this.render(); + } + } + + connectedCallback() { + this.render(); + } + + render() { + const state = { + posterPath: this.posterPath, + title: this.title, + voteAverage: this.voteAverage, + isLoading: this.isLoading, + }; + this.shadowRoot.innerHTML = this.template(state); + } + + template({ posterPath, title, voteAverage, isLoading }) { + return html` + <style> + .movie-card { + display: flex; + flex-direction: column; + } + + .movie-thumbnail { + border-radius: 8px; + width: 180px; + height: 270px; + background-size: contain; + } + + .movie-title { + margin-top: 16px; + font-size: 1.2rem; + font-weight: bold; + } + + .movie-score { + margin-top: 16px; + font-size: 1.2rem; + } + + .movie-score::after { + margin-left: 8px; + } + + a { + color: inherit; + text-decoration: none; + } + + .movie-title.skeleton::after, + .movie-score.skeleton::after { + font-size: 0; + content: "loading"; + } + + .movie-card .skeleton { + background: linear-gradient(-90deg, #aaa, #f0f0f0, #aaa, #f0f0f0); + background-size: 400%; + animation: skeleton-animation 5s infinite ease-out; + border-radius: 8px; + } + + @keyframes skeleton-animation { + 0% { + background-position: 0% 50%; + } + 50% { + background-position: 100% 50%; + } + 100% { + background-position: 0% 50%; + } + } + </style> + <li> + ${isLoading + ? html` + <div class="movie-card"> + <div class="movie-thumbnail skeleton"></div> + <p class="movie-title skeleton"></p> + <p class="movie-score skeleton"></p> + </div> + ` + : html` + <a href="#"> + <div class="movie-card"> + <img + class="movie-thumbnail" + src="${BASE_URL}/${posterPath}" + loading="lazy" + alt="${title}" + /> + <p class="movie-title">${title}</p> + <p class="movie-score"> + <img src="${StarFilled}" alt="๋ณ„์ " /> + ${Number(voteAverage).toFixed(1)} + </p> + </div> + </a> + `} + </li> + `; + } +} + +customElements.define("movie-card", MovieCard);
JavaScript
`a` ํƒœ๊ทธ ๋ฐ `href="#'`๋ฅผ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”?
@@ -3,7 +3,10 @@ <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> - <title>React App</title> + <meta name="description" content="Web site created using create-react-app"/> + <link href="https://fonts.googleapis.com/css2?family=Lobster&display=swap" rel="stylesheet"> + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"> + <title>Westagram</title> </head> <body> <div id="root"></div>
Unknown
description์€ ์—†์–ด๋„ ๋˜์ง€ ์•Š์„๊นŒ์š”? ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž
@@ -0,0 +1,50 @@ +import React from 'react'; +import { withRouter } from 'react-router-dom'; +import './Login.scss'; + + +class LoginJaeyoung extends React.Component { + constructor () { + super(); + this.state = { + id: '', + pw : '', + }; + } + + handleInput = (e) => { + const { name,value} = e.target; + this.setState({ + [name]: value + }) + } + + goToMain = () => { + this.props.history.push('/main-jaeyoung') + } + + + render () { + const { handleInput } = this + const isBtnAble = this.state.id.includes('@') && this.state.pw.length >= 5; + + + return ( + <div className="loginContainer"> + <div className="logoName"> + <h1>Instargram</h1> + </div> + <div className="loginInfo"> + <input type="text" name="id" onChange={handleInput} className="loginId" placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" /> + <input type="password" name="pw" onChange={handleInput} className="loginPs" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ"/> + <button className="loginBt" onClick={this.goToMain} disabled={!isBtnAble}>๋กœ๊ทธ์ธ</button> + </div> + <div className="forgetPassword"> + <a href="https://www.naver.com/">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </div> + ) + } +} + +export default withRouter( LoginJaeyoung ); \ No newline at end of file
JavaScript
https://www.notion.so/wecode/React-Refactoring-Check-List-aea297cf88ed4601b769e4b2c2cfd4e1#089493d6bfca438aa328226e327cff92 ์ด ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•˜๋ฉด ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ๋กค ๋ฌถ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,85 @@ +* { + box-sizing: border-box; +} + +.loginContainer { + border: 2px solid #e6e6e6; + width: 350px; + height: 380px; + margin: 0 auto; + .logoName { + display: flex; + justify-content: center; + align-items: center; + width: 100%; + h1 { + font-family: "Lobster", cursive; + width: 175px; + height: 52px; + text-align: center; + padding-top: 6px; + } + } + .loginInfo { + height: 213px; + display: flex; + flex-direction: column; + align-items: center; + padding-top: 10px; + .loginId { + width: 268px; + height: 38px; + margin-bottom: 5px; + padding-left: 10px; + border-radius: 3px; + border: 1px solid #efefef; + background: #fafafa; + color: #9c9c9c; + font-size: 14px; + &:focus { + outline-color: gray; + } + } + .loginPs { + width: 268px; + height: 38px; + margin-bottom: 5px; + padding-left: 10px; + border-radius: 3px; + border: 1px solid #efefef; + background: #fafafa; + color: #9c9c9c; + font-size: 14px; + &:focus { + outline-color: gray; + } + } + .loginBt { + width: 268px; + height: 30px; + margin-top: 5px; + border-radius: 3px; + background-color: #2795f6; + color: #ffffff; + border-style: none; + + &:focus { + outline: none; + } + + &:disabled { + background-color: #b2dffc; + } + } + } + .forgetPassword{ + display: flex; + justify-content: center; + margin-top: 20px; + a { + color: #043569; text-decoration: none; + } + } + + +}
Unknown
์ด ๋ถ€๋ถ„์€ common.css์— ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,85 @@ +* { + box-sizing: border-box; +} + +.loginContainer { + border: 2px solid #e6e6e6; + width: 350px; + height: 380px; + margin: 0 auto; + .logoName { + display: flex; + justify-content: center; + align-items: center; + width: 100%; + h1 { + font-family: "Lobster", cursive; + width: 175px; + height: 52px; + text-align: center; + padding-top: 6px; + } + } + .loginInfo { + height: 213px; + display: flex; + flex-direction: column; + align-items: center; + padding-top: 10px; + .loginId { + width: 268px; + height: 38px; + margin-bottom: 5px; + padding-left: 10px; + border-radius: 3px; + border: 1px solid #efefef; + background: #fafafa; + color: #9c9c9c; + font-size: 14px; + &:focus { + outline-color: gray; + } + } + .loginPs { + width: 268px; + height: 38px; + margin-bottom: 5px; + padding-left: 10px; + border-radius: 3px; + border: 1px solid #efefef; + background: #fafafa; + color: #9c9c9c; + font-size: 14px; + &:focus { + outline-color: gray; + } + } + .loginBt { + width: 268px; + height: 30px; + margin-top: 5px; + border-radius: 3px; + background-color: #2795f6; + color: #ffffff; + border-style: none; + + &:focus { + outline: none; + } + + &:disabled { + background-color: #b2dffc; + } + } + } + .forgetPassword{ + display: flex; + justify-content: center; + margin-top: 20px; + a { + color: #043569; text-decoration: none; + } + } + + +}
Unknown
font-family๊ฐ™์€ ์†์„ฑ์€ width๋‚˜ margin์•„๋ž˜์— ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,190 @@ +import React from 'react'; +import FeedComponent from './FeedComponent/FeedComponent'; +import './Main.scss'; +import explore from '../../../images/jaeyoungLee/Main/explore.jpg'; +import heart from '../../../images/jaeyoungLee/Main/heart.jpg'; +import profile from '../../../images/jaeyoungLee/Main/profile.jpg'; +import heungminSon from '../../../images/jaeyoungLee/Main/์†ํฅ๋ฏผ.jpg'; +class MainJaeyoung extends React.Component { + constructor () { + super(); + this.state = { + commentFeed : [], + } + } + + componentDidMount() { + fetch('http://localhost:3000/data/feedData.json', { + method: 'GET' + }) + .then(res => res.json()) + .then(data => { + this.setState({ + commentFeed: data, + }); + }); + } + + + + render(){ + const { commentFeed } = this.state; + return( + <> + <nav> + <div className="navcontents"> + <div className="navleft"> + <div className="navleftcontents"> + <span className="logo"><i className="fab fa-instagram"></i></span> + <h1 className="logoname">westagram</h1> + </div> + </div> + <div className="navcenter"> + <div className="navcentercontents"> + <input className="search" type="text" placeholder="๊ฒ€์ƒ‰"/> + <span className="searchimg"><i className="fas fa-search"></i></span> + </div> + </div> + <div className="navright"> + <div className="navrightcontents"> + <img className="navexplore" src={explore} alt="ํƒํ—˜"/> + <img className="navheart" src={heart} alt="ํ•˜ํŠธ"/> + <img className="navprofile" src={profile} alt="ํ”„๋กœํ•„"/> + </div> + </div> + </div> + </nav> + <div className="main"> + <div className="feeds"> + <div className="story"> + <div className="storyimgbox"> + <div className="storycontents"> + <div className="storyprofile"> + <div className="storylastbox"> + <div className="storylast"> + <img className="storyimg1" src={heungminSon} alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">์†ํฅ๋ฏผ</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://instagram.famm6-1.fna.fbcdn.net/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=instagram.famm6-1.fna.fbcdn.net&_nc_ohc=mhDR9_0DpXIAX_KX0iq&ccb=7-4&oh=dcbb150241d6b1336dd54a4a20417b2a&oe=608B680F&_nc_sid=712cc3&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">2wo0</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/35999304_258159611658935_7092278198804676608_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=et4sZhNbgpAAX-qKBH9&ccb=7-4&oh=0b254f6fb3e4182513c3e20ed1a326d0&oe=608BC5C1&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">geg</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/150675895_144233967522798_7515094658452540248_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=xslO4Za0644AX9cwObZ&ccb=7-4&oh=0f5cb82fd80b51b47d74e160e228aa1c&oe=608D7B5E&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">h._.j</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/135771971_1881013862054625_6713353513169410837_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=SxJIiqRh4rcAX8RSLYk&ccb=7-4&oh=6dec08c37a69cb2c2a14f21bc36b5eef&oe=608C0912&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">idon</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/155951500_335125831259326_3729086392261698560_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=3nnTQdlVU98AX_4fVMv&ccb=7-4&oh=4a596fc0e33f2ece37634ae50d38cdde&oe=608C5901&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">dfewg</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/37828240_293117218102022_6759937585105076224_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=jzs6QvrGtHAAX_qFR80&ccb=7-4&oh=f4984cbce61bc445f02622a5468278b3&oe=608B09A1&_nc_sid=57d425" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">33gg</p> + </div> + </div> + </div> + </div> + </div> + </div> + {commentFeed.map((feed,id) =>{ + return ( + <FeedComponent key={feed.id} profileimg={feed.profileimg} userName={feed.userName} feedimg={feed.feedimg} feedcontent={feed.feedcontent} /> + ); + })} + + </div> + <div className="main-right"> + <div className="my_profile"> + <img className="my_profileimg" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/141997127_1523884024669097_6148385093252095280_n.jpg?tp=1&amp;_nc_ht=scontent-ssn1-1.cdninstagram.com&amp;_nc_ohc=ldPmBqcpW3kAX9esSCC&amp;ccb=7-4&amp;oh=6503a99b1b0096ad362012fef7e72aed&amp;oe=6085537E&amp;_nc_sid=7b02f1" alt="ํ”„๋กœํ•„"/> + <div className="my_info"> + <p className="mynickname">2wo0_0</p> + <p className="myname">์ด์žฌ์˜</p> + </div> + <button className="profile_transform" disabled>์ „ํ™˜</button> + </div> + <div className="recommendation"> + <div className="recommendation_header"> + <p className="recommendation_left">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p className="recommendation_right">๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="recommendation_info"> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/156650140_116853317083347_8770042214751161261_n.jpg?tp=1&amp;_nc_ht=scontent-ssn1-1.cdninstagram.com&amp;_nc_ohc=-evbCZABANYAX_WcDzP&amp;edm=AEF8tYYAAAAA&amp;ccb=7-4&amp;oh=5eb6e52a64b2ad8c98bff45dab473831&amp;oe=60880D5B&amp;_nc_sid=a9513d" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">ryu_d_g</p> + <p className="other_friend">1996yunsi๋‹˜ ์™ธ 13๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/159992110_105322351555148_1839915921172216453_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=xfLIvYCwwiIAX8AJI08&ccb=7-4&oh=7bdaf0e022e88f0dd5079c1995892031&oe=608D9B9E&_nc_sid=57d425" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">2s2_f</p> + <p className="other_friend">_sihyeon___๋‹˜ ์™ธ 49๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/160716680_272942871038831_8108440433038115559_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=VmeF5WmeLg4AX-jiUAL&ccb=7-4&oh=cf29d0253e8afb755a9d26ad13a6deda&oe=608D4A0C&_nc_sid=a9513d" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">rdfe_g</p> + <p className="other_friend">111_fkdn๋‹˜ ์™ธ 5๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/164274433_776408706413539_3215024154205561736_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=gCCfowKp9OMAX9_NBIP&ccb=7-4&oh=afbada353869184275b4d3be70f38605&oe=608BE909&_nc_sid=a9513d" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">cat_2d_g</p> + <p className="other_friend">cjdtkseh๋‹˜ ์™ธ 15๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src=" https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/164520013_834814623778500_398528442563386719_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=iLm66uEJKQ4AX88GkVl&ccb=7-4&oh=4fe271ebb6f627b328cb3d24bb811776&oe=608E6098&_nc_sid=86f79a" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">tksdkdnENr</p> + <p className="other_friend">cjfcjfgme1๋‹˜ ์™ธ 32๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + </div> + </div> + <aside> + <ul className="westagram_contents_box"> + {CONTENTS.map((el,id) => + <li key={id} className="westagram_contents">{el.content}</li> + )} + + </ul> + <p className="westagram_facebook">ยฉ 2021 INSTAGRAM FROM FACEBOOK</p> + </aside> + </div> + </div> + </> + ) + } +} +export default MainJaeyoung ; + +const CONTENTS = [ + {id: 1, content: "์†Œ๊ฐœ"}, + {id: 2, content: "๋„์›€๋ง"}, + {id: 3, content: "ํ™๋ณด ์„ผํ„ฐ"}, + {id: 4, content: "API"}, + {id: 5, content: "์ฑ„์šฉ์ •๋ณด"}, + {id: 6, content: "๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ"}, + {id: 7, content: "์•ฝ๊ด€"}, + {id: 8, content: "์œ„์น˜"}, + {id: 9, content: "์ธ๊ธฐ ๊ณ„์ •"}, + {id: 10, content: "ํ•ด์‹œํƒœ๊ทธ"}, + {id: 11, content: "์–ธ์–ด"}, +] \ No newline at end of file
JavaScript
๋ณ€์ˆ˜๋ช…์ด ๋ช…์‹์ ์ด์—ฌ์„œ ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.!
@@ -0,0 +1,190 @@ +import React from 'react'; +import FeedComponent from './FeedComponent/FeedComponent'; +import './Main.scss'; +import explore from '../../../images/jaeyoungLee/Main/explore.jpg'; +import heart from '../../../images/jaeyoungLee/Main/heart.jpg'; +import profile from '../../../images/jaeyoungLee/Main/profile.jpg'; +import heungminSon from '../../../images/jaeyoungLee/Main/์†ํฅ๋ฏผ.jpg'; +class MainJaeyoung extends React.Component { + constructor () { + super(); + this.state = { + commentFeed : [], + } + } + + componentDidMount() { + fetch('http://localhost:3000/data/feedData.json', { + method: 'GET' + }) + .then(res => res.json()) + .then(data => { + this.setState({ + commentFeed: data, + }); + }); + } + + + + render(){ + const { commentFeed } = this.state; + return( + <> + <nav> + <div className="navcontents"> + <div className="navleft"> + <div className="navleftcontents"> + <span className="logo"><i className="fab fa-instagram"></i></span> + <h1 className="logoname">westagram</h1> + </div> + </div> + <div className="navcenter"> + <div className="navcentercontents"> + <input className="search" type="text" placeholder="๊ฒ€์ƒ‰"/> + <span className="searchimg"><i className="fas fa-search"></i></span> + </div> + </div> + <div className="navright"> + <div className="navrightcontents"> + <img className="navexplore" src={explore} alt="ํƒํ—˜"/> + <img className="navheart" src={heart} alt="ํ•˜ํŠธ"/> + <img className="navprofile" src={profile} alt="ํ”„๋กœํ•„"/> + </div> + </div> + </div> + </nav> + <div className="main"> + <div className="feeds"> + <div className="story"> + <div className="storyimgbox"> + <div className="storycontents"> + <div className="storyprofile"> + <div className="storylastbox"> + <div className="storylast"> + <img className="storyimg1" src={heungminSon} alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">์†ํฅ๋ฏผ</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://instagram.famm6-1.fna.fbcdn.net/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=instagram.famm6-1.fna.fbcdn.net&_nc_ohc=mhDR9_0DpXIAX_KX0iq&ccb=7-4&oh=dcbb150241d6b1336dd54a4a20417b2a&oe=608B680F&_nc_sid=712cc3&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">2wo0</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/35999304_258159611658935_7092278198804676608_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=et4sZhNbgpAAX-qKBH9&ccb=7-4&oh=0b254f6fb3e4182513c3e20ed1a326d0&oe=608BC5C1&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">geg</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/150675895_144233967522798_7515094658452540248_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=xslO4Za0644AX9cwObZ&ccb=7-4&oh=0f5cb82fd80b51b47d74e160e228aa1c&oe=608D7B5E&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">h._.j</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/135771971_1881013862054625_6713353513169410837_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=SxJIiqRh4rcAX8RSLYk&ccb=7-4&oh=6dec08c37a69cb2c2a14f21bc36b5eef&oe=608C0912&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">idon</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/155951500_335125831259326_3729086392261698560_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=3nnTQdlVU98AX_4fVMv&ccb=7-4&oh=4a596fc0e33f2ece37634ae50d38cdde&oe=608C5901&_nc_sid=48a2a6" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">dfewg</p> + </div> + <div className="storylast"> + <img className="storyimg1" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/37828240_293117218102022_6759937585105076224_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=jzs6QvrGtHAAX_qFR80&ccb=7-4&oh=f4984cbce61bc445f02622a5468278b3&oe=608B09A1&_nc_sid=57d425" alt="ํ”„๋กœํ•„1"/> + <p className="storyp1">33gg</p> + </div> + </div> + </div> + </div> + </div> + </div> + {commentFeed.map((feed,id) =>{ + return ( + <FeedComponent key={feed.id} profileimg={feed.profileimg} userName={feed.userName} feedimg={feed.feedimg} feedcontent={feed.feedcontent} /> + ); + })} + + </div> + <div className="main-right"> + <div className="my_profile"> + <img className="my_profileimg" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/141997127_1523884024669097_6148385093252095280_n.jpg?tp=1&amp;_nc_ht=scontent-ssn1-1.cdninstagram.com&amp;_nc_ohc=ldPmBqcpW3kAX9esSCC&amp;ccb=7-4&amp;oh=6503a99b1b0096ad362012fef7e72aed&amp;oe=6085537E&amp;_nc_sid=7b02f1" alt="ํ”„๋กœํ•„"/> + <div className="my_info"> + <p className="mynickname">2wo0_0</p> + <p className="myname">์ด์žฌ์˜</p> + </div> + <button className="profile_transform" disabled>์ „ํ™˜</button> + </div> + <div className="recommendation"> + <div className="recommendation_header"> + <p className="recommendation_left">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p className="recommendation_right">๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="recommendation_info"> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/156650140_116853317083347_8770042214751161261_n.jpg?tp=1&amp;_nc_ht=scontent-ssn1-1.cdninstagram.com&amp;_nc_ohc=-evbCZABANYAX_WcDzP&amp;edm=AEF8tYYAAAAA&amp;ccb=7-4&amp;oh=5eb6e52a64b2ad8c98bff45dab473831&amp;oe=60880D5B&amp;_nc_sid=a9513d" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">ryu_d_g</p> + <p className="other_friend">1996yunsi๋‹˜ ์™ธ 13๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/159992110_105322351555148_1839915921172216453_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=xfLIvYCwwiIAX8AJI08&ccb=7-4&oh=7bdaf0e022e88f0dd5079c1995892031&oe=608D9B9E&_nc_sid=57d425" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">2s2_f</p> + <p className="other_friend">_sihyeon___๋‹˜ ์™ธ 49๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/160716680_272942871038831_8108440433038115559_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=VmeF5WmeLg4AX-jiUAL&ccb=7-4&oh=cf29d0253e8afb755a9d26ad13a6deda&oe=608D4A0C&_nc_sid=a9513d" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">rdfe_g</p> + <p className="other_friend">111_fkdn๋‹˜ ์™ธ 5๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src="https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/164274433_776408706413539_3215024154205561736_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=gCCfowKp9OMAX9_NBIP&ccb=7-4&oh=afbada353869184275b4d3be70f38605&oe=608BE909&_nc_sid=a9513d" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">cat_2d_g</p> + <p className="other_friend">cjdtkseh๋‹˜ ์™ธ 15๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + <div className="recommendation_box"> + <img className="friend_profile" src=" https://scontent-ssn1-1.cdninstagram.com/v/t51.2885-19/s150x150/164520013_834814623778500_398528442563386719_n.jpg?tp=1&_nc_ht=scontent-ssn1-1.cdninstagram.com&_nc_ohc=iLm66uEJKQ4AX88GkVl&ccb=7-4&oh=4fe271ebb6f627b328cb3d24bb811776&oe=608E6098&_nc_sid=86f79a" alt="๋‹ค๋ฅธํ”„๋กœํ•„"/> + <div className="friend_info"> + <p className="friend_nickname">tksdkdnENr</p> + <p className="other_friend">cjfcjfgme1๋‹˜ ์™ธ 32๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค</p> + </div> + <button className="follow" disabled>ํŒ”๋กœ์šฐ</button> + </div> + </div> + </div> + <aside> + <ul className="westagram_contents_box"> + {CONTENTS.map((el,id) => + <li key={id} className="westagram_contents">{el.content}</li> + )} + + </ul> + <p className="westagram_facebook">ยฉ 2021 INSTAGRAM FROM FACEBOOK</p> + </aside> + </div> + </div> + </> + ) + } +} +export default MainJaeyoung ; + +const CONTENTS = [ + {id: 1, content: "์†Œ๊ฐœ"}, + {id: 2, content: "๋„์›€๋ง"}, + {id: 3, content: "ํ™๋ณด ์„ผํ„ฐ"}, + {id: 4, content: "API"}, + {id: 5, content: "์ฑ„์šฉ์ •๋ณด"}, + {id: 6, content: "๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ"}, + {id: 7, content: "์•ฝ๊ด€"}, + {id: 8, content: "์œ„์น˜"}, + {id: 9, content: "์ธ๊ธฐ ๊ณ„์ •"}, + {id: 10, content: "ํ•ด์‹œํƒœ๊ทธ"}, + {id: 11, content: "์–ธ์–ด"}, +] \ No newline at end of file
JavaScript
```suggestion inputKeyPress= (e) => { ``` ๊ฐ™์€ ์ด๋ฆ„์€ ์–ด๋–จ๊นŒ์š”? ๋ฉ”์†Œ๋“œ๊ฐ€ ๋งŽ์•„์ง€๋ฉด ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,412 @@ +* { + margin: 0; + padding: 0; +} +nav { + position: fixed; + width: 100%; + top: 0px; + left: 0px; + right: 0px; + background-color: #fff; + border-bottom: 1px solid #dbdbdb; + .navcontents { + width: 935px; + height: 54px; + background-color: #fff; + margin: 0 auto; + display: flex; + justify-content: space-between; + .navleft { + width: 360px; + .navleftcontents { + display: flex; + justify-content: flex-start; + .logo { + font-size: 23px; + margin-top: 12px; + } + .logoname { + border-left: 1px solid #bbbbbb; + font-family: "Lobster", cursive; + margin-left: 15px; + margin-top: 7px; + padding-left: 15px; + } + } + } + .navcenter { + width: 215px; + .navcentercontents { + position: relative; + .search { + width: 213px; + height: 26px; + background-color: #fafafa; + border: 1px solid #dbdbdb; + border-radius: 3px; + text-align: center; + position: absolute; + top: 14px; + } + + .searchimg { + font-size: 10px; + color: #bbbbbb; + position: absolute; + top: 24px; + left: 83px; + } + } + } + .navright { + width: 360px; + .navrightcontents { + display: flex; + justify-content: flex-end; + margin-top: 14px; + .navexplore { + width: 25px; + height: 25px; + margin-right: 20px; + } + + .navheart { + width: 25px; + height: 25px; + margin-right: 20px; + } + + .navprofile { + width: 25px; + height: 25px; + } + } + } + } +} + + + +.main { + border: 1px solid #fafafa; + background-color: #fafafa; + .feeds { + width: 935px; + margin: 0 auto; + margin-top: 84px; + .story { + width: 612px; + height: 116px; + background-color: #fff; + border: 1px solid #dbdbdb; + border-radius: 3px; + display: flex; + align-items: center; + .storyimgbox { + width: 612px; + height: 84px; + .storycontents { + display: flex; + justify-content: space-around; + .storylastbox { + display: flex; + justify-content: center; + .storylast { + width: 80px; + height: 122px; + .storyimg1 { + width: 56px; + height: 56px; + border: 1px solid #ececec; + border-radius: 50%; + padding: 3px; + object-fit: cover; + } + + .storyp1 { + color: #8e8e8e; + font-size: 12px; + margin-left: 18px; + } + } + } + } + } + } + } +} + + +article { + width: 612px; + height: auto; + background-color: #fff; + margin-top: 24px; + border: 1px solid #dbdbdb; + border-radius: 3px; + .articleheader { + width: 612px; + height: 59px; + border: 1px solid #dbdbdb; + .articleheadercontents { + width: 582px; + height: 27px; + margin: 15px auto 15px auto; + display: flex; + justify-content: space-between; + .headerimg { + width: 32px; + height: 32px; + border-radius: 50%; + object-fit: cover; + } + + .headername { + width: 536px; + height: 26px; + .headerlast { + height: 26px; + display: flex; + justify-content: space-between; + align-items: center; + .hname { + font-size: 14px; + color: #262626; + font-weight: bold; + } + } + } + } + } + .articleimg { + width: 612px; + height: 612px; + object-fit: cover; + } + + .article_button { + width: 582px; + height: 49px; + margin: 0 auto; + display: flex; + justify-content: space-between; + .article_button_left{ + .left_button { + margin: 8px; + } + } + .article_button_right { + .right_button { + margin: 8px; + } + } + } + + .number_of_likes { + width: 582px; + height: 18px; + margin: 0 auto; + padding-left: 5px; + .likes_friend { + font-size: 14px; + .like_font { + color: #262626; + font-weight: bold; + } + } + } + .friend_comment { + width: 582px; + height: auto; + margin: 0 auto; + margin-top: 8px; + padding-left: 5px; + .explanation { + font-size: 14px; + .like_font { + color: #262626; + font-weight: bold; + } + } + .commentBox { + list-style: none; + } + } + .time { + width: 598px; + padding-left: 18px; + .a_few_hours_ago { + font-size: 10px; + color: #8e8e8e; + } + } + .leave_comment_box { + border-top: 1px solid #dbdbdb; + margin-top: 10px; + .leave_comment { + width: 582px; + height: 55px; + margin: 0 auto; + display: flex; + justify-content: center; + align-items: center; + .smilebox { + padding: 8px 15px 8px 0px; + } + .inputcomment { + width: 510px; + border: 0; + } + .inputcomment:focus { + outline: none; + } + .posting_button { + background-color: #fff; + color: #0095f6; + border: 0; + } + .posting_button:focus{ + outline: none; + } + } + } +} + + + + +.main-right { + width: 293px; + height: 612px; + position: fixed; + top: 85px; + left: 885px; + .my_profile { + width: 293px; + height: 56px; + margin: 18px 0px 10px 0px; + display: flex; + justify-content: space-between; + align-items: center; + .my_profileimg { + width: 56px; + height: 56px; + border-radius: 50%; + } + + .my_info { + width: 202px; + height: 30px; + .mynickname { + width: 190px; + font-size: 14px; + font-weight: bold; + margin-left: 12px; + color: #262626; + } + + .myname { + width: 190px; + font-size: 14px; + margin-left: 12px; + color: #8e8e8e; + } + } + .profile_transform { + font-weight: 600; + font-size: 12px; + border: 0; + color: #0095f6; + } + } + .recommendation { + width: 291px; + height: 325px; + .recommendation_header { + width: 293px; + height: 11px; + margin-top: 18px; + display: flex; + justify-content: space-between; + align-items: center; + .recommendation_left { + width: 248px; + font-size: 14px; + color: #8e8e8e; + font-weight: 600; + } + + .recommendation_right { + width: 45px; + font-size: 12px; + color: #262626; + font-weight: 600; + } + } + .recommendation_info { + width: 289px; + height: 240px; + margin-top: 20px; + .recommendation_box { + width: 289px; + margin-bottom: 16px; + display: flex; + justify-content: space-between; + flex-wrap: wrap; + align-content: flex-start; + .friend_profile { + width: 32px; + height: 32px; + border-radius: 50%; + } + + .friend_info { + width: 206px; + height: 28px; + .friend_nickname { + width: 205px; + color: #262626; + font-weight: bold; + font-size: 14px; + } + + .other_friend { + width: 205px; + color: #8e8e8e; + font-weight: 400; + font-size: 12px; + } + } + .follow { + height: 15px; + font-weight: 600; + font-size: 12px; + margin-top: 10px; + border: 0; + color: #0095f6; + } + } + } + } + aside { + width: 293px; + height: 68px; + .westagram_contents_box { + width: 293px; + .westagram_contents { + color: #c7c7c7; + font-size: 11px; + font-weight: 400; + display: inline; + } + } + .westagram_facebook { + width: 205px; + color: #c7c7c7; + font-size: 11px; + font-weight: 400; + margin-top: 20px; + } + } +} \ No newline at end of file
Unknown
```suggestion .nav_center { ``` ๊ฐ™์ด ๋„์–ด์“ฐ๊ธฐ๋ฅผ '-'๋‚˜ '_'๋กœ ํ‘œ์‹œํ•ด์ฃผ๋ฉด ์†์„ฑ์ฝ๊ธฐ๊ฐ€ ๋” ์ˆ˜์›”ํ•ด ์งˆ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,412 @@ +* { + margin: 0; + padding: 0; +} +nav { + position: fixed; + width: 100%; + top: 0px; + left: 0px; + right: 0px; + background-color: #fff; + border-bottom: 1px solid #dbdbdb; + .navcontents { + width: 935px; + height: 54px; + background-color: #fff; + margin: 0 auto; + display: flex; + justify-content: space-between; + .navleft { + width: 360px; + .navleftcontents { + display: flex; + justify-content: flex-start; + .logo { + font-size: 23px; + margin-top: 12px; + } + .logoname { + border-left: 1px solid #bbbbbb; + font-family: "Lobster", cursive; + margin-left: 15px; + margin-top: 7px; + padding-left: 15px; + } + } + } + .navcenter { + width: 215px; + .navcentercontents { + position: relative; + .search { + width: 213px; + height: 26px; + background-color: #fafafa; + border: 1px solid #dbdbdb; + border-radius: 3px; + text-align: center; + position: absolute; + top: 14px; + } + + .searchimg { + font-size: 10px; + color: #bbbbbb; + position: absolute; + top: 24px; + left: 83px; + } + } + } + .navright { + width: 360px; + .navrightcontents { + display: flex; + justify-content: flex-end; + margin-top: 14px; + .navexplore { + width: 25px; + height: 25px; + margin-right: 20px; + } + + .navheart { + width: 25px; + height: 25px; + margin-right: 20px; + } + + .navprofile { + width: 25px; + height: 25px; + } + } + } + } +} + + + +.main { + border: 1px solid #fafafa; + background-color: #fafafa; + .feeds { + width: 935px; + margin: 0 auto; + margin-top: 84px; + .story { + width: 612px; + height: 116px; + background-color: #fff; + border: 1px solid #dbdbdb; + border-radius: 3px; + display: flex; + align-items: center; + .storyimgbox { + width: 612px; + height: 84px; + .storycontents { + display: flex; + justify-content: space-around; + .storylastbox { + display: flex; + justify-content: center; + .storylast { + width: 80px; + height: 122px; + .storyimg1 { + width: 56px; + height: 56px; + border: 1px solid #ececec; + border-radius: 50%; + padding: 3px; + object-fit: cover; + } + + .storyp1 { + color: #8e8e8e; + font-size: 12px; + margin-left: 18px; + } + } + } + } + } + } + } +} + + +article { + width: 612px; + height: auto; + background-color: #fff; + margin-top: 24px; + border: 1px solid #dbdbdb; + border-radius: 3px; + .articleheader { + width: 612px; + height: 59px; + border: 1px solid #dbdbdb; + .articleheadercontents { + width: 582px; + height: 27px; + margin: 15px auto 15px auto; + display: flex; + justify-content: space-between; + .headerimg { + width: 32px; + height: 32px; + border-radius: 50%; + object-fit: cover; + } + + .headername { + width: 536px; + height: 26px; + .headerlast { + height: 26px; + display: flex; + justify-content: space-between; + align-items: center; + .hname { + font-size: 14px; + color: #262626; + font-weight: bold; + } + } + } + } + } + .articleimg { + width: 612px; + height: 612px; + object-fit: cover; + } + + .article_button { + width: 582px; + height: 49px; + margin: 0 auto; + display: flex; + justify-content: space-between; + .article_button_left{ + .left_button { + margin: 8px; + } + } + .article_button_right { + .right_button { + margin: 8px; + } + } + } + + .number_of_likes { + width: 582px; + height: 18px; + margin: 0 auto; + padding-left: 5px; + .likes_friend { + font-size: 14px; + .like_font { + color: #262626; + font-weight: bold; + } + } + } + .friend_comment { + width: 582px; + height: auto; + margin: 0 auto; + margin-top: 8px; + padding-left: 5px; + .explanation { + font-size: 14px; + .like_font { + color: #262626; + font-weight: bold; + } + } + .commentBox { + list-style: none; + } + } + .time { + width: 598px; + padding-left: 18px; + .a_few_hours_ago { + font-size: 10px; + color: #8e8e8e; + } + } + .leave_comment_box { + border-top: 1px solid #dbdbdb; + margin-top: 10px; + .leave_comment { + width: 582px; + height: 55px; + margin: 0 auto; + display: flex; + justify-content: center; + align-items: center; + .smilebox { + padding: 8px 15px 8px 0px; + } + .inputcomment { + width: 510px; + border: 0; + } + .inputcomment:focus { + outline: none; + } + .posting_button { + background-color: #fff; + color: #0095f6; + border: 0; + } + .posting_button:focus{ + outline: none; + } + } + } +} + + + + +.main-right { + width: 293px; + height: 612px; + position: fixed; + top: 85px; + left: 885px; + .my_profile { + width: 293px; + height: 56px; + margin: 18px 0px 10px 0px; + display: flex; + justify-content: space-between; + align-items: center; + .my_profileimg { + width: 56px; + height: 56px; + border-radius: 50%; + } + + .my_info { + width: 202px; + height: 30px; + .mynickname { + width: 190px; + font-size: 14px; + font-weight: bold; + margin-left: 12px; + color: #262626; + } + + .myname { + width: 190px; + font-size: 14px; + margin-left: 12px; + color: #8e8e8e; + } + } + .profile_transform { + font-weight: 600; + font-size: 12px; + border: 0; + color: #0095f6; + } + } + .recommendation { + width: 291px; + height: 325px; + .recommendation_header { + width: 293px; + height: 11px; + margin-top: 18px; + display: flex; + justify-content: space-between; + align-items: center; + .recommendation_left { + width: 248px; + font-size: 14px; + color: #8e8e8e; + font-weight: 600; + } + + .recommendation_right { + width: 45px; + font-size: 12px; + color: #262626; + font-weight: 600; + } + } + .recommendation_info { + width: 289px; + height: 240px; + margin-top: 20px; + .recommendation_box { + width: 289px; + margin-bottom: 16px; + display: flex; + justify-content: space-between; + flex-wrap: wrap; + align-content: flex-start; + .friend_profile { + width: 32px; + height: 32px; + border-radius: 50%; + } + + .friend_info { + width: 206px; + height: 28px; + .friend_nickname { + width: 205px; + color: #262626; + font-weight: bold; + font-size: 14px; + } + + .other_friend { + width: 205px; + color: #8e8e8e; + font-weight: 400; + font-size: 12px; + } + } + .follow { + height: 15px; + font-weight: 600; + font-size: 12px; + margin-top: 10px; + border: 0; + color: #0095f6; + } + } + } + } + aside { + width: 293px; + height: 68px; + .westagram_contents_box { + width: 293px; + .westagram_contents { + color: #c7c7c7; + font-size: 11px; + font-weight: 400; + display: inline; + } + } + .westagram_facebook { + width: 205px; + color: #c7c7c7; + font-size: 11px; + font-weight: 400; + margin-top: 20px; + } + } +} \ No newline at end of file
Unknown
scss์˜ nesting์„ ๊ต‰์žฅํžˆ ์ž˜ ์ ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์•„์š” ์ด๋Ÿฌ๋ฉด ๋‚˜์ค‘์— component๋‹จ์œ„๋กœ ๋‚˜๋ˆŒ ๋•Œ ๋” ํŽธํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!