Skip to content

Commit

Permalink
Merge branch 'dev' into feat/#20-user-healthInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jjjjjinseo authored Jul 27, 2024
2 parents 7cfbbaf + 6c03dc0 commit e135481
Show file tree
Hide file tree
Showing 15 changed files with 515 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.beginnerfitbe.declaration.controller;

import com.example.beginnerfitbe.declaration.dto.DeclarationDto;
import com.example.beginnerfitbe.declaration.dto.DeclarationReqDto;
import com.example.beginnerfitbe.declaration.service.DeclarationService;
import com.example.beginnerfitbe.jwt.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
public class DeclarationController {

private final DeclarationService declarationService;
private final JwtUtil jwtUtil;

@PostMapping("/{postId}/declarations")
@Operation(summary = "게시글 신고 메서드", description = "게시글을 신고합니다.")
public ResponseEntity<?> create(HttpServletRequest request, @PathVariable Long postId, @RequestBody DeclarationReqDto declarationReqDto) {
Long userId = jwtUtil.getUserId(jwtUtil.resolveToken(request).substring(7));
return ResponseEntity.ok(declarationService.create(userId, postId, declarationReqDto));
}

@GetMapping("/declarations")
@Operation(summary = "신고 목록 조회 메서드", description = "전체 신고 목록을 조회합니다.")
public ResponseEntity<?> list() {
return ResponseEntity.ok(declarationService.list());
}

@GetMapping("/{postId}/declarations")
@Operation(summary = "게시글 별 신고 목록 조회 메서드", description = "게시글 별 신고 목록을 조회합니다.")
public ResponseEntity<?> getDeclarationsByPost(@PathVariable Long postId) {
return ResponseEntity.ok(declarationService.getDeclarationsByPost(postId));
}

@DeleteMapping("/{postId}/declarations")
@Operation(summary = "게시글 신고 취소 메서드", description = "게시글을 신고를 취소 합니다.")
public ResponseEntity<?> delete(HttpServletRequest request, @PathVariable Long postId) {
Long userId = jwtUtil.getUserId(jwtUtil.resolveToken(request).substring(7));
return ResponseEntity.ok(declarationService.delete(userId, postId));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.beginnerfitbe.declaration.domain;

import com.example.beginnerfitbe.declaration.util.DeclarationReason;
import com.example.beginnerfitbe.post.domain.Post;
import com.example.beginnerfitbe.user.domain.User;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
public class Declaration {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ManyToOne
@JoinColumn(name = "post_id", nullable = false)
private Post post;

@Enumerated(EnumType.STRING)
private DeclarationReason reason;


@Builder
private Declaration (User user, Post post, DeclarationReason reason){
this.user = user;
this.post = post;
this.reason = reason;
}

public void updateReason(DeclarationReason declarationReason) {
this.reason = declarationReason;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.beginnerfitbe.declaration.dto;

import com.example.beginnerfitbe.declaration.domain.Declaration;
import lombok.Data;

@Data
public class DeclarationDto {
private Long userId;
private String userName;
private Long postId;
private String postTitle;
private String reason;

public DeclarationDto(Long userId, String userName, Long postId, String postTitle, String reason) {
this.userId = userId;
this.userName = userName;
this.postId = postId;
this.postTitle = postTitle;
this.reason = reason;
}

public static DeclarationDto fromEntity(Declaration declaration) {
return new DeclarationDto(
declaration.getUser().getId(),
declaration.getUser().getName(),
declaration.getPost().getId(),
declaration.getPost().getTitle(),
declaration.getReason().toString()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.beginnerfitbe.declaration.dto;

import lombok.Data;

@Data
public class DeclarationReqDto {
private String reason;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.beginnerfitbe.declaration.repository;

import com.example.beginnerfitbe.declaration.domain.Declaration;
import com.example.beginnerfitbe.declaration.dto.DeclarationDto;
import com.example.beginnerfitbe.post.domain.Post;
import com.example.beginnerfitbe.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

@Repository
public interface DeclarationRepository extends JpaRepository<Declaration, Long> {
Optional<Declaration> findByUserAndPost(User user, Post post);
List<Declaration> findByPost(Post post);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.example.beginnerfitbe.declaration.service;

import com.example.beginnerfitbe.declaration.domain.Declaration;
import com.example.beginnerfitbe.declaration.dto.DeclarationDto;
import com.example.beginnerfitbe.declaration.dto.DeclarationReqDto;
import com.example.beginnerfitbe.declaration.repository.DeclarationRepository;
import com.example.beginnerfitbe.declaration.util.DeclarationReason;
import com.example.beginnerfitbe.error.StateResponse;
import com.example.beginnerfitbe.post.domain.Post;
import com.example.beginnerfitbe.post.repository.PostRepository;
import com.example.beginnerfitbe.post.service.PostService;
import com.example.beginnerfitbe.user.domain.User;
import com.example.beginnerfitbe.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DeclarationService {

private final UserRepository userRepository;
private final PostRepository postRepository;
private final DeclarationRepository declarationRepository;
private final PostService postService;

public StateResponse create(Long userId, Long postId, DeclarationReqDto declarationReqDto) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("Post not found"));

if (user.getId().equals(post.getUser().getId())) {
throw new IllegalArgumentException("본인 게시물 신고 불가능");
}

DeclarationReason reason;
try {
reason = DeclarationReason.valueOf(declarationReqDto.getReason());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid declaration reason");
}

Optional<Declaration> existingReport = declarationRepository.findByUserAndPost(user, post);

//신고사유만 변경
if (existingReport.isPresent()) {
Declaration report = existingReport.get();
report.updateReason(reason);

declarationRepository.save(report);
return StateResponse.builder()
.code("SUCCESS")
.message("신고 사유가 변경되었습니다.")
.build();
} else {

if (post.getDeclarations().size() >= 9) {
postService.delete(postId, post.getUser().getId()); // 게시글 삭제
return StateResponse.builder()
.code("SUCCESS")
.message("신고 10번 누적으로 게시글이 삭제되었습니다.")
.build();
}

Declaration declaration = Declaration.builder()
.post(post)
.user(user)
.reason(reason)
.build();
declarationRepository.save(declaration);
return StateResponse.builder()
.code("SUCCESS")
.message("SUCCESS 게시글이 정상적으로 신고되었습니다.")
.build();
}
}

public List<DeclarationDto> list(){
return declarationRepository.findAll().stream()
.map(DeclarationDto::fromEntity)
.collect(Collectors.toList());
}

public List<DeclarationDto> getDeclarationsByPost(Long postId){
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("Post not found"));

return declarationRepository.findByPost(post).stream()
.map(DeclarationDto::fromEntity)
.collect(Collectors.toList());
}

public StateResponse delete(Long userId, Long postId){
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("Post not found"));
Optional<Declaration> existingReport = declarationRepository.findByUserAndPost(user, post);

if (existingReport.isPresent()) {
declarationRepository.delete(existingReport.get());
return StateResponse.builder()
.code("SUCCESS")
.message("신고가 취소되었습니다.")
.build();
}
return StateResponse.builder()
.code("FAIL")
.message("신고 내역이 없습니다.")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.beginnerfitbe.declaration.util;

public enum DeclarationReason {
마음에들지않아요,
선정적이에요,
테러를조장해요,
부적절해요,
스팸이에요,
혐오발언이에요,
공격적인내용이있어요,
거짓정보가포함돼있어요,
기타사유
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.beginnerfitbe.like.controller;

import com.example.beginnerfitbe.jwt.util.JwtUtil;
import com.example.beginnerfitbe.like.service.PostLikeService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
public class PostLikeController {
private final JwtUtil jwtUtil;
private final PostLikeService postLikeService;

@PostMapping("/{postId}/likes")
@Operation(summary = "게시글 좋아요 메서드", description = "게시글에 좋아요를 생성합니다.")
public ResponseEntity<?> addLike(HttpServletRequest request, @PathVariable Long postId){
Long userId = jwtUtil.getUserId(jwtUtil.resolveToken(request).substring(7));
return ResponseEntity.ok(postLikeService.create(userId, postId));
}

@DeleteMapping("/{postId}/likes")
@Operation(summary = "게시글 좋아요 취소 메서드", description = "게시글에 생성한 좋아요를 취소합니다.")
public ResponseEntity<?> deleteLike(HttpServletRequest request, @PathVariable Long postId){
Long userId = jwtUtil.getUserId(jwtUtil.resolveToken(request).substring(7));
return ResponseEntity.ok(postLikeService.delete(userId, postId));
}

@GetMapping("/likes/me")
@Operation(summary = "사용자 좋아요 조회 메소드", description = "사용자가 좋아요 한 내역을 조회합니다..")
public ResponseEntity<?> getLikesByUser(HttpServletRequest request) {
Long userId = jwtUtil.getUserId(jwtUtil.resolveToken(request).substring(7));
return ResponseEntity.ok(postLikeService.getLikesByUser(userId));
}

@GetMapping("/{postId}/likes")
@Operation(summary = "게시글 별 좋아요 조회 메소드", description = "게시글의 좋아요 내역을 조회합니다.")
public ResponseEntity<?> getLikesByPost(@PathVariable Long postId) {
return ResponseEntity.ok(postLikeService.getLikesByPost(postId));
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/example/beginnerfitbe/like/domain/PostLike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.beginnerfitbe.like.domain;

import com.example.beginnerfitbe.post.domain.Post;
import com.example.beginnerfitbe.user.domain.User;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;

import static jakarta.persistence.FetchType.LAZY;

@Entity
@Getter
@NoArgsConstructor
public class PostLike {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = LAZY)
private Post post;

@ManyToOne(fetch = LAZY)
private User user;

@CreatedDate
private LocalDateTime createdAt;

@Builder
public PostLike(Post post, User user, LocalDateTime createdAt) {
this.post = post;
this.user = user;
this.createdAt = createdAt;
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/example/beginnerfitbe/like/dto/PostLikeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.beginnerfitbe.like.dto;

import com.example.beginnerfitbe.like.domain.PostLike;
import lombok.Data;

import java.time.LocalDateTime;

@Data
public class PostLikeDto {
private Long id;
private Long userId;
private Long postId;
private LocalDateTime createdAt;

public PostLikeDto(Long id, Long userId, Long postId, LocalDateTime createdAt) {
this.id = id;
this.userId = userId;
this.postId = postId;
this.createdAt = createdAt;
}

public static PostLikeDto fromEntity(PostLike postLike){
return new PostLikeDto(
postLike.getId(),
postLike.getUser().getId(),
postLike.getPost().getId(),
postLike.getCreatedAt());

}
}
Loading

0 comments on commit e135481

Please sign in to comment.