Skip to content

Commit f765e04

Browse files
committed
connected booking service with event service successfully. using FeignClient. Also created a API gateway so that we can access same port eg:8080 but Eureka does the work of finding the correct service based on the endpoint hit
1 parent 19d4916 commit f765e04

14 files changed

Lines changed: 188 additions & 2 deletions

File tree

‎.idea/encodings.xml‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api-gateway/pom.xml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stream-seat</artifactId>
7+
<groupId>com.streamseat</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>api-gateway</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.cloud</groupId>
17+
<artifactId>spring-cloud-starter-gateway</artifactId>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.springframework.cloud</groupId>
22+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.streamseat;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
System.out.println("Hello, World!");
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.streamseat.apigateway;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ApiGatewayApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(ApiGatewayApplication.class, args);
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spring.application.name=api-gateway
2+
server.port=8080
3+
4+
# Connect to Eureka
5+
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
6+
7+
# --- ROUTING RULES ---
8+
9+
# Route 1: Events
10+
# "lb://" means Load Balance using Eureka!
11+
spring.cloud.gateway.routes[0].id=event-service
12+
spring.cloud.gateway.routes[0].uri=lb://event-service
13+
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/events/**
14+
15+
# Route 2: Bookings
16+
spring.cloud.gateway.routes[1].id=booking-service
17+
spring.cloud.gateway.routes[1].uri=lb://booking-service
18+
spring.cloud.gateway.routes[1].predicates[0]=Path=/api/bookings/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.streamseat.bookingservice.client;
2+
3+
import com.streamseat.bookingservice.dto.EventDTO;
4+
import org.springframework.cloud.openfeign.FeignClient;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
8+
// NAME must match the name in Eureka (EVENT-SERVICE)
9+
@FeignClient(name = "event-service")
10+
public interface EventClient {
11+
12+
// We are "mapping" the call to the Event Service's existing endpoint
13+
// GET http://event-service/api/events/{id}
14+
// (Note: We need to go add this endpoint to Event Service later if it's missing!)
15+
@GetMapping("/api/events/{id}")
16+
EventDTO getEventById(@PathVariable("id") Long id);
17+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.streamseat.bookingservice.controller;
2+
3+
import com.streamseat.bookingservice.client.EventClient;
4+
import com.streamseat.bookingservice.dto.EventDTO;
5+
import com.streamseat.bookingservice.model.Booking;
6+
import com.streamseat.bookingservice.repository.BookingRepository;
7+
import org.springframework.web.bind.annotation.*;
8+
import java.time.LocalDateTime;
9+
10+
@RestController
11+
@RequestMapping("/api/bookings")
12+
public class BookingController {
13+
14+
private final BookingRepository bookingRepository;
15+
private final EventClient eventClient; // <--- The connection to Event Service
16+
17+
public BookingController(BookingRepository bookingRepository, EventClient eventClient) {
18+
this.bookingRepository = bookingRepository;
19+
this.eventClient = eventClient;
20+
}
21+
22+
@PostMapping
23+
public String bookTicket(@RequestBody Booking bookingRequest) {
24+
Long eventId = bookingRequest.getEventId();
25+
26+
// 1. TALK to Event Service: "Does this event exist?"
27+
EventDTO event = eventClient.getEventById(eventId);
28+
if (event == null) {
29+
return "Failed: Event not found!";
30+
}
31+
32+
// 2. CHECK Capacity: "Are there seats left?"
33+
long seatsTaken = bookingRepository.countByEventId(eventId);
34+
if (seatsTaken >= event.getCapacity()) {
35+
return "Failed: Housefull! Event is sold out.";
36+
}
37+
38+
// 3. BOOK IT
39+
bookingRequest.setStatus("CONFIRMED");
40+
bookingRequest.setBookingTime(LocalDateTime.now());
41+
bookingRepository.save(bookingRequest);
42+
43+
return "Success! Ticket booked for event: " + event.getTitle();
44+
}
45+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.streamseat.bookingservice.dto;
2+
3+
import lombok.Data;
4+
import java.time.LocalDateTime;
5+
6+
@Data
7+
public class EventDTO {
8+
private Long id;
9+
private String title;
10+
private int capacity;
11+
// private LocalDateTime eventDateTime;
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.streamseat.bookingservice.model;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
import java.time.LocalDateTime;
9+
10+
@Entity
11+
@Table(name = "bookings")
12+
@Data
13+
@Builder
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class Booking {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.IDENTITY)
19+
private Long id;
20+
21+
private Long eventId; // Which event?
22+
private Long userId; // Who booked it? (We'll hardcode this for now)
23+
private String status; // CONFIRMED, CANCELLED
24+
private LocalDateTime bookingTime;
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.streamseat.bookingservice.repository;
2+
3+
import com.streamseat.bookingservice.model.Booking;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface BookingRepository extends JpaRepository<Booking, Long> {
7+
// Custom query to count how many tickets are sold for an event
8+
long countByEventId(Long eventId);
9+
}

0 commit comments

Comments
 (0)