mongodb - Is my implementation of a simple CRUD service with Spring WebFlux correct? -
i implementing simple movie based crud api using spring webflux , reactive spring data mongodb. want make sure implementation correct , using flux , mono implement crud operations. want make sure handling errors or null values. new programming paradigm , spring webflux, not sure correctness of implementation of controller , service layer, want make sure adhering spring webflux , project reactor best practices.
@repository public interface movierepository extends reactivemongorepository<movie, string> { flux<movie> findbyrating(string rating); } public interface movieservice { flux<movie> list(); flux<movie> findbyrating(string rating); mono<movie> update(string id, movierequest movierequest); mono<movie> create(mono<movierequest> movierequest); mono<movie> read(string id); mono<movie> delete(string id); } @service public class movieserviceimpl implements movieservice { @autowired private movierepository movierepository; @override public flux<movie> list(){ return movierepository.findall(); } @override public flux<movie> findbyrating(final string rating){ return movierepository.findbyrating(rating); } @override public mono<movie> update(string id, movierequest movierequest) { return movierepository.findone(id).map(existingmovie -> { if(movierequest.getdescription() != null){ existingmovie.setdescription(movierequest.getdescription()); } if(movierequest.getrating() != null){ existingmovie.setrating(movierequest.getrating()); } if(movierequest.gettitle() != null) { existingmovie.settitle(movierequest.gettitle()); } return existingmovie; }).then(movierepository::save); } @override public mono<movie> create(mono<movierequest> movierequest) { return movierequest.map(newmovie -> { movie movie = new movie(); if(newmovie.getdescription() != null){ movie.setdescription(newmovie.getdescription()); } if(newmovie.getrating() != null){ movie.setrating(newmovie.getrating()); } if(newmovie.gettitle() != null) { movie.settitle(newmovie.gettitle()); } return movie; }).then(movierepository::save); } @override public mono<movie> read(string id) { return movierepository.findone(id); } @override public mono<movie> delete(string id) { mono<movie> movie = movierepository.findone(id); movierepository.delete(id); return movie; } } @restcontroller public class movierestcontroller { @autowired private movieservice movieservice; @getmapping(value = "/movies") public flux<responseentity<movie>> list() { return movieservice.list().map(m -> new responseentity<>(m, httpstatus.ok)); } @getmapping(value = "/moviesbyrating") public flux<responseentity<movie>> findbyrating( @requestparam(value = "rating", required = false) final string rating) { return movieservice.findbyrating(rating) .map(m -> new responseentity<>(m, httpstatus.ok)); } @getmapping("/movies/{movieid}") public mono<responseentity<movie>> read( @pathvariable("movieid") final string movieid) { return movieservice.read(movieid) .map(m -> new responseentity<>(m, httpstatus.ok)); } @deletemapping("/movies/{movieid}") public mono<responseentity<movie>> delete( @pathvariable("movieid") final string movieid) { return movieservice.delete(movieid) .map(m -> new responseentity<>(m, httpstatus.ok)); } @putmapping("/movies/{movieid}") public mono<responseentity<movie>> update( @pathvariable("movieid") final string movieid, @requestbody final movierequest movierequest) { return movieservice.update(movieid, movierequest) .map(m -> new responseentity<>(m, httpstatus.ok)); } @postmapping("/movies") public mono<responseentity<movie>> create( @requestbody final mono<movierequest> movierequest) { return movieservice.create(movierequest) .map(m -> new responseentity<>(m, httpstatus.ok)); } }
Comments
Post a Comment