This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
| /** | |
| * Enforces a deserialize method to ensure a model class | |
| * can construct itself from a JSON string | |
| */ | |
| export interface Deserializable { | |
| deserialize(input: any): this; | |
| } |
This file contains hidden or 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 {Injectable, NgZone} from '@angular/core'; | |
| import {environment} from '../../environments/environment'; | |
| import {BehaviorSubject, Observable} from "rxjs"; | |
| import {TeamModel} from "../models/team.model"; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class TeamService { | |
| private teamsWatchUrl = environment.backendUrl + environment.watchTeamsPath; |
This file contains hidden or 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
| private teamWatchSource = new BehaviorSubject(new TeamModel()); | |
| _teamWatchSource: Observable<TeamModel> = this.teamWatchSource.asObservable(); |