Skip to content

Commit

Permalink
re-adding
Browse files Browse the repository at this point in the history
  • Loading branch information
Peetlebeetle committed May 26, 2023
1 parent 6fb0be4 commit 339259f
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ hms/target
hotel/target
client-new/target
.idea
admin/target
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# astrum distributed systems
localhost:8084/roomInfo.html



Expand Down
Binary file modified admin/target/admin-0.0.1.jar
Binary file not shown.
Binary file modified admin/target/admin-0.0.1.jar.original
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:\Users\Ross\Documents\4th Year UCD\Distributed Systems\GroupProject\gitlab\astrum-distributed-systems\admin\src\main\java\service\Application.java
E:\UCD\4th_year\Semester_2\COMP30220-Distributed Systems\astrum\repo\astrum-distributed-systems\admin\src\main\java\service\Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String handleQuoteResponse(@RequestParam("arg") String responseArg) {
htmlPage.append(" <p>Price: " + price[quoteCounter] + "</p>");

htmlPage.append(
" <p>" + responseArg + "</p>\n" +
//" <p>" + responseArg + "</p>\n" +
" <form action=\"/payments\" method=\"get\">\n" +
" <input type=\"hidden\" name=\"responseArg\" value=\'" + responseArg.replace("'", "&#39;") + "\'/>\n" +
" <input type=\"submit\" value=\"Proceed to Payment\"/>\n" +
Expand All @@ -53,20 +53,6 @@ public String handleQuoteResponse(@RequestParam("arg") String responseArg) {

quoteCounter++;
}


//return "<html>\n"
// + " <body>\n"
// + " <h1>Quote Response</h1>\n"
// + " <p>" + responseArg + "</p>\n"
// + " <p>Company: " + company[0] + "</p>\n"
// + " <form action=\"/payments\" method=\"get\">\n"
// + " <input type=\"hidden\" name=\"responseArg\" value=\'" + responseArg.replace("'", "&#39;") + "\'/>\n"
// + " <input type=\"submit\" value=\"Proceed to Payment\"/>\n"
// + " </form>\n"
// + " </body>\n"
// + "</html>";

}
catch (Exception e) {
System.out.println("\n");
Expand All @@ -79,13 +65,6 @@ public String handleQuoteResponse(@RequestParam("arg") String responseArg) {
" </body>\n" +
"</html>"
);

// return "<html>\n"
// + " <body>\n"
// + " <h1>Error</h1>\n"
// + " <p>An error occurred while processing the quote response.</p>\n"
// + " </body>\n"
// + "</html>";
}

htmlPage.append(
Expand Down
13 changes: 7 additions & 6 deletions client-new/src/main/resources/static/roomInfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@
.then(function(response) {
if (response.ok) {
console.log("Response Ok!");
response.text().then(function(responseText) {
console.log("Response text:", responseText);
createQuotePage(responseText);
response.json().then(function(responseJson) {
//console.log("Response text:", responseJson);
createQuotePage(responseJson);
});
} else {
console.error('Application Creation Failed! HTTP status:', response.status);
console.log('Response: ', response.text());
alert('Application Creation Failed!');
}
})
Expand All @@ -103,9 +104,9 @@
});

function createQuotePage(response) {
var url = 'http://localhost:8084/quoteResponse?arg=' + encodeURIComponent(response);
console.log("Opening URL:", url);
window.open(url,'_blank');
var url = 'http://localhost:8084/quoteResponse?arg=' + encodeURIComponent(JSON.stringify(response));
//console.log("Opening URL:", url);
window.location.href = url;
}
</script>
</body>
Expand Down
54 changes: 48 additions & 6 deletions hms/src/main/java/service/controllers/HMSController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;

@RestController
@RequestMapping("/applications")
@CrossOrigin(origins = "*")
Expand All @@ -29,34 +33,69 @@ public class HMSController {

private List<String> quotationUrls = new ArrayList<>(); // add a list to store the URL

//@PostMapping(consumes = "application/json", produces = "application/json")
//public ResponseEntity<Application> createApplication(
// @RequestBody RoomInfo roomInfo) {
// System.out.println("Received a request to create an application");
// Application application = new Application(roomInfo);
// System.out.println("Creating application: " + application.id);
// applications.put(application.id, application);
//
// for (String quotationUrl : quotationUrls) {
// System.out.println("Requesting quotation from " + quotationUrl);
// ResponseEntity<Quotation> response = restTemplate.postForEntity(quotationUrl, roomInfo, Quotation.class);
// if (response.getStatusCode().equals(HttpStatus.CREATED)) {
// application.quotations.add(response.getBody());
// // print out the body of the quotation
// System.out.println("Quotation received: " + response.getBody());
// } else {
// System.out.println("Error requesting quotation from " + quotationUrl + ". Response status: "
// + response.getStatusCode());
// }
// }
// System.out.println("Application created and saved. Returning application with status CREATED.");
// return ResponseEntity
// .status(HttpStatus.CREATED)
// .body(application);
//}

@PostMapping(consumes = "application/json", produces = "application/json")
public ResponseEntity<Application> createApplication(
@RequestBody RoomInfo roomInfo) {
public ResponseEntity<Application> createApplication(@RequestBody RoomInfo roomInfo) {
System.out.println("Received a request to create an application");
Application application = new Application(roomInfo);
System.out.println("Creating application: " + application.id);
applications.put(application.id, application);

for (String quotationUrl : quotationUrls) {
System.out.println("Requesting quotation from " + quotationUrl);
ResponseEntity<Quotation> response = restTemplate.postForEntity(quotationUrl, roomInfo, Quotation.class);
ResponseEntity<ArrayList<Quotation>> response = restTemplate.exchange(
quotationUrl,
HttpMethod.POST,
new HttpEntity<>(roomInfo),
new ParameterizedTypeReference<ArrayList<Quotation>>() {
}
);
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
application.quotations.add(response.getBody());
// print out the body of the quotation
System.out.println("Quotation received: " + response.getBody());
ArrayList<Quotation> quotations = response.getBody();
application.quotations.addAll(quotations);
// Process the received quotations
// ...
} else {
System.out.println("Error requesting quotation from " + quotationUrl + ". Response status: "
+ response.getStatusCode());
}
}

System.out.println("Application created and saved. Returning application with status CREATED.");
return ResponseEntity
.status(HttpStatus.CREATED)
.body(application);
}


@GetMapping(produces = "application/json")
public ResponseEntity<List<Application>> getApplications() {
System.out.println("HMSController 0");
List<Application> applicationList = new ArrayList<>(applications.values());
return ResponseEntity
.status(HttpStatus.OK)
Expand All @@ -65,6 +104,7 @@ public ResponseEntity<List<Application>> getApplications() {

@GetMapping(value = "/{id}", produces = "application/json")
public ResponseEntity<Application> getApplication(@PathVariable int id) {
System.out.println("HMSController 1");
Application application = applications.get(id);
if (application != null) {
return ResponseEntity
Expand All @@ -79,6 +119,7 @@ public ResponseEntity<Application> getApplication(@PathVariable int id) {

@PostMapping(value = "/services", consumes = "text/plain")
public ResponseEntity<String> addService(@RequestBody String url) {
System.out.println("HMSController 2");
if (!quotationUrls.contains(url)) {
quotationUrls.add(url);
System.out.println("Added new URL: " + url);
Expand All @@ -88,6 +129,7 @@ public ResponseEntity<String> addService(@RequestBody String url) {

@GetMapping(value = "/services", produces = "application/json")
public ResponseEntity<List<String>> getServices() {
System.out.println("HMSController 3");
return ResponseEntity.ok(quotationUrls);
}

Expand Down
8 changes: 8 additions & 0 deletions hotel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!--
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>{version}</version>
</dependency>
-->



<!-- https://mvnrepository.com/artifact/org.jmdns/jmdns -->
Expand Down
62 changes: 51 additions & 11 deletions hotel/src/main/java/service/controllers/HotelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import service.core.BookingInfo;
import service.core.Quotation;
import service.core.RoomInfo;
Expand Down Expand Up @@ -40,6 +43,10 @@ public ResponseEntity<ArrayList<String>> getQuotations() {
list.add("http:" + getHost()
+ "/quotations/" + quotation.reference);
}
System.out.println("\n####################");
System.out.println("HotelController: Get Quotations");
System.out.println("####################\n");

return ResponseEntity.status(HttpStatus.OK).body(list);
}

Expand All @@ -49,24 +56,57 @@ public ResponseEntity<Quotation> getQuotation(@PathVariable String id) {
if (quotation == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

System.out.println("\n####################");
System.out.println("HotelController: Get Quotation");
System.out.println("####################\n");

return ResponseEntity.status(HttpStatus.OK).body(quotation);
}

//@PostMapping(value = "/quotations", consumes = "application/json")
//public ResponseEntity<Quotation> createQuotation(
// @RequestBody RoomInfo info) {
// System.out.println("Service object: " + service);
// System.out.println("RoomInfo object: " + info);
// Quotation quotation = service.generateQuotation(info);
// System.out.println("Quotation object: " + quotation);
// quotations.put(quotation.reference, quotation);
// String url = "http://" + getHost() + "/quotations/"
// + quotation.reference;
//
// System.out.println("\n####################");
// System.out.println("HotelController: generate Quotation");
// System.out.println("####################\n");
//
// return ResponseEntity
// .status(HttpStatus.CREATED)
// .header("Location", url)
// .header("Content-Location", url)
// .body(quotation);
//}

@PostMapping(value = "/quotations", consumes = "application/json")
public ResponseEntity<Quotation> createQuotation(
@RequestBody RoomInfo info) {
System.out.println("Service object: " + service);
System.out.println("RoomInfo object: " + info);
Quotation quotation = service.generateQuotation(info);
System.out.println("Quotation object: " + quotation);
quotations.put(quotation.reference, quotation);
String url = "http://" + getHost() + "/quotations/"
+ quotation.reference;
return ResponseEntity
public ResponseEntity<ArrayList<Quotation>> createQuotations (@RequestBody RoomInfo info) {
ArrayList<Quotation> quotations = service.generateQuotations(info);

if (quotations.isEmpty()) {
System.out.println("No Quotations Generated");
return ResponseEntity.noContent().build();
}

System.out.println("\n##### ##### ##### #####");
for (Quotation quotation : quotations) {
this.quotations.put(quotation.reference, quotation);
}

String url = "http:// " + getHost() + "/quotations";

return ResponseEntity
.status(HttpStatus.CREATED)
.header("Location", url)
.header("Content-Location", url)
.body(quotation);
.body(quotations);
}

@PostMapping(value = "/confirmation", consumes = "application/json")
Expand Down
51 changes: 51 additions & 0 deletions hotel/src/main/java/service/service/HotelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -79,6 +80,56 @@ else if (roomInfo.view.equals("Any View")) {
return new Quotation(COMPANY, generateReference(PREFIX), defaultPrice, roomInfo);
}

public ArrayList<Quotation> generateQuotations(RoomInfo roomInfo) {
if (roomInfo == null) {
throw new IllegalArgumentException("RoomInfo cannot be null.");
}

List<Room> rooms = roomService.getAllRooms();
ArrayList<Quotation> quotations = new ArrayList<Quotation>();

if (rooms == null) {
throw new IllegalArgumentException("Rooms cannot be null.");
}

for (Room room : rooms) {
if ((roomInfo.type.equals(room.getType())) && (roomInfo.beds == room.getBeds()) && (roomInfo.bedSize == room.getBedSize()) && (roomInfo.balcony == room.isBalcony()) && (roomInfo.view.equals(room.getView())) && (roomInfo.accessibility == room.isAccessible())) {
long daysBetween = ChronoUnit.DAYS.between(room.getCheckInDate(), room.getCheckOutDate());
int days = Math.toIntExact(daysBetween);

// Add additional costs based on room attributes
double extraCosts = 0;
if (roomInfo.balcony) {
extraCosts += 20;
}
if (roomInfo.view.equals("Sea View")) {
extraCosts += 30;
}
else if (roomInfo.view.equals("Garden View")) {
extraCosts += 15;
}
else if (roomInfo.view.equals("Any View")) {
extraCosts += 0;
}

totalPrice = room.getPrice() * days + extraCosts;
Quotation quote = new Quotation(COMPANY, generateReference(PREFIX), totalPrice, roomInfo);

quotations.add(quote);
}
}

// If no matching room is found, return a Quotation for the requested RoomInfo.
if (quotations.isEmpty()) {
System.out.println("NO ROOMS AVAILABLE WITH THESE CRITERIA, CREATING A NEW QUOTATION");
// Assume a default price for the non-existent room.
double defaultPrice = 100.0;
quotations.add(new Quotation(COMPANY, generateReference(PREFIX), defaultPrice, roomInfo));
}
System.out.println(quotations.size());
return quotations;
}

public BookingInfo checkout(Checkout checkInfo){
List<Bookings> bookings = bookingsService.getAllBookings();

Expand Down
8 changes: 7 additions & 1 deletion hotel/src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ CREATE TABLE ROOMS(
PRICE DOUBLE,
BOOKED BOOLEAN
);

INSERT INTO ROOMS VALUES(1, 'Single', 1, 1.0, false, 'Sea View', false, '2023-06-01', '2023-06-03', 80, false);
INSERT INTO ROOMS VALUES(2, 'Double', 1, 1.5, true, 'Garden View', false, '2023-06-10', '2023-06-15', 120, false);
INSERT INTO ROOMS VALUES(3, 'Twin', 2, 1.0, false, 'Sea View', true, '2023-07-01', '2023-07-05', 100, false);
INSERT INTO ROOMS VALUES(4, 'Suite', 2, 2.0, true, 'City View', false, '2023-07-20', '2023-07-25', 200, false);
INSERT INTO ROOMS VALUES(5, 'Double', 1, 1.5, false, 'Garden View', true, '2023-08-01', '2023-08-10', 110, false);
INSERT INTO ROOMS VALUES(5, 'Double', 1, 1.5, false, 'Garden View', false, '2023-08-01', '2023-08-10', 110, false);
INSERT INTO ROOMS VALUES(6, 'Twin', 2, 1.0, true, 'Sea View', false, '2023-08-15', '2023-08-20', 90, false);

INSERT INTO ROOMS VALUES(7, 'Single', 1, 1.0, false, 'Sea View', false, '2023-06-01', '2023-06-03', 80, false);
INSERT INTO ROOMS VALUES(8, 'Single', 1, 1.0, false, 'Sea View', false, '2023-06-01', '2023-06-03', 85, false);
INSERT INTO ROOMS VALUES(9, 'Single', 1, 1.0, false, 'Sea View', false, '2023-06-01', '2023-06-03', 90, false);
INSERT INTO ROOMS VALUES(10, 'Single', 1, 1.0, false, 'Sea View', false, '2023-06-01', '2023-06-03', 95, false);

DROP TABLE IF EXISTS BOOKINGS;
CREATE TABLE BOOKINGS(
BOOKING_REF INT PRIMARY KEY,
Expand Down

0 comments on commit 339259f

Please sign in to comment.