Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

충남대 BE_김기웅 6주차 과제 (Step2) #295

Open
wants to merge 11 commits into
base: lit2020
Choose a base branch
from
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
// runtimeOnly 'mysql:mysql-connector-java'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mockito:mockito-core'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/gift/DTO/member/SignupRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class SignupRequest {
@NotBlank
private String password;

@NotBlank
private String confirmPassword;

private Long kakaoId;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/gift/DTO/option/OptionResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static OptionResponse fromEntity(Option option) {
);
}

public Long getId() {
return id;
}

public String getName() {
return name;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gift/DTO/order/OrderResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public record OrderResponse(
Long quantity,
String message,
LocalDateTime orderDateTime
) {
) {

public static OrderResponse fromEntity(Order orderEntity) {
return new OrderResponse(
orderEntity.getId(),
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/gift/DTO/product/ProductPageResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gift.DTO.product;

import gift.domain.Product;
import java.util.List;
import org.springframework.data.domain.Page;

public class ProductPageResponse {

private List<ProductResponse> content;
private int number;
private long totalElements;
private int size;
private boolean last;

public ProductPageResponse() {
}

public ProductPageResponse(Page<ProductResponse> productPage) {
this.content = productPage.getContent();
this.number = productPage.getNumber();
this.totalElements = productPage.getTotalElements();
this.size = productPage.getSize();
this.last = productPage.isLast();
}

public List<ProductResponse> getContent() {
return content;
}
}
21 changes: 21 additions & 0 deletions src/main/java/gift/DTO/wishlist/WishResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gift.DTO.wishlist;

import gift.domain.Product;
import gift.domain.Wishlist;

public record WishResponse(
Long id,
String name,
int price,
String imageUrl
) {

public static WishResponse fromEntity(Wishlist wishEntity) {
return new WishResponse(
wishEntity.getId(),
wishEntity.getProduct().getName(),
wishEntity.getProduct().getPrice(),
wishEntity.getProduct().getImageUrl()
);
}
}
23 changes: 23 additions & 0 deletions src/main/java/gift/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gift.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig {

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*");
}
};
}
}
19 changes: 12 additions & 7 deletions src/main/java/gift/controller/admin/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import gift.DTO.member.MemberResponse;
import gift.DTO.product.ProductRequest;
import gift.DTO.product.ProductResponse;
import gift.domain.Member;
import gift.service.MemberService;
import gift.service.ProductService;
import jakarta.validation.Valid;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -21,7 +25,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/admin")
@RequestMapping("/api")
public class AdminController {

private final ProductService productService;
Expand All @@ -38,12 +42,13 @@ public AdminController(ProductService productService, MemberService memberServic
* @return 모든 상품 목록
*/
@GetMapping("/products")
public ResponseEntity<List<ProductResponse>> getProducts(
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "2") Integer size
public ResponseEntity<Page<ProductResponse>> getProducts(
@PageableDefault(page = 0, size = 10)
@SortDefault(sort = "name", direction = Sort.Direction.ASC) Pageable pageable,
@RequestParam(required = false) Long categoryId
) {
List<ProductResponse> products = productService.getProductsByPage(page, size);
return new ResponseEntity<>(products, HttpStatus.OK);
Page<ProductResponse> products = productService.findPagedProductsByCategoryId(pageable, categoryId);
return ResponseEntity.status(HttpStatus.OK).body(products);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/category")
@RequestMapping("/api/categories")
public class CategoryController {

private final CategoryService categoryService;
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/gift/controller/login/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
@RequestMapping("/api/members/login")
public class LoginController {

private final MemberService memberService;
Expand All @@ -24,15 +24,11 @@ public LoginController(MemberService memberService) {
}

@PostMapping
public ResponseEntity<LoginResponse> login(
public ResponseEntity<String> login(
@RequestBody
@Valid LoginRequest loginRequest
) {
try {
LoginResponse loginResponse = memberService.loginMember(loginRequest);
return new ResponseEntity<>(loginResponse, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
LoginResponse loginResponse = memberService.loginMember(loginRequest);
return ResponseEntity.status(HttpStatus.OK).body(loginResponse.getToken());
}
}
6 changes: 3 additions & 3 deletions src/main/java/gift/controller/signup/SignupController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/signup")
@RequestMapping("/api/members/register")
public class SignupController {

private final MemberService memberService;
Expand All @@ -24,11 +24,11 @@ public SignupController(MemberService memberService) {
}

@PostMapping
public ResponseEntity<SignupResponse> signup(
public ResponseEntity<String> signup(
@RequestBody
@Valid SignupRequest signupRequest
) {
SignupResponse signupResponse = memberService.registerMember(signupRequest);
return new ResponseEntity<>(signupResponse, HttpStatus.CREATED);
return ResponseEntity.status(HttpStatus.CREATED).body("User registered successfully");
}
}
39 changes: 20 additions & 19 deletions src/main/java/gift/controller/wishlist/WishlistController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package gift.controller.wishlist;

import gift.DTO.product.ProductResponse;
import gift.DTO.wishlist.WishResponse;
import gift.domain.Product;
import gift.service.MemberService;
import gift.service.TokenService;
import gift.service.WishlistService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -19,7 +26,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/wishlist")
@RequestMapping("/api/wishes")
public class WishlistController {

private final WishlistService wishlistService;
Expand All @@ -35,34 +42,30 @@ public WishlistController(
}

@GetMapping
public ResponseEntity<List<ProductResponse>> getWishlist(
public ResponseEntity<Page<WishResponse>> getWishlist(
@RequestHeader("Authorization") String authorizationHeader,
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "2") Integer size
@PageableDefault(page = 0, size = 10)
@SortDefault(sort = "createdDate", direction = Direction.DESC) Pageable pageable
) {
String token = tokenService.getBearerTokenFromHeader(authorizationHeader);
if (!tokenService.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
tokenService.validateToken(token);

String email = tokenService.extractEmailFromToken(token);
List<ProductResponse> wishlist = wishlistService.getWishlistByEmail(email, page, size);
return ResponseEntity.ok(wishlist);
Page<WishResponse> pageWishResponse = wishlistService.getWishlistByEmail(email, pageable);
return ResponseEntity.status(HttpStatus.OK).body(pageWishResponse);
}

@PostMapping("/{productId}")
public ResponseEntity<String> addToWishlist(
public ResponseEntity<WishResponse> addToWishlist(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable Long productId
) {
String token = tokenService.getBearerTokenFromHeader(authorizationHeader);
if (!tokenService.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid token");
}
tokenService.validateToken(token);
String email = tokenService.extractEmailFromToken(token);

wishlistService.addWishlist(email, productId);
return ResponseEntity.ok("Product added to wishlist");
WishResponse wish = wishlistService.addWishlist(email, productId);
return ResponseEntity.status(HttpStatus.CREATED).body(wish);
}

@DeleteMapping("/{productId}")
Expand All @@ -71,12 +74,10 @@ public ResponseEntity<String> removeFromWishlist(
@PathVariable Long productId
) {
String token = tokenService.getBearerTokenFromHeader(authorizationHeader);
if (!tokenService.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid token");
}
tokenService.validateToken(token);
String email = tokenService.extractEmailFromToken(token);

wishlistService.removeWishlist(email, productId);
return ResponseEntity.ok("Product removed from wishlist");
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}
14 changes: 10 additions & 4 deletions src/main/java/gift/controller/wishlist/WishlistViewController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package gift.controller.wishlist;

import gift.DTO.product.ProductResponse;
import gift.DTO.wishlist.WishResponse;
import gift.domain.Product;
import gift.service.TokenService;
import gift.service.WishlistService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -33,8 +39,8 @@ public WishlistViewController(
public String showWishlist(
Model model,
@RequestHeader(value = "Authorization", required = false) String authHeader,
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "2") Integer size
@PageableDefault(page = 0, size = 10)
@SortDefault(sort = "createdDate", direction = Direction.DESC) Pageable pageable
) {
if (authHeader == null) {
return "login";
Expand All @@ -52,8 +58,8 @@ public String showWishlist(
}

String email = tokenService.extractEmailFromToken(token);
List<ProductResponse> wishlist = wishlistService.getWishlistByEmail(email, page, size);
model.addAttribute("wishlist", wishlist);
Page<WishResponse> pageWishResponse = wishlistService.getWishlistByEmail(email, pageable);
model.addAttribute("wishlist", pageWishResponse);
return "wishlist";
}
}
Loading