Skip to content

Instantly share code, notes, and snippets.

@davidcsejtei
Created August 17, 2023 21:31
Show Gist options
  • Save davidcsejtei/a10080c803681cff3ae2898f0ff0f4ed to your computer and use it in GitHub Desktop.
Save davidcsejtei/a10080c803681cff3ae2898f0ff0f4ed to your computer and use it in GitHub Desktop.
package com.holnor.moviedatabase.controller;
import com.holnor.moviedatabase.dto.CreateMovieCommand;
import com.holnor.moviedatabase.dto.MovieDetailsItem;
import com.holnor.moviedatabase.dto.MovieListItem;
import com.holnor.moviedatabase.service.MovieService;
import jakarta.validation.Valid;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/movies")
public class MovieController {
private MovieService movieService;
public MovieController(MovieService movieService) {
this.movieService = movieService;
}
...
@PostMapping
public ResponseEntity createMovie(@RequestBody @Valid CreateMovieCommand createMovieCommand) {
try {
movieService.createMovie(createMovieCommand);
} catch (DataIntegrityViolationException de) {
return new ResponseEntity(de.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity(HttpStatus.CREATED);
}
...
}
package com.holnor.moviedatabase.domain;
import jakarta.persistence.*;
@Entity
@Table(name = "movie")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "movie_id")
private Long id;
@Column(name = "title", unique = true)
private String title;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment