rest controller practice
This commit is contained in:
@@ -7,10 +7,7 @@ import com.example.restcontroller.user.service.UserService;
|
||||
import com.example.restcontroller.util.JWTUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -30,4 +27,17 @@ public class ApiUserInterestController {
|
||||
ServiceResult result = userService.addInterestUser(id, email);
|
||||
return ResponseResult.result(result);
|
||||
}
|
||||
|
||||
@DeleteMapping("/api/user/interest/{id}")
|
||||
public ResponseEntity<?> chapter3_19(@PathVariable Long id,
|
||||
@RequestHeader("TOKEN") String token) {
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
ServiceResult result = userService.deleteInterestUser(id, email);
|
||||
return ResponseResult.result(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,12 @@ import com.example.restcontroller.user.entity.UserInterest;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserInterestRepository extends JpaRepository<UserInterest, Long> {
|
||||
|
||||
Long countByUserAndInterestUser(User user, User interestUser);
|
||||
|
||||
Optional<UserInterest> findByUserAndInterestUser(User user, User interestUser);
|
||||
}
|
||||
|
||||
@@ -21,4 +21,6 @@ public interface UserService {
|
||||
List<UserLogCount> getUserLikeBest();
|
||||
|
||||
ServiceResult addInterestUser(Long id, String email);
|
||||
|
||||
ServiceResult deleteInterestUser(Long id, String email);
|
||||
}
|
||||
|
||||
@@ -101,4 +101,34 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
return ServiceResult.success();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult deleteInterestUser(Long id, String email) {
|
||||
Optional<User> optionalUser = userRepository.findByEmail(email);
|
||||
if (!optionalUser.isPresent()) {
|
||||
return ServiceResult.fail("회원 정보가 없습니다.");
|
||||
}
|
||||
User userEntity = optionalUser.get();
|
||||
|
||||
Optional<User> optionalInterestUser = userRepository.findById(id);
|
||||
if (!optionalInterestUser.isPresent()) {
|
||||
return ServiceResult.fail("관심 사용자에 추가할 회원 정보가 없습니다.");
|
||||
}
|
||||
User interestUserEntity = optionalInterestUser.get();
|
||||
|
||||
Optional<UserInterest> optionalUserInterest = userInterestRepository.findByUserAndInterestUser(userEntity, interestUserEntity);
|
||||
|
||||
if (!optionalUserInterest.isPresent()) {
|
||||
return ServiceResult.fail("삭제할 정보가 없습니다.");
|
||||
}
|
||||
UserInterest userInterestEntity = optionalUserInterest.get();
|
||||
|
||||
if (userInterestEntity.getUser().getId() != userEntity.getId()) {
|
||||
return ServiceResult.fail("자신의 관심자 정보만 삭제 할 수 있습니다.");
|
||||
}
|
||||
|
||||
userInterestRepository.delete(userInterestEntity);
|
||||
return ServiceResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user