code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,30 @@ +package christmas.Util; + +public class CommaFormatter { + public static String formatWithComma(int number) { + String numberStr = Integer.toString(number); + int length = numberStr.length(); + + if (length <= 3) { + return numberStr; + } + String result = insertComma(numberStr, length); + + return result; + } + + private static String insertComma(String number, int length) { + int count = 0; + StringBuilder result = new StringBuilder(); + for (int i = length - 1; i >= 0; i--) { + result.insert(0, number.charAt(i)); + count++; + + if (count == 3 && i != 0) { + result.insert(0, ','); + count = 0; + } + } + return result.toString(); + } +}
Java
์ฝค๋งˆ๋ฅผ ์œ„ํ•ด ์ „์šฉ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์…จ๊ตฐ์š”! ์ง์ ‘ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ๋„ ์ข‹๋„ค์š”! ๊ทธ๋Ÿฐ๋ฐ ๋ˆ ๋‹จ์œ„๋ฅผ ์œ„ํ•œ ์ฝค๋งˆ๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ต๋‹ˆ๋‹ค! ใ…Žใ…Ž DecimalFormat ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ์ œ๊ฐ€ [์ฐธ๊ณ ํ–ˆ๋˜ ๊ธ€](https://jamesdreaming.tistory.com/203)์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,67 @@ +package christmas.Domain; + +import christmas.Util.OrderMenuValidator; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class EventPlanner { + private final List<Menu> orderMenu; + + public EventPlanner(String menu) { + validateOrderMenuForm(menu); + List<Menu> orderMenu = createOrderMenuRepository(menu); + this.orderMenu = orderMenu; + } + + public int calculatePreDiscountTotalOrderPrice() { + int totalOrderPrice = 0; + for (Menu menu : orderMenu) { + totalOrderPrice += menu.getMenuTotalPrice(); + } + return totalOrderPrice; + } + + public Map<String, Integer> calculateCategoryCount() { + Map<String, Integer> categoryCount = new HashMap<>(); + int count = 0; + for (Menu menu : orderMenu) { + String menuCategory = menu.getMenuCategory(); + if (categoryCount.containsKey(menuCategory)) { + count = categoryCount.get(menuCategory) + menu.getMenuQuantity(); + categoryCount.put(menuCategory, count); + } + if (!categoryCount.containsKey(menuCategory)) { + categoryCount.put(menuCategory, menu.getMenuQuantity()); + } + } + return categoryCount; + } + + private List<Menu> createOrderMenuRepository(String orderMenu) { + List<Menu> menuList = new ArrayList<>(); + String pattern = "([๊ฐ€-ํžฃ]+)-([1-9]\\d*|0*[1-9]\\d+)(?:,|$)"; + + // ์ •๊ทœํ‘œํ˜„์‹์— ๋งž๋Š” ํŒจํ„ด์„ ์ƒ์„ฑ + Pattern regexPattern = Pattern.compile(pattern); + Matcher matcher = regexPattern.matcher(orderMenu); + + // ๋งค์นญ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ + while (matcher.find()) { + String menuName = matcher.group(1); + String menuQuantity = matcher.group(2); + menuList.add(new Menu(menuName, menuQuantity)); + } + return menuList; + } + + private void validateOrderMenuForm(String orderedMenu) { + OrderMenuValidator.checkValidOrderForm(orderedMenu); + OrderMenuValidator.checkDuplicateMenu(orderedMenu); + OrderMenuValidator.checkMaxOrderQuantity(orderedMenu); + OrderMenuValidator.checkMenuContainsOnlyDrink(orderedMenu); + } +}
Java
```suggestion public Map<String, Integer> calculateCategoryCount() { Map<String, Integer> categoryCount = new HashMap<>(); int count = 0; for (Menu menu : orderMenu) { String menuCategory = menu.getMenuCategory(); categoryCount.put(menuCategory, categoryCount.getOrDefault(menuCategory, 0) + 1) } return categoryCount; } ``` `getOrDefault` ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค! ใ…Žใ…Ž ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ  ์—†์œผ๋ฉด default ๋กœ ์ง€์ •ํ•ด์ค€ ๊ฐ’(์—ฌ๊ธฐ์„  0)์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,102 @@ +package christmas.Domain; + +import christmas.Event.EventBenefit; +import christmas.Event.EventOption; +import christmas.Event.SpecialDiscountDay; +import christmas.Util.OrderMenuValidator; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +public class EventBenefitSettler { + private final int visitDate; + + public EventBenefitSettler(String visitDate) { + OrderMenuValidator.validateDate(visitDate); + this.visitDate = Integer.parseInt(visitDate); + } + + public Map<String, Integer> calculateReceivedBenefits(Map<String, Integer> categoryCount, + int preDiscountTotalOrderPrice) { + Map<String, Integer> receivedBenefits = new HashMap<>(); + if (preDiscountTotalOrderPrice >= EventOption.MINIMUM_ORDER_PRICE_TO_GET_DISCOUNT) { + caculateChristmasDDayDiscount(receivedBenefits); + caculateWeekdayDiscount(receivedBenefits, categoryCount); + caculateWeekendDiscount(receivedBenefits, categoryCount); + caculateSpecialDiscount(receivedBenefits); + checkGiftMenu(receivedBenefits, preDiscountTotalOrderPrice); + } + return receivedBenefits; + } + + public int caculateBenefitsTotalAmount(Map<String, Integer> receivedBenefits) { + int benefitsTotalAmount = 0; + for (Map.Entry<String, Integer> benefit : receivedBenefits.entrySet()) { + benefitsTotalAmount += benefit.getValue(); + } + return benefitsTotalAmount; + } + + public int calculateDiscountedTotalAmount(Map<String, Integer> receivedBenefits, int preDiscountTotalOrderPrice) { + int discountedTotalAmount = preDiscountTotalOrderPrice; + for (Map.Entry<String, Integer> benefit : receivedBenefits.entrySet()) { + if (benefit.getKey() != EventBenefit.GIFT_MENU.getEventType()) { + discountedTotalAmount -= benefit.getValue(); + } + } + return discountedTotalAmount; + } + + private void caculateSpecialDiscount(Map<String, Integer> receivedBenefits) { + if (SpecialDiscountDay.isSpecialDay(this.visitDate)) { + receivedBenefits.put(EventBenefit.SPECIAL_DISCOUNT.getEventType(), + EventBenefit.SPECIAL_DISCOUNT.getBenefitAmount()); + } + } + + private void checkGiftMenu(Map<String, Integer> receivedBenefits, int preDiscountTotalOrderPrice) { + if (preDiscountTotalOrderPrice >= EventOption.MINIMUM_ORDER_PRICE_TO_GET_GIFT_MENU) { + receivedBenefits.put(EventBenefit.GIFT_MENU.getEventType(), EventBenefit.GIFT_MENU.getBenefitAmount()); + } + } + + private void caculateChristmasDDayDiscount(Map<String, Integer> receivedBenefits) { + int discount = 0; + if (this.visitDate <= EventOption.CHRISTMAS_D_DAY_EVENT_END_DATE) { + discount = EventOption.CHRISTMAS_D_DAY_START_DISCOUNT_AMOUNT; + discount += EventBenefit.CHRISTMAS_D_DAY_DISCOUNT.getBenefitTotalAmount(this.visitDate - 1); + receivedBenefits.put(EventBenefit.CHRISTMAS_D_DAY_DISCOUNT.getEventType(), discount); + } + } + + private void caculateWeekdayDiscount(Map<String, Integer> receivedBenefits, Map<String, Integer> categoryCount) { + int discount = 0; + if (!isWeekend()) { + try { + int countDessert = categoryCount.get(EventBenefit.WEEKDAY_DISCOUNT.getEventTarget()); // ์žˆ๋Š”์ง€๋ฅผ ์ฒดํฌ + discount += EventBenefit.WEEKDAY_DISCOUNT.getBenefitTotalAmount(countDessert); + receivedBenefits.put(EventBenefit.WEEKDAY_DISCOUNT.getEventType(), discount); + } catch (NullPointerException e) { + } + } + } + + private void caculateWeekendDiscount(Map<String, Integer> receivedBenefits, Map<String, Integer> categoryCount) { + int discount = 0; + if (isWeekend()) { + try { + int countMain = categoryCount.get(EventBenefit.WEEKEND_DISCOUNT.getEventTarget()); // ์žˆ๋Š”์ง€๋ฅผ ์ฒดํฌ + discount += EventBenefit.WEEKEND_DISCOUNT.getBenefitTotalAmount(countMain); + receivedBenefits.put(EventBenefit.WEEKDAY_DISCOUNT.getEventType(), discount); + } catch (NullPointerException e) { + } + } + } + + private boolean isWeekend() { + LocalDate date = LocalDate.of(EventOption.EVENT_YEAR, EventOption.EVENT_MONTH, this.visitDate); + DayOfWeek dayOfWeek = date.getDayOfWeek(); + return dayOfWeek == DayOfWeek.FRIDAY || dayOfWeek == DayOfWeek.SATURDAY; + } +}
Java
`LocalDate` ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์š”์ผ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ตฐ์š”! ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค! ๐Ÿ˜
@@ -0,0 +1,7 @@ +package nextstep.security.role; + +import java.util.Collection; + +public interface RoleHierarchy { + Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities); +}
Java
> ๊ทธ๋Ÿฐ๋ฐ Spring Security ์ฝ”๋“œ ์ „๋ฐ˜์—์„œ๋Š” Authority๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, > RoleHierarchy์—์„œ ํŠน๋ณ„ํžˆ Role์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•˜๋„ค์š”. > ์ด๋ ‡๊ฒŒ Role์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์—ญํ• ๋กœ ๋น„๊ตํ•œ๋‹ค๊ณ  ๋ช…์‹œ์ ์œผ๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์ผ๊นŒ์š”?? ํ˜น์‹œ ์–ด๋”” ์ฝ”๋“œ์—์„œ `RoleHierarchy.getReachableRoles`๋กœ ๋˜์–ด์žˆ์„๊นŒ์š”? ์‹ค์ œ ์Šคํ”„๋ง ์‹œํ๋ฆฌํ‹ฐ ์ฝ”๋“œ์—์„œ๋„ ๋ง์”€ํ•ด์ฃผ์‹ ๋Œ€๋กœ authority๋ผ๋Š” ํ‘œํ˜„์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์š”. ```java Collection<? extends GrantedAuthority> getReachableGrantedAuthorities( Collection<? extends GrantedAuthority> authorities); ``` https://github.com/spring-projects/spring-security/blob/main/core/src/main/java/org/springframework/security/access/hierarchicalroles/RoleHierarchy.java
@@ -17,6 +17,7 @@ import nextstep.security.matcher.AnyRequestMatcher; import nextstep.security.matcher.MvcRequestMatcher; import nextstep.security.matcher.RequestMatcherEntry; +import nextstep.security.role.*; import nextstep.security.userdetails.UserDetails; import nextstep.security.userdetails.UserDetailsService; import org.springframework.context.annotation.Bean; @@ -27,6 +28,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; @EnableAspectJAutoProxy @Configuration @@ -50,20 +52,17 @@ public FilterChainProxy filterChainProxy(List<SecurityFilterChain> securityFilte @Bean public SecuredMethodInterceptor securedMethodInterceptor() { - AuthorityAuthorizationManager<Set<String>> authorityAuthorizationManager = new AuthorityAuthorizationManager<>(); + AuthorityAuthorizationManager<Set<GrantedAuthority>> authorityAuthorizationManager = new AuthorityAuthorizationManager<>(roleHierarchy()); return new SecuredMethodInterceptor(new SecuredAuthorizationManager(authorityAuthorizationManager)); } -// @Bean -// public SecuredAspect securedAspect() { -// return new SecuredAspect(); -// } @Bean public RequestMatcherDelegatingAuthorizationManager requestMatcherDelegatingAuthorizationManager() { List<RequestMatcherEntry<AuthorizationManager<HttpServletRequest>>> mappings = new ArrayList<>(); mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/members/me"), new AuthenticatedAuthorizationManager())); - mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/members"), new AuthorityAuthorizationManager<>())); - mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/search", "/login"), new PermitAllAuthorizationManager())); + mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/members"), new AuthorityAuthorizationManager<>(new NullRoleHierarchy()))); + mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/search"), new PermitAllAuthorizationManager())); + mappings.add(new RequestMatcherEntry<>(new MvcRequestMatcher(HttpMethod.GET, "/login"), new PermitAllAuthorizationManager())); mappings.add(new RequestMatcherEntry<>(new AnyRequestMatcher(), new DenyAllAuthorizationManager())); return new RequestMatcherDelegatingAuthorizationManager(mappings); } @@ -98,10 +97,20 @@ public String getPassword() { } @Override - public Set<String> getAuthorities() { - return member.getRoles(); + public Set<GrantedAuthority> getAuthorities() { + return member.getRoles() + .stream() + .map(SimpleGrantedAuthority::new) + .collect(Collectors.toSet()); } }; }; } + + @Bean + public RoleHierarchy roleHierarchy() { + return RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER") + .build(); + } }
Java
์ž˜ ์ถ”๊ฐ€ํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,10 @@ +package nextstep.security.role; + +import java.util.Collection; + +public class NullRoleHierarchy implements RoleHierarchy { + @Override + public Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities) { + return authorities; + } +}
Java
`RoleHierarchy`์˜ ์˜๋ฌธ๋ช… ๊ทธ๋Œ€๋กœ ์ด ์ธํ„ฐํŽ˜์ด์Šค์˜ ์—ญํ• ์€ "์—ญํ•  ๊ณ„์ธต"์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์ด์—์š”. ๊ทธ๋ž˜์„œ ๊ตฌํ˜„์ฒด๋“ค๋„ ์—ญํ•  ๊ณ„์ธต์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌ์„ฑ์ด ๋˜์–ด์•ผํ•˜๋Š”๋ฐ์š”. `NullRoleHierarchy`์ด๋ผ๋Š” ๊ฒƒ์€ null์ด๋ผ๋Š” ๋ง์ด ํ‘œํ˜„๋˜์–ด์žˆ๊ธฐ๋Š”ํ•˜์ง€๋งŒ ์ง์—ญํ•˜๋ฉด "์—ญํ•  ๊ณ„์ธต์ด ์—†๋‹ค"์ž…๋‹ˆ๋‹ค. ๊ณ„์ธต์ด ์—†๋‹ค๋Š” ๊ฒƒ์ด์ง€ authority๊ฐ€ ์—†๋‹ค๋Š” ๋œป์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— empty๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์ธ์ž…๋œ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๊ทธ ๋ชฉ์ ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์–ด์š”.
@@ -0,0 +1,96 @@ +package nextstep.security.role; + +import org.springframework.util.StringUtils; + +import java.util.*; + +public class RoleHierarchyImpl implements RoleHierarchy { + + private static final List<GrantedAuthority> EMPTY_AUTHORITIES = Collections.emptyList(); + + private final Map<String, Set<GrantedAuthority>> roleHierarchy; + + + public RoleHierarchyImpl(Map<String, Set<GrantedAuthority>> roleHierarchy) { + this.roleHierarchy = new HashMap<>(roleHierarchy); + } + + public static Builder builder() { + return new RoleHierarchyImpl.Builder(); + } + + @Override + public Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities) { + if (authorities == null || authorities.isEmpty()) { + return EMPTY_AUTHORITIES; + } + + Set<GrantedAuthority> reachableRoles = new HashSet<>(); + for (GrantedAuthority authority : authorities) { + reachableRoles.add(authority); + + Collection<GrantedAuthority> impliedAuthorities = roleHierarchy.get(authority.getAuthority()); + if (impliedAuthorities != null) { + reachableRoles.addAll(impliedAuthorities); + } + } + + return reachableRoles; + } + + public static final class Builder { + + private final Map<String, Set<GrantedAuthority>> hierarchy; + + private Builder() { + this.hierarchy = new HashMap<>(); + } + + public Builder.ImpliedRoles role(String role) { + if (!StringUtils.hasText(role)) { + throw new IllegalArgumentException("role must not be null or empty"); + } + + return new Builder.ImpliedRoles(role); + } + + public RoleHierarchyImpl build() { + return new RoleHierarchyImpl(this.hierarchy); + } + + private RoleHierarchyImpl.Builder addHierarchy(String role, String... impliedRoles) { + Set<GrantedAuthority> grantedAuthorities = new HashSet<>(); + + for (String impliedRole : impliedRoles) { + grantedAuthorities.add(new SimpleGrantedAuthority(impliedRole)); + } + + this.hierarchy.put(role, grantedAuthorities); + return this; + } + + public final class ImpliedRoles { + + private final String role; + + private ImpliedRoles(String role) { + this.role = role; + } + + public Builder addImplies(String... impliedRoles) { + if (impliedRoles == null) { + throw new IllegalArgumentException("at least one implied role must be provided"); + } + + for (String impliedRole : impliedRoles) { + if (!StringUtils.hasText(impliedRole)) { + throw new IllegalArgumentException("at least one implied role must be provided"); + } + } + + return Builder.this.addHierarchy(this.role, impliedRoles); + } + } + + } +}
Java
```suggestion private static final List<GrantedAuthority> EMPTY_AUTHORITIES = Collections.emptyList(); private final Map<String, Set<GrantedAuthority>> roleHierarchy; ``` ์ƒ์ˆ˜๋Š” ์ž๋ฐ” ์ปจ๋ฒค์…˜์ƒ ๊ฐ€์žฅ ์œ„์— ์žˆ์–ด์•ผํ•˜๊ณ , `roleHierarchy`๋Š” ์ ‘๊ทผ์ œ์–ด๊ฐ€ ํ•„์š”ํ•ด๋ณด์ด๋„ค์š” :)
@@ -0,0 +1,96 @@ +package nextstep.security.role; + +import org.springframework.util.StringUtils; + +import java.util.*; + +public class RoleHierarchyImpl implements RoleHierarchy { + + private static final List<GrantedAuthority> EMPTY_AUTHORITIES = Collections.emptyList(); + + private final Map<String, Set<GrantedAuthority>> roleHierarchy; + + + public RoleHierarchyImpl(Map<String, Set<GrantedAuthority>> roleHierarchy) { + this.roleHierarchy = new HashMap<>(roleHierarchy); + } + + public static Builder builder() { + return new RoleHierarchyImpl.Builder(); + } + + @Override + public Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities) { + if (authorities == null || authorities.isEmpty()) { + return EMPTY_AUTHORITIES; + } + + Set<GrantedAuthority> reachableRoles = new HashSet<>(); + for (GrantedAuthority authority : authorities) { + reachableRoles.add(authority); + + Collection<GrantedAuthority> impliedAuthorities = roleHierarchy.get(authority.getAuthority()); + if (impliedAuthorities != null) { + reachableRoles.addAll(impliedAuthorities); + } + } + + return reachableRoles; + } + + public static final class Builder { + + private final Map<String, Set<GrantedAuthority>> hierarchy; + + private Builder() { + this.hierarchy = new HashMap<>(); + } + + public Builder.ImpliedRoles role(String role) { + if (!StringUtils.hasText(role)) { + throw new IllegalArgumentException("role must not be null or empty"); + } + + return new Builder.ImpliedRoles(role); + } + + public RoleHierarchyImpl build() { + return new RoleHierarchyImpl(this.hierarchy); + } + + private RoleHierarchyImpl.Builder addHierarchy(String role, String... impliedRoles) { + Set<GrantedAuthority> grantedAuthorities = new HashSet<>(); + + for (String impliedRole : impliedRoles) { + grantedAuthorities.add(new SimpleGrantedAuthority(impliedRole)); + } + + this.hierarchy.put(role, grantedAuthorities); + return this; + } + + public final class ImpliedRoles { + + private final String role; + + private ImpliedRoles(String role) { + this.role = role; + } + + public Builder addImplies(String... impliedRoles) { + if (impliedRoles == null) { + throw new IllegalArgumentException("at least one implied role must be provided"); + } + + for (String impliedRole : impliedRoles) { + if (!StringUtils.hasText(impliedRole)) { + throw new IllegalArgumentException("at least one implied role must be provided"); + } + } + + return Builder.this.addHierarchy(this.role, impliedRoles); + } + } + + } +}
Java
`RoleHierarchyImpl`์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๋„ ์ถ”๊ฐ€ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ๊ธฐ๋Šฅ์ด ๋ณต์žกํ•œ ๊ฐ์ฒด์ด๋‹ˆ ์ด๋ฅผ ๋ฌธ์„œํ™”ํ•œ๋‹ค๋Š” ๋А๋‚Œ์œผ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์งœ๋ณด์…”๋„ ์ข‹์€ ๊ฒฝํ—˜์ด ๋  ๊ฒƒ ๊ฐ™์•„์š” :)
@@ -0,0 +1,7 @@ +package nextstep.security.role; + +import java.util.Collection; + +public interface RoleHierarchy { + Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities); +}
Java
์ œ๊ฐ€ ์ž˜๋ชป ์งˆ๋ฌธ์„ ๋“œ๋ ธ๋„ค์š”. ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. getReachableRoles๋Š” ๋ฏธ์…˜ ์ฝ”๋“œ์— ๋ถ€๋ถ„์ด์˜€๋„ค์š”. ๐Ÿ˜… ๊ทธ๋ž˜๋„ Role ์ด๋ฆ„์€ ์‹œํ๋ฆฌํ‹ฐ์˜ RoleHierarchy์˜ ๊ตฌํ˜„์ฒด ์ฝ”๋“œ์—๋„ ์ข…์ข… ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š” ์˜ˆ๋ฅผ ๋“ค์–ด์„œ getReachableGrantedAuthorities ์˜ ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ ์ค‘ ``` java Set<GrantedAuthority> lowerRoles = this.rolesReachableInOneOrMoreStepsMap.get(authority.getAuthority()); if (lowerRoles == null) { continue; // No hierarchy for the role } for (GrantedAuthority role : lowerRoles) { if (processedNames.add(role.getAuthority())) { reachableRoles.add(role); } } ``` ์œ„ ๋ถ€๋ถ„๋“ค ๋ณด๋ฉด Authority ์™€ Role์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๋“ฏํ•ด์„œ RolePrefix์™€ ๊ด€๋ จ์žˆ๋Š”๊ฑด๊ฐ€? ํ–ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž https://github.com/spring-projects/spring-security/blob/main/core/src/main/java/org/springframework/security/access/hierarchicalroles/RoleHierarchyImpl.java
@@ -0,0 +1,10 @@ +package nextstep.security.role; + +import java.util.Collection; + +public class NullRoleHierarchy implements RoleHierarchy { + @Override + public Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities) { + return authorities; + } +}
Java
์•„ํ•˜ ์ €๋„ NULL์„ ์™œ ์“ฐ์ง€ ๋ผ๋Š” ์˜๋ฌธ์ด ์žˆ์—ˆ๋Š”๋ฐ , ๋นˆ ๊ถŒํ•œ์„ ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ง ๊ทธ๋Œ€๋กœ RoleHierarchy ๊ฐ€ ์—†๋‹ค๋ผ๋Š” ์˜๋ฏธ๊ตฐ์š”!
@@ -0,0 +1,96 @@ +package nextstep.security.role; + +import org.springframework.util.StringUtils; + +import java.util.*; + +public class RoleHierarchyImpl implements RoleHierarchy { + + private static final List<GrantedAuthority> EMPTY_AUTHORITIES = Collections.emptyList(); + + private final Map<String, Set<GrantedAuthority>> roleHierarchy; + + + public RoleHierarchyImpl(Map<String, Set<GrantedAuthority>> roleHierarchy) { + this.roleHierarchy = new HashMap<>(roleHierarchy); + } + + public static Builder builder() { + return new RoleHierarchyImpl.Builder(); + } + + @Override + public Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities) { + if (authorities == null || authorities.isEmpty()) { + return EMPTY_AUTHORITIES; + } + + Set<GrantedAuthority> reachableRoles = new HashSet<>(); + for (GrantedAuthority authority : authorities) { + reachableRoles.add(authority); + + Collection<GrantedAuthority> impliedAuthorities = roleHierarchy.get(authority.getAuthority()); + if (impliedAuthorities != null) { + reachableRoles.addAll(impliedAuthorities); + } + } + + return reachableRoles; + } + + public static final class Builder { + + private final Map<String, Set<GrantedAuthority>> hierarchy; + + private Builder() { + this.hierarchy = new HashMap<>(); + } + + public Builder.ImpliedRoles role(String role) { + if (!StringUtils.hasText(role)) { + throw new IllegalArgumentException("role must not be null or empty"); + } + + return new Builder.ImpliedRoles(role); + } + + public RoleHierarchyImpl build() { + return new RoleHierarchyImpl(this.hierarchy); + } + + private RoleHierarchyImpl.Builder addHierarchy(String role, String... impliedRoles) { + Set<GrantedAuthority> grantedAuthorities = new HashSet<>(); + + for (String impliedRole : impliedRoles) { + grantedAuthorities.add(new SimpleGrantedAuthority(impliedRole)); + } + + this.hierarchy.put(role, grantedAuthorities); + return this; + } + + public final class ImpliedRoles { + + private final String role; + + private ImpliedRoles(String role) { + this.role = role; + } + + public Builder addImplies(String... impliedRoles) { + if (impliedRoles == null) { + throw new IllegalArgumentException("at least one implied role must be provided"); + } + + for (String impliedRole : impliedRoles) { + if (!StringUtils.hasText(impliedRole)) { + throw new IllegalArgumentException("at least one implied role must be provided"); + } + } + + return Builder.this.addHierarchy(this.role, impliedRoles); + } + } + + } +}
Java
์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,7 @@ +package nextstep.security.role; + +import java.util.Collection; + +public interface RoleHierarchy { + Collection<GrantedAuthority> getReachableRoles(Collection<GrantedAuthority> authorities); +}
Java
๋„ค spring security์—์„œ๋Š” ์ธ๊ฐ€์ฒ˜๋ฆฌ ๊ถŒํ•œ(Authority)๊ฐ€ ์žˆ์ง€๋งŒ, ๊ทธ ๊ถŒํ•œ์„ ์—ญํ• ๋ณ„๋กœ ๊ณ„์ธต์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋ คํ–ˆ๊ณ  ๊ทธ๋ ‡๊ฒŒ ๋‚˜์˜จ ๊ฒƒ์ด `RoleHierarchy`๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„์š”. ๋‘ ์ •๋ณด๊ฐ€ ๋™์ผํ•  ์ˆ˜๋Š” ์žˆ๋Š”๋ฐ ์“ฐ์ž„์ƒˆ ์ž์ฒด๊ฐ€ ๊ถŒํ•œ, ๊ถŒํ•œ๋ณ„ ์—ญํ• ๋กœ ํ•œ๋ฒˆ ๋” ์ชผ๊ฐœ์ง„๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. https://docs.spring.io/spring-security/reference/servlet/authorization/architecture.html?utm_source=chatgpt.com#authz-hierarchical-roles
@@ -0,0 +1,101 @@ +package nextstep.security.role; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RoleHierarchyImplTest { + + RoleHierarchyImpl.Builder builder; + + @BeforeEach + void setUp() { + builder = RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER"); + } + + @ParameterizedTest + @ValueSource(strings = {"ADMIN", "MEMBER", "UNKNOWN"}) + @DisplayName("์—ญํ•  ๊ณ„์ธต์„ Authority ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค") + void getRoleHierarchyTest(String roleName) { + // given + RoleHierarchyImpl roleHierarchy = builder.build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleName); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + List<GrantedAuthority> expect = switch (roleName) { + case "ADMIN" -> List.of( + new SimpleGrantedAuthority("ADMIN"), + new SimpleGrantedAuthority("MEMBER") + ); + case "MEMBER", "UNKNOWN" -> List.of( + new SimpleGrantedAuthority(roleName) + ); + default -> throw new IllegalStateException("Unexpected roleName: " + roleName); + }; + + assertThat(reachableRoles) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @Test + @DisplayName("์ƒˆ๋กœ์šด ์—ญํ•  ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.") + void addRoleHierarchyTest() { + // given + String target = "ISRAEL"; + String nothing = "NOTHING"; + String everything = "EVERYTHING"; + RoleHierarchyImpl roleHierarchy = builder.role(target).addImplies(nothing, everything) + .build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + var expect = List.of( + authority, + new SimpleGrantedAuthority(nothing), + new SimpleGrantedAuthority(everything) + ); + assertThat(reachableRoles).hasSize(3) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("์—ญํ• ์˜ ์ด๋ฆ„์€ ๋น„์–ด์žˆ์„์ˆ˜ ์—†๋‹ค") + void addRoleHierarchyFailTest1(String roleName) { + assertThatThrownBy( + () -> builder.role(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("role must not be null or empty"); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("ํฌํ•จ๋œ ์—ญํ• ๋“ค๋„ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†๋‹ค.") + void addRoleHierarchyFailTest2(String roleName) { + // given + String target = "NEW_ROLE"; + RoleHierarchyImpl.Builder.ImpliedRoles impliedRoles = builder.role(target); + + // when + assertThatThrownBy( + () -> impliedRoles.addImplies(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("at least one implied role must be provided"); + } +}
Java
junit5์—์„œ๋Š” ์ดˆ๊ธฐํ™” ๋‹จ์œ„๊ฐ€ ๋ฉ”์†Œ๋“œ ๋‹จ์œ„์ด๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ setup์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์‹œ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค. https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle
@@ -0,0 +1,101 @@ +package nextstep.security.role; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RoleHierarchyImplTest { + + RoleHierarchyImpl.Builder builder; + + @BeforeEach + void setUp() { + builder = RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER"); + } + + @ParameterizedTest + @ValueSource(strings = {"ADMIN", "MEMBER", "UNKNOWN"}) + @DisplayName("์—ญํ•  ๊ณ„์ธต์„ Authority ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค") + void getRoleHierarchyTest(String roleName) { + // given + RoleHierarchyImpl roleHierarchy = builder.build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleName); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + List<GrantedAuthority> expect = switch (roleName) { + case "ADMIN" -> List.of( + new SimpleGrantedAuthority("ADMIN"), + new SimpleGrantedAuthority("MEMBER") + ); + case "MEMBER", "UNKNOWN" -> List.of( + new SimpleGrantedAuthority(roleName) + ); + default -> throw new IllegalStateException("Unexpected roleName: " + roleName); + }; + + assertThat(reachableRoles) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @Test + @DisplayName("์ƒˆ๋กœ์šด ์—ญํ•  ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.") + void addRoleHierarchyTest() { + // given + String target = "ISRAEL"; + String nothing = "NOTHING"; + String everything = "EVERYTHING"; + RoleHierarchyImpl roleHierarchy = builder.role(target).addImplies(nothing, everything) + .build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + var expect = List.of( + authority, + new SimpleGrantedAuthority(nothing), + new SimpleGrantedAuthority(everything) + ); + assertThat(reachableRoles).hasSize(3) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("์—ญํ• ์˜ ์ด๋ฆ„์€ ๋น„์–ด์žˆ์„์ˆ˜ ์—†๋‹ค") + void addRoleHierarchyFailTest1(String roleName) { + assertThatThrownBy( + () -> builder.role(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("role must not be null or empty"); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("ํฌํ•จ๋œ ์—ญํ• ๋“ค๋„ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†๋‹ค.") + void addRoleHierarchyFailTest2(String roleName) { + // given + String target = "NEW_ROLE"; + RoleHierarchyImpl.Builder.ImpliedRoles impliedRoles = builder.role(target); + + // when + assertThatThrownBy( + () -> impliedRoles.addImplies(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("at least one implied role must be provided"); + } +}
Java
1. ์ถ”๊ฐ€ํ•œ๋‹ค๊ธฐ๋ณด๋‹ค๋Š” ๊ณ„์ธต์„ ๊ฐ€์ ธ์˜จ๋‹ค์— ๋” ๊ฐ€๊นŒ์šด ํ…Œ์ŠคํŠธ๋กœ ๋ณด์ด๋„ค์š”. 2. `SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target);`๋Š” ๊ทธ๋ž˜์„œ ๋”ฐ์ง€๊ณ ๋ณด๋ฉด ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ์ธ given์— ๋” ๊ฐ€๊น์Šต๋‹ˆ๋‹ค. when์—๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์ฆ๋ช…ํ•˜๊ณ ์žํ•˜๋Š” ๋Œ€์ƒ์˜ ์‹คํ–‰ ๊ทธ์ž์ฒด๋งŒ์„ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. 3. expected ๋˜ํ•œ given์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ๋งž๊ฒ ์ฃ . 74๋ฒˆ๋ผ์ธ์— ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋„ฃ๋Š” ๊ฒฝ์šฐ๋Š” ์ƒ๊ด€์ด ์—†์ง€๋งŒ ๊ธฐ๋Œ“๊ฐ’ ๋˜ํ•œ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ผ๊ณ  ๋ณด๋Š” ํŽธ์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,101 @@ +package nextstep.security.role; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RoleHierarchyImplTest { + + RoleHierarchyImpl.Builder builder; + + @BeforeEach + void setUp() { + builder = RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER"); + } + + @ParameterizedTest + @ValueSource(strings = {"ADMIN", "MEMBER", "UNKNOWN"}) + @DisplayName("์—ญํ•  ๊ณ„์ธต์„ Authority ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค") + void getRoleHierarchyTest(String roleName) { + // given + RoleHierarchyImpl roleHierarchy = builder.build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleName); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + List<GrantedAuthority> expect = switch (roleName) { + case "ADMIN" -> List.of( + new SimpleGrantedAuthority("ADMIN"), + new SimpleGrantedAuthority("MEMBER") + ); + case "MEMBER", "UNKNOWN" -> List.of( + new SimpleGrantedAuthority(roleName) + ); + default -> throw new IllegalStateException("Unexpected roleName: " + roleName); + }; + + assertThat(reachableRoles) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @Test + @DisplayName("์ƒˆ๋กœ์šด ์—ญํ•  ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.") + void addRoleHierarchyTest() { + // given + String target = "ISRAEL"; + String nothing = "NOTHING"; + String everything = "EVERYTHING"; + RoleHierarchyImpl roleHierarchy = builder.role(target).addImplies(nothing, everything) + .build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + var expect = List.of( + authority, + new SimpleGrantedAuthority(nothing), + new SimpleGrantedAuthority(everything) + ); + assertThat(reachableRoles).hasSize(3) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("์—ญํ• ์˜ ์ด๋ฆ„์€ ๋น„์–ด์žˆ์„์ˆ˜ ์—†๋‹ค") + void addRoleHierarchyFailTest1(String roleName) { + assertThatThrownBy( + () -> builder.role(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("role must not be null or empty"); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("ํฌํ•จ๋œ ์—ญํ• ๋“ค๋„ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†๋‹ค.") + void addRoleHierarchyFailTest2(String roleName) { + // given + String target = "NEW_ROLE"; + RoleHierarchyImpl.Builder.ImpliedRoles impliedRoles = builder.role(target); + + // when + assertThatThrownBy( + () -> impliedRoles.addImplies(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("at least one implied role must be provided"); + } +}
Java
๊ทธ๋ž˜์„œ ์ถ”๊ฐ€ํ•จ(`addImplies`)๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ณ ์‹ถ๋‹ค๋ฉด ์ •๋ง ๊ฐ’์ด ์ถ”๊ฐ€๋˜์—ˆ๋Š”์ง€ ๊ทธ ์ž์ฒด๋งŒ์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์ด ๋‚˜์„ ์ˆ˜๋„ ์žˆ์–ด์š”. `private final Map<String, Set<GrantedAuthority>> rolesReachableInOneOrMoreStepsMap;`์— ์›ํ•˜๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ์œผ๋กœ์š”.
@@ -0,0 +1,101 @@ +package nextstep.security.role; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RoleHierarchyImplTest { + + RoleHierarchyImpl.Builder builder; + + @BeforeEach + void setUp() { + builder = RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER"); + } + + @ParameterizedTest + @ValueSource(strings = {"ADMIN", "MEMBER", "UNKNOWN"}) + @DisplayName("์—ญํ•  ๊ณ„์ธต์„ Authority ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค") + void getRoleHierarchyTest(String roleName) { + // given + RoleHierarchyImpl roleHierarchy = builder.build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleName); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + List<GrantedAuthority> expect = switch (roleName) { + case "ADMIN" -> List.of( + new SimpleGrantedAuthority("ADMIN"), + new SimpleGrantedAuthority("MEMBER") + ); + case "MEMBER", "UNKNOWN" -> List.of( + new SimpleGrantedAuthority(roleName) + ); + default -> throw new IllegalStateException("Unexpected roleName: " + roleName); + }; + + assertThat(reachableRoles) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @Test + @DisplayName("์ƒˆ๋กœ์šด ์—ญํ•  ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.") + void addRoleHierarchyTest() { + // given + String target = "ISRAEL"; + String nothing = "NOTHING"; + String everything = "EVERYTHING"; + RoleHierarchyImpl roleHierarchy = builder.role(target).addImplies(nothing, everything) + .build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + var expect = List.of( + authority, + new SimpleGrantedAuthority(nothing), + new SimpleGrantedAuthority(everything) + ); + assertThat(reachableRoles).hasSize(3) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("์—ญํ• ์˜ ์ด๋ฆ„์€ ๋น„์–ด์žˆ์„์ˆ˜ ์—†๋‹ค") + void addRoleHierarchyFailTest1(String roleName) { + assertThatThrownBy( + () -> builder.role(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("role must not be null or empty"); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("ํฌํ•จ๋œ ์—ญํ• ๋“ค๋„ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†๋‹ค.") + void addRoleHierarchyFailTest2(String roleName) { + // given + String target = "NEW_ROLE"; + RoleHierarchyImpl.Builder.ImpliedRoles impliedRoles = builder.role(target); + + // when + assertThatThrownBy( + () -> impliedRoles.addImplies(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("at least one implied role must be provided"); + } +}
Java
`@CsvSource`๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๊ธฐ๋Œ“๊ฐ’์— ๋Œ€ํ•œ ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ํ…Œ์ŠคํŠธ์—์„œ ํ•˜์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”.
@@ -0,0 +1,101 @@ +package nextstep.security.role; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RoleHierarchyImplTest { + + RoleHierarchyImpl.Builder builder; + + @BeforeEach + void setUp() { + builder = RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER"); + } + + @ParameterizedTest + @ValueSource(strings = {"ADMIN", "MEMBER", "UNKNOWN"}) + @DisplayName("์—ญํ•  ๊ณ„์ธต์„ Authority ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค") + void getRoleHierarchyTest(String roleName) { + // given + RoleHierarchyImpl roleHierarchy = builder.build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleName); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + List<GrantedAuthority> expect = switch (roleName) { + case "ADMIN" -> List.of( + new SimpleGrantedAuthority("ADMIN"), + new SimpleGrantedAuthority("MEMBER") + ); + case "MEMBER", "UNKNOWN" -> List.of( + new SimpleGrantedAuthority(roleName) + ); + default -> throw new IllegalStateException("Unexpected roleName: " + roleName); + }; + + assertThat(reachableRoles) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @Test + @DisplayName("์ƒˆ๋กœ์šด ์—ญํ•  ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.") + void addRoleHierarchyTest() { + // given + String target = "ISRAEL"; + String nothing = "NOTHING"; + String everything = "EVERYTHING"; + RoleHierarchyImpl roleHierarchy = builder.role(target).addImplies(nothing, everything) + .build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + var expect = List.of( + authority, + new SimpleGrantedAuthority(nothing), + new SimpleGrantedAuthority(everything) + ); + assertThat(reachableRoles).hasSize(3) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("์—ญํ• ์˜ ์ด๋ฆ„์€ ๋น„์–ด์žˆ์„์ˆ˜ ์—†๋‹ค") + void addRoleHierarchyFailTest1(String roleName) { + assertThatThrownBy( + () -> builder.role(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("role must not be null or empty"); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("ํฌํ•จ๋œ ์—ญํ• ๋“ค๋„ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†๋‹ค.") + void addRoleHierarchyFailTest2(String roleName) { + // given + String target = "NEW_ROLE"; + RoleHierarchyImpl.Builder.ImpliedRoles impliedRoles = builder.role(target); + + // when + assertThatThrownBy( + () -> impliedRoles.addImplies(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("at least one implied role must be provided"); + } +}
Java
ํ…Œ์ŠคํŠธ๋กœ ์–ด๋–ค ๊ธฐ๋Šฅ์ด ์žˆ๋Š”์ง€ ์ž˜ ๋ณด์ด๋„ค์š” ๐Ÿ‘
@@ -0,0 +1,101 @@ +package nextstep.security.role; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collection; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RoleHierarchyImplTest { + + RoleHierarchyImpl.Builder builder; + + @BeforeEach + void setUp() { + builder = RoleHierarchyImpl.builder() + .role("ADMIN").addImplies("MEMBER"); + } + + @ParameterizedTest + @ValueSource(strings = {"ADMIN", "MEMBER", "UNKNOWN"}) + @DisplayName("์—ญํ•  ๊ณ„์ธต์„ Authority ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค") + void getRoleHierarchyTest(String roleName) { + // given + RoleHierarchyImpl roleHierarchy = builder.build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleName); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + List<GrantedAuthority> expect = switch (roleName) { + case "ADMIN" -> List.of( + new SimpleGrantedAuthority("ADMIN"), + new SimpleGrantedAuthority("MEMBER") + ); + case "MEMBER", "UNKNOWN" -> List.of( + new SimpleGrantedAuthority(roleName) + ); + default -> throw new IllegalStateException("Unexpected roleName: " + roleName); + }; + + assertThat(reachableRoles) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @Test + @DisplayName("์ƒˆ๋กœ์šด ์—ญํ•  ๊ณ„์ธต์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.") + void addRoleHierarchyTest() { + // given + String target = "ISRAEL"; + String nothing = "NOTHING"; + String everything = "EVERYTHING"; + RoleHierarchyImpl roleHierarchy = builder.role(target).addImplies(nothing, everything) + .build(); + + // when + SimpleGrantedAuthority authority = new SimpleGrantedAuthority(target); + Collection<GrantedAuthority> reachableRoles = roleHierarchy.getReachableRoles(List.of(authority)); + + // then + var expect = List.of( + authority, + new SimpleGrantedAuthority(nothing), + new SimpleGrantedAuthority(everything) + ); + assertThat(reachableRoles).hasSize(3) + .containsExactlyInAnyOrderElementsOf(expect); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("์—ญํ• ์˜ ์ด๋ฆ„์€ ๋น„์–ด์žˆ์„์ˆ˜ ์—†๋‹ค") + void addRoleHierarchyFailTest1(String roleName) { + assertThatThrownBy( + () -> builder.role(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("role must not be null or empty"); + } + + @ParameterizedTest + @NullAndEmptySource + @DisplayName("ํฌํ•จ๋œ ์—ญํ• ๋“ค๋„ ๋น„์–ด์žˆ์„ ์ˆ˜ ์—†๋‹ค.") + void addRoleHierarchyFailTest2(String roleName) { + // given + String target = "NEW_ROLE"; + RoleHierarchyImpl.Builder.ImpliedRoles impliedRoles = builder.role(target); + + // when + assertThatThrownBy( + () -> impliedRoles.addImplies(roleName) + ).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("at least one implied role must be provided"); + } +}
Java
์ „์ฒด ๋™์ž‘์ด ์•„๋‹Œ rolesReachableInOneOrMoreStepsMap ์ž์ฒด์— ๊ฐ’์ด ์ €์žฅ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š”๊ฒŒ ์ถ”๊ฐ€๋˜์—ˆ๋Š”๊ฐ€ ์— ๋Œ€ํ•œ ํ…Œ์ŠคํŠธ๋กœ ๋” ์ขํ˜€์ง€๊ฒ ๋„ค์š”!
@@ -0,0 +1,77 @@ +import React, { useState } from "react"; +import styled from "styled-components"; +import { Star as StarIcon } from "lucide-react"; + +interface StarRatingProps { + totalStars?: number; + initialRating?: number; + onRatingChange?: (rating: number) => void; +} + +const BackgroundContainer = styled.div` + background-color: #f7f7f7; + width: 807.73px; + height: 251.63px; + display: flex; + align-items: center; + justify-content: center; +`; + +const StarContainer = styled.div` + display: flex; + gap: 4px; +`; + +const StyledStar = styled.div<{ filled: boolean }>` + cursor: pointer; + display: flex; + align-items: center; + justify-content: center; + svg { + width: 94.96px; + height: 99.25px; + color: ${(props) => (props.filled ? "#facc15" : "#d1d5db")}; + } +`; + +const StarRating: React.FC<StarRatingProps> = ({ + totalStars = 5, + initialRating = 0, + onRatingChange, +}) => { + const [rating, setRating] = useState(initialRating); + + const handleRating = (index: number) => { + let newRating = rating; + if (index + 1 === rating) { + newRating = index; + } else if (index < rating) { + newRating = index; + } else { + newRating = index + 1; + } + + setRating(newRating); + if (onRatingChange) { + onRatingChange(newRating); + } + }; + + return ( + <BackgroundContainer> + <StarContainer> + {Array.from({ length: totalStars }, (_, index) => ( + <StyledStar + key={index} + filled={index < rating} + onClick={() => handleRating(index)} + > + <StarIcon fill={index < rating ? "#facc15" : "none"} stroke="#facc15" /> + </StyledStar> + ))} + </StarContainer> + </BackgroundContainer> + ); +}; + +export default StarRating;
Unknown
filled ์•ˆ์ชฝ์— String์œผ๋กœ ๊ฐ์‹ธ์ฃผ์„ธ์š”
@@ -0,0 +1,77 @@ +import React, { useState } from "react"; +import styled from "styled-components"; +import { Star as StarIcon } from "lucide-react"; + +interface StarRatingProps { + totalStars?: number; + initialRating?: number; + onRatingChange?: (rating: number) => void; +} + +const BackgroundContainer = styled.div` + background-color: #f7f7f7; + width: 807.73px; + height: 251.63px; + display: flex; + align-items: center; + justify-content: center; +`; + +const StarContainer = styled.div` + display: flex; + gap: 4px; +`; + +const StyledStar = styled.div<{ filled: boolean }>` + cursor: pointer; + display: flex; + align-items: center; + justify-content: center; + svg { + width: 94.96px; + height: 99.25px; + color: ${(props) => (props.filled ? "#facc15" : "#d1d5db")}; + } +`; + +const StarRating: React.FC<StarRatingProps> = ({ + totalStars = 5, + initialRating = 0, + onRatingChange, +}) => { + const [rating, setRating] = useState(initialRating); + + const handleRating = (index: number) => { + let newRating = rating; + if (index + 1 === rating) { + newRating = index; + } else if (index < rating) { + newRating = index; + } else { + newRating = index + 1; + } + + setRating(newRating); + if (onRatingChange) { + onRatingChange(newRating); + } + }; + + return ( + <BackgroundContainer> + <StarContainer> + {Array.from({ length: totalStars }, (_, index) => ( + <StyledStar + key={index} + filled={index < rating} + onClick={() => handleRating(index)} + > + <StarIcon fill={index < rating ? "#facc15" : "none"} stroke="#facc15" /> + </StyledStar> + ))} + </StarContainer> + </BackgroundContainer> + ); +}; + +export default StarRating;
Unknown
์ œ๊ฐ€ ํ›„๊ธฐ ํŽ˜์ด์ง€ ๋งŒ๋“ค๋ฉด์„œ ์ด ๋ถ€๋ถ„ ์ˆ˜์ •ํ•ด์„œ ํ•œ๊บผ๋ฒˆ์— PR ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -1,23 +1,20 @@ <template> - <button - @click=" - () => { - if (!props.disabled) props.onClick(); - } - " - :class="getNextButtonStyle()" - > - <slot></slot> + <button @click="handleNextButtonClick" :class="getNextButtonStyle()"> + <slot /> </button> </template> <script setup lang="ts"> const props = defineProps<{ - isPrimary?: boolean; + isPrimary?: boolean | undefined; onClick: () => void; - disabled?: boolean; + disabled?: boolean | undefined; }>(); +const handleNextButtonClick = () => { + if (!props.disabled) props.onClick(); +}; + const getNextButtonStyle = () => ({ 'h-fit w-[340px] rounded-2xl border-0 p-6 text-large font-bold mb-6': true, 'bg-primary-light dark:bg-primary-dark': props.isPrimary,
Unknown
๋‹จ์ˆœ ๊ถ๊ธˆ์ฆ์œผ๋กœ ์งˆ๋ฌธ๋“œ๋ฆฝ๋‹ˆ๋‹ค. undefined๋ฅผ ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? NextButton.vue์˜ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ isPrimary ๊ฐ’์„ ๋ถ€์—ฌํ•˜์ง€ ์•Š์„ ๋•Œ๊ฐ€ ์žˆ์–ด undefined๋ฅผ ํฌํ•จํ•œ ๊ฒƒ์œผ๋กœ ๋ณด์ด๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ๋Š” ? ๋งŒ์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ํ‘œํ˜„์ด ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŠนํžˆ boolean์ด๊ธฐ๋•Œ๋ฌธ์— undefined์ผ ๋•Œ falsyํ•˜๊ฒŒ ์ธ์‹์ด ๋  ๊ฒƒ ๊ฐ™์€๋ฐ ์ถ”๊ฐ€ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค~
@@ -3,8 +3,8 @@ <p class="text-medium font-semi_bold">{{ title }}</p> <div class="flex gap-x-3"> <button - v-for="size in sizes" - :key="size.id" + v-for="(size, index) in sizes" + :key="getButtonKey(size, index)" class="flex flex-col items-center justify-between gap-y-[10px]" @click="handleSizeClick(size)" > @@ -23,9 +23,10 @@ </template> <script setup lang="ts"> -import { Size } from '@interface/goods'; +import { Size } from '@interface/sizes'; import { useUserSelectStore } from '@store/storeUserSelect'; import { PLACE_HOLD_IMAGE_URL } from '@constants'; +import { storeToRefs } from 'pinia'; defineProps<{ title: string; @@ -38,11 +39,12 @@ const getImageWidth = (size: Size) => { return 74; }; -const { userSelect, setUserSelectSizeId, setUserSelectSizeValue } = - useUserSelectStore(); +const store = useUserSelectStore(); +const { userSelect } = storeToRefs(store); +const { setUserSelectSizeId, setUserSelectSizeValue } = store; const handleSizeClick = (size: Size) => { - if (userSelect.sizeId === size.id) { + if (userSelect.value.sizeId === size.id) { setUserSelectSizeId(0); setUserSelectSizeValue(0); return; @@ -51,11 +53,13 @@ const handleSizeClick = (size: Size) => { setUserSelectSizeValue(size.value); }; +const getButtonKey = (size: Size, index: number) => `size-${size.id}-${index}`; + const getUserSelectStyle = (size: Size) => ({ 'flex h-[74px] w-[74px] items-center justify-center rounded-2xl border-[3px] bg-gray_01-light dark:bg-gray_01-dark': true, - 'border-secondary-light': userSelect.sizeId === size.id, + 'border-secondary-light': userSelect.value.sizeId === size.id, 'border-gray_01-light dark:border-gray_01-dark': - userSelect.sizeId !== size.id, + userSelect.value.sizeId !== size.id, }); </script>
Unknown
storeToRefs ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ˜์‘์„ฑ์„ ์‚ด๋ฆฌ์…จ๋„ค์š” ๐Ÿ‘
@@ -3,16 +3,12 @@ <section class="mx-auto flex h-full flex-col items-center justify-between bg-gray_00-light text-gray_05-light dark:bg-gray_00-dark dark:text-gray_05-dark sm:w-screen md:w-96" > - <ProgressNavBar prevPage="/size" pageName="ingredient" /> - <div v-if="isLoading">๋กœ๋”ฉ์ค‘์ž…๋‹ˆ๋‹ค.</div> + <ProgressNavBar prevPage="size" pageName="ingredient" /> + <Loading v-if="isLoading" /> <IngredientBoard :ingredients="data?.ingredients ?? []" v-else /> <div v-if="!isAbleToRecommend">์žฌ๋ฃŒ๋ฅผ ์กฐ๊ธˆ ๋” ๊ณจ๋ผ๋ณผ๊นŒ์š”?</div> <NextButton - :onClick=" - () => { - mutation.mutate(userItem); - } - " + :onClick="postUserItem" isPrimary :disabled="!isAbleToRecommend || !userSelect.sizeValue" > @@ -29,6 +25,7 @@ import { storeToRefs } from 'pinia'; import ProgressNavBar from '@containers/ProgressNavBar.vue'; import IngredientBoard from '@containers/IngredientBoard.vue'; import NextButton from '@components/NextButton.vue'; +import Loading from '@src/components/Loading.vue'; import { useUserSelectStore } from '@store/storeUserSelect'; import { useGetIngredients } from '@apis/ingredients'; import { usePostRecipe } from '@apis/recipes'; @@ -38,6 +35,9 @@ const { userSelect, userItem } = storeToRefs(store); const { data, isLoading } = useGetIngredients(); const mutation = usePostRecipe(); +const postUserItem = () => { + mutation.mutate(userItem.value); +}; const allFlavorIdList = computed( () =>
Unknown
์„œ๋ฒ„ ์ƒํƒœ๊ด€๋ฆฌ์™€ ํด๋ผ์ด์–ธํŠธ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ฆฌํ•˜์…จ๊ตฐ์š” ๐Ÿ‘
@@ -1,23 +1,20 @@ <template> - <button - @click=" - () => { - if (!props.disabled) props.onClick(); - } - " - :class="getNextButtonStyle()" - > - <slot></slot> + <button @click="handleNextButtonClick" :class="getNextButtonStyle()"> + <slot /> </button> </template> <script setup lang="ts"> const props = defineProps<{ - isPrimary?: boolean; + isPrimary?: boolean | undefined; onClick: () => void; - disabled?: boolean; + disabled?: boolean | undefined; }>(); +const handleNextButtonClick = () => { + if (!props.disabled) props.onClick(); +}; + const getNextButtonStyle = () => ({ 'h-fit w-[340px] rounded-2xl border-0 p-6 text-large font-bold mb-6': true, 'bg-primary-light dark:bg-primary-dark': props.isPrimary,
Unknown
์‚ฌ์‹ค ์ €๋„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•˜๋‹ค๊ฐ€, ์ข€๋” ๊ฐ€์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ ค๊ณ  ์ž‘์„ฑ์„ ํ–ˆ์–ด์š”! ์Œ, ๋ฉ”์ด๋ธŒ๋‹˜ ๋ง์”€๋“ฃ๊ณ  ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๊ฐ€์‹œ์„ฑ๋งŒ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฑด ์ข€ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™๊ธดํ•˜๋„ค์š”!!
@@ -3,20 +3,18 @@ class="text-gray_05-light dark:text-gray_05-dark w-6 h-6" @click="goBack" > - <slot></slot> + <slot /> </button> </template> <script setup lang="ts"> -import { useRouter } from 'vue-router'; +import { pushPage } from '@router/route.helper'; const props = defineProps<{ prevPage: string; }>(); -const router = useRouter(); - function goBack() { - router.push(props.prevPage); + pushPage(props.prevPage); } </script>
Unknown
ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ํ•ด์ฃผ์„ธ์š”~
@@ -1,5 +1,7 @@ package com.chone.server.domains.review.domain; +import static org.springframework.util.StringUtils.hasText; + import com.chone.server.commons.jpa.BaseEntity; import com.chone.server.domains.order.domain.Order; import com.chone.server.domains.store.domain.Store; @@ -80,4 +82,12 @@ public static ReviewBuilder builder( .rating(rating) .isPublic(isPublic); } + + public void update(String content, BigDecimal rating, String imageUrl, Boolean isPublic) { + + if (hasText(content)) this.content = content; + if (rating != null) this.rating = rating; + if (hasText(imageUrl)) this.imageUrl = imageUrl; + if (isPublic != null) this.isPublic = isPublic; + } }
Java
ํ•ด๋‹น update ๋ฉ”์„œ๋“œ์˜ ๊ฒฝ์šฐ์— not null์ด๋‚˜ not blank ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ์•ˆ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋กœ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”? ๋งŒ์ผ ์ด๋Ÿด ๊ฒฝ์šฐ request์—์„œ null์ด ์žˆ๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์—”ํ‹ฐํ‹ฐ์˜ ํ•„๋“œ ๊ฐ’์ด null๋กœ ๋ณ€๊ฒฝ๋˜๊ณ , ์ด๊ฒƒ์ด ๋ณ€๊ฒฝ๊ฐ์ง€๊ฐ€ ๋˜์–ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ์ดํ„ฐ๊ฐ€ null ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค!(์˜ˆ๋ฅผ ๋“ค์–ด, `update(null, null, null, null)`์„ ํ˜ธ์ถœํ•˜๋ฉด ํ•ด๋‹น ํ•„๋“œ๊ฐ€ null๋กœ ๋ฎ์–ด์”Œ์›Œ์ง‘๋‹ˆ๋‹ค) ```java import static org.springframework.util.StringUtils.hasText; public void update(String content, BigDecimal rating, String imageUrl, Boolean isPublic) { if (hasText(content)) this.content = content; if (rating != null) this.rating = rating; if (hasText(imageUrl)) this.imageUrl = imageUrl; if (isPublic != null) this.isPublic = isPublic ``` ์ด๋Ÿฐ์‹์œผ๋กœ ์ˆ˜์ •ํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”? ์ฐธ๊ณ ๋กœ String์˜ white space๊นŒ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด hasText ์œ ํ‹ธ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.
@@ -8,9 +8,11 @@ import com.chone.server.domains.review.domain.Review; import com.chone.server.domains.review.dto.request.CreateRequestDto; import com.chone.server.domains.review.dto.request.ReviewListRequestDto; +import com.chone.server.domains.review.dto.request.UpdateRequestDto; import com.chone.server.domains.review.dto.response.ReviewDetailResponseDto; import com.chone.server.domains.review.dto.response.ReviewListResponseDto; import com.chone.server.domains.review.dto.response.ReviewResponseDto; +import com.chone.server.domains.review.dto.response.ReviewUpdateResponseDto; import com.chone.server.domains.review.exception.ReviewExceptionCode; import com.chone.server.domains.review.repository.ReviewRepository; import com.chone.server.domains.review.repository.ReviewSearchRepository; @@ -19,6 +21,8 @@ import com.chone.server.domains.user.domain.User; import jakarta.transaction.Transactional; import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.UUID; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -106,7 +110,33 @@ public ReviewDetailResponseDto getReviewById(UUID reviewId, CustomUserDetails pr private void validateAccess(User user, Review review) { if (!review.getIsPublic() && !review.getUser().getId().equals(user.getId())) { - throw new ApiBusinessException(ReviewExceptionCode.REVIEW_ACCESS_DENIED); + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_FORBIDDEN_ACTION); } } + + @Transactional + public ReviewUpdateResponseDto updateReview( + UUID reviewId, UpdateRequestDto request, CustomUserDetails principal) { + if (principal == null || principal.getUser() == null) { + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_UNAUTHORIZED); + } + + Review review = + reviewRepository + .findById(reviewId) + .orElseThrow(() -> new ApiBusinessException(ReviewExceptionCode.REVIEW_NOT_FOUND)); + + if (!review.getUser().getId().equals(principal.getUser().getId())) { + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_FORBIDDEN_ACTION); + } + + if (ChronoUnit.HOURS.between(review.getCreatedAt(), LocalDateTime.now()) > 72) { + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_UPDATE_EXPIRED); + } + + review.update( + request.getContent(), request.getRating(), request.getImageUrl(), request.getIsPublic()); + + return new ReviewUpdateResponseDto(review.getId(), review.getUpdatedAt()); + } }
Java
์ €๋„ ์ด๋ฒˆ์— ์ฃผ๋ฌธ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ๊ฐ‘์ž๊ธฐ ๊นจ๋‹ฌ์€ ๊ฑด๋ฐ, ์ €ํฌ๊ฐ€ soft delete๋ฅผ ํ•˜๊ณ  ์žˆ์–ด์„œ findById๋ฅผ ํ•  ๋•Œ ์กฐ๊ฑด์œผ๋กœ deletedAt null ์กฐ๊ฑด์„ ๊ฑธ์–ด์•ผ ์‚ญ์ œ๋œ ๊ฒƒ์„ ์ œ์™ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์กฐํšŒ ์‹œ ํ•ด๋‹น ์กฐ๊ฑด์„ ํฌํ•จํ•˜์—ฌ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! ์•„๋‹ˆ๋ฉด ๋ธŒ๋žœ์น˜๋ฅผ ์ƒˆ๋กœ ํŒŒ์„œ ํ•ด๋‹น ๊ด€๋ จ ์กฐํšŒ๋“ค์„ ์ˆ˜์ •ํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -1,5 +1,7 @@ package com.chone.server.domains.review.domain; +import static org.springframework.util.StringUtils.hasText; + import com.chone.server.commons.jpa.BaseEntity; import com.chone.server.domains.order.domain.Order; import com.chone.server.domains.store.domain.Store; @@ -80,4 +82,12 @@ public static ReviewBuilder builder( .rating(rating) .isPublic(isPublic); } + + public void update(String content, BigDecimal rating, String imageUrl, Boolean isPublic) { + + if (hasText(content)) this.content = content; + if (rating != null) this.rating = rating; + if (hasText(imageUrl)) this.imageUrl = imageUrl; + if (isPublic != null) this.isPublic = isPublic; + } }
Java
์„œ๋น„์Šค ๋ ˆ์ด์–ด์—์„œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ํ•„๋“œ์—์„œ null ๋กœ DB ๋ฐ˜์˜๋œ๋‹ค๋Š” ๊ฑธ ๋†“์ณค์Šต๋‹ˆ๋‹ค! ์•Œ๋ ค์ฃผ์‹  ๋ฐฉ๋ฒ•์œผ๋กœ ์ˆ˜์ •ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค :)
@@ -8,9 +8,11 @@ import com.chone.server.domains.review.domain.Review; import com.chone.server.domains.review.dto.request.CreateRequestDto; import com.chone.server.domains.review.dto.request.ReviewListRequestDto; +import com.chone.server.domains.review.dto.request.UpdateRequestDto; import com.chone.server.domains.review.dto.response.ReviewDetailResponseDto; import com.chone.server.domains.review.dto.response.ReviewListResponseDto; import com.chone.server.domains.review.dto.response.ReviewResponseDto; +import com.chone.server.domains.review.dto.response.ReviewUpdateResponseDto; import com.chone.server.domains.review.exception.ReviewExceptionCode; import com.chone.server.domains.review.repository.ReviewRepository; import com.chone.server.domains.review.repository.ReviewSearchRepository; @@ -19,6 +21,8 @@ import com.chone.server.domains.user.domain.User; import jakarta.transaction.Transactional; import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.UUID; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -106,7 +110,33 @@ public ReviewDetailResponseDto getReviewById(UUID reviewId, CustomUserDetails pr private void validateAccess(User user, Review review) { if (!review.getIsPublic() && !review.getUser().getId().equals(user.getId())) { - throw new ApiBusinessException(ReviewExceptionCode.REVIEW_ACCESS_DENIED); + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_FORBIDDEN_ACTION); } } + + @Transactional + public ReviewUpdateResponseDto updateReview( + UUID reviewId, UpdateRequestDto request, CustomUserDetails principal) { + if (principal == null || principal.getUser() == null) { + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_UNAUTHORIZED); + } + + Review review = + reviewRepository + .findById(reviewId) + .orElseThrow(() -> new ApiBusinessException(ReviewExceptionCode.REVIEW_NOT_FOUND)); + + if (!review.getUser().getId().equals(principal.getUser().getId())) { + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_FORBIDDEN_ACTION); + } + + if (ChronoUnit.HOURS.between(review.getCreatedAt(), LocalDateTime.now()) > 72) { + throw new ApiBusinessException(ReviewExceptionCode.REVIEW_UPDATE_EXPIRED); + } + + review.update( + request.getContent(), request.getRating(), request.getImageUrl(), request.getIsPublic()); + + return new ReviewUpdateResponseDto(review.getId(), review.getUpdatedAt()); + } }
Java
์กฐํšŒ ์‹œ ์‚ญ์ œ๋œ ๋ฆฌ๋ทฐ ์กฐํšŒ๋˜์ง€ ์•Š๋„๋ก ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~
@@ -0,0 +1,48 @@ +package com.ioteam.order_management_platform.review.entity; + +import com.ioteam.order_management_platform.global.entity.BaseEntity; +import com.ioteam.order_management_platform.review.dto.CreateReviewRequestDto; +import com.ioteam.order_management_platform.review.dto.ReviewResponseDto; +import jakarta.persistence.*; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +public class Review extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID reviewId; + private int reviewScore; + @Column(length = 1000) + private String reviewContent; + @Column(length = 100) + private String reviewImageUrl; + private Boolean isPublic; + private LocalDateTime deletedAt; + private UUID deletedBy; + + public Review(CreateReviewRequestDto requestDto) { + this.reviewScore = requestDto.getReviewScore(); + this.reviewContent = requestDto.getReviewContent(); + this.reviewImageUrl = requestDto.getReviewImageUrl(); + this.isPublic = requestDto.getIsPublic(); + } + + public ReviewResponseDto toResponseDto() { + return ReviewResponseDto + .builder() + .reviewId(this.reviewId) + .reviewScore(this.reviewScore) + .reviewContent(this.reviewContent) + .reviewImageUrl(this.reviewImageUrl) + //.reviewOrderId(this.) + .isPublic(this.isPublic) + .build(); + } +}
Java
P5: ๋‹จ์ˆœ ๊ถ๊ธˆ์ฆ์ž…๋‹ˆ๋‹ค! ๊ธฐ๋ณธ ์ง€์‹์ด ๋ถ€์กฑํ•˜์—ฌ Boolean์€ default ๊ฐ’์ด null์ด๊ณ , boolean์€ false์ธ ๊ฒƒ์ด ์ฐจ์ด์ ์ด๋ผ๋Š” ๊ฒƒ๋งŒ ์•Œ๊ณ  ์—ฌ์ญค๋ณด๋Š” ์ .. ์–‘ํ•ด๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค..! ๐Ÿ˜… Boolean์„ ์‚ฌ์šฉํ•œ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. P4: ๊ณต๊ฐœ ์—ฌ๋ถ€ ๊ธฐ๋ณธ๊ฐ’์ด true ์ด๋ฏ€๋กœ @ColumnDefault ๋กœ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •์ด ํ•„์š”ํ•˜์ง„ ์•Š์„๊นŒ์š”?? ๐Ÿ˜€
@@ -0,0 +1,48 @@ +package com.ioteam.order_management_platform.review.entity; + +import com.ioteam.order_management_platform.global.entity.BaseEntity; +import com.ioteam.order_management_platform.review.dto.CreateReviewRequestDto; +import com.ioteam.order_management_platform.review.dto.ReviewResponseDto; +import jakarta.persistence.*; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +public class Review extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID reviewId; + private int reviewScore; + @Column(length = 1000) + private String reviewContent; + @Column(length = 100) + private String reviewImageUrl; + private Boolean isPublic; + private LocalDateTime deletedAt; + private UUID deletedBy; + + public Review(CreateReviewRequestDto requestDto) { + this.reviewScore = requestDto.getReviewScore(); + this.reviewContent = requestDto.getReviewContent(); + this.reviewImageUrl = requestDto.getReviewImageUrl(); + this.isPublic = requestDto.getIsPublic(); + } + + public ReviewResponseDto toResponseDto() { + return ReviewResponseDto + .builder() + .reviewId(this.reviewId) + .reviewScore(this.reviewScore) + .reviewContent(this.reviewContent) + .reviewImageUrl(this.reviewImageUrl) + //.reviewOrderId(this.) + .isPublic(this.isPublic) + .build(); + } +}
Java
@Ryujy ๋‹˜, ์˜๊ฒฌ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! @ColumnDefault ๋กœ ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋†“์ณค๋„ค์š”. ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Boolean์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ํŠน๋ณ„ํžˆ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,48 @@ +package com.ioteam.order_management_platform.review.entity; + +import com.ioteam.order_management_platform.global.entity.BaseEntity; +import com.ioteam.order_management_platform.review.dto.CreateReviewRequestDto; +import com.ioteam.order_management_platform.review.dto.ReviewResponseDto; +import jakarta.persistence.*; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +public class Review extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private UUID reviewId; + private int reviewScore; + @Column(length = 1000) + private String reviewContent; + @Column(length = 100) + private String reviewImageUrl; + private Boolean isPublic; + private LocalDateTime deletedAt; + private UUID deletedBy; + + public Review(CreateReviewRequestDto requestDto) { + this.reviewScore = requestDto.getReviewScore(); + this.reviewContent = requestDto.getReviewContent(); + this.reviewImageUrl = requestDto.getReviewImageUrl(); + this.isPublic = requestDto.getIsPublic(); + } + + public ReviewResponseDto toResponseDto() { + return ReviewResponseDto + .builder() + .reviewId(this.reviewId) + .reviewScore(this.reviewScore) + .reviewContent(this.reviewContent) + .reviewImageUrl(this.reviewImageUrl) + //.reviewOrderId(this.) + .isPublic(this.isPublic) + .build(); + } +}
Java
@Ryujy ๋‹˜ Boolean ์‚ฌ์šฉํ•œ ์ด์œ  ๊ณต๋ถ€ํ•ด๋‘๊ณ  ๊นŒ๋จน๊ณ  ์žˆ์—ˆ๋„ค์š”. - boolean์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ž๋ฐ” ๋นˆ ๊ทœ์•ฝ์— ๋”ฐ๋ฅด๋ฉด getter ๋ฉ”์†Œ๋“œ๊ฐ€ isXXX() ํ˜•ํƒœ๋กœ ์ƒ์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. - ๊ทธ๋ž˜์„œ getter ๋ฉ”์†Œ๋“œ ์ž๋™์ƒ์„ฑ(๋กฌ๋ณต, ์ธํ…”๋ฆฌ์ œ์ด ํ™œ์šฉ)ํ•˜๋Š” ๊ฒฝ์šฐ์— ๊ฐ์ฒด ํ”„๋กœํผํ„ฐ์˜ ๋„ค์ด๋ฐ์—์„œ ๋ฌธ์ œ(isXXX ์ด ์•„๋‹Œ XXX ํ˜•ํƒœ๋กœ ๋„ค์ด๋ฐ์ด ๋˜๋Š” ๋ฌธ์ œ)๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. - ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์œผ๋กœ ๋ช‡ ๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ( [์ œ ๋ธ”๋กœ๊ทธ์— ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋’€์–ด์š”.](https://ichmww2.tistory.com/87)) ์ €๋Š” wrapper ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•ด์„œ Boolean ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -1,8 +1,79 @@ import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends Component { + constructor() { + super(); + this.state = { + id: '', + pw: '', + }; + } + + handleInput = event => { + const { name, value } = event.target; + this.setState({ + [name]: value, + }); + }; + + goToMain = () => { + this.props.history.push('/main-Soojeong#'); + }; + render() { - return <div>๋กœ๊ทธ์ธ</div>; + const isValid = + this.state.id.includes('@') && + this.state.id.length >= 5 && + this.state.pw.length >= 8; + + return ( + <div className="Login"> + <main> + <header> + <h1>Westagram</h1> + </header> + + <section className="loginInputBox"> + <h2 className="sr-only">login page</h2> + <form> + <input + name="id" + autoComplete="off" + onChange={this.handleInput} + type="text" + id="loginId" + value={this.state.id} + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + name="pw" + onChange={this.handleInput} + type="password" + value={this.state.pw} + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button + onClick={this.goToMain} + type="button" + className="loginBtn" + disabled={!isValid} + > + ๋กœ๊ทธ์ธ + </button> + </form> + </section> + + <footer> + <Link to="" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </Link> + </footer> + </main> + </div> + ); } }
JavaScript
์˜ค ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น~
@@ -1,8 +1,79 @@ import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends Component { + constructor() { + super(); + this.state = { + id: '', + pw: '', + }; + } + + handleInput = event => { + const { name, value } = event.target; + this.setState({ + [name]: value, + }); + }; + + goToMain = () => { + this.props.history.push('/main-Soojeong#'); + }; + render() { - return <div>๋กœ๊ทธ์ธ</div>; + const isValid = + this.state.id.includes('@') && + this.state.id.length >= 5 && + this.state.pw.length >= 8; + + return ( + <div className="Login"> + <main> + <header> + <h1>Westagram</h1> + </header> + + <section className="loginInputBox"> + <h2 className="sr-only">login page</h2> + <form> + <input + name="id" + autoComplete="off" + onChange={this.handleInput} + type="text" + id="loginId" + value={this.state.id} + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + name="pw" + onChange={this.handleInput} + type="password" + value={this.state.pw} + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button + onClick={this.goToMain} + type="button" + className="loginBtn" + disabled={!isValid} + > + ๋กœ๊ทธ์ธ + </button> + </form> + </section> + + <footer> + <Link to="" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </Link> + </footer> + </main> + </div> + ); } }
JavaScript
this.state.id.includes('@') && this.state.id.length >= 5 && this.state.pw.length >= 8 ์ด๊ฑธ true/false Boolean?? ์ด๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ํ•ด๋„ ๋˜์ง€ ์•Š์„๊นŒ์š” ์ˆ˜์ •๋‹˜์€ ์ž˜ํ•˜์‹œ๋‹ˆ ์•„๋งˆ๊ฐ€๋Šฅํ•˜์‹ค๋“ฏ!
@@ -0,0 +1,61 @@ +.Login { + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + + main { + display: flex; + flex-direction: column; + justify-content: space-around; + width: 350px; + height: 380px; + padding: 40px; + border: 1px solid #ccc; + + header { + h1 { + font-family: 'Lobster', cursive; + text-align: center; + } + } + + .loginInputBox { + form { + display: flex; + flex-direction: column; + justify-content: center; + + input { + margin-bottom: 5px; + padding: 9px 8px 7px; + background-color: rgb(250, 250, 250); + border: 1px solid rgb(38, 38, 38); + border-radius: 3px; + } + + .loginBtn { + margin-top: 10px; + padding: 9px 8px 7px; + border: none; + border-radius: 3px; + color: #fff; + background-color: #0096f6; + &:disabled { + background-color: #c0dffd; + } + } + } + } + + footer { + .findPassword { + display: block; + margin-top: 50px; + text-align: center; + color: #00376b; + font-size: 14px; + } + } + } +}
Unknown
์ด๊ฑด ์™œ ์ฃผ์„์œผ๋กœ ๋˜์žˆ๋‚˜์š”???
@@ -0,0 +1,134 @@ +[ + { + "id": 1, + "userName": "eessoo__", + "src": "../images/soojeongLee/user2.jpg", + "feedText": "๐Ÿ’Ÿ", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "soojeonglee", + "content": "Hi there.", + "commnetLike": false + }, + { + "id": 3, + "userName": "jaehyunlee", + "content": "Hey.", + "commnetLike": false + }, + { + "id": 4, + "userName": "gyeongminlee", + "content": "Hi.", + "commnetLike": true + } + ], + "isLike": false + }, + { + "id": 2, + "userName": "zz_ing94", + "src": "../../images/soojeongLee/user3.jpg", + "feedText": "Toy story ๐Ÿ’š", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "jisuoh", + "content": "โœจ", + "commnetLike": true + }, + { + "id": 3, + "userName": "jungjunsung", + "content": "coffee", + "commnetLike": false + }, + { + "id": 4, + "userName": "somihwang", + "content": "ํฌํ‚ค", + "commnetLike": true + } + ], + "isLike": false + }, + { + "id": 3, + "userName": "hwayoonci", + "src": "../../images/soojeongLee/user4.jpg", + "feedText": "์˜ค๋Š˜์˜ ์ผ๊ธฐ", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "yunkyunglee", + "content": "๊ท€์—ฌ์šด ์Šคํ‹ฐ์ปค", + "commnetLike": true + }, + { + "id": 3, + "userName": "summer", + "content": "๐Ÿถ", + "commnetLike": false + }, + { + "id": 4, + "userName": "uiyeonlee", + "content": "๐Ÿ‘๐Ÿป", + "commnetLike": true + } + ], + "isLike": true + }, + { + "id": 4, + "userName": "cosz_zy", + "src": "../../images/soojeongLee/user5.jpg", + "feedText": "1์ผ ํ™”๊ฐ€ ๐ŸŽจ ", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "soojeonglee", + "content": "Hi there.", + "commnetLike": false + }, + { + "id": 3, + "userName": "jaehyunlee", + "content": "Hey.", + "commnetLike": true + }, + { + "id": 4, + "userName": "gyeongminlee", + "content": "Hi.", + "commnetLike": true + } + ], + "isLike": false + } +]
Unknown
์ œ์ด์Šจ ํŒŒ์ผ๋„ ์žˆ์—ˆ๊ตฐ์š”!
@@ -0,0 +1,175 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import Comment from './Comment/Comment'; + +import '../Feed/Feed.scss'; + +export class Feed extends Component { + constructor() { + super(); + this.state = { + comment: '', + // commentList: [], + // isLike: false, + }; + } + + // componentDidMount() { + // this.setState({ + // commentList: this.props.commentData, + // isLike: this.props.isLike, + // }); + // } + + hadComment = event => { + this.setState({ + comment: event.target.value, + }); + }; + + // ํด๋ฆญํ•จ์ˆ˜ + submitComent = () => { + // this.setState({ + // commentList: this.state.commentList.concat([ + // { + // id: this.state.commentList.length + 1, + // userName: 'eessoo__', + // content: this.state.comment, + // commentLike: false, + // }, + // ]), + // comment: '', + // }); + }; + + handleKeyPress = event => { + if (event.key === 'Enter') { + event.preventDefault(); + this.submitComent(); + } + }; + + handleLike = () => { + this.setState({ + isLike: !this.state.isLike, + }); + }; + + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete = id => { + // const newCommentList = this.state.commentList.filter(comment => { + // return comment.id !== id; + // }); + + // this.setState({ + // commentList: newCommentList, + // }); + // }; + + render() { + return ( + <article> + <header> + <h1> + <Link className="contentHeader"></Link> + <Link className="userName">{this.props.userName}</Link> + </h1> + <button className="ellipsis" type="button"> + <i className="fas fa-ellipsis-h ellipsisIcon"></i> + </button> + </header> + <section className="feedContent"> + <img alt="feedImage" src={this.props.src} /> + <ul className="reactionsBox"> + <li className="reactionsLeft"> + <button + className="leftButton" + type="button" + onClick={this.handleLike} + > + {this.state.isLike ? ( + <i className="fas fa-heart" /> + ) : ( + <i className="far fa-heart" /> + )} + </button> + <button className="leftButton" type="button"> + <i className="far fa-comment" /> + </button> + <button className="leftButton" type="button"> + <i className="fas fa-external-link-alt" /> + </button> + </li> + <li className="reactionsRight"> + <button className="rightButton"> + <i className="far fa-bookmark" /> + </button> + </li> + </ul> + <h2> + <Link className="userProfile feedConUser"></Link> + <b>eessoo__</b>๋‹˜ ์™ธ + <button className="likeCount"> + <b> 7๋ช…</b> + </button> + ์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </h2> + <p className="feedText"> + {this.props.feedText} + <span className="postTime">54๋ถ„ ์ „</span> + </p> + <ul id="commnetBox"> + {/* {this.state.commentList.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} */} + {this.props.commentData.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} + </ul> + </section> + <footer className="feedFooter"> + <form className="inputBox" onKeyPress={this.handleKeyPress}> + <input + autoComplete="off" + onChange={this.hadComment} + className="comment" + value={this.state.comment} + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + /> + <button + onClick={this.submitComent} + className="commentSubmit" + type="button" + > + ๊ฒŒ์‹œ + </button> + </form> + </footer> + </article> + ); + } +} + +export default Feed;
JavaScript
์˜ค ์ด๋ ‡๊ฒŒ ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ๋‚˜์˜์ง€ ์•Š์€ ๋“ฏ ? ๋‚˜์ค‘์— ๊ฐ€์„œ๋Š” ์ง€์›Œ์•ผ ๋  ์ˆ˜๋„ ์žˆ์ง€๋งŒ...?
@@ -0,0 +1,175 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import Comment from './Comment/Comment'; + +import '../Feed/Feed.scss'; + +export class Feed extends Component { + constructor() { + super(); + this.state = { + comment: '', + // commentList: [], + // isLike: false, + }; + } + + // componentDidMount() { + // this.setState({ + // commentList: this.props.commentData, + // isLike: this.props.isLike, + // }); + // } + + hadComment = event => { + this.setState({ + comment: event.target.value, + }); + }; + + // ํด๋ฆญํ•จ์ˆ˜ + submitComent = () => { + // this.setState({ + // commentList: this.state.commentList.concat([ + // { + // id: this.state.commentList.length + 1, + // userName: 'eessoo__', + // content: this.state.comment, + // commentLike: false, + // }, + // ]), + // comment: '', + // }); + }; + + handleKeyPress = event => { + if (event.key === 'Enter') { + event.preventDefault(); + this.submitComent(); + } + }; + + handleLike = () => { + this.setState({ + isLike: !this.state.isLike, + }); + }; + + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete = id => { + // const newCommentList = this.state.commentList.filter(comment => { + // return comment.id !== id; + // }); + + // this.setState({ + // commentList: newCommentList, + // }); + // }; + + render() { + return ( + <article> + <header> + <h1> + <Link className="contentHeader"></Link> + <Link className="userName">{this.props.userName}</Link> + </h1> + <button className="ellipsis" type="button"> + <i className="fas fa-ellipsis-h ellipsisIcon"></i> + </button> + </header> + <section className="feedContent"> + <img alt="feedImage" src={this.props.src} /> + <ul className="reactionsBox"> + <li className="reactionsLeft"> + <button + className="leftButton" + type="button" + onClick={this.handleLike} + > + {this.state.isLike ? ( + <i className="fas fa-heart" /> + ) : ( + <i className="far fa-heart" /> + )} + </button> + <button className="leftButton" type="button"> + <i className="far fa-comment" /> + </button> + <button className="leftButton" type="button"> + <i className="fas fa-external-link-alt" /> + </button> + </li> + <li className="reactionsRight"> + <button className="rightButton"> + <i className="far fa-bookmark" /> + </button> + </li> + </ul> + <h2> + <Link className="userProfile feedConUser"></Link> + <b>eessoo__</b>๋‹˜ ์™ธ + <button className="likeCount"> + <b> 7๋ช…</b> + </button> + ์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </h2> + <p className="feedText"> + {this.props.feedText} + <span className="postTime">54๋ถ„ ์ „</span> + </p> + <ul id="commnetBox"> + {/* {this.state.commentList.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} */} + {this.props.commentData.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} + </ul> + </section> + <footer className="feedFooter"> + <form className="inputBox" onKeyPress={this.handleKeyPress}> + <input + autoComplete="off" + onChange={this.hadComment} + className="comment" + value={this.state.comment} + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + /> + <button + onClick={this.submitComent} + className="commentSubmit" + type="button" + > + ๊ฒŒ์‹œ + </button> + </form> + </footer> + </article> + ); + } +} + +export default Feed;
JavaScript
๋ฒ„ํŠผ์œผ๋กœ ํ•œ ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”? ๊ถ๊ธˆ
@@ -0,0 +1,175 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import Comment from './Comment/Comment'; + +import '../Feed/Feed.scss'; + +export class Feed extends Component { + constructor() { + super(); + this.state = { + comment: '', + // commentList: [], + // isLike: false, + }; + } + + // componentDidMount() { + // this.setState({ + // commentList: this.props.commentData, + // isLike: this.props.isLike, + // }); + // } + + hadComment = event => { + this.setState({ + comment: event.target.value, + }); + }; + + // ํด๋ฆญํ•จ์ˆ˜ + submitComent = () => { + // this.setState({ + // commentList: this.state.commentList.concat([ + // { + // id: this.state.commentList.length + 1, + // userName: 'eessoo__', + // content: this.state.comment, + // commentLike: false, + // }, + // ]), + // comment: '', + // }); + }; + + handleKeyPress = event => { + if (event.key === 'Enter') { + event.preventDefault(); + this.submitComent(); + } + }; + + handleLike = () => { + this.setState({ + isLike: !this.state.isLike, + }); + }; + + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete = id => { + // const newCommentList = this.state.commentList.filter(comment => { + // return comment.id !== id; + // }); + + // this.setState({ + // commentList: newCommentList, + // }); + // }; + + render() { + return ( + <article> + <header> + <h1> + <Link className="contentHeader"></Link> + <Link className="userName">{this.props.userName}</Link> + </h1> + <button className="ellipsis" type="button"> + <i className="fas fa-ellipsis-h ellipsisIcon"></i> + </button> + </header> + <section className="feedContent"> + <img alt="feedImage" src={this.props.src} /> + <ul className="reactionsBox"> + <li className="reactionsLeft"> + <button + className="leftButton" + type="button" + onClick={this.handleLike} + > + {this.state.isLike ? ( + <i className="fas fa-heart" /> + ) : ( + <i className="far fa-heart" /> + )} + </button> + <button className="leftButton" type="button"> + <i className="far fa-comment" /> + </button> + <button className="leftButton" type="button"> + <i className="fas fa-external-link-alt" /> + </button> + </li> + <li className="reactionsRight"> + <button className="rightButton"> + <i className="far fa-bookmark" /> + </button> + </li> + </ul> + <h2> + <Link className="userProfile feedConUser"></Link> + <b>eessoo__</b>๋‹˜ ์™ธ + <button className="likeCount"> + <b> 7๋ช…</b> + </button> + ์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </h2> + <p className="feedText"> + {this.props.feedText} + <span className="postTime">54๋ถ„ ์ „</span> + </p> + <ul id="commnetBox"> + {/* {this.state.commentList.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} */} + {this.props.commentData.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} + </ul> + </section> + <footer className="feedFooter"> + <form className="inputBox" onKeyPress={this.handleKeyPress}> + <input + autoComplete="off" + onChange={this.hadComment} + className="comment" + value={this.state.comment} + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + /> + <button + onClick={this.submitComent} + className="commentSubmit" + type="button" + > + ๊ฒŒ์‹œ + </button> + </form> + </footer> + </article> + ); + } +} + +export default Feed;
JavaScript
autoComplete ์ด๊ฑฐ๋Š” ๋ญ”๊ฐ€์š”? ๊ถ๊ธˆ~
@@ -0,0 +1,128 @@ +@import '../../../../styles/variables.scss'; + +article { + margin-bottom: 20px; + background-color: #fff; + border-radius: 3px; + border: 1px solid $border-color; + + header { + @include flex-center; + justify-content: space-between; + align-content: center; + padding: 10px; + + h1 { + @include flex-center; + + .contentHeader { + @include userProfile; + margin-right: 10px; + background-image: url('http://localhost:3000/images/soojeongLee/userImage.jpg'); + background-size: cover; + } + .userName { + font-size: 16px; + font-weight: 600; + } + } + + .ellipsis { + @include button; + font-size: 22px; + .ellipsisIcon { + font-weight: 400; + } + } + } + + .feedContent { + img { + width: 100%; + } + + .reactionsBox { + @include flex-center; + justify-content: space-between; + padding: 5px 10px; + + .reactionsLeft { + display: flex; + + .leftButton { + @include button; + font-size: 22px; + margin-right: 10px; + + .fa-external-link-alt { + font-size: 20px; + } + } + } + + .reactionsRight { + .rightButton { + @include button; + font-size: 22px; + } + } + } + + h2 { + display: flex; + align-items: center; + padding: 0 10px 10px; + font-size: 16px; + font-weight: normal; + + .feedConUser { + @include userProfile; + margin-right: 5px; + background-image: url('http://localhost:3000/images/soojeongLee/user7.jpg'); + background-size: cover; + } + + .likeCount { + @include button; + margin-left: 4px; + font-size: 16px; + } + } + + .feedText { + margin-bottom: 10px; + padding: 0 10px; + + .postTime { + @include postTime; + margin-top: 5px; + font-size: 14px; + font-weight: bold; + } + } + } + + .feedFooter { + background-color: #fff; + border-top: 1px solid $border-color; + + .inputBox { + display: flex; + padding: 10px; + + .comment { + flex: 9; + border: none; + outline: none; + } + + .commentSubmit { + @include button; + flex: 1; + &:hover { + color: $summit-color; + } + } + } + } +}
Unknown
์ด๋Ÿฐ scss ๊ธฐ๋Šฅ ์ €๋Š” ํ—ท๊ฐˆ๋ ค์„œ ์ž˜ ๋ชปํ•˜๊ฒ ๋˜๋ฐ ์ž˜ํ•˜์‹œ๋„ค์š”..
@@ -0,0 +1,23 @@ +//๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +//์ปดํฌ๋„ŒํŠธ +import { FOOTLIST } from '../FootLists/footData'; + +// css +import './FootLists.scss'; + +export class FootLists extends Component { + render() { + return FOOTLIST.map(footlists => { + return ( + <Link to="" className="listDot" key={footlists.id}> + {footlists.footlist} + </Link> + ); + }); + } +} + +export default FootLists;
JavaScript
๋Œ“๊ธ€ ํ”„๋ž์Šค!
@@ -0,0 +1,46 @@ +export const FOOTLIST = [ + { + id: 1, + footlist: '์†Œ๊ฐœ', + }, + { + id: 2, + footlist: '๋„์›€๋ง', + }, + { + id: 3, + footlist: 'ํ™๋ณด ์„ผํ„ฐ', + }, + { + id: 4, + footlist: 'API', + }, + { + id: 5, + footlist: '์ฑ„์šฉ ์ •๋ณด', + }, + { + id: 6, + footlist: '๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ', + }, + { + id: 7, + footlist: '์•ฝ๊ด€', + }, + { + id: 8, + footlist: '์œ„์น˜', + }, + { + id: 9, + footlist: '์ธ๊ธฐ ๊ณ„์ •', + }, + { + id: 10, + footlist: 'ํ•ด์‹œํƒœ๊ทธ', + }, + { + id: 11, + footlist: '์–ธ์–ด', + }, +];
JavaScript
ํ™”์ดํŒ… ํ‘ธํ„ฐ ๋งŒ๋“ค์–ด์„œ ๊ฐ•์˜ ๋ถ€ํƒ๋“œ๋ ค์š”
@@ -1,14 +1,148 @@ -// ํ•„์ˆ˜ import React from 'react'; +import { Link } from 'react-router-dom'; -// ์ปดํฌ๋„ŒํŠธ import Nav from '../../../components/Nav/Nav'; +import Feed from '../Main/Feed/Feed'; +import OtherUserPro from './OtherUserPro/OtherUserPro'; +import FootLists from '../Main/Feed/FootLists/FootLists'; + +import './Main.scss'; class Main extends React.Component { + constructor() { + super(); + this.state = { + feedList: [], + }; + } + + componentDidMount() { + fetch('/data/soojeonglee/feedData.json') + .then(res => res.json()) + .then(data => { + this.setState({ + feedList: data, + }); + }); + } + render() { + // console.log(this.state.feedList); // ๋ฐ์ดํ„ฐ ํ™•์ธ์„ ์œ„ํ•œ ์ฝ˜์†” + const { feedList } = this.state; return ( - <div> + <div className="Main"> <Nav /> + <main> + <div className="feeds"> + {feedList.map(feed => { + return ( + <Feed + key={feed.id} + userName={feed.userName} + src={feed.src} + feedText={feed.feedText} + commentData={feed.commentData} + isLike={feed.isLike} + /> + ); + })} + </div> + <div className="main-right"> + <header className="userAccount"> + <h1> + <Link to="" className="userProfile mainRightProfile"></Link> + <Link to=""> + <strong>wecode_bootcamp</strong> + <span className="accountDec">WeCode - ์œ„์ฝ”๋“œ</span> + </Link> + </h1> + </header> + <aside className="asideBox storyAside"> + <header> + <h2>์Šคํ† ๋ฆฌ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + <ul className="storyUser"> + <OtherUserPro /> + </ul> + </aside> + + <aside className="asideBox recommandAside"> + <header> + <h2>ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + + <ul className="recommandUser"> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user5.jpg" + /> + </Link> + <Link to=""> + limpack_official + <span className="followReco">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user6.jpg" + /> + </Link> + <Link to=""> + les_photos_de_cat + <span className="followReco"> + geee____nie๋‹˜ ์™ธ 1๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค. + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user7.jpg" + /> + </Link> + <Link to=""> + mornstar_nail + <span className="followReco"> + effie_yxz๋‹˜ ์™ธ 3๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + </ul> + </aside> + <footer className="main-right-footer"> + <ul className="footList"> + <FootLists /> + </ul> + <span className="copyright">ยฉ 2021 INSTAGRAM FROM FACEBOOK</span> + </footer> + </div> + </main> </div> ); }
JavaScript
๋ฐ์ดํ„ฐ ํ™•์ธ ๋‹ค ํ•˜์…จ๋‚˜์š”~~?
@@ -1,14 +1,148 @@ -// ํ•„์ˆ˜ import React from 'react'; +import { Link } from 'react-router-dom'; -// ์ปดํฌ๋„ŒํŠธ import Nav from '../../../components/Nav/Nav'; +import Feed from '../Main/Feed/Feed'; +import OtherUserPro from './OtherUserPro/OtherUserPro'; +import FootLists from '../Main/Feed/FootLists/FootLists'; + +import './Main.scss'; class Main extends React.Component { + constructor() { + super(); + this.state = { + feedList: [], + }; + } + + componentDidMount() { + fetch('/data/soojeonglee/feedData.json') + .then(res => res.json()) + .then(data => { + this.setState({ + feedList: data, + }); + }); + } + render() { + // console.log(this.state.feedList); // ๋ฐ์ดํ„ฐ ํ™•์ธ์„ ์œ„ํ•œ ์ฝ˜์†” + const { feedList } = this.state; return ( - <div> + <div className="Main"> <Nav /> + <main> + <div className="feeds"> + {feedList.map(feed => { + return ( + <Feed + key={feed.id} + userName={feed.userName} + src={feed.src} + feedText={feed.feedText} + commentData={feed.commentData} + isLike={feed.isLike} + /> + ); + })} + </div> + <div className="main-right"> + <header className="userAccount"> + <h1> + <Link to="" className="userProfile mainRightProfile"></Link> + <Link to=""> + <strong>wecode_bootcamp</strong> + <span className="accountDec">WeCode - ์œ„์ฝ”๋“œ</span> + </Link> + </h1> + </header> + <aside className="asideBox storyAside"> + <header> + <h2>์Šคํ† ๋ฆฌ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + <ul className="storyUser"> + <OtherUserPro /> + </ul> + </aside> + + <aside className="asideBox recommandAside"> + <header> + <h2>ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + + <ul className="recommandUser"> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user5.jpg" + /> + </Link> + <Link to=""> + limpack_official + <span className="followReco">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user6.jpg" + /> + </Link> + <Link to=""> + les_photos_de_cat + <span className="followReco"> + geee____nie๋‹˜ ์™ธ 1๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค. + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user7.jpg" + /> + </Link> + <Link to=""> + mornstar_nail + <span className="followReco"> + effie_yxz๋‹˜ ์™ธ 3๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + </ul> + </aside> + <footer className="main-right-footer"> + <ul className="footList"> + <FootLists /> + </ul> + <span className="copyright">ยฉ 2021 INSTAGRAM FROM FACEBOOK</span> + </footer> + </div> + </main> </div> ); }
JavaScript
ํ”ผ์–ด ๋ฆฌ๋ทฐ ์ค‘ Return์˜ ๋น„๋ฐ€์„? ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
@@ -1,8 +1,79 @@ import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends Component { + constructor() { + super(); + this.state = { + id: '', + pw: '', + }; + } + + handleInput = event => { + const { name, value } = event.target; + this.setState({ + [name]: value, + }); + }; + + goToMain = () => { + this.props.history.push('/main-Soojeong#'); + }; + render() { - return <div>๋กœ๊ทธ์ธ</div>; + const isValid = + this.state.id.includes('@') && + this.state.id.length >= 5 && + this.state.pw.length >= 8; + + return ( + <div className="Login"> + <main> + <header> + <h1>Westagram</h1> + </header> + + <section className="loginInputBox"> + <h2 className="sr-only">login page</h2> + <form> + <input + name="id" + autoComplete="off" + onChange={this.handleInput} + type="text" + id="loginId" + value={this.state.id} + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + name="pw" + onChange={this.handleInput} + type="password" + value={this.state.pw} + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button + onClick={this.goToMain} + type="button" + className="loginBtn" + disabled={!isValid} + > + ๋กœ๊ทธ์ธ + </button> + </form> + </section> + + <footer> + <Link to="" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </Link> + </footer> + </main> + </div> + ); } }
JavaScript
Boolean ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ ์šฉ์‹œ์ผœ๋ณด์•˜์•„์š”. ์ž˜ ์„ธ์› ๋˜ ์ฝ”๋“œ๊ฐ€ ์ž‘๋™๋˜์ง€ ์•Š์„๊นŒ ์‹œ๋„ํ•˜๊ธฐ ์–ด๋ ค์› ๋Š”๋ฐ ๊ฒฉ๋ ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹น~
@@ -0,0 +1,61 @@ +.Login { + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + + main { + display: flex; + flex-direction: column; + justify-content: space-around; + width: 350px; + height: 380px; + padding: 40px; + border: 1px solid #ccc; + + header { + h1 { + font-family: 'Lobster', cursive; + text-align: center; + } + } + + .loginInputBox { + form { + display: flex; + flex-direction: column; + justify-content: center; + + input { + margin-bottom: 5px; + padding: 9px 8px 7px; + background-color: rgb(250, 250, 250); + border: 1px solid rgb(38, 38, 38); + border-radius: 3px; + } + + .loginBtn { + margin-top: 10px; + padding: 9px 8px 7px; + border: none; + border-radius: 3px; + color: #fff; + background-color: #0096f6; + &:disabled { + background-color: #c0dffd; + } + } + } + } + + footer { + .findPassword { + display: block; + margin-top: 50px; + text-align: center; + color: #00376b; + font-size: 14px; + } + } + } +}
Unknown
๊ธฐ์กด html ์ฝ”๋“œ๋ฅผ ์˜ฎ๊ธฐ๋‹ค ๋ณด๋‹ˆ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์˜€์–ด์š” :) ํ•ด๊ฒฐ๋˜์—ˆ๋‹ต๋‹ˆ๋‹น !!
@@ -0,0 +1,134 @@ +[ + { + "id": 1, + "userName": "eessoo__", + "src": "../images/soojeongLee/user2.jpg", + "feedText": "๐Ÿ’Ÿ", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "soojeonglee", + "content": "Hi there.", + "commnetLike": false + }, + { + "id": 3, + "userName": "jaehyunlee", + "content": "Hey.", + "commnetLike": false + }, + { + "id": 4, + "userName": "gyeongminlee", + "content": "Hi.", + "commnetLike": true + } + ], + "isLike": false + }, + { + "id": 2, + "userName": "zz_ing94", + "src": "../../images/soojeongLee/user3.jpg", + "feedText": "Toy story ๐Ÿ’š", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "jisuoh", + "content": "โœจ", + "commnetLike": true + }, + { + "id": 3, + "userName": "jungjunsung", + "content": "coffee", + "commnetLike": false + }, + { + "id": 4, + "userName": "somihwang", + "content": "ํฌํ‚ค", + "commnetLike": true + } + ], + "isLike": false + }, + { + "id": 3, + "userName": "hwayoonci", + "src": "../../images/soojeongLee/user4.jpg", + "feedText": "์˜ค๋Š˜์˜ ์ผ๊ธฐ", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "yunkyunglee", + "content": "๊ท€์—ฌ์šด ์Šคํ‹ฐ์ปค", + "commnetLike": true + }, + { + "id": 3, + "userName": "summer", + "content": "๐Ÿถ", + "commnetLike": false + }, + { + "id": 4, + "userName": "uiyeonlee", + "content": "๐Ÿ‘๐Ÿป", + "commnetLike": true + } + ], + "isLike": true + }, + { + "id": 4, + "userName": "cosz_zy", + "src": "../../images/soojeongLee/user5.jpg", + "feedText": "1์ผ ํ™”๊ฐ€ ๐ŸŽจ ", + "commentData": [ + { + "id": 1, + "userName": "wecode", + "content": "Welcome to world best coding bootcamp!", + "commnetLike": false + }, + { + "id": 2, + "userName": "soojeonglee", + "content": "Hi there.", + "commnetLike": false + }, + { + "id": 3, + "userName": "jaehyunlee", + "content": "Hey.", + "commnetLike": true + }, + { + "id": 4, + "userName": "gyeongminlee", + "content": "Hi.", + "commnetLike": true + } + ], + "isLike": false + } +]
Unknown
fetching ์„ ์—ฐ์Šตํ•ด๋ณด๋ ค๊ณ  ๋งŒ๋“ค์–ด๋ณด์•˜์–ด์š”
@@ -0,0 +1,175 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import Comment from './Comment/Comment'; + +import '../Feed/Feed.scss'; + +export class Feed extends Component { + constructor() { + super(); + this.state = { + comment: '', + // commentList: [], + // isLike: false, + }; + } + + // componentDidMount() { + // this.setState({ + // commentList: this.props.commentData, + // isLike: this.props.isLike, + // }); + // } + + hadComment = event => { + this.setState({ + comment: event.target.value, + }); + }; + + // ํด๋ฆญํ•จ์ˆ˜ + submitComent = () => { + // this.setState({ + // commentList: this.state.commentList.concat([ + // { + // id: this.state.commentList.length + 1, + // userName: 'eessoo__', + // content: this.state.comment, + // commentLike: false, + // }, + // ]), + // comment: '', + // }); + }; + + handleKeyPress = event => { + if (event.key === 'Enter') { + event.preventDefault(); + this.submitComent(); + } + }; + + handleLike = () => { + this.setState({ + isLike: !this.state.isLike, + }); + }; + + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete = id => { + // const newCommentList = this.state.commentList.filter(comment => { + // return comment.id !== id; + // }); + + // this.setState({ + // commentList: newCommentList, + // }); + // }; + + render() { + return ( + <article> + <header> + <h1> + <Link className="contentHeader"></Link> + <Link className="userName">{this.props.userName}</Link> + </h1> + <button className="ellipsis" type="button"> + <i className="fas fa-ellipsis-h ellipsisIcon"></i> + </button> + </header> + <section className="feedContent"> + <img alt="feedImage" src={this.props.src} /> + <ul className="reactionsBox"> + <li className="reactionsLeft"> + <button + className="leftButton" + type="button" + onClick={this.handleLike} + > + {this.state.isLike ? ( + <i className="fas fa-heart" /> + ) : ( + <i className="far fa-heart" /> + )} + </button> + <button className="leftButton" type="button"> + <i className="far fa-comment" /> + </button> + <button className="leftButton" type="button"> + <i className="fas fa-external-link-alt" /> + </button> + </li> + <li className="reactionsRight"> + <button className="rightButton"> + <i className="far fa-bookmark" /> + </button> + </li> + </ul> + <h2> + <Link className="userProfile feedConUser"></Link> + <b>eessoo__</b>๋‹˜ ์™ธ + <button className="likeCount"> + <b> 7๋ช…</b> + </button> + ์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </h2> + <p className="feedText"> + {this.props.feedText} + <span className="postTime">54๋ถ„ ์ „</span> + </p> + <ul id="commnetBox"> + {/* {this.state.commentList.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} */} + {this.props.commentData.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} + </ul> + </section> + <footer className="feedFooter"> + <form className="inputBox" onKeyPress={this.handleKeyPress}> + <input + autoComplete="off" + onChange={this.hadComment} + className="comment" + value={this.state.comment} + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + /> + <button + onClick={this.submitComent} + className="commentSubmit" + type="button" + > + ๊ฒŒ์‹œ + </button> + </form> + </footer> + </article> + ); + } +} + +export default Feed;
JavaScript
์ธ์Šคํƒ€๊ทธ๋žจ์—์„œ ์‹ค์ œ ์ข‹์•„์š”๋ฅผ ์ข‹์•„ํ•œ ์ˆซ์ž์— ์ ‘๊ทผํ•˜๊ฒŒ ๋˜๋ฉด ๋ˆ„๊ฐ€ ์ข‹์•„์š”๋ฅผ ๋ˆŒ๋ €๋Š”์ง€ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ, ์ถ”ํ›„์— ๊ธฐ๋Šฅ์„ ๋„ฃ๊ฒŒ ๋œ๋‹ค๋ฉด button์„ ๋„ฃ์–ด์•ผ ํ•˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ๋„ฃ์–ด๋ดค์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,23 @@ +//๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +//์ปดํฌ๋„ŒํŠธ +import { FOOTLIST } from '../FootLists/footData'; + +// css +import './FootLists.scss'; + +export class FootLists extends Component { + render() { + return FOOTLIST.map(footlists => { + return ( + <Link to="" className="listDot" key={footlists.id}> + {footlists.footlist} + </Link> + ); + }); + } +} + +export default FootLists;
JavaScript
๊ผญ ํ’‹ ๋ฆฌ์ŠคํŠธ๋ฅผ... ์ปดํฌ๋„ŒํŠธํ™”ํ•ด๋ณด๊ฒ ์–ด์š”...!!!!
@@ -1,14 +1,148 @@ -// ํ•„์ˆ˜ import React from 'react'; +import { Link } from 'react-router-dom'; -// ์ปดํฌ๋„ŒํŠธ import Nav from '../../../components/Nav/Nav'; +import Feed from '../Main/Feed/Feed'; +import OtherUserPro from './OtherUserPro/OtherUserPro'; +import FootLists from '../Main/Feed/FootLists/FootLists'; + +import './Main.scss'; class Main extends React.Component { + constructor() { + super(); + this.state = { + feedList: [], + }; + } + + componentDidMount() { + fetch('/data/soojeonglee/feedData.json') + .then(res => res.json()) + .then(data => { + this.setState({ + feedList: data, + }); + }); + } + render() { + // console.log(this.state.feedList); // ๋ฐ์ดํ„ฐ ํ™•์ธ์„ ์œ„ํ•œ ์ฝ˜์†” + const { feedList } = this.state; return ( - <div> + <div className="Main"> <Nav /> + <main> + <div className="feeds"> + {feedList.map(feed => { + return ( + <Feed + key={feed.id} + userName={feed.userName} + src={feed.src} + feedText={feed.feedText} + commentData={feed.commentData} + isLike={feed.isLike} + /> + ); + })} + </div> + <div className="main-right"> + <header className="userAccount"> + <h1> + <Link to="" className="userProfile mainRightProfile"></Link> + <Link to=""> + <strong>wecode_bootcamp</strong> + <span className="accountDec">WeCode - ์œ„์ฝ”๋“œ</span> + </Link> + </h1> + </header> + <aside className="asideBox storyAside"> + <header> + <h2>์Šคํ† ๋ฆฌ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + <ul className="storyUser"> + <OtherUserPro /> + </ul> + </aside> + + <aside className="asideBox recommandAside"> + <header> + <h2>ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + + <ul className="recommandUser"> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user5.jpg" + /> + </Link> + <Link to=""> + limpack_official + <span className="followReco">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user6.jpg" + /> + </Link> + <Link to=""> + les_photos_de_cat + <span className="followReco"> + geee____nie๋‹˜ ์™ธ 1๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค. + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user7.jpg" + /> + </Link> + <Link to=""> + mornstar_nail + <span className="followReco"> + effie_yxz๋‹˜ ์™ธ 3๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + </ul> + </aside> + <footer className="main-right-footer"> + <ul className="footList"> + <FootLists /> + </ul> + <span className="copyright">ยฉ 2021 INSTAGRAM FROM FACEBOOK</span> + </footer> + </div> + </main> </div> ); }
JavaScript
๋„ค..! ๋ฌด์—‡์ด ์–ด๋””์— ์ „๋‹ฌ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์„œ ๋„ฃ์–ด๋’€์–ด์š” ...!
@@ -1,14 +1,148 @@ -// ํ•„์ˆ˜ import React from 'react'; +import { Link } from 'react-router-dom'; -// ์ปดํฌ๋„ŒํŠธ import Nav from '../../../components/Nav/Nav'; +import Feed from '../Main/Feed/Feed'; +import OtherUserPro from './OtherUserPro/OtherUserPro'; +import FootLists from '../Main/Feed/FootLists/FootLists'; + +import './Main.scss'; class Main extends React.Component { + constructor() { + super(); + this.state = { + feedList: [], + }; + } + + componentDidMount() { + fetch('/data/soojeonglee/feedData.json') + .then(res => res.json()) + .then(data => { + this.setState({ + feedList: data, + }); + }); + } + render() { + // console.log(this.state.feedList); // ๋ฐ์ดํ„ฐ ํ™•์ธ์„ ์œ„ํ•œ ์ฝ˜์†” + const { feedList } = this.state; return ( - <div> + <div className="Main"> <Nav /> + <main> + <div className="feeds"> + {feedList.map(feed => { + return ( + <Feed + key={feed.id} + userName={feed.userName} + src={feed.src} + feedText={feed.feedText} + commentData={feed.commentData} + isLike={feed.isLike} + /> + ); + })} + </div> + <div className="main-right"> + <header className="userAccount"> + <h1> + <Link to="" className="userProfile mainRightProfile"></Link> + <Link to=""> + <strong>wecode_bootcamp</strong> + <span className="accountDec">WeCode - ์œ„์ฝ”๋“œ</span> + </Link> + </h1> + </header> + <aside className="asideBox storyAside"> + <header> + <h2>์Šคํ† ๋ฆฌ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + <ul className="storyUser"> + <OtherUserPro /> + </ul> + </aside> + + <aside className="asideBox recommandAside"> + <header> + <h2>ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</h2> + <button className="btnTextColor" type="button"> + ๋ชจ๋‘ ๋ณด๊ธฐ + </button> + </header> + + <ul className="recommandUser"> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user5.jpg" + /> + </Link> + <Link to=""> + limpack_official + <span className="followReco">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user6.jpg" + /> + </Link> + <Link to=""> + les_photos_de_cat + <span className="followReco"> + geee____nie๋‹˜ ์™ธ 1๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค. + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + <li className="otherUserProfile"> + <span className="profileBox"> + <Link to="" className="otherUserImg"> + <img + alt="User Profile" + src="images/soojeongLee/user7.jpg" + /> + </Link> + <Link to=""> + mornstar_nail + <span className="followReco"> + effie_yxz๋‹˜ ์™ธ 3๋ช…์ด ํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค + </span> + </Link> + </span> + <button className="follow" type="button"> + ํŒ”๋กœ์šฐ + </button> + </li> + </ul> + </aside> + <footer className="main-right-footer"> + <ul className="footList"> + <FootLists /> + </ul> + <span className="copyright">ยฉ 2021 INSTAGRAM FROM FACEBOOK</span> + </footer> + </div> + </main> </div> ); }
JavaScript
ใ…Žใ…Žใ…Ž Return ์€ ์ƒ๋žต๋  ์ˆ˜ ์žˆ๋‹ค ~
@@ -1,8 +1,79 @@ import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends Component { + constructor() { + super(); + this.state = { + id: '', + pw: '', + }; + } + + handleInput = event => { + const { name, value } = event.target; + this.setState({ + [name]: value, + }); + }; + + goToMain = () => { + this.props.history.push('/main-Soojeong#'); + }; + render() { - return <div>๋กœ๊ทธ์ธ</div>; + const isValid = + this.state.id.includes('@') && + this.state.id.length >= 5 && + this.state.pw.length >= 8; + + return ( + <div className="Login"> + <main> + <header> + <h1>Westagram</h1> + </header> + + <section className="loginInputBox"> + <h2 className="sr-only">login page</h2> + <form> + <input + name="id" + autoComplete="off" + onChange={this.handleInput} + type="text" + id="loginId" + value={this.state.id} + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + name="pw" + onChange={this.handleInput} + type="password" + value={this.state.pw} + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button + onClick={this.goToMain} + type="button" + className="loginBtn" + disabled={!isValid} + > + ๋กœ๊ทธ์ธ + </button> + </form> + </section> + + <footer> + <Link to="" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </Link> + </footer> + </main> + </div> + ); } }
JavaScript
๊ณ„์‚ฐ๋œ์†์„ฑ๋ช… ๊ตฟ
@@ -0,0 +1,175 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import Comment from './Comment/Comment'; + +import '../Feed/Feed.scss'; + +export class Feed extends Component { + constructor() { + super(); + this.state = { + comment: '', + // commentList: [], + // isLike: false, + }; + } + + // componentDidMount() { + // this.setState({ + // commentList: this.props.commentData, + // isLike: this.props.isLike, + // }); + // } + + hadComment = event => { + this.setState({ + comment: event.target.value, + }); + }; + + // ํด๋ฆญํ•จ์ˆ˜ + submitComent = () => { + // this.setState({ + // commentList: this.state.commentList.concat([ + // { + // id: this.state.commentList.length + 1, + // userName: 'eessoo__', + // content: this.state.comment, + // commentLike: false, + // }, + // ]), + // comment: '', + // }); + }; + + handleKeyPress = event => { + if (event.key === 'Enter') { + event.preventDefault(); + this.submitComent(); + } + }; + + handleLike = () => { + this.setState({ + isLike: !this.state.isLike, + }); + }; + + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete = id => { + // const newCommentList = this.state.commentList.filter(comment => { + // return comment.id !== id; + // }); + + // this.setState({ + // commentList: newCommentList, + // }); + // }; + + render() { + return ( + <article> + <header> + <h1> + <Link className="contentHeader"></Link> + <Link className="userName">{this.props.userName}</Link> + </h1> + <button className="ellipsis" type="button"> + <i className="fas fa-ellipsis-h ellipsisIcon"></i> + </button> + </header> + <section className="feedContent"> + <img alt="feedImage" src={this.props.src} /> + <ul className="reactionsBox"> + <li className="reactionsLeft"> + <button + className="leftButton" + type="button" + onClick={this.handleLike} + > + {this.state.isLike ? ( + <i className="fas fa-heart" /> + ) : ( + <i className="far fa-heart" /> + )} + </button> + <button className="leftButton" type="button"> + <i className="far fa-comment" /> + </button> + <button className="leftButton" type="button"> + <i className="fas fa-external-link-alt" /> + </button> + </li> + <li className="reactionsRight"> + <button className="rightButton"> + <i className="far fa-bookmark" /> + </button> + </li> + </ul> + <h2> + <Link className="userProfile feedConUser"></Link> + <b>eessoo__</b>๋‹˜ ์™ธ + <button className="likeCount"> + <b> 7๋ช…</b> + </button> + ์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </h2> + <p className="feedText"> + {this.props.feedText} + <span className="postTime">54๋ถ„ ์ „</span> + </p> + <ul id="commnetBox"> + {/* {this.state.commentList.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} */} + {this.props.commentData.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} + </ul> + </section> + <footer className="feedFooter"> + <form className="inputBox" onKeyPress={this.handleKeyPress}> + <input + autoComplete="off" + onChange={this.hadComment} + className="comment" + value={this.state.comment} + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + /> + <button + onClick={this.submitComent} + className="commentSubmit" + type="button" + > + ๊ฒŒ์‹œ + </button> + </form> + </footer> + </article> + ); + } +} + +export default Feed;
JavaScript
์˜ค....
@@ -0,0 +1,175 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import Comment from './Comment/Comment'; + +import '../Feed/Feed.scss'; + +export class Feed extends Component { + constructor() { + super(); + this.state = { + comment: '', + // commentList: [], + // isLike: false, + }; + } + + // componentDidMount() { + // this.setState({ + // commentList: this.props.commentData, + // isLike: this.props.isLike, + // }); + // } + + hadComment = event => { + this.setState({ + comment: event.target.value, + }); + }; + + // ํด๋ฆญํ•จ์ˆ˜ + submitComent = () => { + // this.setState({ + // commentList: this.state.commentList.concat([ + // { + // id: this.state.commentList.length + 1, + // userName: 'eessoo__', + // content: this.state.comment, + // commentLike: false, + // }, + // ]), + // comment: '', + // }); + }; + + handleKeyPress = event => { + if (event.key === 'Enter') { + event.preventDefault(); + this.submitComent(); + } + }; + + handleLike = () => { + this.setState({ + isLike: !this.state.isLike, + }); + }; + + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete = id => { + // const newCommentList = this.state.commentList.filter(comment => { + // return comment.id !== id; + // }); + + // this.setState({ + // commentList: newCommentList, + // }); + // }; + + render() { + return ( + <article> + <header> + <h1> + <Link className="contentHeader"></Link> + <Link className="userName">{this.props.userName}</Link> + </h1> + <button className="ellipsis" type="button"> + <i className="fas fa-ellipsis-h ellipsisIcon"></i> + </button> + </header> + <section className="feedContent"> + <img alt="feedImage" src={this.props.src} /> + <ul className="reactionsBox"> + <li className="reactionsLeft"> + <button + className="leftButton" + type="button" + onClick={this.handleLike} + > + {this.state.isLike ? ( + <i className="fas fa-heart" /> + ) : ( + <i className="far fa-heart" /> + )} + </button> + <button className="leftButton" type="button"> + <i className="far fa-comment" /> + </button> + <button className="leftButton" type="button"> + <i className="fas fa-external-link-alt" /> + </button> + </li> + <li className="reactionsRight"> + <button className="rightButton"> + <i className="far fa-bookmark" /> + </button> + </li> + </ul> + <h2> + <Link className="userProfile feedConUser"></Link> + <b>eessoo__</b>๋‹˜ ์™ธ + <button className="likeCount"> + <b> 7๋ช…</b> + </button> + ์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค. + </h2> + <p className="feedText"> + {this.props.feedText} + <span className="postTime">54๋ถ„ ์ „</span> + </p> + <ul id="commnetBox"> + {/* {this.state.commentList.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} */} + {this.props.commentData.map(comment => { + return ( + <Comment + key={comment.id} + userName={comment.userName} + content={comment.content} + commnetLike={comment.commnetLike} + // ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ตฌํ˜„ ์ค‘ + // handleCommnetDelete={this.handleCommnetDelete} + // id={comment.id} + /> + ); + })} + </ul> + </section> + <footer className="feedFooter"> + <form className="inputBox" onKeyPress={this.handleKeyPress}> + <input + autoComplete="off" + onChange={this.hadComment} + className="comment" + value={this.state.comment} + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + /> + <button + onClick={this.submitComent} + className="commentSubmit" + type="button" + > + ๊ฒŒ์‹œ + </button> + </form> + </footer> + </article> + ); + } +} + +export default Feed;
JavaScript
์ด๋Ÿฐ๋ฐฉ๋ฒ•์ด.. ํ”ผ๋“œ๋งˆ๋‹ค ๋Œ“๊ธ€ ๋ฐ์ดํ„ฐ๋ฅผ ์Šคํ…Œ์ดํŠธ๋กœ ํฌํ•จํ•˜๊ฒŒ ํ•ด์ฃผ๋‹ˆ ๋ณด๊ธฐ ์‰ฝ๊ณ  ์ข‹๋„ค์š”
@@ -1,8 +1,79 @@ import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends Component { + constructor() { + super(); + this.state = { + id: '', + pw: '', + }; + } + + handleInput = event => { + const { name, value } = event.target; + this.setState({ + [name]: value, + }); + }; + + goToMain = () => { + this.props.history.push('/main-Soojeong#'); + }; + render() { - return <div>๋กœ๊ทธ์ธ</div>; + const isValid = + this.state.id.includes('@') && + this.state.id.length >= 5 && + this.state.pw.length >= 8; + + return ( + <div className="Login"> + <main> + <header> + <h1>Westagram</h1> + </header> + + <section className="loginInputBox"> + <h2 className="sr-only">login page</h2> + <form> + <input + name="id" + autoComplete="off" + onChange={this.handleInput} + type="text" + id="loginId" + value={this.state.id} + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + name="pw" + onChange={this.handleInput} + type="password" + value={this.state.pw} + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button + onClick={this.goToMain} + type="button" + className="loginBtn" + disabled={!isValid} + > + ๋กœ๊ทธ์ธ + </button> + </form> + </section> + + <footer> + <Link to="" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </Link> + </footer> + </main> + </div> + ); } }
JavaScript
์ด๋Ÿฐ ์ฃผ์„๋“ค์€ ๋ถˆํ•„์š”ํ•˜๋‹ˆ๊นŒ ์‚ญ์ œํ•ด์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์•„๋ณด์ž…๋‹ˆ๋‹ค!
@@ -1,8 +1,79 @@ import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends Component { + constructor() { + super(); + this.state = { + id: '', + pw: '', + }; + } + + handleInput = event => { + const { name, value } = event.target; + this.setState({ + [name]: value, + }); + }; + + goToMain = () => { + this.props.history.push('/main-Soojeong#'); + }; + render() { - return <div>๋กœ๊ทธ์ธ</div>; + const isValid = + this.state.id.includes('@') && + this.state.id.length >= 5 && + this.state.pw.length >= 8; + + return ( + <div className="Login"> + <main> + <header> + <h1>Westagram</h1> + </header> + + <section className="loginInputBox"> + <h2 className="sr-only">login page</h2> + <form> + <input + name="id" + autoComplete="off" + onChange={this.handleInput} + type="text" + id="loginId" + value={this.state.id} + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + name="pw" + onChange={this.handleInput} + type="password" + value={this.state.pw} + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button + onClick={this.goToMain} + type="button" + className="loginBtn" + disabled={!isValid} + > + ๋กœ๊ทธ์ธ + </button> + </form> + </section> + + <footer> + <Link to="" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </Link> + </footer> + </main> + </div> + ); } }
JavaScript
event.target ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋Š”๋ฐ ๊ตฌ์กฐ๋ถ„ํ•ดํ•ด์„œ ํ™œ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”! ```const {name,value} = event.target;```
@@ -0,0 +1,40 @@ +package nextstep.security.authentication; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import nextstep.security.core.Authentication; +import nextstep.security.core.context.SecurityContextHolder; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.web.authentication.AuthenticationConverter; +import nextstep.security.web.authentication.BasicAuthenticationConverter; +import org.springframework.web.filter.OncePerRequestFilter; + +public class BasicAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter(); + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { + try { + Authentication authRequest = authenticationConverter.convert(request); + + if (authRequest != null) { + Authentication authResult = authenticationManager.authenticate(authRequest); + SecurityContextHolder.getContext().setAuthentication(authResult); + } + + filterChain.doFilter(request, response); + } catch (AuthenticationException e) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + } +}
Java
BasicAuthenticationFilter ํ•„ํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ UserDetailsService ๋Œ€์‹  AuthenticationManager๋ฅผ ์ฃผ์ž… ๋ฐ›์œผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,35 @@ +package nextstep.security.authentication; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; +import nextstep.security.core.Authentication; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.exception.AuthenticationServiceException; +import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter; + +public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + private static final String DEFAULT_REQUEST_URI = "/login"; + + public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) { + super(DEFAULT_REQUEST_URI, authenticationManager); + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException, IOException, ServletException { + if (!"POST".equalsIgnoreCase(request.getMethod())) { + throw new AuthenticationServiceException(); + } + Map<String, String[]> parameterMap = request.getParameterMap(); + String username = parameterMap.get("username")[0]; + String password = parameterMap.get("password")[0]; + + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken( + username, password, false); + + return this.getAuthenticationManager().authenticate(authRequest); + } +}
Java
์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ UserDetailsService ๋Œ€์‹  AuthenticationManager๋ฅผ ์ฃผ์ž… ๋ฐ›์œผ๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,40 @@ +package nextstep.security.authentication; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import nextstep.security.core.Authentication; +import nextstep.security.core.context.SecurityContextHolder; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.web.authentication.AuthenticationConverter; +import nextstep.security.web.authentication.BasicAuthenticationConverter; +import org.springframework.web.filter.OncePerRequestFilter; + +public class BasicAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter(); + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { + try { + Authentication authRequest = authenticationConverter.convert(request); + + if (authRequest != null) { + Authentication authResult = authenticationManager.authenticate(authRequest); + SecurityContextHolder.getContext().setAuthentication(authResult); + } + + filterChain.doFilter(request, response); + } catch (AuthenticationException e) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + } +}
Java
Authentication ์ƒ์„ฑ์˜ ์ฑ…์ž„์„ ๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,40 @@ +package nextstep.security.authentication; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import nextstep.security.core.Authentication; +import nextstep.security.core.context.SecurityContextHolder; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.web.authentication.AuthenticationConverter; +import nextstep.security.web.authentication.BasicAuthenticationConverter; +import org.springframework.web.filter.OncePerRequestFilter; + +public class BasicAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter(); + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { + try { + Authentication authRequest = authenticationConverter.convert(request); + + if (authRequest != null) { + Authentication authResult = authenticationManager.authenticate(authRequest); + SecurityContextHolder.getContext().setAuthentication(authResult); + } + + filterChain.doFilter(request, response); + } catch (AuthenticationException e) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + } +}
Java
convert ๋ฉ”์„œ๋“œ๋Š” Token์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ์—ญํ• ๋งŒ ํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,35 @@ +package nextstep.security.authentication; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; +import nextstep.security.core.Authentication; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.exception.AuthenticationServiceException; +import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter; + +public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + private static final String DEFAULT_REQUEST_URI = "/login"; + + public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) { + super(DEFAULT_REQUEST_URI, authenticationManager); + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException, IOException, ServletException { + if (!"POST".equalsIgnoreCase(request.getMethod())) { + throw new AuthenticationServiceException(); + } + Map<String, String[]> parameterMap = request.getParameterMap(); + String username = parameterMap.get("username")[0]; + String password = parameterMap.get("password")[0]; + + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken( + username, password, false); + + return this.getAuthenticationManager().authenticate(authRequest); + } +}
Java
์‹œํ๋ฆฌํ‹ฐ์˜ ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์ธ์ฆ ํ•„ํ„ฐ์—์„œ ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ AbstractAuthenticationProcessingFilter๋กœ ๋ถ„๋ฆฌ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค! UsernamePasswordAuthenticationFilter์—์„œ ํ•ด๋‹น ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•ด๋ณด๋Š” ์‹œ๋„๋ฅผ ํ•ด๋ณด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”? ํ•„์ˆ˜ ์š”๊ตฌ์‚ฌํ•ญ์€ ์•„๋‹ˆ์ง€๋งŒ ๋น ๋ฅด๊ฒŒ ๋ฏธ์…˜์„ ์ž˜ ํ•ด์ฃผ์…จ๊ณ  ์„ ํƒ ์š”๊ตฌ์‚ฌํ•ญ์œผ๋กœ 3์ฃผ์ฐจ ๋ฏธ์…˜ ์ˆ˜ํ–‰์— ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„์„œ ์ œ์•ˆ๋“œ๋ ค์š”~
@@ -0,0 +1,40 @@ +package nextstep.security.authentication; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import nextstep.security.core.Authentication; +import nextstep.security.core.context.SecurityContextHolder; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.web.authentication.AuthenticationConverter; +import nextstep.security.web.authentication.BasicAuthenticationConverter; +import org.springframework.web.filter.OncePerRequestFilter; + +public class BasicAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter(); + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { + try { + Authentication authRequest = authenticationConverter.convert(request); + + if (authRequest != null) { + Authentication authResult = authenticationManager.authenticate(authRequest); + SecurityContextHolder.getContext().setAuthentication(authResult); + } + + filterChain.doFilter(request, response); + } catch (AuthenticationException e) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + } +}
Java
๊ธฐ์กด ๊ตฌ์กฐ์—์„œ ๊ณ„์† ๋ฐœ์ „์‹œ์ผœ ๋‚˜๊ฐ€๋‹ค ๋ณด๋‹ˆ ๋ฏธ์ฒ˜ ์‹ ๊ฒฝ์“ฐ์ง€ ๋ชปํ•œ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ˜‚ ๊ตฌ์ฒด๊ฐ€ ์•„๋‹Œ ์ถ”์ƒ์— ์˜์กดํ•ด์•ผ ํ•œ๋‹ค๋Š”๊ฑธ ์ƒ๊ธฐ์‹œ์ผœ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ‘
@@ -0,0 +1,35 @@ +package nextstep.security.authentication; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; +import nextstep.security.core.Authentication; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.exception.AuthenticationServiceException; +import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter; + +public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + private static final String DEFAULT_REQUEST_URI = "/login"; + + public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) { + super(DEFAULT_REQUEST_URI, authenticationManager); + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException, IOException, ServletException { + if (!"POST".equalsIgnoreCase(request.getMethod())) { + throw new AuthenticationServiceException(); + } + Map<String, String[]> parameterMap = request.getParameterMap(); + String username = parameterMap.get("username")[0]; + String password = parameterMap.get("password")[0]; + + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken( + username, password, false); + + return this.getAuthenticationManager().authenticate(authRequest); + } +}
Java
๋งˆ์ฐฌ๊ฐ€์ง€๋กœ [fa5f619](https://github.com/next-step/spring-security-authentication/pull/33/commits/fa5f61942bfba0e2f794bb43c5b86af416c679b8) ์ปค๋ฐ‹์œผ๋กœ ๋ฐ˜์˜ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +package nextstep.security.authentication; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import nextstep.security.core.Authentication; +import nextstep.security.core.context.SecurityContextHolder; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.web.authentication.AuthenticationConverter; +import nextstep.security.web.authentication.BasicAuthenticationConverter; +import org.springframework.web.filter.OncePerRequestFilter; + +public class BasicAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter(); + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { + try { + Authentication authRequest = authenticationConverter.convert(request); + + if (authRequest != null) { + Authentication authResult = authenticationManager.authenticate(authRequest); + SecurityContextHolder.getContext().setAuthentication(authResult); + } + + filterChain.doFilter(request, response); + } catch (AuthenticationException e) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + } +}
Java
[81fa490](https://github.com/next-step/spring-security-authentication/pull/33/commits/81fa490d0c5ef702821a1b226a17c3363bb5131a)๋กœ ๋ฐ˜์˜ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +package nextstep.security.authentication; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import nextstep.security.core.Authentication; +import nextstep.security.core.context.SecurityContextHolder; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.web.authentication.AuthenticationConverter; +import nextstep.security.web.authentication.BasicAuthenticationConverter; +import org.springframework.web.filter.OncePerRequestFilter; + +public class BasicAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + private final AuthenticationConverter authenticationConverter = new BasicAuthenticationConverter(); + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws IOException, ServletException { + try { + Authentication authRequest = authenticationConverter.convert(request); + + if (authRequest != null) { + Authentication authResult = authenticationManager.authenticate(authRequest); + SecurityContextHolder.getContext().setAuthentication(authResult); + } + + filterChain.doFilter(request, response); + } catch (AuthenticationException e) { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + } +}
Java
DRY ์œ„๋ฐ˜๋„ ๊ฒธํ•˜๊ณ  ์žˆ์—ˆ๋„ค์š”! ํด๋ž˜์Šค ๋ถ„๋ฆฌํ•˜๊ณ  ๋ณด๋‹ˆ ๊ธฐ์กด ๋ฉ”์„œ๋“œ์— ์ฑ…์ž„์ด ๊ณผ๋„ํ–ˆ๊ณ , ํ•„์š”์น˜ ์•Š์€ ๋”๋ธ”์ฒดํฌ๊ฐ€ ๋“ค์–ด๊ฐ”๋‹ค๋Š”๊ฒŒ ๋ฐ”๋กœ ํ™•์ธ๋˜์–ด์„œ ์ข‹์•˜์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,35 @@ +package nextstep.security.authentication; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; +import nextstep.security.core.Authentication; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.exception.AuthenticationServiceException; +import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter; + +public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + private static final String DEFAULT_REQUEST_URI = "/login"; + + public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) { + super(DEFAULT_REQUEST_URI, authenticationManager); + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException, IOException, ServletException { + if (!"POST".equalsIgnoreCase(request.getMethod())) { + throw new AuthenticationServiceException(); + } + Map<String, String[]> parameterMap = request.getParameterMap(); + String username = parameterMap.get("username")[0]; + String password = parameterMap.get("password")[0]; + + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken( + username, password, false); + + return this.getAuthenticationManager().authenticate(authRequest); + } +}
Java
[dbde31c](https://github.com/next-step/spring-security-authentication/pull/33/commits/dbde31cb10aee34fe4e1c767064389589b2fa2ba)๋กœ ์‹œ๋„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค! ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๋ฉด์„œ ํฅ๋ฏธ๋กœ์› ๋˜์ ์€ SecurityContextRepository.saveContext() ํ˜ธ์ถœ์„ SecurityContextHolderFilter๊ฐ€ ์•„๋‹Œ AbstractAuthenticationProcessingFilter์—์„œ ํ•ด์ฃผ๊ณ  ์žˆ๋‹ค๋Š” ์ ์ด์—ˆ๋Š”๋ฐ์š”. ![image](https://github.com/user-attachments/assets/7fac5996-fe6a-443b-a8d0-1b06fb11d3c5) ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ์œ„์— ๊ทธ๋ฆผ์ƒ 3๋ฒˆ ํ”Œ๋กœ์šฐ๋Š” ์•ˆ๋งž๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€? ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•œ ๊ทธ๋ฆผ์ผ๊นŒ? ์ด๋Ÿฐ ์ €๋Ÿฐ ์ƒ๊ฐ๋“ค์„ ํ•˜๋ฉด์„œ ๋˜๊ฒŒ ์žฌ๋ฏธ์žˆ๊ฒŒ ๋ณธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ใ…Žใ…Žใ…Ž.. ์ œ๊ฐ€ ์ž˜๋ชป ์ดํ•ดํ•œ๊ฑฐ๋ผ๋ฉด ์งš์–ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๋˜ํ•œ AbstractAuthenticationProcessingFilter ๊ตฌํ˜„ํ•˜๋ฉด์„œ RequestMatcher์ด๋‚˜, strategy ๋””์ž์ธ ํŒจํ„ด์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์€ ํ˜„ ์‹œ์ ์—์„œ๋Š” ์˜ค๋ฒ„์—”์ง€๋‹ˆ์–ด๋ง์ด๋ผ๊ณ  ํŒ๋‹จํ•ด ๋„ฃ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค! ์•„๋งˆ ์ด๋Ÿฐ ๋ถ€๋ถ„๊นŒ์ง€๋Š” ๋ฐ”๋ผ์ง€ ์•Š์œผ์…จ์„..๊ฒƒ ๊ฐ™์•„์„œ์š”! ๐Ÿ˜‚
@@ -0,0 +1,35 @@ +package nextstep.security.authentication; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; +import nextstep.security.core.Authentication; +import nextstep.security.exception.AuthenticationException; +import nextstep.security.exception.AuthenticationServiceException; +import nextstep.security.web.authentication.AbstractAuthenticationProcessingFilter; + +public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { + private static final String DEFAULT_REQUEST_URI = "/login"; + + public UsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) { + super(DEFAULT_REQUEST_URI, authenticationManager); + } + + @Override + public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) + throws AuthenticationException, IOException, ServletException { + if (!"POST".equalsIgnoreCase(request.getMethod())) { + throw new AuthenticationServiceException(); + } + Map<String, String[]> parameterMap = request.getParameterMap(); + String username = parameterMap.get("username")[0]; + String password = parameterMap.get("password")[0]; + + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken( + username, password, false); + + return this.getAuthenticationManager().authenticate(authRequest); + } +}
Java
์šฐ์„  ์ค€ํ˜•๋‹˜๊ป˜์„œ ์ฝ”๋“œ๋กœ ํ™•์ธํ•œ ๋ถ€๋ถ„์ด ๋งž์Šต๋‹ˆ๋‹ค! ์ œ๊ฐ€ ๊ฐ•์˜์—์„œ SecurityContextPersistenceFilter์™€ SecurityContextHolderFilter๋Š” ๊ฐ™๊ณ  ๋ฒ„์ „์˜ ์ฐจ์ด๋ผ๊ณ ๋งŒ ๋ง์”€๋“œ๋ ธ๋Š”๋ฐ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค๋Š” ๊ด€์ ์—์„œ ๊ฐ™๋‹ค๊ณ  ๋ง์”€๋“œ๋ ธ๊ณ  ์‚ฌ์‹ค ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. [SecurityContextPersistenceFilter](https://docs.spring.io/spring-security/reference/servlet/authentication/persistence.html#securitycontextpersistencefilter)์˜ ์„ค๋ช…์— ๋ณด๋ฉด repository์— ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ํฌํ•จ์ด์ง€๋งŒ [SecurityContextHolderFilter](https://docs.spring.io/spring-security/reference/servlet/authentication/persistence.html#securitycontextholderfilter)์˜ ์„ค๋ช…์„ ๋ณด๋ฉด ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„์„ ํฌํ•จํ•˜๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
import ์ˆœ์„œ์—๋„ ์œ ์ง€๋ณด์ˆ˜ ๋ฐ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•œ ์ปจ๋ฒค์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์ฝ”๋“œ์˜ ์ปจ๋ฒค์…˜์€ ๊ฐ„๋žตํ•˜๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™๊ณ , ์ฐธ๊ณ ํ•ด์„œ ์ˆ˜์ •ํ•ด ์ฃผ์„ธ์š”! - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ - React ๊ด€๋ จ ํŒจํ‚ค์ง€ - ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ - ์ปดํฌ๋„ŒํŠธ - ๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ โ†’ ๋จผ ์ปดํฌ๋„ŒํŠธ โ†’ ๊ฐ€๊นŒ์šด ์ปดํฌ๋„ŒํŠธ - ํ•จ์ˆ˜, ๋ณ€์ˆ˜ ๋ฐ ์„ค์ • ํŒŒ์ผ - ์‚ฌ์ง„ ๋“ฑ ๋ฏธ๋””์–ด ํŒŒ์ผ(`.png`) - css ํŒŒ์ผ (.`scss`)
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
event ๊ฐ์ฒด์—๋Š” ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ค‘์—์„œ target์˜ value(input์— ์ž…๋ ฅํ•œ ๊ฐ’)๋งŒ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ธ์ž๋กœ ์•„์˜ˆ event.target.value๋งŒ์„ ์ธ์ž๋กœ ๋„˜๊ฒจ์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ด ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ์ด ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ฒ ๋„ค์š”!
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
์‹ค์ˆ˜๋กœ ๋ˆŒ๋ €์„ ๊ฒฝ์šฐ์—๋„ ๋ฌด์กฐ๊ฑด list ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋  ํ…๋ฐ, ์„ ํƒ๊ถŒ์„ ์ค„ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”. `window.confirm` ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด๊ณ  ์ ์šฉํ•ด๋ณด์…”๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
fetch ํ•จ์ˆ˜์˜ ์ธ์ž๋Š” ๋ณดํ†ต 2๊ฐœ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”๋ฐ, ์ง€๊ธˆ์€ ์˜ต์…˜์— ๊ด€๋ จ๋œ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋งŒ ์ž‘์„ฑ๋˜์–ด ์žˆ๋„ค์š”! ์‹ค์ œ๋กœ ํ†ต์‹ ํ•˜์‹ค ๋•Œ์—๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž์ธ `API` ๊ฐ’๋„ ์ž˜ ์ „๋‹ฌํ•ด ์ฃผ์„ธ์š”!
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
๋ถˆํ•„์š”ํ•˜๊ฒŒ 3์ค‘์œผ๋กœ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, ์ผ๋‹จ ์ตœ์ƒ์œ„์˜ div๋Š” ๋ถˆํ•„์š”ํ•ด ๋ณด์ด๋„ค์š”! ์š”์†Œ๊ฐ€ ์ค‘์ฒฉ๋  ์ˆ˜๋ก, ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ ์ˆ˜๋ก ๋กœ๋”ฉ๋˜๋Š” ์‹œ๊ฐ„์ด ๊ธธ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ค‘์ฒฉ๋œ ์š”์†Œ๋Š” ์ง€์›Œ์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
img ํƒœ๊ทธ์˜ alt ๊ฐ’์€ ์–ด๋””์— ํ™œ์šฉ๋ ๊นŒ์š”? ์ฐพ์•„๋ณด์‹œ๊ณ  ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”!
@@ -0,0 +1,79 @@ +.userBox { + margin: 20px; + gap: 0 24px; + display: flex; + justify-content: center; + flex-direction: column; + + .userWrap { + display: flex; + flex-direction: row; + justify-content: center; + + .user { + width: 50px; + height: 50px; + border-radius: 50%; + } + + .contentBox { + display: flex; + flex-direction: column; + gap: 10px; + margin-left: 20px; + + .userName { + font-size: 20px; + font-weight: 700; + } + + .textArea { + padding: 10px; + border-radius: 6px; + border: 1px solid var(--grey-88, #e0e0e0); + background: var(--white, #fff); + resize: none; + transition: border-color 0.3s; + outline: none; + } + .textArea:hover { + border-color: var(--gray, #999999); + } + + .buttonWrap { + justify-content: space-between; + display: flex; + + .customButton { + width: 120px; + height: 50px; + border-radius: 6px; + } + + .cancelButton { + border: 1px solid var(--blue, #2d71f7); + background: var(--white, #fff); + color: var(--blue, #2d71f7); + cursor: pointer; + } + .cancelButton:hover { + border: 1px solid var(--navy, #083e7f); + background: var(--white, #fff); + color: var(--navy, #083e7f); + } + + .addButton { + border: none; + background: var(--white, #2d71f7); + color: var(--white, #fff); + cursor: pointer; + } + .addButton:hover { + border: none; + background: var(--blue, #083e7f); + color: var(--white, #fff); + } + } + } + } +}
Unknown
PR์ด ๋จธ์ง€๋œ๋‹ค๋ฉด, ์ด ํ”„๋กœ์ ํŠธ์˜ ๋ชจ๋“  button ํƒœ๊ทธ์— `width: 120px, height: 50px` ์†์„ฑ์ด ์ ์šฉ๋˜๊ฒ ๋„ค์š”! ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์— ์ž˜ ํ•ด์ฃผ์‹  ๊ฒƒ์ฒ˜๋Ÿผ nesting๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€๋กœ, ํƒœ๊ทธ ์„ ํƒ์ž์˜ ์‚ฌ์šฉ์€ ๋‹ค์Œ์˜ ๊ฒฝ์šฐ ์™ธ์—๋Š” ์ง€์–‘ํ•˜๋Š” ๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค. 1. ์ „์—ญ์— ์ ์šฉ๋˜์–ด์•ผ ํ•˜๋Š” ์Šคํƒ€์ผ (e.g. common.scss, reset.scss) 2. ์•ž์œผ๋กœ๋„ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ํ™•์‹ ์ด ์žˆ๋Š” ์š”์†Œ className์„ ๋ถ€์—ฌํ•ด์„œ ์Šคํƒ€์ผ ์†์„ฑ ๋ถ€์—ฌํ•ด ์ฃผ์„ธ์š”!
@@ -1,7 +1,17 @@ @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100;300;400;500;700;900&display=swap'); - * { box-sizing: border-box; font-family: 'Noto Sans KR', sans-serif; -} \ No newline at end of file +} +body { + width: 100vw; + height: 100vh; + display: flex; + align-items: center; + justify-content: center; +} + +button { + cursor: pointer; +}
Unknown
๊ณต์šฉ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์„œ ์˜ฌ๋ ค์ฃผ์…จ๋Š”๋ฐ, ๊ณต์šฉ ํŒŒ์ผ์˜ ์ˆ˜์ •์€ 1. ํŒ€์›๊ณผ์˜ ์ถฉ๋ถ„ํ•œ ์ƒ์˜ ํ›„์— 2. ํ•ด๋‹น ํŒŒ์ผ๋งŒ ์ˆ˜์ •ํ•˜๋Š” ๋‚ด์šฉ์˜ PR ์„ ์˜ฌ๋ ค์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
๋ฉ˜ํ† ๋ฆฌ๋ทฐ ๋ฐ˜์˜ ์™„๋ฃŒ
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
๋ฉ˜ํ†  ๋ฆฌ๋ทฐ ๋ฐ˜์˜ ์™„๋ฃŒ
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
๋ฉ˜ํ†  ๋ฆฌ๋ทฐ ๋ฐ˜์˜์™„๋ฃŒ
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
alt ์†์„ฑ์€ ํ™”๋ฉด์— ์ด๋ฏธ์ง€๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์„๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ๋Œ€์ฒด ํ…์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ ๋–„๋ฌธ์— alt ๊ฐ’์— userPicture๋ฅผ ๋„ฃ๊ฒ ์๋‹ˆ๋‹ค..
@@ -0,0 +1,79 @@ +.userBox { + margin: 20px; + gap: 0 24px; + display: flex; + justify-content: center; + flex-direction: column; + + .userWrap { + display: flex; + flex-direction: row; + justify-content: center; + + .user { + width: 50px; + height: 50px; + border-radius: 50%; + } + + .contentBox { + display: flex; + flex-direction: column; + gap: 10px; + margin-left: 20px; + + .userName { + font-size: 20px; + font-weight: 700; + } + + .textArea { + padding: 10px; + border-radius: 6px; + border: 1px solid var(--grey-88, #e0e0e0); + background: var(--white, #fff); + resize: none; + transition: border-color 0.3s; + outline: none; + } + .textArea:hover { + border-color: var(--gray, #999999); + } + + .buttonWrap { + justify-content: space-between; + display: flex; + + .customButton { + width: 120px; + height: 50px; + border-radius: 6px; + } + + .cancelButton { + border: 1px solid var(--blue, #2d71f7); + background: var(--white, #fff); + color: var(--blue, #2d71f7); + cursor: pointer; + } + .cancelButton:hover { + border: 1px solid var(--navy, #083e7f); + background: var(--white, #fff); + color: var(--navy, #083e7f); + } + + .addButton { + border: none; + background: var(--white, #2d71f7); + color: var(--white, #fff); + cursor: pointer; + } + .addButton:hover { + border: none; + background: var(--blue, #083e7f); + color: var(--white, #fff); + } + } + } + } +}
Unknown
๋ฉ˜ํ†  ๋ฆฌ๋ทฐ ๋ฐ˜์˜ ์™„๋ฃŒ
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
๋„ต!
@@ -1,14 +1,77 @@ -import React from "react"; -import "./PostAdd.scss"; +import React, { useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import './PostAdd.scss'; +import { Button } from '../PostAdd/components/Post.jsx'; const PostAdd = () => { + const [comment, setComment] = useState(''); -return( - <div className="postAdd"> - <div>๋กœ๊ทธ์ธ</div> - </div> -); + const navigate = useNavigate(); -}; + const handleText = value => { + setComment(value); + }; + console.log(handleText); + + const cancel = () => { + const confirmed = window.confirm( + '์ ์œผ์‹  ๊ธ€์„ ์ทจ์†Œํ•˜๊ณ  post-list๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๊นŒ?', + ); + if (confirmed) { + navigate('/post-list'); + } + }; + + const posting = () => { + if (comment.length >= 1) { + navigate('/post-list'); -export default PostAdd; \ No newline at end of file + fetch({ + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + }, + body: JSON.stringify({ + content: '์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€', + }), + }) + .then(response => response.json) + .then(data => console.log(data)); + } else { + alert('๋‚ด์šฉ์„ ์ž‘์„ฑ์ฃผ์„ธ์š”.'); + } + }; + + return ( + <div className="userBox"> + <div className="userWrap"> + <img className="user" src="/images/user.png" alt="userPicture" /> + <div className="contentBox"> + <div className="userName">NAME</div> + <textarea + onChange={event => handleText(event.target.value)} + className="textArea" + cols="80" + rows="30" + type="text" + value={comment} + /> + + <div className="buttonWrap"> + <Button + className="customButton cancelButton" + buttonName="์ทจ์†Œ" + active={cancel} + /> + <Button + className="customButton addButton" + buttonName="๊ฒŒ์‹œ" + active={posting} + /> + </div> + </div> + </div> + </div> + ); +}; +export default PostAdd;
JavaScript
๋ฉ˜ํ†  ๋ฆฌ๋ทฐ ๋ฐ˜์˜ ์™„๋ฃŒ(?)
@@ -0,0 +1,37 @@ +package nextstep.authentication; + +public class UsernamePasswordAuthenticationToken implements Authentication { + + private final String principal; + private final String credentials; + private boolean authenticated; + + public UsernamePasswordAuthenticationToken(String principal, String credentials, boolean authenticated) { + this.principal = principal; + this.credentials = credentials; + this.authenticated = authenticated; + } + + public static UsernamePasswordAuthenticationToken authenticated(String principal, String credentials) { + return new UsernamePasswordAuthenticationToken(principal, credentials, true); + } + + public static UsernamePasswordAuthenticationToken unauthenticated(String principal, String credentials) { + return new UsernamePasswordAuthenticationToken(principal, credentials, false); + } + + @Override + public String getPrincipal() { + return principal; + } + + @Override + public String getCredentials() { + return credentials; + } + + @Override + public boolean isAuthenticated() { + return authenticated; + } +}
Java
`AuthenticationToken`์€ ๋ง๊ทธ๋Œ€๋กœ ์ธ์ฆ๋œ ํ† ํฐ์ด๊ธฐ ๋•Œ๋ฌธ์— `UserDetails`๋Š” ํ•„์š”๋กœ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. `UserDetails`์˜ ์‚ฌ์šฉ์ฒ˜๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ์ตœ์ดˆ ์ธ์ž…๋œ ์œ ์ €๊ฐ€ ์ธ์ฆ์ด ๊ฐ€๋Šฅํ•œ ์œ ์ €์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋  ๋ฟ์ด์ง€ ์ด๋ฏธ ์ธ์ฆ๋œ ํ† ํฐ์—๋Š” ๋ณ„๋„๋กœ `UserDetails`๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ๋งŒ๋“ค์–ด์ฃผ์‹  ์ฝ”๋“œ์—๋„ ์‚ฌ์šฉ์ฒ˜๊ฐ€ ์—†๊ธฐ๋„ ํ•˜๊ตฌ์š”.
@@ -0,0 +1,37 @@ +package nextstep.authentication; + +public class UsernamePasswordAuthenticationToken implements Authentication { + + private final String principal; + private final String credentials; + private boolean authenticated; + + public UsernamePasswordAuthenticationToken(String principal, String credentials, boolean authenticated) { + this.principal = principal; + this.credentials = credentials; + this.authenticated = authenticated; + } + + public static UsernamePasswordAuthenticationToken authenticated(String principal, String credentials) { + return new UsernamePasswordAuthenticationToken(principal, credentials, true); + } + + public static UsernamePasswordAuthenticationToken unauthenticated(String principal, String credentials) { + return new UsernamePasswordAuthenticationToken(principal, credentials, false); + } + + @Override + public String getPrincipal() { + return principal; + } + + @Override + public String getCredentials() { + return credentials; + } + + @Override + public boolean isAuthenticated() { + return authenticated; + } +}
Java
์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ๋ชจ๋‘ ์ œ๊ฑฐํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” :)
@@ -0,0 +1,29 @@ +package nextstep.authentication; + +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.exception.ProviderNotFoundException; + +import java.util.List; +import java.util.Objects; + +public class ProviderManager implements AuthenticationManager { + + private final List<AuthenticationProvider> providers; + + public ProviderManager(List<AuthenticationProvider> providers) { + this.providers = providers; + } + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + + Class<? extends Authentication> target = authentication.getClass(); + + return providers.stream() + .filter(provider -> provider.supports(target)) + .map(provider -> provider.authenticate(authentication)) + .filter(Objects::nonNull) + .findAny() + .orElseThrow(() -> new ProviderNotFoundException(String.format("No AuthenticationProvider found for %s", target.getName()))); + } +}
Java
`toTest`๋ณด๋‹ค ์ ์ ˆํ•œ ๋„ค์ด๋ฐ์€ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,29 @@ +package nextstep.authentication; + +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.exception.ProviderNotFoundException; + +import java.util.List; +import java.util.Objects; + +public class ProviderManager implements AuthenticationManager { + + private final List<AuthenticationProvider> providers; + + public ProviderManager(List<AuthenticationProvider> providers) { + this.providers = providers; + } + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + + Class<? extends Authentication> target = authentication.getClass(); + + return providers.stream() + .filter(provider -> provider.supports(target)) + .map(provider -> provider.authenticate(authentication)) + .filter(Objects::nonNull) + .findAny() + .orElseThrow(() -> new ProviderNotFoundException(String.format("No AuthenticationProvider found for %s", target.getName()))); + } +}
Java
```suggestion return providers.stream() .filter(provider -> provider.supports(toTest)) .map(provider -> provider.authenticate(authentication)) .filter(Objects::nonNull) .findAny() .orElseThrow(); ``` ๋กœ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š” :)
@@ -0,0 +1,68 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class FormLoginAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.POST, "/login"); + + private final AuthenticationManager authenticationManager; + + public FormLoginAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + return authenticationManager.authenticate(authRequest); + } +}
Java
์ธ์ฆ์ด null์ผ ๋•Œ ๋ฐ”๋กœ returnํ•˜๋ฉด ํ•„ํ„ฐ ๋™์ž‘์ด ์–ด๋””๋กœ ํ–ฅํ• ๊นŒ์š”?
@@ -0,0 +1,85 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.Base64Convertor; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class BasicAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.GET, "/members"); + + private final AuthenticationManager authenticationManager; + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + UsernamePasswordAuthenticationToken authRequest; + + var securityContext = SecurityContextHolder.getContext(); + + if (securityContext.getAuthentication() == null) { + + String authorization = request.getHeader("Authorization"); + String credentials = authorization.split(" ")[1]; + String decodedString = Base64Convertor.decode(credentials); + String[] usernameAndPassword = decodedString.split(":"); + + String username = usernameAndPassword[0]; + String password = usernameAndPassword[1]; + + authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + } else { + authRequest = (UsernamePasswordAuthenticationToken) securityContext.getAuthentication(); + } + + return authenticationManager.authenticate(authRequest); + } +}
Java
`attempAuthentication`ํ•˜๋Š” ๋™์ž‘์€ request์—์„œ ํ—ค๋”๋ฅผ ๋ณด๊ณ  ํ† ํฐ์„ ์ฝ์–ด `authenticationManager.authenticate`๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ณผ์ •์ธ๋ฐ์š”. ์ด ๊ณผ์ •์—์„œ `SecurityContext`๋Š” ์‚ฌ์‹ค ๋ถˆํ•„์š”ํ•œ ๋™์ž‘์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ํ•„ํ„ฐ์—์„œ์˜ ๋™์ž‘์€ 1. ํ—ค๋”์— ์›ํ•˜๋Š” ๊ฐ’์„ ๋ฝ‘์•„๋‚ด์–ด ํ† ํฐ์„ ์ƒ์„ฑํ•œ๋‹ค. 2. ํ† ํฐ์„ ๊ฐ€์ง€๊ณ  `AuthenticationManager.authenticate`๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. 3. ์„ฑ๊ณตํ•˜๋ฉด SecurityContext์— ์ธ์ฆ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. 4. ์‹คํŒจํ•˜๋ฉด ์ดˆ๊ธฐํ™”ํ•˜๊ณ  401์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ž…๋‹ˆ๋‹ค. ์ฆ‰, `SecurityContext`๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ํ–‰์œ„๋Š” ๋ชจ๋“  ์ธ์ฆ ๊ณผ์ •์ด ๋๋‚˜๊ณ  ์„ฑ๊ณต๊ณผ ์‹คํŒจ์— ๋”ฐ๋ฅธ ํ–‰์œ„์ด์ง€ ๊ทธ ์ค‘๊ฐ„์— `SecurityContext`๋ฅผ ๊ฑด๋“ค์ง„ ์•Š์•„์š”.
@@ -0,0 +1,38 @@ +package nextstep.authentication.context; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +public class HttpSessionSecurityContextRepository implements SecurityContextRepository { + + private static final String SPRING_SECURITY_CONTEXT_KEY = "SPRING_SECURITY_CONTEXT"; + + @Override + public SecurityContext loadContext(HttpServletRequest request) { + + HttpSession httpSession = request.getSession(false); + if (httpSession == null) { + return null; + } + + return (SecurityContext) httpSession.getAttribute(SPRING_SECURITY_CONTEXT_KEY); + } + + @Override + public void saveContext(SecurityContext context, HttpServletRequest request, HttpServletResponse response) { + + // SecurityContext๊ฐ€ null์ด๊ฑฐ๋‚˜ ์ธ์ฆ์ •๋ณด๊ฐ€ ์—†์œผ๋ฉด ์„ธ์…˜์—์„œ ์ œ๊ฑฐ + if (context == null || context.getAuthentication() == null) { + HttpSession session = request.getSession(false); + if (session != null) { + session.removeAttribute(SPRING_SECURITY_CONTEXT_KEY); + } + return; + } + + // ์„ธ์…˜์ด ์—†์œผ๋ฉด ์ƒ์„ฑํ•˜๊ณ , SecurityContext๋ฅผ ์ €์žฅ + HttpSession session = request.getSession(true); + session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, context); + } +}
Java
๋กœ์ง์ด ๋ณต์žกํ•  ํ•„์š” ์—†์ด `loadContext`๋Š” ๋‹จ์ˆœํžˆ 1. request.getSession์˜ ๊ฐ’์ด null์ด๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 2. httpSession.getAttribute(SPRING_SECURITY_CONTEXT_KEY)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฉด ๋ฉ๋‹ˆ๋‹ค. `readSecurityContextFromSession`์˜ ๋ถ„๊ธฐ๋Š” ์œ„ ๊ณผ์ •์œผ๋กœ ์ด๋ฏธ ๋™์ผํ•˜๊ฒŒ ๋กœ์ง์ด ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ์–ด ๋ณ„๋„๋กœ ํ•„์š” ์—†์–ด๋ณด์—ฌ์š”.
@@ -0,0 +1,29 @@ +package nextstep.authentication; + +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.exception.ProviderNotFoundException; + +import java.util.List; +import java.util.Objects; + +public class ProviderManager implements AuthenticationManager { + + private final List<AuthenticationProvider> providers; + + public ProviderManager(List<AuthenticationProvider> providers) { + this.providers = providers; + } + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + + Class<? extends Authentication> target = authentication.getClass(); + + return providers.stream() + .filter(provider -> provider.supports(target)) + .map(provider -> provider.authenticate(authentication)) + .filter(Objects::nonNull) + .findAny() + .orElseThrow(() -> new ProviderNotFoundException(String.format("No AuthenticationProvider found for %s", target.getName()))); + } +}
Java
target์œผ๋กœ ๋ณ€์ˆ˜๋ช…์„ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,68 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class FormLoginAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.POST, "/login"); + + private final AuthenticationManager authenticationManager; + + public FormLoginAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + return authenticationManager.authenticate(authRequest); + } +}
Java
์ธ์ฆ์ด null์ผ ๋•Œ ๋‹ค์Œ ํ•„ํ„ฐ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก `filterChain.doFilter(servletRequest, servletResponse);` ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,85 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.Base64Convertor; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class BasicAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.GET, "/members"); + + private final AuthenticationManager authenticationManager; + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + UsernamePasswordAuthenticationToken authRequest; + + var securityContext = SecurityContextHolder.getContext(); + + if (securityContext.getAuthentication() == null) { + + String authorization = request.getHeader("Authorization"); + String credentials = authorization.split(" ")[1]; + String decodedString = Base64Convertor.decode(credentials); + String[] usernameAndPassword = decodedString.split(":"); + + String username = usernameAndPassword[0]; + String password = usernameAndPassword[1]; + + authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + } else { + authRequest = (UsernamePasswordAuthenticationToken) securityContext.getAuthentication(); + } + + return authenticationManager.authenticate(authRequest); + } +}
Java
SecurityContext๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด FormLoginTest.login_after_members() ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ์•Š๋Š”๋ฐ ํ…Œ์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•˜๋Š”๊ฒŒ ์ข‹์„๊นŒ์š”?
@@ -0,0 +1,85 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.Base64Convertor; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class BasicAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.GET, "/members"); + + private final AuthenticationManager authenticationManager; + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + UsernamePasswordAuthenticationToken authRequest; + + var securityContext = SecurityContextHolder.getContext(); + + if (securityContext.getAuthentication() == null) { + + String authorization = request.getHeader("Authorization"); + String credentials = authorization.split(" ")[1]; + String decodedString = Base64Convertor.decode(credentials); + String[] usernameAndPassword = decodedString.split(":"); + + String username = usernameAndPassword[0]; + String password = usernameAndPassword[1]; + + authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + } else { + authRequest = (UsernamePasswordAuthenticationToken) securityContext.getAuthentication(); + } + + return authenticationManager.authenticate(authRequest); + } +}
Java
SecurityContext๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ ค๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆ˜์ •์„ ์‹œ๋„ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ```java private Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException { String authorization = request.getHeader("Authorization"); String credentials = authorization.split(" ")[1]; String decodedString = Base64Convertor.decode(credentials); String[] usernameAndPassword = decodedString.split(":"); String username = usernameAndPassword[0]; String password = usernameAndPassword[1]; UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); return authenticationManager.authenticate(authRequest); } ```
@@ -0,0 +1,85 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.Base64Convertor; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class BasicAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.GET, "/members"); + + private final AuthenticationManager authenticationManager; + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + UsernamePasswordAuthenticationToken authRequest; + + var securityContext = SecurityContextHolder.getContext(); + + if (securityContext.getAuthentication() == null) { + + String authorization = request.getHeader("Authorization"); + String credentials = authorization.split(" ")[1]; + String decodedString = Base64Convertor.decode(credentials); + String[] usernameAndPassword = decodedString.split(":"); + + String username = usernameAndPassword[0]; + String password = usernameAndPassword[1]; + + authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + } else { + authRequest = (UsernamePasswordAuthenticationToken) securityContext.getAuthentication(); + } + + return authenticationManager.authenticate(authRequest); + } +}
Java
ํ…Œ์ŠคํŠธ๋Š” ๊ทธ๋Œ€๋กœ ๋‘๋Š” ๊ฒƒ์ด ๋งž์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ๊ฐ€ ํ„ฐ์ง€๋Š” ์›์ธ์€ `String authorization = request.getHeader("Authorization");`์—์„œ ํ—ค๋”๊ฐ’์ด ์—†๋Š”๋ฐ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์—†์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ๋” ๋™์ž‘์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•„ํ„ฐ๋Š” ํ—ค๋”์— `Authorization`์ด ์—†์œผ๋ฉด ๋™์ž‘ํ•  ํ•„์š”๊ฐ€ ์—†์ด ๋ฐ”์ดํŒจ์Šคํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ์š”. ํ˜„์žฌ๋‹จ๊ณ„์—์„œ๋Š” ๋ณ„๋„ path์— ๋”ฐ๋ฅธ ์ธ๊ฐ€์ฒ˜๋ฆฌ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ์ •๋„๋กœ๋งŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ฃผ์…”๋„๋ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,10 @@ +package nextstep.authentication; + +public interface Authentication { + + String getPrincipal(); + + String getCredentials(); + + boolean isAuthenticated(); +}
Java
์‹ค์ œ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋Š” `authentication`์•ˆ์— ๋ชจ๋“  ๊ฒƒ์ด ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์›น์— ๋Œ€ํ•œ ํ˜ธ์ถœ์„ ์ฒ˜๋ฆฌํ•˜๋Š” `FilterChainProxy`์™€ ๊ฐ™์€ ํด๋ž˜์Šค๋“ค์€ `web`ํŒจํ‚ค์ง€์—, `UserDetails`์™€ ๊ฐ™์ด ์œ ์ €์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์€ ๋ณ„๋„๋กœ `userdetails`์—์„œ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,27 @@ +package nextstep.authentication.context; + +public class SecurityContextHolder { + + private static final ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<>(); + + public static SecurityContext getContext() { + SecurityContext context = contextHolder.get(); + if (context == null) { + context = createEmptyContext(); + contextHolder.set(context); + } + return context; + } + + public static void setContext(SecurityContext context) { + contextHolder.set(context); + } + + public static void clearContext() { + contextHolder.remove(); + } + + public static SecurityContext createEmptyContext() { + return new SecurityContextImpl(); + } +}
Java
`SecurityContextHolder`๋ผ๋Š” ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„์ฒด์— ์˜์กดํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ค์šด ๊ตฌ์กฐ๊ฐ€ ๋  ์ˆ˜ ์žˆ์–ด ์‹ค์ œ๋กœ๋Š” ์ด๋ฅผ ๋ž˜ํ•‘ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ๋” ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/spring-projects/spring-security/blob/main/core/src/main/java/org/springframework/security/core/context/SecurityContextHolderStrategy.java
@@ -0,0 +1,19 @@ +package nextstep.authentication.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.UNAUTHORIZED) +public class AuthenticationException extends RuntimeException { + + public AuthenticationException() { + } + + public AuthenticationException(String msg, Throwable cause) { + super(msg, cause); + } + + public AuthenticationException(String msg) { + super(msg); + } +}
Java
์‚ฌ์šฉํ•˜๊ณ  ์žˆ์ง€ ์•Š๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -0,0 +1,85 @@ +package nextstep.authentication.filter; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import nextstep.authentication.Authentication; +import nextstep.authentication.AuthenticationManager; +import nextstep.authentication.UsernamePasswordAuthenticationToken; +import nextstep.authentication.context.SecurityContextHolder; +import nextstep.authentication.exception.AuthenticationException; +import nextstep.authentication.util.Base64Convertor; +import nextstep.authentication.util.matcher.MvcRequestMatcher; +import org.springframework.http.HttpMethod; + +import java.io.IOException; + +public class BasicAuthenticationFilter implements Filter { + + private static final MvcRequestMatcher DEFAULT_REQUEST_MATCHER = new MvcRequestMatcher(HttpMethod.GET, "/members"); + + private final AuthenticationManager authenticationManager; + + public BasicAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + if (!DEFAULT_REQUEST_MATCHER.matches((HttpServletRequest) servletRequest)) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + HttpServletRequest request = (HttpServletRequest) servletRequest; + HttpServletResponse response = (HttpServletResponse) servletResponse; + + try { + + Authentication authenticationResult = attemptAuthentication(request, response); + if (authenticationResult == null) { + filterChain.doFilter(servletRequest, servletResponse); + return; + } + + SecurityContextHolder.getContext().setAuthentication(authenticationResult); + + } catch (Exception e) { + SecurityContextHolder.clearContext(); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + + return; + } + + filterChain.doFilter(servletRequest, servletResponse); + } + + private Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { + + UsernamePasswordAuthenticationToken authRequest; + + var securityContext = SecurityContextHolder.getContext(); + + if (securityContext.getAuthentication() == null) { + + String authorization = request.getHeader("Authorization"); + String credentials = authorization.split(" ")[1]; + String decodedString = Base64Convertor.decode(credentials); + String[] usernameAndPassword = decodedString.split(":"); + + String username = usernameAndPassword[0]; + String password = usernameAndPassword[1]; + + authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password); + + } else { + authRequest = (UsernamePasswordAuthenticationToken) securityContext.getAuthentication(); + } + + return authenticationManager.authenticate(authRequest); + } +}
Java
ํŠน์ • path๋กœ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋‹ค๋ฅธ path๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒฝ์šฐ ์ด ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋žŒ์ด ์ƒ์ˆ˜๋ฅผ ๊ฑด๋“œ๋ฆด ์ˆ˜ ์—†์œผ๋‹ˆ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ ๋” ์ ์ ˆํ•˜๊ฒ ๋„ค์š”.
@@ -3,21 +3,17 @@ <mapper namespace="com.midnear.midnearshopping.mapper.coupon_point.PointMapper"> <insert id="grantPoints" useGeneratedKeys="true" keyProperty="pointId"> INSERT INTO point ( - amount, reason + amount, reason, review_id, user_id, grant_date ) VALUES ( - #{amount}, #{reason} + #{amount}, #{reason}, #{reviewId}, #{userId}, NOW() ) </insert> - <insert id="setReviewPointAmount"> - INSERT INTO review_point ( - text_review, photo_review - ) VALUES ( - #{textReview}, #{photoReview} - ) - </insert> - <delete id="deletePreviousData"> - delete from review_point - </delete> + <select id="getProductInfoByReviewId" resultType="java.lang.String"> + select concat(op.product_name, ' _ ', op.color) + from reviews r + join order_products op on r.order_product_id = op.order_product_id + where review_id = #{reviewId} + </select> <!--์–˜๋Š” ํŒŒ์ผ ๋”ฐ๋กœ ๋บด๊ธฐ ์• ๋งคํ•ด์„œ ์—ฌ๊ธฐ์— ๋„ฃ์Œ--> <select id="getPointList"> select
Unknown
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค..
@@ -0,0 +1,24 @@ +import { BASE_URL } from '@/constants'; +import { GetReviewsParams, ReviewScore } from '@/types/reviewType'; + +export const getReviewScore = async ({ + gatheringId, + type, +}: GetReviewsParams): Promise<ReviewScore[]> => { + const params = new URLSearchParams(); + if (gatheringId) params.append('gatheringId', gatheringId); + if (type) params.append('type', type); + + const res = await fetch(`${BASE_URL}/reviews/scores?${params.toString()}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!res.ok) { + throw new Error(`${res.status}`); + } + + return res.json(); +};
TypeScript
[p5] BASE_URL์„ importํ•ด์„œ ์‚ฌ์šฉํ•œ ๋ถ€๋ถ„ ์ข‹์Šต๋‹ˆ๋‹ค!๐Ÿฑ