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 @@ -20,8 +20,10 @@ repositories {
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'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.booleanuk.api.cinema.Controller;

import com.booleanuk.api.cinema.Model.Customer;
import com.booleanuk.api.cinema.Repository.CustomerRepository;
import com.booleanuk.api.cinema.ResponseWrapper.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

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

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<ResponseWrapper<Object>> create(@RequestBody Customer newCustomer) {
try {

LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
newCustomer.setCreatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
newCustomer.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
Customer savedCustomer = this.customerRepository.save(newCustomer);
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", savedCustomer.getId());
response.put("name", savedCustomer.getName());
response.put("email", savedCustomer.getEmail());
response.put("phone", savedCustomer.getPhone());
response.put("createdAt", currentDateTime.format(formatter));
response.put("updatedAt", currentDateTime.format(formatter));

return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", response));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not create customer, please check all required fields are correct."));
}
}

@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseWrapper getAll() {
return new ResponseWrapper<>("success", this.customerRepository.findAll());
}

@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> update(@PathVariable("id") Integer id, @RequestBody Customer updatedCustomer) {
Optional<Customer> existingCustomerOptional = this.customerRepository.findById(id);
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

if (existingCustomerOptional.isPresent()) {
try {
Customer existingCustomer = existingCustomerOptional.get();
existingCustomer.setName(updatedCustomer.getName());
existingCustomer.setEmail(updatedCustomer.getEmail());
existingCustomer.setPhone(updatedCustomer.getPhone());
existingCustomer.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));

Customer savedCustomer = this.customerRepository.save(existingCustomer);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedCustomer));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not update customer, please check all fields are correct."));
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No customer with that id found."));
}
}

@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> delete(@PathVariable("id") Integer id) {
Optional<Customer> customerOptional = this.customerRepository.findById(id);

if (customerOptional.isPresent()) {
Customer deletedCustomer = customerOptional.get();
this.customerRepository.deleteById(id);
return ResponseEntity.ok(new ResponseWrapper<>("success", deletedCustomer));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No customer with that ID was found."));
}
}
}
102 changes: 102 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/Controller/MovieController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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.ResponseWrapper.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

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

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<ResponseWrapper<Object>> create(@RequestBody Movie newMovie) {

try {


LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
newMovie.setCreatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
newMovie.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
newMovie.setScreenings(new ArrayList<Screening>());
Movie savedMovie = this.movieRepository.save(newMovie);
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", savedMovie.getId());
response.put("title", newMovie.getTitle());
response.put("description", newMovie.getDescription());
response.put("runtimeMins", newMovie.getRuntimeMins());
response.put("createdAt", currentDateTime.format(formatter));
response.put("updatedAt", currentDateTime.format(formatter));
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", response));

} catch (Exception e) {
//System.out.println(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not create movie, please check all required fields are correct."));

}
}

@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseWrapper getAll() {
return new ResponseWrapper<>("success", this.movieRepository.findAll());
}

@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> update(@PathVariable("id") Integer id, @RequestBody Movie updatedMovie) {
Optional<Movie> existingMovieOptional = this.movieRepository.findById(id);
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");

if (existingMovieOptional.isPresent()) {
try {
Movie existingMovie = existingMovieOptional.get();
existingMovie.setTitle(updatedMovie.getTitle());
existingMovie.setRating(updatedMovie.getRating());
existingMovie.setDescription(updatedMovie.getDescription());
existingMovie.setRuntimeMins(updatedMovie.getRuntimeMins());
existingMovie.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));

Movie savedMovie = this.movieRepository.save(existingMovie);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedMovie));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not update movie, please check all fields are correct."));
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No movie with that id found."));
//return new ResponseWrapper<>("error", "No movie with that id found.");
}
}

@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public Optional<Movie> delete(@PathVariable("id") Integer id) {
Optional<Movie> movieOptional = this.movieRepository.findById(id);

if (movieOptional.isPresent()) {
Movie deletedMovie = movieOptional.get();
this.movieRepository.deleteById(id);
// return ResponseEntity.ok(new ResponseWrapper<>("success", deletedMovie));
return movieOptional;
} else {
return Optional.empty();
// ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No movie with that id found."));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.booleanuk.api.cinema.Controller;

import com.booleanuk.api.cinema.Model.Screening;
import com.booleanuk.api.cinema.Repository.ScreeningRepository;
import com.booleanuk.api.cinema.ResponseWrapper.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

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

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<ResponseWrapper<Object>> create(@PathVariable("movieId") Integer movieId, @RequestBody Screening newScreening) {
try {
newScreening.setMovieId(movieId);
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
newScreening.setCreatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
newScreening.setUpdatedAt(LocalDateTime.parse(currentDateTime.format(formatter)));
Screening savedScreening = this.screeningRepository.save(newScreening);
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", savedScreening.getId());
response.put("screenNumber", savedScreening.getScreenNumber());
response.put("capacity", savedScreening.getCapacity());
response.put("startsAt", savedScreening.getStartsAt());
response.put("createdAt", currentDateTime.format(formatter));
response.put("updatedAt", currentDateTime.format(formatter));

return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", response));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not create screening, please check all required fields are correct."));
}
}

@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseWrapper getAll() {
return new ResponseWrapper<>("success", this.screeningRepository.findAll());
}



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

import com.booleanuk.api.cinema.Model.Ticket;
import com.booleanuk.api.cinema.Repository.TicketRepository;
import com.booleanuk.api.cinema.ResponseWrapper.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("tickets")
public class TicketController {
@Autowired
private TicketRepository ticketRepository;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<ResponseWrapper<Object>> create(@RequestBody Ticket newTicket) {
try {
Ticket savedTicket = this.ticketRepository.save(newTicket);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedTicket));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not create ticket, please check all required fields are correct."));
}
}

@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseWrapper getAll() {
return new ResponseWrapper<>("success", this.ticketRepository.findAll());
}

@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> update(@PathVariable("id") Integer id, @RequestBody Ticket updatedTicket) {
Optional<Ticket> existingTicketOptional = this.ticketRepository.findById(id);

if (existingTicketOptional.isPresent()) {
try {
Ticket existingTicket = existingTicketOptional.get();
existingTicket.setCustomerId(updatedTicket.getCustomerId());
existingTicket.setScreeningId(updatedTicket.getScreeningId());
existingTicket.setNumSeats(updatedTicket.getNumSeats());
Ticket savedTicket = this.ticketRepository.save(existingTicket);
return ResponseEntity.status(HttpStatus.CREATED).body(new ResponseWrapper<>("success", savedTicket));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseWrapper<>("error", "Could not update ticket, please check all fields are correct."));
}
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No ticket with that id found."));
}
}

@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public ResponseEntity<ResponseWrapper<Object>> delete(@PathVariable("id") Integer id) {
if (this.ticketRepository.existsById(id)) {
this.ticketRepository.deleteById(id);
return ResponseEntity.ok(new ResponseWrapper<>("success", "Ticket deleted successfully."));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseWrapper<>("error", "No ticket with that ID was found."));
}
}
}
12 changes: 12 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,12 @@
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);
}
}
Loading