Skip to content

Commit

Permalink
투데이 댓글 추가
Browse files Browse the repository at this point in the history
투데이 댓글 CRUD 추가 및 시큐리티 추가
  • Loading branch information
duhwan05 committed Aug 22, 2024
1 parent ac28619 commit 4e7d21e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ protected void configure(HttpSecurity http) throws Exception {
.cors().configurationSource(corsConfigurationSource()) // CORS 설정 추가
.and()
.authorizeRequests()
.antMatchers("/community/register", "/community/update", "/community/delete/**","/community/recommend/**","/community/myCommunityContents").authenticated()
.antMatchers("/user/one","/user/delete","/user/update").authenticated()
.antMatchers("/community/register", "/community/update", "/community/delete/**","/community/recommend/**","/community/myCommunityContents").authenticated()
.antMatchers("/communityComments/insert", "/communityComments/update", "/communityComments/delete/**").authenticated()
.antMatchers("/today/create","/today/myTodayContents","/today/update/**","/today/delete/**","/today/todayDetail/**").authenticated()
.antMatchers("/todayComments/register", "/todayComments/update", "/todayComments/delete/**").authenticated()
.anyRequest().permitAll()
.and()
.addFilterBefore(new JwtAuthenticationFilter(jwtUtil), UsernamePasswordAuthenticationFilter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,66 @@
import com.example.healthylife.service.TodayCommentsService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/todayComments")
@RequiredArgsConstructor
public class TodayCommentsController {
private final TodayCommentsService todayCommentsService;


@ApiOperation(value = "오운완 댓글 전체조회")
@GetMapping("/todayCommentsAll")
public List<TodayCommentsEntity> todayCommentsList (){
return todayCommentsService.todayCommentsList();
}
private final TodayCommentsService todayCommentsService;

@ApiOperation(value = "오운완 댓글 작성")
@PostMapping("/register")
public TodayCommentsEntity register(@RequestBody TodayCommentsEntity todayCommentsEntity){
return todayCommentsService.insertTodayComments(todayCommentsEntity);
}
public ResponseEntity<TodayCommentsEntity> register(@RequestBody TodayCommentsEntity todayCommentsEntity, Authentication authentication){
String userId = authentication.getName();
TodayCommentsEntity savedComment = todayCommentsService.insertTodayComments(todayCommentsEntity,userId);

return ResponseEntity.status(HttpStatus.CREATED).body(savedComment);
}


@ApiOperation(value = "오운완 내가 쓴 댓글 조회")
@GetMapping("/myTodayCommentsContents")
public List<TodayCommentsEntity> myTodayCommentsContents(@RequestParam String userId){
return todayCommentsService.findMyTodayComments(userId);

@ApiOperation(value = "오운완 댓글 수정")
@PostMapping("/update")
public ResponseEntity<TodayCommentsEntity> todayCommentsUpdate(@RequestBody TodayCommentsEntity updateTodayCommentsEntity, Authentication authentication){
String userId = authentication.getName();
try {
TodayCommentsEntity updatedComment = todayCommentsService.updateComments(
updateTodayCommentsEntity.getTodayCommentsSq(),
updateTodayCommentsEntity,
userId
);
return ResponseEntity.ok(updatedComment);
} catch (SecurityException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

@ApiOperation(value = "오운완 댓글 삭제")
@PostMapping("/todayCommentsDelete")
public Boolean todayCommentsDelete(@RequestParam long todayCommentsSq){
todayCommentsService.deleteByTodayCommentsSq(todayCommentsSq);
return true;
@PostMapping("/delete/{todayCommentSq}")
public ResponseEntity<Void> todayCommentsDelete(@PathVariable("todayCommentSq") Long todayCommentsSq, Authentication authentication){
String userId = authentication.getName();

try {
todayCommentsService.deleteByTodayCommentsSq(todayCommentsSq,userId);
return ResponseEntity.noContent().build();
} catch (SecurityException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

// @ApiOperation(value = "오운완 내가 쓴 댓글 조회")
// @GetMapping("/myTodayCommentsContents")
// public List<TodayCommentsEntity> myTodayCommentsContents(@RequestParam String userId){
// return todayCommentsService.findMyTodayComments(userId);
//
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public class TodayCommentsEntity implements Serializable {
@JoinColumn(name = "user_sq")
private UserEntity user;




//builder
@Builder(toBuilder = true)
public TodayCommentsEntity(long todayCommentsSq, String todayCommentsContents, Date todayCommentsCreated,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.example.healthylife.repository;

import com.example.healthylife.entity.CommunityEntity;
import com.example.healthylife.entity.TodayCommentsEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface TodayCommentsRepository extends JpaRepository<TodayCommentsEntity,Long> {

//내가 작성한 글 조회하(유저아이디로)
List<TodayCommentsEntity> findByUserUserId(String userId);

List<TodayCommentsEntity> findByTodayEntity_todaySq(long todaySq);
List<TodayCommentsEntity> findByTodayCommentsSq(Long todayCommentsSq);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class CommunityCommentsService {
private final CommunityCommentsRepository communityCommentsRepository;
private final UserRepository userRepository;

// 댓글 작성
// 커뮤니티 댓글 작성
public CommunityCommentsEntity insertComments(CommunityCommentsEntity communityCommentsEntity, String userId) {
UserEntity user = userRepository.findByUserId(userId)
.orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다."));
communityCommentsEntity.setUser(user); // UserEntity 설정
return communityCommentsRepository.save(communityCommentsEntity);
}

// 댓글 수정
// 커뮤니티 댓글 수정
public CommunityCommentsEntity updateComments(Long commentsSq, CommunityCommentsEntity updatedCommunityCommentsEntity, String userId) {
CommunityCommentsEntity existingComment = communityCommentsRepository.findById(commentsSq)
.orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다."));
Expand All @@ -51,11 +51,10 @@ public void deleteBySq(Long commentsSq, String userId) {
if (!existingComment.getUser().getUserId().equals(userId)) {
throw new AccessDeniedException("해당 댓글을 삭제할 권한이 없습니다.");
}

communityCommentsRepository.delete(existingComment);
}


// 추후 리팩토링 시 삭제예정
public List<CommunityCommentsEntity> getCommentsByCommunitySq(Long communitySq) {
return communityCommentsRepository.findByCommunityCommunitySq(communitySq);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
package com.example.healthylife.service;

import com.example.healthylife.entity.TodayCommentsEntity;
import com.example.healthylife.entity.UserEntity;
import com.example.healthylife.repository.TodayCommentsRepository;
import com.example.healthylife.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;

import java.util.List;
@RequiredArgsConstructor
@Service
public class TodayCommentsService {

private final TodayCommentsRepository todayCommentsRepository;
private final UserRepository userRepository;


public List<TodayCommentsEntity> todayCommentsList() {
return todayCommentsRepository.findAll();
// 오운완 댓글 작성
public TodayCommentsEntity insertTodayComments(TodayCommentsEntity todayCommentsEntity, String userId) {
UserEntity user = userRepository.findByUserId(userId)
.orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다."));
todayCommentsEntity.setUser(user);
return todayCommentsRepository.save(todayCommentsEntity);
}

public TodayCommentsEntity insertTodayComments(TodayCommentsEntity todayCommentsEntity) {
return todayCommentsRepository.save(todayCommentsEntity);
// 오운완 댓글 수정
public TodayCommentsEntity updateComments(Long todayCommentsSq, TodayCommentsEntity updatedTodayCommentsEntity, String userId) {
TodayCommentsEntity existingComment = todayCommentsRepository.findById(todayCommentsSq)
.orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다."));

if (!existingComment.getUser().getUserId().equals(userId)) {
throw new AccessDeniedException("해당 댓글을 수정할 권한이 없습니다.");
}
existingComment.setTodayCommentsContents(updatedTodayCommentsEntity.getTodayCommentsContents());
existingComment.setTodayCommentsCreated(updatedTodayCommentsEntity.getTodayCommentsCreated());

return todayCommentsRepository.save(existingComment);
}

public void deleteByTodayCommentsSq(long todayCommentsSq) {
todayCommentsRepository.deleteById(todayCommentsSq);
// 오운완 댓글 삭제
public void deleteByTodayCommentsSq(Long todayCommentsSq, String userId) {
TodayCommentsEntity existingComment = todayCommentsRepository.findById(todayCommentsSq)
.orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다."));

if(!existingComment.getUser().getUserId().equals(userId)) {
throw new AccessDeniedException("해당 댓글을 삭제할 권한이 없습니다.");
}
todayCommentsRepository.delete(existingComment);
}

// 추후 리팩토링 시 삭제예정
public List<TodayCommentsEntity> findMyTodayComments(String userId) {
return todayCommentsRepository.findByUserUserId(userId);
}
Expand Down

0 comments on commit 4e7d21e

Please sign in to comment.