From ac3e5475ac14cd8bfded560baf07d01db52fbe82 Mon Sep 17 00:00:00 2001 From: LeeJaeHyeok97 Date: Wed, 30 Aug 2023 13:58:32 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]#16=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=EC=99=84=EB=A3=8C(?= =?UTF-8?q?=EC=B1=84=EC=9A=A9=EA=B3=B5=EA=B3=A0=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C=20=EC=8B=9C=20=EC=97=B0=EA=B2=B0,=20Response?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=ED=95=84=EC=9A=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../heart/application/HeartService.java | 65 +++++++++++++++++++ .../rcp1/domain/heart/domain/Heart.java | 31 +++++++++ .../domain/repository/HeartRepository.java | 14 ++++ .../rcp1/domain/heart/dto/HeartReq.java | 8 +++ .../heart/presentation/HeartController.java | 44 +++++++++++++ .../domain/user/application/UserService.java | 1 + .../example/rcp1/domain/user/domain/User.java | 7 +- .../rcp1/domain/user/dto/SignUpReq.java | 2 + .../config/security/SecurityConfig.java | 1 + 9 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/rcp1/domain/heart/application/HeartService.java create mode 100644 src/main/java/com/example/rcp1/domain/heart/domain/Heart.java create mode 100644 src/main/java/com/example/rcp1/domain/heart/domain/repository/HeartRepository.java create mode 100644 src/main/java/com/example/rcp1/domain/heart/dto/HeartReq.java create mode 100644 src/main/java/com/example/rcp1/domain/heart/presentation/HeartController.java diff --git a/src/main/java/com/example/rcp1/domain/heart/application/HeartService.java b/src/main/java/com/example/rcp1/domain/heart/application/HeartService.java new file mode 100644 index 0000000..6410e57 --- /dev/null +++ b/src/main/java/com/example/rcp1/domain/heart/application/HeartService.java @@ -0,0 +1,65 @@ +package com.example.rcp1.domain.heart.application; + +import com.example.rcp1.domain.heart.domain.Heart; +import com.example.rcp1.domain.heart.domain.repository.HeartRepository; +import com.example.rcp1.domain.user.domain.User; +import com.example.rcp1.domain.user.domain.repository.UserRepository; +import com.example.rcp1.domain.user.presentation.UserController; +import com.example.rcp1.global.config.security.util.JwtUtil; +import io.jsonwebtoken.Jwts; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.swing.text.html.Option; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class HeartService { + + @Value("${SECRET_KEY}") + private String secret_key; + + private final HeartRepository heartRepository; + private final UserRepository userRepository; +// private final CompanyRepository companyRepository; + + @Transactional + public String insert(String access_token, Long postId) { + + String email = JwtUtil.getUserEmail(access_token, secret_key); + + Optional optionalUser = userRepository.findByEmail(email); + + User user = optionalUser.get(); + + if (heartRepository.findByUser(user).isPresent()) { + return null; + } + + Heart heart = Heart.builder() + .user(user) + .build(); + + heartRepository.save(heart); + + return "success"; + + } + + @Transactional + public void delete(String access_token, Long postId) { + + String email = JwtUtil.getUserEmail(access_token, secret_key); + + User user = userRepository.findByEmail(email).get(); + + // Company company = companyRepository.findById(postId); + + Heart heart = heartRepository.findByUser(user).get(); // company 코드 구현되면 findByUserAndCompany로 수정해야함 + + heartRepository.delete(heart); + } +} diff --git a/src/main/java/com/example/rcp1/domain/heart/domain/Heart.java b/src/main/java/com/example/rcp1/domain/heart/domain/Heart.java new file mode 100644 index 0000000..7f95884 --- /dev/null +++ b/src/main/java/com/example/rcp1/domain/heart/domain/Heart.java @@ -0,0 +1,31 @@ +package com.example.rcp1.domain.heart.domain; + +import com.example.rcp1.domain.user.domain.User; +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@NoArgsConstructor +@Table(name = "HEART") +public class Heart { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + private User user; + +// @ManyToOne(fetch = FetchType.LAZY) +// private Company company; + + + @Builder + public Heart(Long id, User user) { + this.user = user; + } +} diff --git a/src/main/java/com/example/rcp1/domain/heart/domain/repository/HeartRepository.java b/src/main/java/com/example/rcp1/domain/heart/domain/repository/HeartRepository.java new file mode 100644 index 0000000..a44ee85 --- /dev/null +++ b/src/main/java/com/example/rcp1/domain/heart/domain/repository/HeartRepository.java @@ -0,0 +1,14 @@ +package com.example.rcp1.domain.heart.domain.repository; + +import com.example.rcp1.domain.heart.domain.Heart; +import com.example.rcp1.domain.user.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.lang.reflect.Member; +import java.util.Optional; + +public interface HeartRepository extends JpaRepository { + + Optional findByUser(User user); + // Optional findByUserAndCompany(User user, Company company); +} diff --git a/src/main/java/com/example/rcp1/domain/heart/dto/HeartReq.java b/src/main/java/com/example/rcp1/domain/heart/dto/HeartReq.java new file mode 100644 index 0000000..08d0f0a --- /dev/null +++ b/src/main/java/com/example/rcp1/domain/heart/dto/HeartReq.java @@ -0,0 +1,8 @@ +package com.example.rcp1.domain.heart.dto; + +import lombok.Data; + +@Data +public class HeartReq { + +} diff --git a/src/main/java/com/example/rcp1/domain/heart/presentation/HeartController.java b/src/main/java/com/example/rcp1/domain/heart/presentation/HeartController.java new file mode 100644 index 0000000..7789d89 --- /dev/null +++ b/src/main/java/com/example/rcp1/domain/heart/presentation/HeartController.java @@ -0,0 +1,44 @@ +package com.example.rcp1.domain.heart.presentation; + +import com.example.rcp1.domain.heart.application.HeartService; +import com.example.rcp1.domain.heart.dto.HeartReq; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/heart") +public class HeartController { + + private final HeartService heartService; + + // 좋아요 + @PostMapping("/like/{postId}") + public ResponseEntity insert( + @RequestHeader("Authorization") String Authorization, + @PathVariable("postId") Long postId) { + + String access_token = Authorization.substring(7); + + + heartService.insert(access_token, postId); + + return null; + + } + + // 좋아요 취소 + @DeleteMapping("/unlike/{postId}") + public ResponseEntity delete( + @RequestHeader("Authorization") String Authorization, + @PathVariable("postId") Long postId) { + + String access_token = Authorization.substring(7); + + heartService.delete(access_token, postId); + + return null; + } +} diff --git a/src/main/java/com/example/rcp1/domain/user/application/UserService.java b/src/main/java/com/example/rcp1/domain/user/application/UserService.java index b7d5a48..11f82d6 100644 --- a/src/main/java/com/example/rcp1/domain/user/application/UserService.java +++ b/src/main/java/com/example/rcp1/domain/user/application/UserService.java @@ -36,6 +36,7 @@ public User signUp(SignUpReq signUpReq) { User user = User.builder() .email(signUpReq.getEmail()) .password(BCrypt.hashpw(signUpReq.getPassword(), BCrypt.gensalt())) // 패스워드 암호화 + .userType(signUpReq.getUserType()) .name(signUpReq.getName()) .phoneNumber(signUpReq.getPhoneNumber()) .specializedField(signUpReq.getSpecializedField()) diff --git a/src/main/java/com/example/rcp1/domain/user/domain/User.java b/src/main/java/com/example/rcp1/domain/user/domain/User.java index 4564ef8..d6ad007 100644 --- a/src/main/java/com/example/rcp1/domain/user/domain/User.java +++ b/src/main/java/com/example/rcp1/domain/user/domain/User.java @@ -29,6 +29,9 @@ public class User extends BaseEntity { @Column private String password; + @Column + private String userType; + @Column private String name; @@ -61,10 +64,11 @@ public class User extends BaseEntity { @Builder - public User(Long id, String email, String password, String name, String phoneNumber, String specializedField, Long career, String position, String school, String job, LocalDateTime created, LocalDateTime updated, String status) { + public User(Long id, String email, String password, String userType, String name, String phoneNumber, String specializedField, Long career, String position, String school, String job, LocalDateTime created, LocalDateTime updated, String status) { this.id = id; this.email = email; this.password = password; + this.userType = userType; this.name = name; this.phoneNumber = phoneNumber; this.specializedField = specializedField; @@ -78,6 +82,7 @@ public User(Long id, String email, String password, String name, String phoneNum } + // 논리 삭제 상태 수정 public void setStatusD() { this.status = "D"; diff --git a/src/main/java/com/example/rcp1/domain/user/dto/SignUpReq.java b/src/main/java/com/example/rcp1/domain/user/dto/SignUpReq.java index 7f4076e..2173158 100644 --- a/src/main/java/com/example/rcp1/domain/user/dto/SignUpReq.java +++ b/src/main/java/com/example/rcp1/domain/user/dto/SignUpReq.java @@ -16,6 +16,8 @@ public class SignUpReq { @NotBlank private String password; + private String userType; + private String name; private String phoneNumber; diff --git a/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java b/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java index 9eeea5a..ecaa79d 100644 --- a/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java +++ b/src/main/java/com/example/rcp1/global/config/security/SecurityConfig.java @@ -34,6 +34,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .cors().and() .authorizeHttpRequests() .requestMatchers("/user/signUp", "/user/signIn", "/user/delete", "/user/profile").permitAll() + .requestMatchers("/heart/**").permitAll() .requestMatchers(HttpMethod.POST, "/user/**").authenticated() .and() .sessionManagement()