0% found this document useful (0 votes)
9 views2 pages

Cab Service Impl

The document defines a Java class that implements methods for managing vehicle data in a cab booking service. It uses Spring annotations and interfaces to define services for adding, updating, fetching and removing vehicle data from a repository using methods like addVehicle, updateVehicle, fetchVehicleTypes, countVehiclesOfType and removeVehicle.

Uploaded by

donkartikraut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Cab Service Impl

The document defines a Java class that implements methods for managing vehicle data in a cab booking service. It uses Spring annotations and interfaces to define services for adding, updating, fetching and removing vehicle data from a repository using methods like addVehicle, updateVehicle, fetchVehicleTypes, countVehiclesOfType and removeVehicle.

Uploaded by

donkartikraut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.cabbooking.cabservice.

service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cabbooking.cabservice.entity.Cab;
import com.cabbooking.cabservice.exception.NotFoundException;
import com.cabbooking.cabservice.model.CabInfo;
import com.cabbooking.cabservice.model.DriverInfo;
import com.cabbooking.cabservice.repository.VehicleRepository;

@Service
public class VehicleServiceImpl implements VehicleService {
@Autowired
private VehicleRepository vehicleRepository;

@Autowired
private DriverService driverServiceClient;

@Override
public Cab addVehicle(Cab vehicle) {
return vehicleRepository.save(vehicle);
}

@Override
public Cab updateVehicle(Cab vehicle) throws NotFoundException {
Optional<Cab> optionalVehicle =
vehicleRepository.findById(vehicle.getId());
if (optionalVehicle.isEmpty()) {
throw new NotFoundException("Vehicle not found with ID: " +
vehicle.getId());
}
vehicleRepository.save(vehicle);
return vehicle;
}

@Override
public List<String> fetchVehicleTypes(String vehicleType) throws
NotFoundException {
List<String> vehicleTypes =
vehicleRepository.fetchDistinctVehicleTypes(vehicleType);
return vehicleTypes;
}

@Override
public int countVehiclesOfType() throws NotFoundException {
List<Cab> totalVehicles = vehicleRepository.findAll();
return totalVehicles.size();
}

@Override
public void removeVehicle(int vehicleId) {
Optional<Cab> optionalVehicle = vehicleRepository.findById(vehicleId);
if (optionalVehicle.isEmpty()) {
throw new NotFoundException("Vehicle not found with ID: " +
vehicleId);
}
Cab vehicle = optionalVehicle.get();
vehicleRepository.delete(vehicle);
}

@Override
public List<CabInfo> getAllVehicles() {
List<Cab> vehicleList = vehicleRepository.findAll();
List<CabInfo> vehicleInfoList = new ArrayList<>();
for (Cab vehicle : vehicleList) {
CabInfo vehicleInfo = new CabInfo();
vehicleInfo.setVehicleId(vehicle.getId());
vehicleInfo.setVehicleType(vehicle.getVehicleType());
vehicleInfo.setRatePerKm(vehicle.getRatePerKm());
vehicleInfo.setDriverId(vehicle.getDriverId());

int driverId = vehicle.getDriverId();

DriverInfo driverInfo =
driverServiceClient.fetchDriverInfo(driverId);

vehicleInfo.setDriverInfo(driverInfo);
vehicleInfoList.add(vehicleInfo);

return vehicleInfoList;
}

You might also like