rest controller practice

This commit is contained in:
haerong22
2021-03-13 22:06:14 +09:00
parent 706c9cd32c
commit fa9d7c6919
4 changed files with 50 additions and 4 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -21,4 +21,6 @@ public interface UserService {
List<UserLogCount> getUserLikeBest();
ServiceResult addInterestUser(Long id, String email);
ServiceResult deleteInterestUser(Long id, String email);
}

View File

@@ -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();
}
}