-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from commonground-project/feat/add-internal-issue
Add internal issue api
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/tw/commonground/backend/service/internal/issue/InternalIssueController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tw.commonground.backend.service.internal.issue; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import tw.commonground.backend.service.internal.issue.dto.InternalIssueResponse; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequestMapping("/api/internal/issues") | ||
public class InternalIssueController { | ||
|
||
private final InternalIssueService internalIssueService; | ||
|
||
public InternalIssueController(InternalIssueService internalIssueService) { | ||
this.internalIssueService = internalIssueService; | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<InternalIssueResponse>> getIssues() { | ||
List<InternalIssueResponse> issues = internalIssueService.getIssues(); | ||
return ResponseEntity.ok(issues); | ||
} | ||
|
||
@GetMapping("/{issueId}") | ||
public ResponseEntity<InternalIssueResponse> getIssue(@PathVariable UUID issueId) { | ||
InternalIssueResponse issue = internalIssueService.getIssue(issueId); | ||
return ResponseEntity.ok(issue); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/tw/commonground/backend/service/internal/issue/InternalIssueService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tw.commonground.backend.service.internal.issue; | ||
|
||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import tw.commonground.backend.exception.EntityNotFoundException; | ||
import tw.commonground.backend.service.internal.issue.dto.InternalIssueMapper; | ||
import tw.commonground.backend.service.issue.entity.IssueEntity; | ||
import tw.commonground.backend.service.internal.issue.dto.InternalIssueResponse; | ||
import tw.commonground.backend.service.issue.entity.IssueRepository; | ||
import tw.commonground.backend.service.viewpoint.ViewpointService; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class InternalIssueService { | ||
|
||
private final IssueRepository issueRepository; | ||
private final ViewpointService viewpointService; | ||
|
||
public InternalIssueService(IssueRepository issueRepository, ViewpointService viewpointService) { | ||
this.issueRepository = issueRepository; | ||
this.viewpointService = viewpointService; | ||
} | ||
|
||
public List<InternalIssueResponse> getIssues() { | ||
List<IssueEntity> issues = issueRepository.findAll(); | ||
|
||
return issues.stream() | ||
.map(issue -> { | ||
int viewpointCount = viewpointService.getIssueViewpoints(issue.getId(), Pageable.unpaged()) | ||
.getContent().size(); | ||
return InternalIssueMapper.toResponse(issue, viewpointCount); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public InternalIssueResponse getIssue(UUID issueId) { | ||
IssueEntity issue = issueRepository.findById(issueId).orElseThrow( | ||
() -> new EntityNotFoundException("InternalIssue", "issue id", issueId.toString()) | ||
); | ||
|
||
int viewpointCount = viewpointService.getIssueViewpoints(issueId, Pageable.unpaged()).getContent().size(); | ||
return InternalIssueMapper.toResponse(issue, viewpointCount); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/tw/commonground/backend/service/internal/issue/dto/InternalIssueMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package tw.commonground.backend.service.internal.issue.dto; | ||
|
||
import tw.commonground.backend.service.issue.entity.IssueEntity; | ||
|
||
public final class InternalIssueMapper { | ||
private InternalIssueMapper() { | ||
// hide constructor | ||
} | ||
|
||
public static InternalIssueResponse toResponse(IssueEntity issue, int viewpointCount) { | ||
return new InternalIssueResponse( | ||
issue.getId(), | ||
issue.getCreatedAt(), | ||
issue.getUpdatedAt(), | ||
issue.getTitle(), | ||
issue.getAuthorId(), | ||
0, // TODO: Replace hardcoded viewCount with actual value | ||
viewpointCount | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/tw/commonground/backend/service/internal/issue/dto/InternalIssueResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tw.commonground.backend.service.internal.issue.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
public class InternalIssueResponse { | ||
@JsonProperty("issue_id") | ||
private UUID issueId; | ||
|
||
@JsonProperty("created_at") | ||
private LocalDateTime createdAt; | ||
|
||
@JsonProperty("updated_at") | ||
private LocalDateTime updatedAt; | ||
|
||
private String title; | ||
|
||
@JsonProperty("publisher_id") | ||
private UUID authorId; | ||
|
||
@JsonProperty("view_count") | ||
private int viewCount; | ||
|
||
@JsonProperty("viewpoint_count") | ||
private int viewpointCount; | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/java/tw/commonground/backend/service/internal/issue/dto/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package tw.commonground.backend.service.internal.issue.dto; |
1 change: 1 addition & 0 deletions
1
src/main/java/tw/commonground/backend/service/internal/issue/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package tw.commonground.backend.service.internal.issue; |