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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-validation'
}

tasks.named('test') {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.booleanuk.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.booleanuk.api"})
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
128 changes: 128 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.booleanuk.api.cinema;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;

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


@Entity
@Table(name = "customers")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@NotBlank
@Column
private String name;
@NotBlank
@Column
private String email;
@NotBlank
@Column
private String phone;
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(name = "updated_at")
private LocalDateTime updatedAt;

@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference(value = "customer-tickets")
@JsonIgnore
private List<Ticket> tickets;






public Customer(String name, String email, String phone) {
this.name = name;
this.email = email;
this.phone = phone;
this.tickets = new ArrayList<>();
}

public Customer() {
}

@PrePersist
protected void onCreate() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}

@PreUpdate
protected void onUpdate() {
this.updatedAt = LocalDateTime.now();
}


public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}


public List<Ticket> getTickets() {
return tickets;
}

public void setTickets(List<Ticket> tickets) {
this.tickets = tickets;
}




}


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

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

public class BadRequestException extends RuntimeException {
public BadRequestException(String message) {
super(message);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/DTO/ErrorData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.booleanuk.api.cinema.DTO;


public class ErrorData {
private String message;

// Constructor, getters, and setters
public ErrorData(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/DTO/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.booleanuk.api.cinema.DTO;

public class ErrorResponse {
private String status;
private ErrorData data;

// No-argument constructor
public ErrorResponse() {}

// Parameterized constructor
public ErrorResponse(String status, ErrorData data) {
this.status = status;
this.data = data;
}

// Getters and setters
public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public ErrorData getData() {
return data;
}

public void setData(ErrorData data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.booleanuk.api.cinema.DTO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFoundException(NotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse("error", new ErrorData(ex.getMessage()));
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(BadRequestException ex) {
ErrorResponse errorResponse = new ErrorResponse("error", new ErrorData(ex.getMessage()));
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/DTO/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.booleanuk.api.cinema.DTO;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
82 changes: 82 additions & 0 deletions src/main/java/com/booleanuk/api/cinema/DTO/TicketDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.booleanuk.api.cinema.DTO;

import java.time.LocalDateTime;

public class TicketDTO {
private Integer id;
private int numSeats;
private LocalDateTime startsAt;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Integer customerId;
private Integer screeningId;

public TicketDTO() {
}

public TicketDTO(Integer id, int numSeats, LocalDateTime startsAt, LocalDateTime createdAt, LocalDateTime updatedAt, Integer customerId, Integer screeningId) {
this.id = id;
this.numSeats = numSeats;
this.startsAt = startsAt;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.customerId = customerId;
this.screeningId = screeningId;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public int getNumSeats() {
return numSeats;
}

public void setNumSeats(int numSeats) {
this.numSeats = numSeats;
}

public LocalDateTime getStartsAt() {
return startsAt;
}

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

public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}

public Integer getCustomerId() {
return customerId;
}

public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}

public Integer getScreeningId() {
return screeningId;
}

public void setScreeningId(Integer screeningId) {
this.screeningId = screeningId;
}
}
Loading