Travel Adventure project - Create REST APIs with Spring

The project itself here:
https://www.codecademy.com/paths/create-rest-apis-with-spring-and-java/tracks/spring-apis-responding-to-requests/modules/spring-controllers/projects/travel-adventures-api

Can anyone help and tell me why am I facing an error like this??

[ERROR] /home/ccuser/worspace/spring-traveladventuresapi-v2/src/main/java/com/codecademy/plants/controllers/TravelAdventuresController.java:
[40,36] cannot find symbol
[ERROR] symbol: method finbByCountry(java.lang.String)
[ERROR] location: variable adventureRepository of type com.codecademy.plants.repositories.AdventureRepository

And I have the same with findByState() method. Might this be a problem with interface implementation? As far as I know I haven’t change anything in other files than just the TravelAdventureController.java.

Thanks in advance!

I have had many problems with this exercise, but I have done it looking at the superheroes and it is working for me.
I copied all the superhero imports

package com.codecademy.plants.controllers;

import java.util.List;
import java.lang.Iterable;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.codecademy.plants.entities.Adventure;
import com.codecademy.plants.repositories.AdventureRepository;

@RestController
@RequestMapping(“/traveladventures”)
public class TravelAdventuresController {

private final AdventureRepository adventureRepository;

public TravelAdventuresController(AdventureRepository adventureRepo) {
this.adventureRepository = adventureRepo;
}

// Add controller methods below:
@GetMapping()
public Iterable getAllAdventure(){
Iterable adventures = this.adventureRepository.findAll();
return adventures;
}

@GetMapping(“/bycountry/{country}”)
public List getAdventureByCountrycurl (@PathVariable String country){
return adventureRepository.findByCountry(country);
}

///accepts GET request to traveladventures/bystate?state=Lisboa
@GetMapping(“/bystate”)
public List getAdventureByState(@PathVariable String state){
return adventureRepository.findByState(state);
}

}

I tested it with:
curl http://localhost:4001/traveladventures
curl http://localhost:4001/traveladventures/bycountry/Greece
curl http://localhost:4001/traveladventures/“”

2 Likes

Thank you very much for that!

I finally solved the problem some time ago (with help from the project walkthrough video, I was surprised to find it but yes, there is one!), so now I don’t even remember what was my issue then… though, I believe your answer will help next learners for sure!

Thanks for this!
I c/p and double-checked, but it’s still not working for me. I think there’s a Codecademy bug - one of the files seems to be missing because codecademy’s saying the file AdventureRepository.java has been relocated and it won’t run properly until it’s been redirected.

Here’s my code (still not working though)

package com.codecademy.plants.controllers;

import java.util.List;
import java.lang.Iterable;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.codecademy.plants.entities.Adventure;
import com.codecademy.plants.repositories.AdventureRepository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseStatusException;
import org.springframework.http.HttpStatus;
import java.util.Optional;
import org.springframework.web.bind.annotation.DeleteMapping;


@RestController
@RequestMapping(“/traveladventures”)
public class TravelAdventuresController {
  
  private final AdventureRepository adventureRepository;

  public TravelAdventuresController(AdventureRepository adventureRepo) {
    return this.adventureRepository = adventureRepo;
  }

  // Add controller methods below:
  @GetMapping()
  public Iterable<Adventure> getAllAdventures(){
    return this.adventureRepository.findAll();
  }

  @GetMapping("/bycountry/{country}")
  public List getAdventureByCountrycurl (@PathVariable String country){
    return adventureRepository.findByCountry(country);
  }

  // accepts GET request to traveladventures/bystate?state=Lisboa
  @GetMapping("/bystate")
  public List<Adventure> getAdventureByState(@PathVariable String state){
    return adventureRepository.findByState(state);
  }

  @PostMapping()
  public Adventure createNewAdventure(@RequestBody Adventure adventure){
    Adventure newAdventure = this.adventureRepository.save(adventure);
    return newAdventure;
  }

  @PostMapping("{/id}")
  public Adventure updateAdventure(@PathVariable("id") Integer id, @RequestBody Adventure adventure){

    Optional<Adventure> adventureToUpdateOptional = this.adventureRepository.findById(id);

    if (!adventureToUpdateOptional.isPresent()){
      throw new ResponseStatusException(HttpStatus.NOT_FOUND);
    }

    Adventure adventureToUpdate = adventureToUpdateOptional.get();
    
    if (!adventure.getBlogCompleted()){
      adventureToUpdate.setBlogCompleted(true);
    } else {
      adventureToUpdate.setBlogCompleted(false);
    }

    Adventure updatedAdventure = this.adventureRepository.save(adventureToUpdate);
    return updatedAdventure;
  }

  @DeleteMapping("/{id}")
  public void deleteAdventure(@PathVariable("id") Integer id){
    Optional<Adventure> adventureToDeleteOptional = this.adventureRepository.findById(id);

    if (adventureToDeleteOptional.isPresent()){
      Adventure adventureToDelete = adventureToDeleteOptional.get();
      this.adventureRepository.delete(adventureToDelete);
    }
  }
}

There must really be a problem on their side!
My code, which worked before and is pretty similar to yours, does not compile any more. I get the same errors related to finbByCountry(java.lang.String) and finbByState(java.lang.String)) form AdventureRepository.java.
And, again, I have the problem with ‘Failed to connect to localhost…’ permanently blocking my curl tests.

Where did you find the information about AdventureRepository.java being relocated - what do you mean by “codecademy’s saying…”?

It was a error notice Codecademy.com gave me. I didn’t know if it was for us the students or the developers @ Codecademy so I ignored it. It seems they moved that file somewhere else and forgot to update the path ^^

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.