Skip to content

Commit

Permalink
feat: add an API for moment detail (#80)
Browse files Browse the repository at this point in the history
#### What type of PR is this?

/kind feature

#### What this PR does / why we need it:

提供获取单条瞬间详细信息的接口,此接口返回数据包括瞬间作者信息及点赞数、评论数等。

#### How to test it?

测试接口 `/apis/console.api.moment.halo.run/v1alpha1/moments/{name}` 是否可用。

#### Which issue(s) this PR fixes:

Fixes #74 

#### Does this PR introduce a user-facing change?
```release-note
提供获取单条瞬间详细信息的接口
```
  • Loading branch information
LIlGG authored Feb 21, 2024
1 parent c8f66d0 commit e41c389
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/run/halo/moments/ListedMoment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
@Builder
public class ListedMoment {

@Schema(required = true)
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private Moment moment;

@Schema(required = true)
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private Contributor owner;

@Schema(required = true)
@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
private Stats stats;
}
20 changes: 20 additions & 0 deletions src/main/java/run/halo/moments/MomentEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public RouterFunction<ServerResponse> endpoint() {
);
QueryParamBuildUtil.buildParametersFromType(builder, MomentQuery.class);
})
.GET("moments/{name}", this::getMoment,
builder -> builder.operationId("GetMoment")
.description("Get a moment by name.")
.tag(tag)
.parameter(parameterBuilder()
.name("name")
.in(ParameterIn.PATH)
.description("Moment name")
.required(true)
.implementation(String.class)
)
.response(responseBuilder()
.implementation(ListedMoment.class)
))
.GET("tags", this::listTags,
builder -> builder.operationId("ListTags")
.description("List all moment tags.")
Expand Down Expand Up @@ -78,6 +92,12 @@ public RouterFunction<ServerResponse> endpoint() {
.build();
}

private Mono<ServerResponse> getMoment(ServerRequest request) {
var name = request.pathVariable("name");
return momentService.findMomentByName(name)
.flatMap(moment -> ServerResponse.ok().bodyValue(moment));
}

@Override
public GroupVersion groupVersion() {
return GroupVersion.parseAPIVersion("console.api.moment.halo.run/v1alpha1");
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/run/halo/moments/exception/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package run.halo.moments.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

/**
* NotFoundException.
*
* @author LIlGG
*/
public class NotFoundException extends ResponseStatusException {
public NotFoundException(String reason) {
super(HttpStatus.NOT_FOUND, reason);
}
}
2 changes: 2 additions & 0 deletions src/main/java/run/halo/moments/service/MomentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface MomentService {
Mono<Moment> create(Moment moment);

Flux<String> listAllTags();

Mono<ListedMoment> findMomentByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.Counter;
Expand All @@ -20,6 +21,7 @@
import run.halo.moments.Moment;
import run.halo.moments.MomentQuery;
import run.halo.moments.Stats;
import run.halo.moments.exception.NotFoundException;
import run.halo.moments.service.MomentService;
import run.halo.moments.util.MeterUtils;

Expand Down Expand Up @@ -76,6 +78,13 @@ public Flux<String> listAllTags() {
.distinct();
}

@Override
public Mono<ListedMoment> findMomentByName(String name) {
return client.fetch(Moment.class, name)
.switchIfEmpty(Mono.error(new NotFoundException("Moment not found.")))
.flatMap(this::toListedMoment);
}

private Mono<ListedMoment> toListedMoment(Moment moment) {
ListedMoment.ListedMomentBuilder momentBuilder = ListedMoment.builder()
.moment(moment);
Expand Down

0 comments on commit e41c389

Please sign in to comment.