This is movie class
. I have code for add the movie in postman using post request.
public class Movie {
private int id;
private String movieName;
private String description;
private String hero;
private String heroine;
public Movie(){
}
public Movie(int id, String movieName, String description, String hero, String heroine) {
this.id = id;
this.movieName = movieName;
this.description = description;
this.hero = hero;
this.heroine = heroine;
}
//here have setter and getter methods
This is controller class.now what the logic for not accept duplicate moviename
public class MovieController extends Controller {
public static List<Movie> movies;
static {
movies = new ArrayList<>();
movies.add(new Movie(1, "Fprd vs Ferrari", "Movie on Racing", "abcd", "xyz"));
movies.add(new Movie(2, "F2", "Comedy Movie", "Venkatesh", "Tamanna"));
movies.add(new Movie(3, "Titanic", "Movie", "Hero", "Heroine"));
movies.add(new Movie(4, "Saaho", "action", "Prabhas", "Shradda kapoor"));
movies.add(new Movie(5, "Bhahubali 1", "action", "Prabhas", "Tamanna"));
}
public Result insert(Http.Request request) {
JsonNode jsonNode = request.body().asJson();
if (jsonNode == null) {
return badRequest("insufficient movie information");
}
Movie movie = Json.fromJson(jsonNode, Movie.class);
movies.add(movie);
return ok(Json.toJson(movie));
}
This is the Routing for post request
POST /newmovies controllers.MovieController.insert(request: Request)
You can do something like this:
Movie movie = Json.fromJson(jsonNode, Movie.class);
if(movies.stream().noneMatch(m -> m.getMovieName().equals(movie.getName()))) {
movies.add(movie);
return ok(Json.toJson(movie));
} else {
// Movie name already exists... Throw exception here
return badRequest("Movie already exists");
}