Skip to content

Commit 3d74785

Browse files
authored
Merge pull request #12 from prgrms-web-devcourse-final-project/feature/reports&supports
Feature/reports&supports
2 parents 1f603bf + b14db56 commit 3d74785

File tree

20 files changed

+550
-1
lines changed

20 files changed

+550
-1
lines changed

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ dependencies {
5151

5252
// Swagger
5353
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.1")
54+
55+
// Querydsl
56+
implementation("com.querydsl:querydsl-jpa:5.1.0:jakarta")
57+
implementation("com.querydsl:querydsl-apt:5.1.0:jakarta")
58+
annotationProcessor("com.querydsl:querydsl-apt:5.1.0:jakarta")
59+
annotationProcessor("jakarta.persistence:jakarta.persistence-api:3.1.0")
5460
}
5561

5662
tasks.withType<Test> {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.log4u.domain.reports.controller;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import com.example.log4u.domain.reports.dto.ReportCreateRequestDto;
12+
import com.example.log4u.domain.reports.service.ReportService;
13+
14+
import lombok.RequiredArgsConstructor;
15+
16+
@RestController
17+
@RequiredArgsConstructor
18+
@RequestMapping("/reports")
19+
public class ReportsController {
20+
private final ReportService reportService;
21+
22+
@PostMapping("/diaries/{diaryId}")
23+
public ResponseEntity<Void> createReportForDiary(
24+
@RequestBody ReportCreateRequestDto reportCreateRequestDto,
25+
@PathVariable Long diaryId) {
26+
long reporterId = 1L; // SecurityContextHolder 에서 온다고 가정
27+
reportService.createDiaryReport(reporterId, reportCreateRequestDto, diaryId);
28+
return new ResponseEntity<>(HttpStatus.CREATED);
29+
}
30+
31+
@PostMapping("/comments/{commentId}")
32+
public ResponseEntity<Void> createReport(
33+
@RequestBody ReportCreateRequestDto reportCreateRequestDto,
34+
@PathVariable Long commentId) {
35+
long reporterId = 1L;
36+
reportService.createCommentReport(reporterId, reportCreateRequestDto, commentId);
37+
return new ResponseEntity<>(HttpStatus.CREATED);
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.log4u.domain.reports.dto;
2+
3+
import org.hibernate.validator.constraints.Length;
4+
5+
import com.example.log4u.domain.reports.entity.Report;
6+
import com.example.log4u.domain.reports.reportTargetType.ReportTargetType;
7+
import com.example.log4u.domain.reports.reportType.ReportType;
8+
9+
import jakarta.validation.constraints.NotBlank;
10+
import jakarta.validation.constraints.NotNull;
11+
12+
public record ReportCreateRequestDto(
13+
@NotNull
14+
ReportType reportType,
15+
16+
@NotBlank
17+
@Length(min = 2)
18+
String content
19+
) {
20+
public Report toEntity(long reporterId, ReportTargetType reportTargetType, Long targetId) {
21+
return Report.builder()
22+
.reporterId(reporterId)
23+
.reportTargetType(reportTargetType)
24+
.reportType(reportType)
25+
.reportTargetId(targetId)
26+
.content(content)
27+
.build();
28+
}
29+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.example.log4u.domain.reports.entity;
2+
3+
import java.time.LocalDateTime;
4+
5+
import org.springframework.data.annotation.CreatedDate;
6+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
7+
8+
import com.example.log4u.domain.reports.reportTargetType.ReportTargetType;
9+
import com.example.log4u.domain.reports.reportType.ReportType;
10+
11+
import jakarta.persistence.Entity;
12+
import jakarta.persistence.EntityListeners;
13+
import jakarta.persistence.EnumType;
14+
import jakarta.persistence.Enumerated;
15+
import jakarta.persistence.GeneratedValue;
16+
import jakarta.persistence.GenerationType;
17+
import jakarta.persistence.Id;
18+
import lombok.AccessLevel;
19+
import lombok.AllArgsConstructor;
20+
import lombok.Builder;
21+
import lombok.Getter;
22+
import lombok.NoArgsConstructor;
23+
24+
@Builder
25+
@Getter
26+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
27+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
28+
29+
@Entity
30+
@EntityListeners(AuditingEntityListener.class)
31+
public class Report {
32+
@Id
33+
@GeneratedValue(strategy = GenerationType.IDENTITY)
34+
private Long id;
35+
36+
private Long reporterId;
37+
38+
@Enumerated(EnumType.STRING)
39+
private ReportTargetType reportTargetType;
40+
41+
@Enumerated(EnumType.STRING)
42+
private ReportType reportType;
43+
44+
private Long reportTargetId;
45+
46+
private String content;
47+
48+
@CreatedDate
49+
private LocalDateTime createdAt;
50+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.log4u.domain.reports.reportTargetType;
2+
3+
public enum ReportTargetType {
4+
DIARY,
5+
COMMENT
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.log4u.domain.reports.reportType;
2+
3+
public enum ReportType {
4+
INAPPROPRIATE_CONTENT,
5+
FALSE_INFORMATION,
6+
SPAM,
7+
COPYRIGHT_INFRINGEMENT,
8+
PRIVACY_VIOLATION,
9+
ETC
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.log4u.domain.reports.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import com.example.log4u.domain.reports.entity.Report;
6+
7+
public interface ReportRepository extends JpaRepository<Report, Long> {
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.example.log4u.domain.reports.service;
2+
3+
import org.springframework.stereotype.Service;
4+
import org.springframework.transaction.annotation.Transactional;
5+
6+
import com.example.log4u.domain.reports.dto.ReportCreateRequestDto;
7+
import com.example.log4u.domain.reports.entity.Report;
8+
import com.example.log4u.domain.reports.reportTargetType.ReportTargetType;
9+
import com.example.log4u.domain.reports.repository.ReportRepository;
10+
11+
import lombok.RequiredArgsConstructor;
12+
13+
@Service
14+
@Transactional
15+
@RequiredArgsConstructor
16+
public class ReportService {
17+
private final ReportRepository reportRepository;
18+
19+
public void createDiaryReport(
20+
long reporterId,
21+
ReportCreateRequestDto reportCreateRequestDto,
22+
Long diaryId) {
23+
Report report = reportCreateRequestDto.toEntity(reporterId, ReportTargetType.DIARY, diaryId);
24+
reportRepository.save(report);
25+
}
26+
27+
public void createCommentReport(
28+
long reporterId,
29+
ReportCreateRequestDto reportCreateRequestDto,
30+
Long commentId) {
31+
Report report = reportCreateRequestDto.toEntity(reporterId, ReportTargetType.COMMENT, commentId);
32+
reportRepository.save(report);
33+
}
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.log4u.domain.supports.controller;
2+
3+
import org.springframework.data.domain.Page;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import com.example.log4u.domain.supports.dto.SupportCreateRequestDto;
15+
import com.example.log4u.domain.supports.dto.SupportGetResponseDto;
16+
import com.example.log4u.domain.supports.dto.SupportOverviewGetResponseDto;
17+
import com.example.log4u.domain.supports.service.SupportService;
18+
import com.example.log4u.domain.supports.supportType.SupportType;
19+
20+
import jakarta.validation.Valid;
21+
import lombok.RequiredArgsConstructor;
22+
23+
@RestController
24+
@RequiredArgsConstructor
25+
@RequestMapping("/supports")
26+
public class SupportController {
27+
private final SupportService supportService;
28+
29+
@PostMapping
30+
public ResponseEntity<Void> createSupport(
31+
@RequestBody @Valid SupportCreateRequestDto supportCreateRequestDto
32+
) {
33+
long requesterId = 1L;
34+
supportService.createSupport(requesterId, supportCreateRequestDto);
35+
return new ResponseEntity<>(HttpStatus.CREATED);
36+
}
37+
38+
@GetMapping
39+
public ResponseEntity<Page<SupportOverviewGetResponseDto>> getSupportOverviewPage(
40+
@RequestParam(required = false) Integer page,
41+
@RequestParam(required = false) SupportType supportType
42+
) {
43+
long requesterId = 1L;
44+
Page<SupportOverviewGetResponseDto> supportOverviewPage = supportService.getSupportPage(requesterId, page,
45+
supportType);
46+
return ResponseEntity.ok().body(supportOverviewPage);
47+
}
48+
49+
@GetMapping("/{supportId}")
50+
public ResponseEntity<SupportGetResponseDto> getSupportBySupportId(
51+
@PathVariable Long supportId) {
52+
long requesterId = 1L;
53+
SupportGetResponseDto supportGetResponseDto = supportService.getSupportById(requesterId, supportId);
54+
return ResponseEntity.ok().body(supportGetResponseDto);
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.log4u.domain.supports.dto;
2+
3+
import org.hibernate.validator.constraints.Length;
4+
5+
import com.example.log4u.domain.supports.entity.Support;
6+
import com.example.log4u.domain.supports.supportType.SupportType;
7+
8+
import jakarta.validation.constraints.NotBlank;
9+
import jakarta.validation.constraints.NotNull;
10+
11+
public record SupportCreateRequestDto(
12+
@NotNull
13+
SupportType supportType,
14+
15+
@NotBlank
16+
@Length(min = 2)
17+
String title,
18+
19+
@NotBlank
20+
@Length(min = 2)
21+
String content
22+
) {
23+
public Support toEntity(long requesterId) {
24+
return Support.builder()
25+
.requesterId(requesterId)
26+
.supportType(supportType)
27+
.title(title)
28+
.content(content)
29+
.build();
30+
}
31+
}

0 commit comments

Comments
 (0)