From b40c4b33c9ebb4b2e21cc181eed97f853ce19577 Mon Sep 17 00:00:00 2001 From: fekorte Date: Mon, 18 Dec 2023 14:17:59 -0500 Subject: [PATCH] clean up --- .../backend/controller/MainController.java | 2 +- .../opensourcedev/backend/mapper/RentalMapper.java | 3 +-- .../opensourcedev/backend/service/MainService.java | 2 +- .../backend/service/MainServiceImpl.java | 12 ++++-------- back-end/src/main/resources/xml/RentalMapper.xml | 2 +- front-end/src/App.js | 4 ++-- front-end/src/RentalHistory.js | 2 -- 7 files changed, 10 insertions(+), 17 deletions(-) diff --git a/back-end/src/main/java/com/opensourcedev/backend/controller/MainController.java b/back-end/src/main/java/com/opensourcedev/backend/controller/MainController.java index ad73317..851f4d2 100644 --- a/back-end/src/main/java/com/opensourcedev/backend/controller/MainController.java +++ b/back-end/src/main/java/com/opensourcedev/backend/controller/MainController.java @@ -38,7 +38,7 @@ public List getRentalHistory(@PathVariable String username) { @PostMapping("/return/{rentalId}") public String returnCharger(@PathVariable Integer rentalId) { - return (mainService.returnCharger(rentalId)) + return (mainService.returnPowerBank(rentalId)) ? "Rental returned successfully" : "Error, rental couldn't be returned."; } diff --git a/back-end/src/main/java/com/opensourcedev/backend/mapper/RentalMapper.java b/back-end/src/main/java/com/opensourcedev/backend/mapper/RentalMapper.java index a21c89e..fb2f08e 100644 --- a/back-end/src/main/java/com/opensourcedev/backend/mapper/RentalMapper.java +++ b/back-end/src/main/java/com/opensourcedev/backend/mapper/RentalMapper.java @@ -7,13 +7,12 @@ import java.util.Map; import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Param; @Mapper public interface RentalMapper { List getRentalHistory(Integer userId); - boolean returnCharger(Map paramMap); + boolean returnPowerBank(Map paramMap); boolean returnChargerStation(Integer chargingStationId); diff --git a/back-end/src/main/java/com/opensourcedev/backend/service/MainService.java b/back-end/src/main/java/com/opensourcedev/backend/service/MainService.java index 9f35107..6dbb6fe 100644 --- a/back-end/src/main/java/com/opensourcedev/backend/service/MainService.java +++ b/back-end/src/main/java/com/opensourcedev/backend/service/MainService.java @@ -17,7 +17,7 @@ public interface MainService { List getRentalHistory(String username); - boolean returnCharger(Integer rentalId); + boolean returnPowerBank(Integer rentalId); boolean rentPowerBank(RentalDetail rentalDetail); diff --git a/back-end/src/main/java/com/opensourcedev/backend/service/MainServiceImpl.java b/back-end/src/main/java/com/opensourcedev/backend/service/MainServiceImpl.java index 4a3effe..11aa46d 100644 --- a/back-end/src/main/java/com/opensourcedev/backend/service/MainServiceImpl.java +++ b/back-end/src/main/java/com/opensourcedev/backend/service/MainServiceImpl.java @@ -42,10 +42,11 @@ public List getRentalHistory(String username) { } @Override - public boolean returnCharger(Integer rentalId) { + public boolean returnPowerBank(Integer rentalId) { Rental rentalToReturn = rentalMapper.getRentalByID(rentalId); Date startTime = rentalToReturn.getRentalDate(); - Integer hoursBetween = calculateHoursBetweenDates(startTime, new Date()); + long durationInMillis = new Date().getTime() - startTime.getTime(); + int hoursBetween = (int) TimeUnit.MILLISECONDS.toHours(durationInMillis); Integer chargesPerHour = rentalToReturn.getCharges(); Integer endPrice = chargesPerHour; @@ -58,7 +59,7 @@ public boolean returnCharger(Integer rentalId) { paramMap.put("charges", endPrice); paramMap.put("duration", hoursBetween); return ( - rentalMapper.returnCharger(paramMap) && + rentalMapper.returnPowerBank(paramMap) && rentalMapper.returnChargerStation(rentalToReturn.getChargingStationId()) ); } @@ -85,11 +86,6 @@ public boolean rentPowerBank(RentalDetail rentalDetail) { } } - private Integer calculateHoursBetweenDates(Date startDate, Date endDate) { - long durationInMillis = endDate.getTime() - startDate.getTime(); - return (int) TimeUnit.MILLISECONDS.toHours(durationInMillis); - } - @Override public ResponseEntity registerUser(User user) { boolean isRegistered = false; diff --git a/back-end/src/main/resources/xml/RentalMapper.xml b/back-end/src/main/resources/xml/RentalMapper.xml index 722dfed..a88c9b2 100644 --- a/back-end/src/main/resources/xml/RentalMapper.xml +++ b/back-end/src/main/resources/xml/RentalMapper.xml @@ -18,7 +18,7 @@ SELECT * FROM rental_history WHERE user_id = #{userId}; - + UPDATE rental_history SET rental_status = 'Returned', diff --git a/front-end/src/App.js b/front-end/src/App.js index 7fcb394..471991c 100644 --- a/front-end/src/App.js +++ b/front-end/src/App.js @@ -36,8 +36,8 @@ function App() { }, { path: "/rentals", - element: - //Todo: once it's working add: loader: checkAuthLoader, + element: , + loader: checkAuthLoader, }, { path: "/logout", diff --git a/front-end/src/RentalHistory.js b/front-end/src/RentalHistory.js index 98a1c72..3691b95 100644 --- a/front-end/src/RentalHistory.js +++ b/front-end/src/RentalHistory.js @@ -15,9 +15,7 @@ const RentalHistory = () => { const fetchRentalHistory = async () => { try { - //Todo: fetch current username / user id const username = localStorage.getItem("username"); - //const username = 'fe'; const response = await fetch( `${API_BASE_URL + "/getRentalHistory/" + username}` );