Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up #20

Merged
merged 1 commit into from
Dec 18, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<Rental> 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.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Rental> getRentalHistory(Integer userId);

boolean returnCharger(Map<String, Object> paramMap);
boolean returnPowerBank(Map<String, Object> paramMap);

boolean returnChargerStation(Integer chargingStationId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface MainService {

List<Rental> getRentalHistory(String username);

boolean returnCharger(Integer rentalId);
boolean returnPowerBank(Integer rentalId);

boolean rentPowerBank(RentalDetail rentalDetail);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public List<Rental> 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;
Expand All @@ -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())
);
}
Expand All @@ -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<LoginResponse> registerUser(User user) {
boolean isRegistered = false;
Expand Down
2 changes: 1 addition & 1 deletion back-end/src/main/resources/xml/RentalMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SELECT * FROM rental_history WHERE user_id = #{userId};
</select>

<update id="returnCharger" parameterType="Integer">
<update id="returnPowerBank" parameterType="Integer">
UPDATE rental_history
SET
rental_status = 'Returned',
Expand Down
4 changes: 2 additions & 2 deletions front-end/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ function App() {
},
{
path: "/rentals",
element: <RentalHistory />
//Todo: once it's working add: loader: checkAuthLoader,
element: <RentalHistory />,
loader: checkAuthLoader,
},
{
path: "/logout",
Expand Down
2 changes: 0 additions & 2 deletions front-end/src/RentalHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
Expand Down