From 7da424b8b7cb0db8f9cb097535b179dab261eebd Mon Sep 17 00:00:00 2001 From: HyunJunSon Date: Tue, 16 Jan 2024 19:13:23 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat::myPage,main=20codeSet=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20Entity=20=EC=97=B0=EA=B4=80=EA=B4=80=EA=B3=84=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/controller/MainContoller.java | 16 ++++ .../mypage/controller/MyPageController.java | 61 +++++++++++++ .../product/service/ProductService.java | 7 ++ .../controller/MyPageControllerTest.java | 86 +++++++++++++++++++ .../auth/repository/AuthRepository.java | 3 +- .../com/kernel360/brand/entity/Brand.java | 6 ++ .../com/kernel360/carinfo/entity/CarInfo.java | 7 +- .../carinfo/repository/CarInfoRepository.java | 7 ++ .../com/kernel360/member/entity/Member.java | 42 ++++++--- .../com/kernel360/product/entity/Product.java | 5 ++ .../product/repository/ProductRepository.java | 2 + .../kernel360/washinfo/entity/WashInfo.java | 4 +- .../repository/WashInfoRepository.java | 7 ++ 13 files changed, 236 insertions(+), 17 deletions(-) create mode 100644 module-api/src/main/java/com/kernel360/main/controller/MainContoller.java create mode 100644 module-api/src/main/java/com/kernel360/mypage/controller/MyPageController.java create mode 100644 module-api/src/test/java/com/kernel360/mypage/controller/MyPageControllerTest.java create mode 100644 module-domain/src/main/java/com/kernel360/carinfo/repository/CarInfoRepository.java create mode 100644 module-domain/src/main/java/com/kernel360/washinfo/repository/WashInfoRepository.java diff --git a/module-api/src/main/java/com/kernel360/main/controller/MainContoller.java b/module-api/src/main/java/com/kernel360/main/controller/MainContoller.java new file mode 100644 index 00000000..5995e6ba --- /dev/null +++ b/module-api/src/main/java/com/kernel360/main/controller/MainContoller.java @@ -0,0 +1,16 @@ +package com.kernel360.main.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +public class MainContoller { + @GetMapping("") + ResponseEntity mainPage(){ + return ResponseEntity.status(HttpStatus.OK).body("main page입니다."); + } + +} diff --git a/module-api/src/main/java/com/kernel360/mypage/controller/MyPageController.java b/module-api/src/main/java/com/kernel360/mypage/controller/MyPageController.java new file mode 100644 index 00000000..a8222dad --- /dev/null +++ b/module-api/src/main/java/com/kernel360/mypage/controller/MyPageController.java @@ -0,0 +1,61 @@ +package com.kernel360.mypage.controller; + +import com.kernel360.member.dto.MemberDto; +import com.kernel360.member.service.MemberService; +import com.kernel360.product.dto.ProductDto; +import com.kernel360.product.service.ProductService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/mypage") +@RequiredArgsConstructor +public class MyPageController { + private final MemberService memberService; + private final ProductService productService; + @GetMapping("/main") + ResponseEntity main(Model model) { + List productDtoList = productService.getProductListOrderByViewCount(); + String bannerImageUrl = "http://localhost:8080/bannersample.png"; + String suggestImageUrl = "http://localhost:8080/suggestsample.png"; + + model.addAllAttributes(Map.of("Banner" , bannerImageUrl, + "Suggest", suggestImageUrl, + "Product", productDtoList)); + + return ResponseEntity.status(HttpStatus.OK).body(model); + } + + @GetMapping("/member") + ResponseEntity myInfo() { + return ResponseEntity.status(HttpStatus.OK).body("mypage 내정보 page입니다."); + } + + @GetMapping("/car") + ResponseEntity myCar() { + return ResponseEntity.status(HttpStatus.OK).body("mypage 차량정보 page입니다."); + } + + @DeleteMapping("/member") + ResponseEntity memberDelete(@RequestBody MemberDto memberDto) { + return ResponseEntity.status(HttpStatus.OK).body(memberDto.id() + " 회원이 탈퇴되었습니다."); + } + + @PostMapping("/member") + ResponseEntity memberInfoAdd(@RequestBody MemberDto memberDto) { + return ResponseEntity.status(HttpStatus.OK).body(memberDto.id() + "회원의 회원정보가 추가되었습니다."); + } + + @PatchMapping("/member") + ResponseEntity changePassword(@RequestBody MemberDto memberDto) { + return ResponseEntity.status(HttpStatus.OK).body(memberDto.id() + "회원의 비밀번호가 변경 되었습니다."); + } + + +} diff --git a/module-api/src/main/java/com/kernel360/product/service/ProductService.java b/module-api/src/main/java/com/kernel360/product/service/ProductService.java index 1e704fb9..cb02e7a4 100644 --- a/module-api/src/main/java/com/kernel360/product/service/ProductService.java +++ b/module-api/src/main/java/com/kernel360/product/service/ProductService.java @@ -34,4 +34,11 @@ public List getProductListByKeyword(String keyword) { return products.stream().map(ProductDto::from).toList(); } + + @Transactional(readOnly = true) + public List getProductListOrderByViewCount(){ + List products = productRepository.findAllByOrderByViewCountDesc(); + + return products.stream().map(ProductDto::from).toList(); + } } diff --git a/module-api/src/test/java/com/kernel360/mypage/controller/MyPageControllerTest.java b/module-api/src/test/java/com/kernel360/mypage/controller/MyPageControllerTest.java new file mode 100644 index 00000000..8bc0ed35 --- /dev/null +++ b/module-api/src/test/java/com/kernel360/mypage/controller/MyPageControllerTest.java @@ -0,0 +1,86 @@ +package com.kernel360.mypage.controller; + +import com.kernel360.member.service.MemberService; +import com.kernel360.product.dto.ProductDto; +import com.kernel360.product.service.ProductService; +import com.navercorp.fixturemonkey.FixtureMonkey; +import com.navercorp.fixturemonkey.api.introspector.*; +import com.navercorp.fixturemonkey.api.type.TypeReference; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.ui.Model; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@WebMvcTest(MyPageController.class) +class MyPageControllerTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext context; + + @MockBean + private MemberService memberService; + + @MockBean + private ProductService productService; + + @MockBean + private Model model; + private FixtureMonkey fixtureMonkey; + + @BeforeEach + void 준비(WebApplicationContext webApplicationContext) { + fixtureMonkey = FixtureMonkey.builder() + .objectIntrospector(new FailoverIntrospector( + Arrays.asList( + BuilderArbitraryIntrospector.INSTANCE, + FieldReflectionArbitraryIntrospector.INSTANCE, + ConstructorPropertiesArbitraryIntrospector.INSTANCE, + BeanArbitraryIntrospector.INSTANCE + ) + )) + .build(); + + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .build(); + } + + @Test + void 마이페이지_메인요청이왔을때_200요청과_응답이_잘반환되는지() throws Exception { + //given + List productDtoList = fixtureMonkey.giveMeBuilder(ProductDto.class).sampleList(5); + + //when + when(productService.getProductListOrderByViewCount()).thenReturn(productDtoList); + + //then + mockMvc.perform(get("/mypage/main")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.Banner").value("http://localhost:8080/bannersample.png")) + .andExpect(jsonPath("$.Suggest").value("http://localhost:8080/suggestsample.png")) + .andExpect(jsonPath("$.Product", hasSize(5))); + + verify(productService, times(1)).getProductListOrderByViewCount(); + } + + +} \ No newline at end of file diff --git a/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java b/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java index c6a7d9e7..1ab2d942 100644 --- a/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java +++ b/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java @@ -1,10 +1,9 @@ package com.kernel360.auth.repository; import com.kernel360.auth.entity.Auth; -import jakarta.persistence.Id; import org.springframework.data.jpa.repository.JpaRepository; -public interface AuthRepository extends JpaRepository { +public interface AuthRepository extends JpaRepository { Auth findOneByMemberNo(Long memberNo); diff --git a/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java b/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java index 03ba649f..ce7856d3 100644 --- a/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java +++ b/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java @@ -1,10 +1,13 @@ package com.kernel360.brand.entity; import com.kernel360.base.BaseEntity; +import com.kernel360.product.entity.Product; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; +import java.util.List; + @Getter @Setter @Entity @@ -18,6 +21,9 @@ public class Brand extends BaseEntity { @Column(name = "brand_name", nullable = false, length = Integer.MAX_VALUE) private String brandName; + @OneToMany(fetch = FetchType.LAZY, mappedBy = "brand") + private List productList; + @Column(name = "description", length = Integer.MAX_VALUE) private String description; diff --git a/module-domain/src/main/java/com/kernel360/carinfo/entity/CarInfo.java b/module-domain/src/main/java/com/kernel360/carinfo/entity/CarInfo.java index 76305597..da71be5c 100644 --- a/module-domain/src/main/java/com/kernel360/carinfo/entity/CarInfo.java +++ b/module-domain/src/main/java/com/kernel360/carinfo/entity/CarInfo.java @@ -1,6 +1,7 @@ package com.kernel360.carinfo.entity; import com.kernel360.base.BaseEntity; +import com.kernel360.member.entity.Member; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; @@ -14,7 +15,11 @@ public class CarInfo extends BaseEntity { @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "car_info_id_gen") @SequenceGenerator(name = "car_info_id_gen", sequenceName = "car_info_car_no_seq") @Column(name = "car_no", nullable = false) - private Integer carNo; + private Long carNo; + + @OneToOne + @JoinColumn(name = "member_no") + private Member member; @Column(name = "car_brand") private String carBrand; diff --git a/module-domain/src/main/java/com/kernel360/carinfo/repository/CarInfoRepository.java b/module-domain/src/main/java/com/kernel360/carinfo/repository/CarInfoRepository.java new file mode 100644 index 00000000..8409f299 --- /dev/null +++ b/module-domain/src/main/java/com/kernel360/carinfo/repository/CarInfoRepository.java @@ -0,0 +1,7 @@ +package com.kernel360.carinfo.repository; + +import com.kernel360.carinfo.entity.CarInfo; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CarInfoRepository extends JpaRepository { +} diff --git a/module-domain/src/main/java/com/kernel360/member/entity/Member.java b/module-domain/src/main/java/com/kernel360/member/entity/Member.java index aa25437d..b1471804 100644 --- a/module-domain/src/main/java/com/kernel360/member/entity/Member.java +++ b/module-domain/src/main/java/com/kernel360/member/entity/Member.java @@ -1,6 +1,8 @@ package com.kernel360.member.entity; import com.kernel360.base.BaseEntity; +import com.kernel360.carinfo.entity.CarInfo; +import com.kernel360.washinfo.entity.WashInfo; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; @@ -22,6 +24,12 @@ public class Member extends BaseEntity { @Column(name = "id", nullable = false) private String id; + @OneToOne(mappedBy = "member") + private CarInfo carInfo; + + @OneToOne(mappedBy = "member") + private WashInfo washInfo; + @Column(name = "email", nullable = false) private String email; @@ -39,7 +47,9 @@ public static Member of(Long memberNo, String id, String email, String password, return new Member(memberNo, id, email, password, gender, birthdate); } - /** All Binding **/ + /** + * All Binding + **/ private Member( Long memberNo, String id, @@ -47,7 +57,7 @@ private Member( String password, String gender, LocalDate birthdate - ) { + ) { this.memberNo = memberNo; this.id = id; this.email = email; @@ -56,27 +66,35 @@ private Member( this.birthdate = birthdate; } - /** joinMember **/ - public static Member createJoinMember(String id, String email, String password){ + /** + * joinMember + **/ + public static Member createJoinMember(String id, String email, String password) { - return new Member(id,email,password); + return new Member(id, email, password); } - /** joinMember Binding **/ - private Member(String id, String email, String password){ + /** + * joinMember Binding + **/ + private Member(String id, String email, String password) { this.id = id; this.email = email; this.password = password; } - /** loginMember **/ - public static Member loginMember(String id, String password){ + /** + * loginMember + **/ + public static Member loginMember(String id, String password) { - return new Member(id,password); + return new Member(id, password); } - /** loginMember Binding **/ - private Member(String id, String password){ + /** + * loginMember Binding + **/ + private Member(String id, String password) { this.id = id; this.password = password; } diff --git a/module-domain/src/main/java/com/kernel360/product/entity/Product.java b/module-domain/src/main/java/com/kernel360/product/entity/Product.java index 43a97cba..03b5141a 100644 --- a/module-domain/src/main/java/com/kernel360/product/entity/Product.java +++ b/module-domain/src/main/java/com/kernel360/product/entity/Product.java @@ -1,6 +1,7 @@ package com.kernel360.product.entity; import com.kernel360.base.BaseEntity; +import com.kernel360.brand.entity.Brand; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; @@ -21,6 +22,10 @@ public class Product extends BaseEntity { @Column(name = "product_name", nullable = false) private String productName; + @ManyToOne + @JoinColumn(name = "brand_no") + private Brand brand; + @Column(name = "barcode") private String barcode; diff --git a/module-domain/src/main/java/com/kernel360/product/repository/ProductRepository.java b/module-domain/src/main/java/com/kernel360/product/repository/ProductRepository.java index d8b48b09..3ac9fcc7 100644 --- a/module-domain/src/main/java/com/kernel360/product/repository/ProductRepository.java +++ b/module-domain/src/main/java/com/kernel360/product/repository/ProductRepository.java @@ -11,4 +11,6 @@ public interface ProductRepository extends JpaRepository { @Query(value = "SELECT * FROM Product WHERE product_name LIKE CONCAT('%', :keyword, '%') OR barcode LIKE CONCAT('%', :keyword, '%') OR description LIKE CONCAT('%', :keyword, '%')", nativeQuery = true) List findByKeyword(@Param("keyword") String keyword); + + List findAllByOrderByViewCountDesc(); } diff --git a/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java b/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java index ff03fbfb..581d707a 100644 --- a/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java +++ b/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java @@ -17,9 +17,9 @@ public class WashInfo extends BaseEntity { @Column(name = "wash_no", nullable = false) private Integer washNo; - @ManyToOne(fetch = FetchType.LAZY, optional = false) + @OneToOne @JoinColumn(name = "member_no", nullable = false) - private Member memberNo; + private Member member; @Column(name = "wash_count") private Integer washCount; diff --git a/module-domain/src/main/java/com/kernel360/washinfo/repository/WashInfoRepository.java b/module-domain/src/main/java/com/kernel360/washinfo/repository/WashInfoRepository.java new file mode 100644 index 00000000..ffe41686 --- /dev/null +++ b/module-domain/src/main/java/com/kernel360/washinfo/repository/WashInfoRepository.java @@ -0,0 +1,7 @@ +package com.kernel360.washinfo.repository; + +import com.kernel360.washinfo.entity.WashInfo; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface WashInfoRepository extends JpaRepository { +} From e477458d888f67fc18132c8d65de82f2b2de4eb4 Mon Sep 17 00:00:00 2001 From: HyunJunSon Date: Wed, 17 Jan 2024 18:40:20 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat::mypage=5FCRUD=20review=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/kernel360/auth/repository/AuthRepository.java | 3 ++- .../src/main/java/com/kernel360/brand/entity/Brand.java | 2 -- .../src/main/java/com/kernel360/washinfo/entity/WashInfo.java | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java b/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java index 1ab2d942..c6a7d9e7 100644 --- a/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java +++ b/module-domain/src/main/java/com/kernel360/auth/repository/AuthRepository.java @@ -1,9 +1,10 @@ package com.kernel360.auth.repository; import com.kernel360.auth.entity.Auth; +import jakarta.persistence.Id; import org.springframework.data.jpa.repository.JpaRepository; -public interface AuthRepository extends JpaRepository { +public interface AuthRepository extends JpaRepository { Auth findOneByMemberNo(Long memberNo); diff --git a/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java b/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java index ce7856d3..72f447e1 100644 --- a/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java +++ b/module-domain/src/main/java/com/kernel360/brand/entity/Brand.java @@ -4,12 +4,10 @@ import com.kernel360.product.entity.Product; import jakarta.persistence.*; import lombok.Getter; -import lombok.Setter; import java.util.List; @Getter -@Setter @Entity @Table(name = "brand") public class Brand extends BaseEntity { diff --git a/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java b/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java index 581d707a..d8e532e6 100644 --- a/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java +++ b/module-domain/src/main/java/com/kernel360/washinfo/entity/WashInfo.java @@ -17,7 +17,7 @@ public class WashInfo extends BaseEntity { @Column(name = "wash_no", nullable = false) private Integer washNo; - @OneToOne + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_no", nullable = false) private Member member;