Skip to content

Commit

Permalink
feat: add refer, update validator, update jackson, add rpc serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
sixwaaaay committed Jun 8, 2024
1 parent c4082c1 commit 7ef6ad9
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
flags: comments
files: ./graal/target/site/jacoco/jacoco.xml
verbose: true
token: ${{ secrets.CODECOV_TOKEN }}

release:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions graal/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ CREATE TABLE `comments`
`created_at` datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP(3)),
`reply_count` int NOT NULL DEFAULT '0',
`like_count` int NOT NULL DEFAULT '0',
`refer_to` bigint,
PRIMARY KEY (`id`),
INDEX `finder` (`belong_to`, `reply_to`, `id`)
) ENGINE = InnoDB
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023-2024 sixwaaaay.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sixwaaaay.sharingcomment.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfig {

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
builder.serializerByType(Long.class, ToStringSerializer.instance);
builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sixwaaaay.sharingcomment.service.CommentService;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
Expand All @@ -34,6 +35,7 @@
@RestController
@RequestMapping("/comments")
@AllArgsConstructor
@Validated
public class CommentController {

private final CommentService commentService;
Expand Down Expand Up @@ -64,11 +66,11 @@ public CommentResult getMainCommentList(
public ReplyResult getReplyCommentList(
@RequestParam("belong_to") Long belongTo,
@RequestParam("reply_to") Long replyTo,
@RequestParam("page") Optional<Long> id,
@RequestParam(value = "page", defaultValue = "0") long id,
@RequestParam(value = "size", defaultValue = "10") Integer size
) {
var userId = principal.get().map(Principal::getId).orElse(0L);
return commentService.getReplyCommentList(belongTo, replyTo, id.orElse(0L), size, userId);
return commentService.getReplyCommentList(belongTo, replyTo, id, size, userId);
}

/**
Expand All @@ -83,6 +85,7 @@ public Comment createComment(@Valid @RequestBody CommentRequest request) {
comment.setUserId(id); // throw exception if principal is empty
comment.setBelongTo(request.getBelongTo());
comment.setContent(request.getContent());
comment.setReferTo(request.getReferTo());
comment.setReplyTo(request.getReplyTo());
var epochSecond = System.currentTimeMillis() / 1000;
comment.setCreatedAt(LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.ofHours(8)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public class Comment implements Serializable {
@JsonProperty("reply_to")
private Long replyTo;

/**
* The unique identifier of the parent comment.
*/
@Column("refer_to")
@JsonProperty("refer_to")
private Long referTo;

/**
* The unique identifier of the entity to which this comment belongs.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
Expand All @@ -41,6 +42,12 @@ public class CommentRequest {
@JsonProperty("reply_to")
private Long replyTo;

/**
* The ID of the comment that this comment refers to.
*/
@JsonProperty("refer_to")
private Long referTo;

/**
* The ID of the entity to which the comment belongs.
* It must be greater than 0.
Expand All @@ -49,4 +56,12 @@ public class CommentRequest {
@Range(min = 1, message = "belong_to must be greater than 0")
@JsonProperty("belong_to")
private Long belongTo;

/**
* reply_to and parent_id must be both null or not null at the same time
*/
@AssertTrue(message = "reply_to and refer_to must be both null or not null at the same time")
public boolean isValid() {
return (replyTo == null && referTo == null) || (replyTo != null && referTo != null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -117,6 +118,8 @@ public CommentResult getMainCommentList(Long belongTo, Long id, Integer size, Lo
public ReplyResult getReplyCommentList(Long belongTo, Long replyTo, Long id, Integer size, Long userId) {
DbContext.set(DbContextEnum.READ); // set read context
var comments = commentRepo.findByBelongToAndReplyToAndIdGreaterThanOrderByIdAsc(belongTo, replyTo, id, Limit.of(size));
// sort by id asc
comments.sort(Comparator.comparingLong(Comment::getId));
composeComment(comments, userId);
var result = new ReplyResult();
result.setComments(comments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
package io.sixwaaaay.sharingcomment.transmission;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.sixwaaaay.sharingcomment.util.LongRpcSerializer;
import lombok.Data;

@Data
public class ScanVotedReq {
private Integer limit;
@JsonProperty("subject_id")
@JsonSerialize(using = LongRpcSerializer.class)
private Long subjectId;
@JsonProperty("target_type")
private String targetType;
@JsonSerialize(using = LongRpcSerializer.class)
private Long token;
private String type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package io.sixwaaaay.sharingcomment.transmission;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.sixwaaaay.sharingcomment.util.ListLongRpcSerializer;
import io.sixwaaaay.sharingcomment.util.LongRpcSerializer;
import lombok.Data;

import java.util.List;
Expand All @@ -22,10 +25,12 @@
public class VoteExistsReq {
private String type;
@JsonProperty("subject_id")
@JsonSerialize(using = LongRpcSerializer.class)
private Long subjectId;
@JsonProperty("target_type")
private String targetType;
@JsonProperty("target_ids")
@JsonSerialize(using = ListLongRpcSerializer.class)
private List<Long> targetIds;

public VoteExistsReq(long subjectId, List<Long> targetIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package io.sixwaaaay.sharingcomment.transmission;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.sixwaaaay.sharingcomment.util.LongRpcSerializer;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
Expand All @@ -25,12 +27,14 @@ public class VoteReq {
@NotNull
@Range(min = 1, message = "subject_id must be greater than 0")
@JsonProperty("subject_id")
@JsonSerialize(using = LongRpcSerializer.class)
private Long subjectId;
@JsonProperty("target_type")
private String targetType;
@NotNull
@Range(min = 1, message = "target_id must be greater than 0")
@JsonProperty("target_id")
@JsonSerialize(using = LongRpcSerializer.class)
private Long targetId;

public VoteReq(long subjectId, long targetId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023-2024 sixwaaaay.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sixwaaaay.sharingcomment.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.List;

public class ListLongRpcSerializer extends JsonSerializer<List<Long>> {
@Override
public void serialize(List<Long> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartArray();
for (var v : value) {
gen.writeNumber(v);
}
gen.writeEndArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2023-2024 sixwaaaay.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sixwaaaay.sharingcomment.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

public class LongRpcSerializer extends JsonSerializer<Long> {
@Override
public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeNumber(value);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void getReplyCommentListTest() throws Exception {
@Test
public void createCommentTest() throws Exception {
var token = jwtUtil.generateToken("n", "1111111");
var json = "{ \"content\": \"This is a test comment\", \"reply_to\": 1, \"belong_to\": 1 }";
var json = "{ \"content\": \"This is a test comment\", \"reply_to\": 1, \"belong_to\": 1, \"refer_to\": 1 }";
mockMvc.perform(MockMvcRequestBuilders.post("/comments")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit 7ef6ad9

Please sign in to comment.