-
Notifications
You must be signed in to change notification settings - Fork 98
Matilda Sönnergaard #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pudkipz
wants to merge
19
commits into
boolean-uk:main
Choose a base branch
from
pudkipz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0e43044
Create all classes and add fields to model classes
pudkipz fb8ef8a
Extend repositories
pudkipz af0acab
Add constructors without createdAt
pudkipz 0d220dc
Implement get all mappings
pudkipz c8a86fc
Make sure timestamps are added
pudkipz a73b61b
Add getOne endpoints
pudkipz 8a90adc
Refactor some screening endpoints to MovieController
pudkipz ad05ba5
Implement create endpoint for screenings
pudkipz 30adff8
Implement create endpoints for movies and customers
pudkipz 7936719
Implement update and delete endpoints for movies and customers
pudkipz c095102
Create fields for Ticket
pudkipz f5b65a3
Create endpoints for getting all tickets
pudkipz a765066
Create endpoint for booking a ticket
pudkipz 540a369
Implement error handling with responses
pudkipz b25956c
Cascade on delete
pudkipz b072003
Add option to supply screenings array when creating movies
pudkipz 84c3329
Fix ignoring fields that are not provided in update
pudkipz b83cc42
Fix ignoring fields that are not provided in update
pudkipz b2bebb2
Merge remote-tracking branch 'origin/main'
pudkipz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.booleanuk.api.cinema; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(Main.class, args); | ||
| } | ||
| } |
119 changes: 119 additions & 0 deletions
119
src/main/java/com/booleanuk/api/cinema/controller/CustomerController.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package com.booleanuk.api.cinema.controller; | ||
|
|
||
| import com.booleanuk.api.cinema.model.Customer; | ||
| import com.booleanuk.api.cinema.model.Screening; | ||
| import com.booleanuk.api.cinema.model.Ticket; | ||
| import com.booleanuk.api.cinema.repository.CustomerRepository; | ||
| import com.booleanuk.api.cinema.repository.ScreeningRepository; | ||
| import com.booleanuk.api.cinema.repository.TicketRepository; | ||
| import com.booleanuk.api.cinema.responses.ErrorResponse; | ||
| import com.booleanuk.api.cinema.responses.Response; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.server.ResponseStatusException; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/customers") | ||
| public class CustomerController { | ||
| private CustomerRepository customerRepository; | ||
| private TicketRepository ticketRepository; | ||
| private ScreeningRepository screeningRepository; | ||
|
|
||
| public CustomerController(CustomerRepository customerRepository, TicketRepository ticketRepository, ScreeningRepository screeningRepository) { | ||
| this.customerRepository = customerRepository; | ||
| this.ticketRepository = ticketRepository; | ||
| this.screeningRepository = screeningRepository; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Response<?>> create(@RequestBody Customer customer) { | ||
| Customer addedCustomer = customerRepository.save(customer); | ||
| if (addedCustomer == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("bad request"), HttpStatus.BAD_REQUEST); | ||
| } | ||
| return new ResponseEntity<>(new Response<>("success", addedCustomer), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Response<?>> getOne(@PathVariable int id) { | ||
| // return ResponseEntity.ok(customerRepository.findById(id).orElseThrow( | ||
| // () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that ID was found.") | ||
| // )); | ||
| Customer customer = customerRepository.findById(id).orElse(null); | ||
| if (customer == null) { | ||
| ErrorResponse error = new ErrorResponse("not found"); | ||
| return new ResponseEntity<>(error, HttpStatus.NOT_FOUND); | ||
| } | ||
| Response<Customer> response = new Response<>(); | ||
| response.set(customer); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<Response<?>> getAll() { | ||
| return ResponseEntity.ok(new Response<>("success", customerRepository.findAll())); | ||
|
|
||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<Response<?>> update(@PathVariable int id, @RequestBody Customer customer) { | ||
| Customer customerToUpdate = customerRepository.findById(id).orElse(null); | ||
| if (customerToUpdate == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
| if (customer.getName() != null) | ||
| customerToUpdate.setName(customer.getName()); | ||
| if (customer.getEmail() != null) | ||
| customerToUpdate.setEmail(customer.getEmail()); | ||
| if (customer.getPhone() != null) | ||
| customerToUpdate.setPhone(customer.getPhone()); | ||
| customerToUpdate.setUpdatedAt(LocalDateTime.now()); | ||
| // todo error 400 | ||
| return new ResponseEntity<>(new Response<>(customerRepository.save(customerToUpdate)), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Response<?>> deleteCustomer(@PathVariable int id) { | ||
| Customer customer = customerRepository.findById(id).orElse(null); | ||
| if (customer == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| customerRepository.deleteById(id); | ||
| return ResponseEntity.ok(new Response<>("success", customer)); | ||
| } | ||
|
|
||
| @PostMapping("/{c_id}/screenings/{s_id}") | ||
| public ResponseEntity<Response<?>> bookTicket(@PathVariable(name = "c_id") int customerId, @PathVariable(name = "s_id") int screeningId, @RequestBody Ticket ticket) { | ||
| Customer customer = customerRepository.findById(customerId).orElse(null); | ||
| Screening screening = screeningRepository.findById(screeningId).orElse(null); | ||
|
|
||
| if (customer == null || screening == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| ticket.setCustomer(customer); | ||
| ticket.setScreening(screening); | ||
| ticket.setCreatedAt(LocalDateTime.now()); | ||
| ticket.setUpdatedAt(LocalDateTime.now()); | ||
| return new ResponseEntity<>(new Response<>("success", ticketRepository.save(ticket)), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/{c_id}/screenings/{s_id}") | ||
| public ResponseEntity<Response<?>> getAllTickets(@PathVariable(name = "c_id") int customerId, @PathVariable(name = "s_id") int screeningId) { | ||
| Customer customer = customerRepository.findById(customerId).orElse(null); | ||
| Screening screening = screeningRepository.findById(screeningId).orElse(null); | ||
|
|
||
| if (customer == null || screening == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| return new ResponseEntity<>(new Response<>("success", ticketRepository.findAll().stream().filter( | ||
| t -> t.getCustomerId() == customer.getId() && t.getScreeningId() == screening.getId() | ||
| ).toList()), HttpStatus.OK); | ||
| } | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
src/main/java/com/booleanuk/api/cinema/controller/MovieController.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package com.booleanuk.api.cinema.controller; | ||
|
|
||
| import com.booleanuk.api.cinema.model.Movie; | ||
| import com.booleanuk.api.cinema.model.Screening; | ||
| import com.booleanuk.api.cinema.repository.MovieRepository; | ||
| import com.booleanuk.api.cinema.repository.ScreeningRepository; | ||
| import com.booleanuk.api.cinema.responses.ErrorResponse; | ||
| import com.booleanuk.api.cinema.responses.Response; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/movies") | ||
| public class MovieController { | ||
| private MovieRepository movieRepository; | ||
| private ScreeningRepository screeningRepository; | ||
|
|
||
| public MovieController(MovieRepository movieRepository, ScreeningRepository screeningRepository) { | ||
| this.movieRepository = movieRepository; | ||
| this.screeningRepository = screeningRepository; | ||
| } | ||
|
|
||
| private record MovieWithScreenings(String title, String rating, String description, int runtimeMins, List<Screening> screenings) {}; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Response<?>> createMovie(@RequestBody MovieWithScreenings movieWithScreenings) { | ||
| Movie movie = new Movie(movieWithScreenings.title, movieWithScreenings.rating, movieWithScreenings.description, movieWithScreenings.runtimeMins); | ||
| Movie createdMovie = movieRepository.save(movie); | ||
| if (createdMovie == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("bad request"), HttpStatus.BAD_REQUEST); | ||
| } | ||
| if (movieWithScreenings.screenings != null) { | ||
| for (Screening s : movieWithScreenings.screenings) { | ||
| s.setMovie(createdMovie); | ||
| screeningRepository.save(s); | ||
| } | ||
| } | ||
| return new ResponseEntity<>(new Response<>("success", createdMovie), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Response<?>> getOne(@PathVariable int id) { | ||
| Movie movie = movieRepository.findById(id).orElse(null); | ||
| if (movie == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
| return new ResponseEntity<>(new Response<>("success", movie), HttpStatus.OK); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<Response<?>> getAll() { | ||
| return new ResponseEntity<>(new Response<>("success", movieRepository.findAll()), HttpStatus.OK); | ||
| } | ||
|
|
||
| @PostMapping("{id}/screenings") | ||
| public ResponseEntity<Response<?>> createScreening(@PathVariable(name = "id") int movieId, @RequestBody Screening screening) { | ||
| Movie movie = movieRepository.findById(movieId).orElse(null); | ||
| if (movie == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| screening.setMovie(movie); | ||
| screening.setCreatedAt(LocalDateTime.now()); | ||
| screening.setUpdatedAt(LocalDateTime.now()); | ||
| Screening createdScreening = screeningRepository.save(screening); | ||
|
|
||
| if (createdScreening == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("bad request"), HttpStatus.BAD_REQUEST); | ||
| } | ||
| return new ResponseEntity<>(new Response<>("success", createdScreening), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/{id}/screenings") | ||
| public ResponseEntity<Response<?>> getAllScreenings(@PathVariable(name = "id") int movieId) { | ||
| Movie movie = movieRepository.findById(movieId).orElse(null); | ||
| if (movie == null) | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
|
|
||
| return new ResponseEntity<>(new Response<>("success", screeningRepository.findAll().stream().filter( | ||
| s -> s.getMovieId() == movie.getId()).toList()), HttpStatus.OK); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<Response<?>> updateMovie(@PathVariable int id, @RequestBody Movie movie) { | ||
| Movie movieToUpdate = movieRepository.findById(id).orElse(null); | ||
| if (movieToUpdate == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| } | ||
|
|
||
| if (movie.getTitle() != null) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You coiuld write a generic 'updateifnotnull()' functionh for this |
||
| movieToUpdate.setTitle(movie.getTitle()); | ||
| if (movie.getRating() != null) | ||
| movieToUpdate.setRating(movie.getRating()); | ||
| if (movie.getDescription() != null) | ||
| movieToUpdate.setDescription(movie.getDescription()); | ||
| if (movie.getRuntimeMins() > 0) | ||
| movieToUpdate.setRuntimeMins(movie.getRuntimeMins()); | ||
| movieToUpdate.setUpdatedAt(LocalDateTime.now()); | ||
|
|
||
| if (movieRepository.save(movieToUpdate) == null) { | ||
| return new ResponseEntity<>(new ErrorResponse("bad request"), HttpStatus.BAD_REQUEST); | ||
| } | ||
| return new ResponseEntity<>(new Response<>("success", movieToUpdate), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Response<?>> deleteMovie(@PathVariable int id) { | ||
| Movie movie = movieRepository.findById(id).orElse(null); | ||
| if (movie == null) | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| movieRepository.deleteById(id); | ||
| return ResponseEntity.ok(new Response<>("success", movie)); | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
src/main/java/com/booleanuk/api/cinema/controller/ScreeningController.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.booleanuk.api.cinema.controller; | ||
|
|
||
| import com.booleanuk.api.cinema.model.Screening; | ||
| import com.booleanuk.api.cinema.repository.MovieRepository; | ||
| import com.booleanuk.api.cinema.repository.ScreeningRepository; | ||
| import com.booleanuk.api.cinema.responses.ErrorResponse; | ||
| import com.booleanuk.api.cinema.responses.Response; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/screenings") | ||
| public class ScreeningController { | ||
| ScreeningRepository screeningRepository; | ||
| MovieRepository movieRepository; | ||
|
|
||
| public ScreeningController(ScreeningRepository screeningRepository, MovieRepository movieRepository) { | ||
| this.screeningRepository = screeningRepository; | ||
| this.movieRepository = movieRepository; | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Response<?>> getOne(@PathVariable(name = "id") int id) { | ||
| Screening screening = screeningRepository.findById(id).orElse(null); | ||
| if (screening == null) | ||
| return new ResponseEntity<>(new ErrorResponse("not found"), HttpStatus.NOT_FOUND); | ||
| return ResponseEntity.ok(new Response<>("success", screening)); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/booleanuk/api/cinema/controller/TicketController.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.booleanuk.api.cinema.controller; | ||
|
|
||
| import com.booleanuk.api.cinema.model.Ticket; | ||
| import com.booleanuk.api.cinema.repository.MovieRepository; | ||
| import com.booleanuk.api.cinema.repository.ScreeningRepository; | ||
| import com.booleanuk.api.cinema.repository.TicketRepository; | ||
| import com.booleanuk.api.cinema.responses.Response; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/tickets") | ||
| public class TicketController { | ||
| private TicketRepository ticketRepository; | ||
| private MovieRepository movieRepository; | ||
| private ScreeningRepository screeningRepository; | ||
|
|
||
| public TicketController(TicketRepository ticketRepository, MovieRepository movieRepository, ScreeningRepository screeningRepository) { | ||
| this.ticketRepository = ticketRepository; | ||
| this.movieRepository = movieRepository; | ||
| this.screeningRepository = screeningRepository; | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<Response<?>> getAll() { | ||
| return new ResponseEntity<>(new Response<>("success", ticketRepository.findAll()), HttpStatus.OK); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See previous comment