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 @@ -22,6 +22,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 repository;


@PostMapping
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer){
try {
return new ResponseEntity<Customer>(this.repository.save(customer),
HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not create a new customer, please check all required fields are correct.");
}


}

@GetMapping
public List<Customer> getAll() {
return this.repository.findAll();
}


@PutMapping("{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable int id,
@RequestBody Customer customer){
Customer customerToUpdate=this.repository.findById(id).orElseThrow(
()->new ResponseStatusException(HttpStatus.NOT_FOUND,
"No customer with that ID found")
);
customerToUpdate.setName(customer.getName());
customerToUpdate.setEmail(customerToUpdate.getEmail());
customerToUpdate.setPhone(customer.getPhone());
customerToUpdate.setUpdatedAt(LocalDateTime.now());
try{
return new ResponseEntity<Customer>(this.repository.save(customerToUpdate
), HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not update customer, please check all required fields are correct.");
}




}

@DeleteMapping("{id}")
public ResponseEntity<Customer> deleteCustomer(@PathVariable int id){
Customer customerToDelete=this.repository.findById(id).orElseThrow(
()->new ResponseStatusException(HttpStatus.NOT_FOUND,
"No customer with that ID found")
);

this.repository.delete(customerToDelete);
return ResponseEntity.ok(customerToDelete);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.booleanuk.api.cinema.Controller;

import com.booleanuk.api.cinema.Model.Customer;
import com.booleanuk.api.cinema.Model.Movie;
import com.booleanuk.api.cinema.Repository.CustomerRepository;
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 repository;


@PostMapping
public ResponseEntity<Movie> createMovie(@RequestBody Movie movie){
try {
return new ResponseEntity<Movie>(this.repository.save(movie),
HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not create a new movie, please check all required fields are correct.");
}


}

@GetMapping
public List<Movie> getAll() {
return this.repository.findAll();
}


@PutMapping("{id}")
public ResponseEntity<Movie> updateMovie(@PathVariable int id,
@RequestBody Movie movie){
Movie movieToUpdate=this.repository.findById(id).orElseThrow(
()->new ResponseStatusException(HttpStatus.NOT_FOUND,
"No movie with that ID found")
);
movieToUpdate.setTitle(movie.getTitle());
movieToUpdate.setRating(movie.getRating());
movieToUpdate.setDescription(movie.getDescription());
movieToUpdate.setRuntimeMins(movie.getRuntimeMins());
movieToUpdate.setUpdatedAt(LocalDateTime.now());
try{
return new ResponseEntity<Movie>(this.repository.save(movieToUpdate
), HttpStatus.CREATED);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not update movie, please check all required fields are correct.");
}




}

@DeleteMapping("{id}")
public ResponseEntity<Movie> deleteMovie(@PathVariable int id){
Movie movieToDelete=this.repository.findById(id).orElseThrow(
()->new ResponseStatusException(HttpStatus.NOT_FOUND,
"No movie with that ID found")
);

this.repository.delete(movieToDelete);
return ResponseEntity.ok(movieToDelete);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("screenings")
public class ScreeningController {

@Autowired
private MovieRepository movieRepository;

@Autowired
private ScreeningRepository screeningRepository;

@PostMapping("{id}")
public ResponseEntity<Screening> createScreening(@RequestBody Screening screening, @PathVariable("id") Integer id){


try {


Movie movie = this.movieRepository.findById(
id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No movie with that id exists")
);



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

} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not create screening for the specified " +
"movie, please check all required fields are correct.");
}


}

@GetMapping("{id}")
public ResponseEntity<List<Screening>> getAll(@PathVariable("id") Integer id) {
List<Screening> allMovs=new ArrayList<>();


Movie movie = this.movieRepository.findById(
id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No movie with that id exists")
);


return new ResponseEntity<List<Screening>>(movie.getScreenings(), HttpStatus.FOUND);

}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.booleanuk.api.cinema.Controller;

import com.booleanuk.api.cinema.Model.Customer;
import com.booleanuk.api.cinema.Model.Movie;
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.MovieRepository;
import com.booleanuk.api.cinema.Repository.ScreeningRepository;
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.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("customers/{customerId}/screenings/{screeningId}")
public class TicketController {

@Autowired
private CustomerRepository customerRepository;

@Autowired
private ScreeningRepository screeningRepository;

@Autowired
private TicketRepository ticketRepository;

@PostMapping()
public ResponseEntity<Ticket> createTicket(@RequestBody Ticket ticket, @PathVariable("screeningId") Integer
screeningId, @PathVariable("customerId") Integer customerId){


try {


Screening screening = this.screeningRepository.findById(
screeningId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No screening with that id exists")
);

Customer customer = this.customerRepository.findById(
customerId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that id exists")
);




ticket.setCustomer(customer);
ticket.setScreening(screening);
return new ResponseEntity<Ticket>(this.ticketRepository.save(ticket),
HttpStatus.CREATED);

} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not create ticket for the specified " +
"customer/screening, please check all required fields are correct.");
}


}

@GetMapping()
public ResponseEntity<List<Ticket>> getAll(@PathVariable("screeningId") Integer
screeningId, @PathVariable("customerId") Integer customerId) {
List<Ticket> allTickets=new ArrayList<>();


Customer customer = this.customerRepository.findById(
customerId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that id exists")
);

Screening screening = this.screeningRepository.findById(
screeningId).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No customer with that id exists")
);

return new ResponseEntity<List<Ticket>>(screening.getTickets(), HttpStatus.FOUND);

}
}
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);
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/Model/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.booleanuk.api.cinema.Model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

@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;

@OneToMany(mappedBy = "customer")
@JsonIgnoreProperties(value ={"screenings", "tickets", "movie","customer","screening"})
private List<Ticket> tickets;

@Column
private LocalDateTime createdAt=LocalDateTime.now();

@Column
private LocalDateTime updatedAt=LocalDateTime.now();




}
Loading