Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencies {
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
compileOnly 'org.projectlombok:lombok:1.18.34'
annotationProcessor 'org.projectlombok:lombok'
}

tasks.named('test') {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/Main.java
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.booleanuk.api.cinema.controller;

import com.booleanuk.api.cinema.model.Customer;
import com.booleanuk.api.cinema.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
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 {

@Autowired
private CustomerRepository customerRepository;

@PostMapping
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer) {
try {
customer.setCreatedAt(LocalDateTime.now());
customer.setUpdatedAt(LocalDateTime.now());
return new ResponseEntity<>(this.customerRepository.save(customer), HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not save the customer: " + e.getMessage());
}
}


@GetMapping
public ResponseEntity<List<Customer>> getAllCustomers() {
return ResponseEntity.ok(this.customerRepository.findAll());
}


@GetMapping("/{id}")
public ResponseEntity<Customer> getOneCustomer(@PathVariable int id) {
Customer customer = this.customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Customer with ID " + id + " not found.")
);
return ResponseEntity.ok(customer);
}


@PutMapping("/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable int id, @RequestBody Customer customer) {
Customer customerToUpdate = this.customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Customer with ID " + id + " not found.")
);
customerToUpdate.setName(customer.getName());
customerToUpdate.setEmail(customer.getEmail());
customerToUpdate.setPhone(customer.getPhone());
customerToUpdate.setUpdatedAt(LocalDateTime.now());
return new ResponseEntity<>(this.customerRepository.save(customerToUpdate), HttpStatus.OK);
}


@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteCustomer(@PathVariable int id) {
Customer customerToDelete = this.customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Customer with ID " + id + " not found.")
);
this.customerRepository.delete(customerToDelete);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.booleanuk.api.cinema.controller;

import com.booleanuk.api.cinema.model.Movie;
import com.booleanuk.api.cinema.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
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("movies")
public class MovieController {

@Autowired
private MovieRepository movieRepository;

@PostMapping
public ResponseEntity<Movie> createMovie(@RequestBody Movie movie) {
try {
movie.setCreatedAt(LocalDateTime.now());
movie.setUpdatedAt(LocalDateTime.now());
return new ResponseEntity<>(this.movieRepository.save(movie), HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not save the movie: " + e.getMessage());
}
}

@GetMapping
public ResponseEntity<List<Movie>> getAllMovies() {
return ResponseEntity.ok(this.movieRepository.findAll());
}

@GetMapping("/{id}")
public ResponseEntity<Movie> getOneMovie(@PathVariable int id) {
Movie movie = this.movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with ID " + id + " not found.")
);
return ResponseEntity.ok(movie);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public Movie updateMovie(@PathVariable int id, @RequestBody Movie updatedMovie) {
return this.movieRepository.findById(id)
.map(movie -> {
movie.setTitle(updatedMovie.getTitle());
movie.setRating(updatedMovie.getRating());
movie.setDescription(updatedMovie.getDescription());
movie.setRuntimeMins(updatedMovie.getRuntimeMins());
movie.setUpdatedAt(LocalDateTime.now());
return this.movieRepository.save(movie);
})
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with ID " + id + " not found."));
}


@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteMovie(@PathVariable int id) {
Movie movieToDelete = this.movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with ID " + id + " not found.")
);
this.movieRepository.delete(movieToDelete);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 org.springframework.beans.factory.annotation.Autowired;
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("/movies")
public class ScreeningController {

@Autowired
private MovieRepository movieRepository;

@Autowired
private ScreeningRepository screeningRepository;

@PostMapping("/{id}/screenings")
public ResponseEntity<Screening> createScreening(@PathVariable int id, @RequestBody Screening screeningDetails) {
Movie movie = this.movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with ID " + id + " not found.")
);

Screening screening = new Screening(
screeningDetails.getScreenNumber(),
screeningDetails.getCapacity(),
screeningDetails.getStartsAt(),
movie
);
screening.setCreatedAt(LocalDateTime.now());
screening.setUpdatedAt(LocalDateTime.now());

return new ResponseEntity<>(this.screeningRepository.save(screening), HttpStatus.CREATED);
}

@GetMapping("/{id}/screenings")
public ResponseEntity<List<Screening>> getAllScreeningsForMovie(@PathVariable int id) {
List<Screening> screenings = this.screeningRepository.findByMovieId(id);

if (screenings.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No screenings found for Movie with ID " + id);
}

return ResponseEntity.ok(screenings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.booleanuk.api.cinema.controller;

import com.booleanuk.api.cinema.model.Ticket;
import com.booleanuk.api.cinema.repository.TicketRepository;
import org.springframework.beans.factory.annotation.Autowired;
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("/tickets")
public class TicketController {

@Autowired
private TicketRepository ticketRepository;

@PostMapping
public ResponseEntity<Ticket> createTicket(@RequestBody Ticket ticket) {
try {
ticket.setCreatedAt(LocalDateTime.now());
ticket.setUpdatedAt(LocalDateTime.now());
return new ResponseEntity<>(this.ticketRepository.save(ticket), HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not create the ticket: " + e.getMessage());
}
}

@GetMapping
public ResponseEntity<List<Ticket>> getAllTickets() {
return ResponseEntity.ok(this.ticketRepository.findAll());
}

@GetMapping("/{id}")
public ResponseEntity<Ticket> getTicketById(@PathVariable int id) {
Ticket ticket = this.ticketRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Ticket not found with ID: " + id)
);
return ResponseEntity.ok(ticket);
}

@PutMapping("/{id}")
public ResponseEntity<Ticket> updateTicket(@PathVariable int id, @RequestBody Ticket updatedTicket) {
Ticket ticket = this.ticketRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Ticket not found with ID: " + id)
);
ticket.setNumSeats(updatedTicket.getNumSeats());
ticket.setCustomer(updatedTicket.getCustomer());
ticket.setScreening(updatedTicket.getScreening());
ticket.setUpdatedAt(LocalDateTime.now());
return new ResponseEntity<>(this.ticketRepository.save(ticket), HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteTicket(@PathVariable int id) {
Ticket ticket = this.ticketRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Ticket not found with ID: " + id)
);
this.ticketRepository.delete(ticket);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/dto/MovieDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.booleanuk.api.cinema.dto;

import java.util.List;


public class MovieDTO {
private String title;
private String rating;
private String description;
private Integer runtimeMins;
private List<ScreeningDTO> screenings;

public MovieDTO() {
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getRating() {
return rating;
}

public void setRating(String rating) {
this.rating = rating;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getRuntimeMins() {
return runtimeMins;
}

public void setRuntimeMins(Integer runtimeMins) {
this.runtimeMins = runtimeMins;
}

public List<ScreeningDTO> getScreenings() {
return screenings;
}

public void setScreenings(List<ScreeningDTO> screenings) {
this.screenings = screenings;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/dto/ScreeningDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.booleanuk.api.cinema.dto;

import java.time.LocalDateTime;

public class ScreeningDTO {
private Integer screenNumber;
private Integer capacity;
private LocalDateTime startsAt;

public ScreeningDTO() {
}

public Integer getScreenNumber() {
return screenNumber;
}

public void setScreenNumber(Integer screenNumber) {
this.screenNumber = screenNumber;
}

public Integer getCapacity() {
return capacity;
}

public void setCapacity(Integer capacity) {
this.capacity = capacity;
}

public LocalDateTime getStartsAt() {
return startsAt;
}

public void setStartsAt(LocalDateTime startsAt) {
this.startsAt = startsAt;
}
}
Loading