-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT]#16 좋아요 기능 구현완료(채용공고 구현 완료 시 연결, Response 수정필요)
- Loading branch information
1 parent
a3ea806
commit ac3e547
Showing
9 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/com/example/rcp1/domain/heart/application/HeartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<User> 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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/example/rcp1/domain/heart/domain/Heart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/rcp1/domain/heart/domain/repository/HeartRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Heart, Long> { | ||
|
||
Optional<Heart> findByUser(User user); | ||
// Optional<Heart> findByUserAndCompany(User user, Company company); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/rcp1/domain/heart/dto/HeartReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.rcp1.domain.heart.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class HeartReq { | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/example/rcp1/domain/heart/presentation/HeartController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters