Skip to content

Commit

Permalink
[FEAT]#16 좋아요 기능 구현완료(채용공고 구현 완료 시 연결, Response 수정필요)
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 committed Aug 30, 2023
1 parent a3ea806 commit ac3e547
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 1 deletion.
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 src/main/java/com/example/rcp1/domain/heart/domain/Heart.java
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;
}
}
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 src/main/java/com/example/rcp1/domain/heart/dto/HeartReq.java
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 {

}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/example/rcp1/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class User extends BaseEntity {
@Column
private String password;

@Column
private String userType;

@Column
private String name;

Expand Down Expand Up @@ -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;
Expand All @@ -78,6 +82,7 @@ public User(Long id, String email, String password, String name, String phoneNum
}



// 논리 삭제 상태 수정
public void setStatusD() {
this.status = "D";
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/rcp1/domain/user/dto/SignUpReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class SignUpReq {
@NotBlank
private String password;

private String userType;

private String name;

private String phoneNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ac3e547

Please sign in to comment.