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_이풍헌 5주차 과제(Step2) #172

Open
wants to merge 16 commits into
base: canyos
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.3.1'
id 'io.spring.dependency-management' version '1.1.5'
id "org.asciidoctor.jvm.convert" version "3.3.2"
}

group = 'camp.nextstep.edu'
version = '0.0.1-SNAPSHOT'

configurations {
asciidoctorExt // (2)
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
Expand All @@ -18,6 +21,9 @@ repositories {
}

dependencies {
asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
compileOnly 'org.projectlombok:lombok'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
implementation 'io.jsonwebtoken:jjwt-impl:0.12.3'
implementation 'io.jsonwebtoken:jjwt-jackson:0.12.3'
Expand All @@ -28,11 +34,33 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

ext { // (5)
snippetsDir = file('build/generated-snippets')
}

tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
}



tasks.named('asciidoctor') {
inputs.dir snippetsDir // (8)
dependsOn test // (10)
}

bootJar{
dependsOn asciidoctor
copy {
from "${asciidoctor.outputDir}"
into 'src/main/resources/static/docs'
}
}
2 changes: 2 additions & 0 deletions src/main/java/gift/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.jdbc.core.JdbcTemplate;

@EnableJpaAuditing
@ConfigurationPropertiesScan
@SpringBootApplication
public class Application {
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/gift/Util/JWTUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.time.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Component
Expand All @@ -23,13 +26,16 @@ public JWTUtil(@Value("${jwt.secretKey}") String jwtSecret,
this.expirationMs = jwtExpirationMs;
}

public String generateToken(User user) {
Date date = new Date();
Date expire = new Date(date.getTime() + expirationMs);
public String generateToken(User user, String kakaoToken) {
LocalDateTime now = LocalDateTime.now();
Instant nowInstant = now.atZone(ZoneId.systemDefault()).toInstant();
Instant expireInstant = nowInstant.plus(Duration.ofMillis(expirationMs));

return Jwts.builder()
.subject(Integer.toString(user.getId()))
.issuedAt(date)
.expiration(expire)
.claim("kakaoToken", kakaoToken)
.issuedAt(Date.from(nowInstant))
.expiration(Date.from(expireInstant))
.signWith(key)
.compact();
}
Expand All @@ -51,4 +57,14 @@ public Integer getUserIdFromToken(String token) {
.getPayload()
.getSubject());
}

public String getKakaoTokenFromToken(String token) {
Claims claims = Jwts.parser()
.verifyWith(key)
.build()
.parseSignedClaims(token)
.getPayload();

return (String) claims.get("kakaoToken");
}
}
11 changes: 6 additions & 5 deletions src/main/java/gift/controller/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gift.dto.category.CategoryDTO;
import gift.entity.Category;
import gift.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -14,9 +15,9 @@
import org.springframework.web.bind.annotation.*;

@Controller
@RequiredArgsConstructor
public class CategoryController {
@Autowired
CategoryService categoryService;
private final CategoryService categoryService;

@GetMapping("/api/category")
@ResponseBody
Expand All @@ -28,18 +29,18 @@ public Page<CategoryDTO> getCategory(@RequestParam(value = "page", defaultValue
@PostMapping("/api/category")
@ResponseStatus(HttpStatus.CREATED)
public void addCategory(@RequestBody Category category) {
categoryService.save(category);
categoryService.saveCategory(category);
}

@PutMapping("/api/category")
@ResponseStatus(HttpStatus.OK)
public void updateCategory(@RequestBody CategoryDTO categoryDTO) {
categoryService.update(categoryDTO);
categoryService.updateCategory(categoryDTO);
}

@DeleteMapping("/api/category/{id}")
public String deleteCategory(@PathVariable("id") int id) {
categoryService.delete(id);
categoryService.deleteCategory(id);
return "redirect:/api/category";
}

Expand Down
20 changes: 12 additions & 8 deletions src/main/java/gift/controller/OptionController.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
package gift.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import gift.dto.option.OptionQuantityDTO;
import gift.dto.option.OrderResponseDTO;
import gift.dto.option.SaveOptionDTO;
import gift.dto.option.UpdateOptionDTO;
import gift.service.OptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequiredArgsConstructor
public class OptionController {
@Autowired
OptionService optionService;
private final OptionService optionService;

@PostMapping("/api/option")
@ResponseStatus(HttpStatus.CREATED)
public void addOption(@RequestBody SaveOptionDTO saveOptionDTO) {
optionService.add(saveOptionDTO);
optionService.saveOption(saveOptionDTO);
}

@DeleteMapping("/api/option/{id}")
@ResponseStatus(HttpStatus.CREATED)
public void deleteOption(@PathVariable int id) {
optionService.delete(id);
optionService.deleteOption(id);
}

@PutMapping("/api/option")
@ResponseStatus(HttpStatus.OK)
public void updateOption(@RequestBody UpdateOptionDTO updateOptionDTO) {
optionService.update(updateOptionDTO);
optionService.updateOption(updateOptionDTO);
}

@PostMapping("/api/option/refill")
@ResponseStatus(HttpStatus.OK)
public void refill(@RequestBody OptionQuantityDTO optionQuantityDTO) {
optionService.refill(optionQuantityDTO);
optionService.refillQuantity(optionQuantityDTO);
}

@PostMapping("/api/option/order")
@ResponseStatus(HttpStatus.OK)
public void order(@RequestBody OptionQuantityDTO optionQuantityDTO) {
optionService.order(optionQuantityDTO);
@ResponseBody
public OrderResponseDTO order(@RequestHeader("Authorization") String token, @RequestBody OptionQuantityDTO optionQuantityDTO) {
return optionService.order(optionQuantityDTO, token);
}
}
5 changes: 3 additions & 2 deletions src/main/java/gift/controller/ProductAdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gift.dto.product.ProductWithOptionDTO;
import gift.service.ProductService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -18,9 +19,9 @@


@Controller
@RequiredArgsConstructor
public class ProductAdminController {
@Autowired
private ProductService productService;
private final ProductService productService;

@GetMapping("/admin/products")
public String adminProducts(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "0") int pageNum) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/gift/controller/ProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import gift.dto.product.ShowProductDTO;
import gift.entity.Product;
import gift.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -19,10 +20,9 @@


@Controller
@RequiredArgsConstructor
public class ProductController {
@Autowired
private ProductService productService;

private final ProductService productService;

@ResponseStatus(HttpStatus.OK)
@ResponseBody
Expand All @@ -49,8 +49,8 @@ public String deleteProduct(@PathVariable int Id) {

@ResponseStatus(HttpStatus.OK)
@PutMapping("/api/products")
public String modifyProduct(@RequestBody ModifyProductDTO product) {
productService.modifyProduct(product);
public String updateProduct(@RequestBody ModifyProductDTO product) {
productService.updateProduct(product);
return "redirect:api/products";
}

Expand Down
18 changes: 7 additions & 11 deletions src/main/java/gift/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@
import gift.dto.user.SignUpDTO;
import gift.dto.user.Token;

import gift.service.KakaoService;
import gift.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequiredArgsConstructor
public class UserController {
@Autowired
UserService userService;
@Autowired
KakaoService kakaoService;
@Autowired
ObjectMapper objectMapper;
private final UserService userService;
private final ObjectMapper objectMapper;

@ResponseStatus(HttpStatus.OK)
@GetMapping("/signup")
Expand Down Expand Up @@ -53,10 +49,10 @@ public String signIn(@RequestBody LoginDTO loginDTO) throws JsonProcessingExcept
return objectMapper.writeValueAsString(token);
}

@GetMapping("/api/kakao/token")
@PostMapping("/api/kakao/login")
@ResponseBody
public String getToken(){
return kakaoService.getToken();
public Token kakakLogin() {
return userService.kakaoLogin();
}

}
7 changes: 4 additions & 3 deletions src/main/java/gift/controller/WishListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import gift.dto.product.ShowProductDTO;
import gift.service.WishListService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -16,14 +17,14 @@


@Controller
@RequiredArgsConstructor
public class WishListController {
@Autowired
WishListService wishListService;
private final WishListService wishListService;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/api/wishlist/{product_id}")
public String addWishList(@RequestHeader("Authorization") String token, @PathVariable int product_id) {
wishListService.add(token, product_id);
wishListService.saveWishList(token, product_id);
return "redirect:/wishlist";
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gift/dto/option/OptionQuantityDTO.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gift.dto.option;

public record OptionQuantityDTO(int id, int quantity) {
public record OptionQuantityDTO(int optionId, int quantity, String message) {
}
6 changes: 6 additions & 0 deletions src/main/java/gift/dto/option/OrderResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gift.dto.option;

import java.time.LocalDateTime;

public record OrderResponseDTO(int optionId, int quantity, LocalDateTime orderDateTime, String message) {
}
6 changes: 6 additions & 0 deletions src/main/java/gift/dto/user/KakaoToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gift.dto.user;

public record KakaoToken(String access_token, String token_type, String refresh_token, Integer expires_in, String scope,
Integer refresh_token_expires_in) {

}
35 changes: 35 additions & 0 deletions src/main/java/gift/entity/KakaoUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gift.entity;

import jakarta.persistence.*;

@Entity
public class KakaoUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;

Long kakaoUserId;

@OneToOne(fetch = FetchType.LAZY)
User user;

public KakaoUser() {
}

public KakaoUser(Long kakaoUserId, User user) {
this.kakaoUserId = kakaoUserId;
this.user = user;
}

public int getId() {
return id;
}

public Long getKakaoUserId() {
return kakaoUserId;
}

public User getUser() {
return user;
}
}
Loading