-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: api group for notification in user center
- Loading branch information
Showing
5 changed files
with
112 additions
and
58 deletions.
There are no files selected for viewing
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
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
95 changes: 95 additions & 0 deletions
95
application/src/main/java/run/halo/app/notification/endpoint/UserNotifierEndpoint.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,95 @@ | ||
package run.halo.app.notification.endpoint; | ||
|
||
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder; | ||
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder; | ||
import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder; | ||
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springdoc.core.fn.builders.schema.Builder; | ||
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import org.springframework.web.reactive.function.server.ServerRequest; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
import org.springframework.web.server.ServerWebInputException; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.core.extension.endpoint.CustomEndpoint; | ||
import run.halo.app.extension.GroupVersion; | ||
import run.halo.app.notification.NotifierConfigStore; | ||
|
||
/** | ||
* Notifier endpoint for user center. | ||
* | ||
* @author guqing | ||
* @since 2.10.0 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class UserNotifierEndpoint implements CustomEndpoint { | ||
|
||
private final NotifierConfigStore notifierConfigStore; | ||
|
||
@Override | ||
public RouterFunction<ServerResponse> endpoint() { | ||
var tag = "api.notification.halo.run/v1alpha1/Notifier"; | ||
return SpringdocRouteBuilder.route() | ||
.GET("/notifiers/{name}/receiverConfig", this::fetchReceiverConfig, | ||
builder -> builder.operationId("FetchReceiverConfig") | ||
.description("Fetch receiver config of notifier") | ||
.tag(tag) | ||
.parameter(parameterBuilder() | ||
.in(ParameterIn.PATH) | ||
.name("name") | ||
.description("Notifier name") | ||
.required(true) | ||
) | ||
.response(responseBuilder().implementation(ObjectNode.class)) | ||
) | ||
.POST("/notifiers/{name}/receiverConfig", this::saveReceiverConfig, | ||
builder -> builder.operationId("SaveReceiverConfig") | ||
.description("Save receiver config of notifier") | ||
.tag(tag) | ||
.parameter(parameterBuilder() | ||
.in(ParameterIn.PATH) | ||
.name("name") | ||
.description("Notifier name") | ||
.required(true) | ||
) | ||
.requestBody(requestBodyBuilder() | ||
.required(true) | ||
.content(contentBuilder() | ||
.mediaType(MediaType.APPLICATION_JSON_VALUE) | ||
.schema(Builder.schemaBuilder() | ||
.implementation(ObjectNode.class)) | ||
) | ||
) | ||
.response(responseBuilder().implementation(Void.class)) | ||
) | ||
.build(); | ||
} | ||
|
||
private Mono<ServerResponse> fetchReceiverConfig(ServerRequest request) { | ||
var name = request.pathVariable("name"); | ||
return notifierConfigStore.fetchReceiverConfig(name) | ||
.flatMap(config -> ServerResponse.ok().bodyValue(config)); | ||
} | ||
|
||
private Mono<ServerResponse> saveReceiverConfig(ServerRequest request) { | ||
var name = request.pathVariable("name"); | ||
return request.bodyToMono(ObjectNode.class) | ||
.switchIfEmpty(Mono.error( | ||
() -> new ServerWebInputException("Request body must not be empty.")) | ||
) | ||
.flatMap(jsonNode -> notifierConfigStore.saveReceiverConfig(name, jsonNode)) | ||
.then(ServerResponse.ok().build()); | ||
} | ||
|
||
@Override | ||
public GroupVersion groupVersion() { | ||
return GroupVersion.parseAPIVersion("api.notification.halo.run/v1alpha1"); | ||
} | ||
} |
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
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