UserController.java(处理用户注册与登录)
JAVA代码复制
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody UserDto userDto) {
userService.register(userDto);
return ResponseEntity.ok("User registered successfully");
}
@PostMapping("/login")
public ResponseEntity<UserDto> login(@RequestBody LoginDto loginDto) {
UserDto userDto = userService.login(loginDto);
return ResponseEntity.ok(userDto);
}
}
RideRequestController.java(处理顺风车需求发布与查看)
java复制代码
@RestController
@RequestMapping("/ride")
public class RideRequestController {
@Autowired
private RideRequestService rideRequestService;
@PostMapping("/request")
public ResponseEntity<String> createRideRequest(@RequestBody RideRequestDto rideRequestDto) {
rideRequestService.createRideRequest(rideRequestDto);
return ResponseEntity.ok("Ride request created successfully");
}
@GetMapping("/requests")
public ResponseEntity<List<RideRequestDto>> getNearbyRideRequests(@RequestParam double latitude, @RequestParam double longitude) {
List<RideRequestDto> rideRequests = rideRequestService.getNearbyRideRequests(latitude, longitude);
return ResponseEntity.ok(rideRequests);
}
}
RideRequestService.java(业务逻辑层)
java复制代码
@Service
public class RideRequestService {
@Autowired
private RideRequestRepository rideRequestRepository;
@Autowired
private UserService userService;
public void createRideRequest(RideRequestDto rideRequestDto) {
User user = userService.findById(rideRequestDto.getUserId());
RideRequest rideRequest = new RideRequest();
rideRequest.setUser(user);
rideRequest.setPickupLocation(rideRequestDto.getPickupLocation());
rideRequest.setDestination(rideRequestDto.getDestination());
rideRequest.setDepartureTime(rideRequestDto.getDepartureTime());
rideRequestRepository.save(rideRequest);
}
public List<RideRequestDto> getNearbyRideRequests(double latitude, double longitude) {
List<RideRequest> rideRequests = rideRequestRepository.findByLocationNear(latitude, longitude);
// Convert RideRequest to RideRequestDto and return
}
}