Skip to content

Commit

Permalink
Merge pull request #178 from Soongsil-CoffeeChat/feat/#175
Browse files Browse the repository at this point in the history
[FEAT] 신고하기 기능 추가
  • Loading branch information
KimKyoHwee authored Nov 8, 2024
2 parents 196a289 + 622c48c commit e15a20e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.soongsil.CoffeeChat.controller;

import com.soongsil.CoffeeChat.dto.Oauth.CustomOAuth2User;
import com.soongsil.CoffeeChat.dto.ReportDto;
import com.soongsil.CoffeeChat.service.ReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.soongsil.CoffeeChat.enums.RequestUri.REPORT_URI;


@RestController
@RequestMapping(REPORT_URI)
@RequiredArgsConstructor
public class ReportController {
private final ReportService reportService;

private String getUserNameByAuthentication(Authentication authentication) throws Exception {
CustomOAuth2User principal = (CustomOAuth2User)authentication.getPrincipal();
if (principal == null)
throw new Exception(); //TODO : Exception 만들기
return principal.getUsername();
}

@PostMapping("/mentor")
public ResponseEntity<?> createReportMentor(Authentication authentication,
@RequestBody ReportDto request) throws Exception {
ReportDto response = reportService.createReportMentor(request, getUserNameByAuthentication(authentication));
return ResponseEntity.status(HttpStatus.CREATED).body(response);

}

@PostMapping("/mentee")
public ResponseEntity<?> createReportMentee(Authentication authentication,
@RequestBody ReportDto request) throws Exception {
ReportDto response = reportService.createReportMentee(request, getUserNameByAuthentication(authentication));
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/dto/ReportDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.soongsil.CoffeeChat.dto;

import com.soongsil.CoffeeChat.entity.Report;
import com.soongsil.CoffeeChat.enums.ReportReason;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class ReportDto {
//private Long reporterId;
private Long reportedUserId;
private ReportReason reason;
private String additionalDetails;

public static ReportDto from(Report report){
return ReportDto.builder()
.reportedUserId(report.getReportedUserId())
.reason(report.getReason())
.additionalDetails(report.getAdditionalDetails())
.build();
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/entity/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.soongsil.CoffeeChat.entity;

import com.soongsil.CoffeeChat.dto.ReportDto;
import com.soongsil.CoffeeChat.enums.ReportReason;
import com.soongsil.CoffeeChat.enums.ReportStatus;
import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@Table
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class Report {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long reporterId;

// ID of the person being reported (신고 받은 사람 ID, Long 타입)
private Long reportedUserId;

// Reason for the report (e.g., abusive language, spam, inappropriate content, etc.)
private ReportReason reason;

// Additional explanation provided by the reporter
private String additionalDetails;

// Date and time when the report was created
private LocalDateTime reportedAt;

// Status of the report (e.g., PENDING, REVIEWED, ACTION_TAKEN)
private ReportStatus status;

public static Report from(ReportDto dto, Long reporterId){
return Report.builder()
.reporterId(reporterId)
.reportedUserId(dto.getReportedUserId())
.reason(dto.getReason())
.additionalDetails(dto.getAdditionalDetails())
.reportedAt(LocalDateTime.now())
.status(ReportStatus.PENDING)
.build();
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/enums/ReportReason.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.soongsil.CoffeeChat.enums;

public enum ReportReason {
ABUSIVE_LANGUAGE,
SPAM,
INAPPROPRIATE_CONTENT,
PRIVACY_VIOLATION,
OTHER
}
7 changes: 7 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/enums/ReportStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.soongsil.CoffeeChat.enums;

public enum ReportStatus {
PENDING,
REVIEWED,
ACTION_TAKEN
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class RequestUri {
// public static final String REFRESH_URI = "/reissue";
public static final String USER_URI = prefix + "/users";
public static final String EMAIL_URI = "/auth/email";
public static final String REPORT_URI = prefix+"/report";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.soongsil.CoffeeChat.repository;

import com.soongsil.CoffeeChat.entity.Report;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ReportRepository extends JpaRepository<Report, Long> {

}
34 changes: 34 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/service/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.soongsil.CoffeeChat.service;

import com.soongsil.CoffeeChat.dto.ReportDto;
import com.soongsil.CoffeeChat.entity.Report;
import com.soongsil.CoffeeChat.entity.User;
import com.soongsil.CoffeeChat.repository.ReportRepository;
import com.soongsil.CoffeeChat.repository.User.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class ReportService {
private final ReportRepository reportRepository;
private final UserRepository userRepository;

public ReportDto createReportMentor(ReportDto request, String username) throws Exception {
Optional<User> user=userRepository.findByUsername(username);
if (user.isPresent()){
return ReportDto.from(reportRepository.save(Report.from(request, user.get().getId())));
}
else throw new Exception();
}

public ReportDto createReportMentee(ReportDto request, String username) throws Exception {
Optional<User> user=userRepository.findByUsername(username);
if (user.isPresent()){
return ReportDto.from(reportRepository.save(Report.from(request, user.get().getId())));
}
else throw new Exception();
}
}

0 comments on commit e15a20e

Please sign in to comment.