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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ out/

### VS Code ###
.vscode/

application.yml
16 changes: 13 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.1'
id 'io.spring.dependency-management' version '1.1.5'
id 'org.springframework.boot' version '3.4.2'
id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.booleanuk'
Expand All @@ -13,19 +13,29 @@ java {
}
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.3'
}

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

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "customers")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String name;
@Column
private String email;
@Column
private String phone;
@Column
private String createdAt;
@Column
private String updatedAt;

public Customer(String name, String email, String phone) {
this.name = name;
this.email = email;
this.phone = phone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.booleanuk.api.cinema.customers;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController
@RequestMapping("customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;

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

@PostMapping
public ResponseEntity<Customer> addCustomer(@RequestBody Customer customer) {
customer.setCreatedAt(LocalDateTime.now().toString());
customer.setUpdatedAt(LocalDateTime.now().toString());
return new ResponseEntity<Customer>(customerRepository.save(customer), HttpStatus.CREATED);
}

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

return new ResponseEntity<Customer>(customerRepository.save(customerToUpdate), HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Customer> deleteCustomer(@PathVariable int id) {
Customer customerToDelete = customerRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find customer with this id"));
customerRepository.delete(customerToDelete);

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

import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerRepository extends JpaRepository<Customer, Integer> {
}
53 changes: 53 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/movies/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.booleanuk.api.cinema.movies;

import java.util.List;

import com.booleanuk.api.cinema.screenings.Screening;
import com.fasterxml.jackson.annotation.JsonIgnore;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "movies")
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String title;
@Column
private String rating;
@Column
private String description;
@Column
private int runtimeMins;
@Column
private String createdAt;
@Column
private String updatedAt;

@OneToMany(mappedBy = "movie")
@JsonIgnore
private List<Screening> screenings;

public Movie(String title, String rating, String description, int runtimeMins) {
this.title = title;
this.rating = rating;
this.description = description;
this.runtimeMins = runtimeMins;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/movies/MovieController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.booleanuk.api.cinema.movies;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController
@RequestMapping("movies")
public class MovieController {
@Autowired
private MovieRepository movieRepository;

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

@PostMapping
public ResponseEntity<Movie> addMovie(@RequestBody Movie movie) {
movie.setCreatedAt(LocalDateTime.now().toString());
movie.setUpdatedAt(LocalDateTime.now().toString());
return new ResponseEntity<Movie>(movieRepository.save(movie), HttpStatus.CREATED);
}

@PutMapping("{id}")
public ResponseEntity<Movie> updateMovie(@PathVariable int id, @RequestBody Movie movie) {
Movie movieToUpdate = movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find movie with this id"));
movieToUpdate.setTitle(movie.getTitle());
movieToUpdate.setRating(movie.getRating());
movieToUpdate.setDescription(movie.getDescription());
movieToUpdate.setRuntimeMins(movie.getRuntimeMins());
movieToUpdate.setUpdatedAt(LocalDateTime.now().toString());

return new ResponseEntity<Movie>(movieRepository.save(movieToUpdate), HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Movie> deleteMovie(@PathVariable int id) {
Movie movieToDelete = movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find movie with this id"));
movieRepository.delete(movieToDelete);

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

import org.springframework.data.jpa.repository.JpaRepository;

public interface MovieRepository extends JpaRepository<Movie, Integer> {
}
52 changes: 52 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/screenings/Screening.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.booleanuk.api.cinema.screenings;

import com.booleanuk.api.cinema.movies.Movie;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "screenings")
public class Screening {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private int screenNumber;
@Column
private int capacity;
@Column
private String startsAt;
@Column
private String createdAt;
@Column
private String updatedAt;

@ManyToOne
@JoinColumn(name = "movie_id")
@JsonIgnoreProperties({ "screenings", "movie" })
@JsonIgnore
private Movie movie;

public Screening(int screenNumber, int capacity, String startsAt) {
this.screenNumber = screenNumber;
this.capacity = capacity;
this.startsAt = startsAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.booleanuk.api.cinema.screenings;

import java.time.LocalDateTime;
import java.util.List;
import com.booleanuk.api.cinema.movies.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.http.HttpStatus;

@RestController
@RequestMapping("movies/{id}/screenings")
public class ScreeningController {
@Autowired
private ScreeningRepository screeningRepository;
@Autowired
private MovieRepository movieRepository;

@GetMapping
public ResponseEntity<List<Screening>> getAllScreeningsOfMovie(@PathVariable int id) {
Movie movie = movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find movie with this id"));
return ResponseEntity.ok(movie.getScreenings());
}

@PostMapping
public ResponseEntity<Screening> addScreening(@PathVariable int id, @RequestBody Screening screening) {
Movie movie = movieRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find movie with this id"));
movie.getScreenings().add(screening);
screening.setMovie(movie);
screening.setCreatedAt(LocalDateTime.now().toString());
screening.setUpdatedAt(LocalDateTime.now().toString());

return new ResponseEntity<Screening>(screeningRepository.save(screening), HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.booleanuk.api.cinema.screenings;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ScreeningRepository extends JpaRepository<Screening, Integer> {
}