In my app users can add movie titles to their watchlist. In my component I have this function:
createMovie(movie: Movie): void {
this._dataService.createMovie<Movie>({'name': 'Star Wars', 'id': 111, 'description': 'A war in the Stars'})
.subscribe((data) => this.movies.push(data),
error => () => {
'something went wrong';
},
() => {
// console.log(this.movies);
});
}
This has some dummy info for now.
In my service I have:
public createMovie<T>(movie: Movie): Observable<T> {
return this.http.post<T>('/api/movies/', movie, {headers: this.getToken()});
}
So I pass the movie object and the token to the back-end.
In my MovieController.java I have:
@RestController
@RequestMapping("api/movies")
public class MovieController {
@Autowired
private MovieService movieService;
@RequestMapping(value = "/", method = RequestMethod.POST)
public Movie createMovie(@RequestBody Movie movie){
return movieService.createMovie(movie);
}
}
And the createMovie function in the movieService:
@Override
public Movie createMovie(Movie movie) {
movieRepository.save(movie);
User current_user = userService.getUser();
current_user.addMovie(movie);
userRepository.save(current_user);
return movie;
}
This all works fine, but I would like to return a message to the angular application when a movie was successfully added to the list (database). I think I should use @ResponseBody for it, but I'm unsure how to return the movie object and a status text to the angular application.
So for example, when a movie is added I would like to return a message "movie.name was successfully added to your watchlist" from the back-end.