Skip to content

Commit

Permalink
Merge pull request #30 from commonground-project/feat/impl-user-endpo…
Browse files Browse the repository at this point in the history
…int-and-metadata

feat: impl author in fact, viewpoint and issue
  • Loading branch information
YukinaMochizuki authored Dec 3, 2024
2 parents 6d5ca69 + b017d59 commit 09e8d02
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import tw.commonground.backend.service.fact.dto.FactRequest;
import tw.commonground.backend.service.fact.dto.FactResponse;
import tw.commonground.backend.service.user.entity.FullUserEntity;
import tw.commonground.backend.shared.pagination.PaginationParser;
import tw.commonground.backend.service.reference.ReferenceRequest;
import tw.commonground.backend.service.reference.ReferenceResponse;
Expand Down Expand Up @@ -36,8 +38,9 @@ public WrappedPaginationResponse<List<FactResponse>> listFacts(@Valid Pagination
}

@PostMapping("/api/facts")
public FactResponse createFact(@Valid @RequestBody FactRequest factRequest) {
return factService.createFact(factRequest);
public FactResponse createFact(@AuthenticationPrincipal FullUserEntity user,
@Valid @RequestBody FactRequest factRequest) {
return factService.createFact(factRequest, user);
}

@GetMapping("/api/fact/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tw.commonground.backend.service.fact.entity.FactEntity;
import tw.commonground.backend.service.fact.entity.FactRepository;
import tw.commonground.backend.service.reference.*;
import tw.commonground.backend.service.user.entity.FullUserEntity;
import tw.commonground.backend.shared.pagination.PaginationMapper;
import tw.commonground.backend.shared.pagination.WrappedPaginationResponse;

Expand Down Expand Up @@ -57,16 +58,14 @@ public FactResponse getFact(UUID id) {
return FactMapper.toResponse(factEntity);
}

public FactResponse createFact(FactRequest factRequest) {
//TODO: Map to logged in user
public FactResponse createFact(FactRequest factRequest, FullUserEntity user) {
FactEntity factEntity = FactEntity.builder()
.title(factRequest.getTitle())
.authorId(1L)
.authorName("aaa")
.authorAvatar("asd")
.references(new HashSet<>())
.build();

factEntity.setAuthor(user);

Set<ReferenceEntity> referenceEntities = parseReferenceEntity(factRequest.getUrls());
factEntity.setReferences(referenceEntities);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static FactResponse toResponse(FactEntity factEntity) {
.updateAt(factEntity.getUpdatedAt())
.authorId(factEntity.getAuthorId())
.authorName(factEntity.getAuthorName())
.authorAvatar(factEntity.getAuthorAvatar())
.references(factEntity.getReferences().stream().map(ReferenceMapper::toResponse).toList())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
public class FactResponse {

private UUID id;

private LocalDateTime createAt;

private LocalDateTime updateAt;

private String title;
private Long authorId;

private UUID authorId;

private String authorName;

private String authorAvatar;

private List<ReferenceResponse> references;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import tw.commonground.backend.service.reference.ReferenceEntity;
import tw.commonground.backend.service.user.entity.BaseEntityWithAuthor;

import java.time.LocalDateTime;
import java.util.Set;
Expand All @@ -18,7 +19,7 @@
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class FactEntity {
public class FactEntity extends BaseEntityWithAuthor {

@Id
@GeneratedValue
Expand All @@ -37,13 +38,6 @@ public class FactEntity {
@Column(nullable = false)
private String title;

// TODO: Waiting User Entity
private Long authorId;

private String authorName;

private String authorAvatar;

@ManyToMany
private Set<ReferenceEntity> references;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import tw.commonground.backend.service.fact.FactService;
import tw.commonground.backend.service.fact.dto.FactMapper;
Expand All @@ -14,6 +15,7 @@
import tw.commonground.backend.service.issue.dto.IssueResponse;
import tw.commonground.backend.service.issue.dto.SimpleIssueResponse;
import tw.commonground.backend.service.issue.entity.IssueEntity;
import tw.commonground.backend.service.user.entity.FullUserEntity;
import tw.commonground.backend.shared.pagination.PaginationMapper;
import tw.commonground.backend.shared.pagination.PaginationRequest;
import tw.commonground.backend.shared.pagination.PaginationParser;
Expand Down Expand Up @@ -56,8 +58,10 @@ public WrappedPaginationResponse<List<SimpleIssueResponse>> listIssues(@Valid Pa
}

@PostMapping("/issues")
public IssueResponse createIssue(@Valid @RequestBody IssueRequest issueRequest) {
IssueEntity issueEntity = issueService.createIssue(issueRequest);
public IssueResponse createIssue(@AuthenticationPrincipal FullUserEntity user,
@Valid @RequestBody IssueRequest issueRequest) {

IssueEntity issueEntity = issueService.createIssue(issueRequest, user);
ContentContainFact contentContainFact = ContentContainFactParser
.separateContentAndFacts(issueEntity.getInsight());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tw.commonground.backend.service.fact.entity.FactRepository;
import tw.commonground.backend.service.issue.dto.IssueRequest;
import tw.commonground.backend.service.issue.entity.*;
import tw.commonground.backend.service.user.entity.FullUserEntity;
import tw.commonground.backend.shared.content.ContentContainFactParser;

import java.util.*;
Expand Down Expand Up @@ -45,7 +46,7 @@ public IssueEntity getIssue(UUID id) {
);
}

public IssueEntity createIssue(IssueRequest request) {
public IssueEntity createIssue(IssueRequest request, FullUserEntity user) {
factService.throwIfFactsNotExist(request.getFacts());

String insight;
Expand All @@ -55,14 +56,11 @@ public IssueEntity createIssue(IssueRequest request) {
throw new ValidationException("Insight is invalid: " + e.getMessage());
}

IssueEntity issueEntity = IssueEntity.builder()
.title(request.getTitle())
.description(request.getDescription())
.insight(insight)
// .authorId(request.getAuthorId())
// .authorName(request.getAuthorName())
// .authorAvatar(request.getAuthorAvatar())
.build();
IssueEntity issueEntity = new IssueEntity();
issueEntity.setTitle(request.getTitle());
issueEntity.setDescription(request.getDescription());
issueEntity.setInsight(insight);
issueEntity.setAuthor(user);
return issueRepository.save(issueEntity);
}

Expand All @@ -86,9 +84,6 @@ public IssueEntity updateIssue(UUID id, IssueRequest issueRequest) {

issueEntity.setTitle(issueRequest.getTitle());
issueEntity.setDescription(issueRequest.getDescription());
// issueEntity.setAuthorId(issueRequest.getAuthorId());
// issueEntity.setAuthorName(issueRequest.getAuthorName());
// issueEntity.setAuthorAvatar(issueRequest.getAuthorAvatar());
return issueRepository.save(issueEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
Expand All @@ -25,7 +26,7 @@ public class IssueResponse {

private String insight;

private String authorId;
private UUID authorId;

private String authorName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import tw.commonground.backend.service.user.entity.BaseEntityWithAuthor;

import java.time.LocalDateTime;
import java.util.Set;
Expand All @@ -17,7 +18,7 @@
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class IssueEntity implements SimpleIssueEntity {
public class IssueEntity extends BaseEntityWithAuthor implements SimpleIssueEntity {

public IssueEntity(UUID id) {
this.id = id;
Expand All @@ -42,13 +43,7 @@ public IssueEntity(UUID id) {
@Column(columnDefinition = "TEXT")
private String insight;

private String authorId;

private String authorName;

private String authorAvatar;

// Added for POST /api/issue/{id}/viewpoints endpoint
// Added for POST /api/issue/{id}/viewpoints endpoint
@OneToMany(mappedBy = "issue")
private Set<ManualIssueFactEntity> manualFacts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ResponseEntity<List<UserResponse>> getUser() {
}

@GetMapping("/user/{username}")
@PreAuthorize("hasRole('ADMIN')")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<UserResponse> getUserByUsername(@PathVariable @NotBlank String username) {
FullUserEntity userEntity = userService.getUserByUsername(username).orElseThrow(
() -> new EntityNotFoundException("User", "username", username));
Expand All @@ -63,7 +63,7 @@ public ResponseEntity<UserResponse> userSetup(@Valid @RequestBody UserSetupReque
return ResponseEntity.ok(response);
}

@GetMapping(value = "/user/profile-image/{username}", produces = "image/png")
@GetMapping(value = "/user/avatar/{username}", produces = "image/png")
public ResponseEntity<byte[]> getProfileImage(@Valid @NotBlank @PathVariable String username) {
return ResponseEntity.ok(userService.getProfileImage(username));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
@Getter
@Setter
public class UserResponse {
// only response data in API spec
private String username;
private String nickname;
private String email;
private String role;

public UserResponse(String username, String nickname, String email, String role) {
this.username = username;
this.nickname = nickname;
this.email = email;
this.role = role;
this.username = username == null ? "" : username;
this.nickname = nickname == null ? "" : nickname;
this.email = email == null ? "" : email;
this.role = role == null ? "" : role;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tw.commonground.backend.service.user.entity;

import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;

import java.util.UUID;

@Getter
@Setter
@MappedSuperclass
public class BaseEntityWithAuthor {
private UUID authorId;

private String authorName;

private String authorAvatar;

public void setAuthor(FullUserEntity user) {
this.authorId = user.getUuid();
this.authorName = user.getNickname();
this.authorAvatar = "/api/user/avatar/" + user.getUsername();
}
}
Loading

0 comments on commit 09e8d02

Please sign in to comment.