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
@Bean | |
CorsWebFilter corsFilter() { | |
CorsConfiguration config = new CorsConfiguration(); | |
// Possibly... | |
// config.applyPermitDefaultValues() | |
config.setAllowCredentials(true); | |
// allow access to my dev Angular instance |
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
import org.springframework.stereotype.Component; | |
import org.springframework.web.server.ServerWebExchange; | |
import org.springframework.web.server.WebFilter; | |
import org.springframework.web.server.WebFilterChain; | |
import reactor.core.publisher.Mono; | |
@Component | |
public class StaticWebFilter implements WebFilter { | |
@Override | |
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
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
public Mono<ServerResponse> randomizeScore(ServerRequest request) { | |
String countString = request.pathVariable("count"); | |
int count = Integer.parseInt(countString); | |
if (count < 0 || count > 40) { | |
return ServerResponse.badRequest().body(BodyInserters.fromObject("Count must be between 0 and 40")); | |
} | |
Flux<String> playerNames = this.teamRepository.findAll() | |
.map(Team::getPlayers) |
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
/** | |
* Subscribe to watch updates to a particular team | |
* | |
* @param request - must be of the form /{name} | |
* @return a subscription to a Server Sent Event which | |
* fires every time the requested team is updated in the fantasy_db.teams collection. | |
* The subscription is watching a change stream in MongoDB | |
*/ | |
public Mono<ServerResponse> watchTeam(ServerRequest request) { | |
String teamName = request.pathVariable("name"); |
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
@Bean | |
public RouterFunction<ServerResponse> route(TeamHandler teamHandler) { | |
return RouterFunctions | |
.route(RequestPredicates.GET("/teams"), teamHandler::getTeams) | |
.andRoute(RequestPredicates.GET("/teams/watch"), teamHandler::watchTeams) | |
.andRoute(RequestPredicates.GET("/team/{name}"), teamHandler::watchTeam); | |
} |
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
/** | |
* Return all teams from the fantasy_db.teams collection | |
* | |
* @param request - the request (unused in this operation) | |
* @return a list of all teams | |
*/ | |
public Mono<ServerResponse> getTeams(ServerRequest request) { | |
return ServerResponse.ok() | |
.contentType(MediaType.APPLICATION_JSON) | |
.body(BodyInserters.fromPublisher(this.teamRepository.findAll(), Team.class)); |
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
@Bean | |
public RouterFunction<ServerResponse> route(TeamHandler teamHandler) { | |
return RouterFunctions | |
.route(RequestPredicates.GET("/teams"), teamHandler::getTeams); | |
} |
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
/** | |
* Watch for changes to the teams collection | |
* | |
* @return a subscription to the change stream | |
*/ | |
public Flux<Team> watchForTeamCollectionChanges() { | |
// set changestream options to watch for any changes to the businesses collection | |
ChangeStreamOptions options = ChangeStreamOptions.builder() | |
.filter(Aggregation.newAggregation(Team.class, | |
Aggregation.match( |
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
import com.mongodb.reactivestreams.client.MongoClient; | |
import com.mongodb.reactivestreams.client.MongoClients; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration; | |
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; | |
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; | |
/** | |
* Config class to set up necessary components for watching the MongoDB change stream | |
*/ |
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
import dev.timjones.reactive.data.model.Team; | |
import org.bson.types.ObjectId; | |
import org.springframework.data.mongodb.repository.Query; | |
import org.springframework.data.mongodb.repository.ReactiveMongoRepository; | |
import reactor.core.publisher.Mono; | |
public interface TeamRepository extends ReactiveMongoRepository<Team, ObjectId> { | |
@Query(value = "{ 'players.name' : ?0 }") | |
Mono<Team> findDistinctByPlayerName(String playerName); |