Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dapperAuteur/e0c9abd1a55ddf6fcfd9af6862c4e056 to your computer and use it in GitHub Desktop.
Save dapperAuteur/e0c9abd1a55ddf6fcfd9af6862c4e056 to your computer and use it in GitHub Desktop.
this is the controller and repository
package com.intraedge.project.prok.controllers;
import java.util.List;
import javax.validation.Valid;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.intraedge.project.prok.domain.League;
import com.intraedge.project.prok.repositories.LeagueRepository;
@RestController
//@RequestMapping("/leagues")
public class LeagueController {
@Autowired
private LeagueRepository leagueRepository;
@RequestMapping(value="/leagues", method=RequestMethod.GET)
public List<League> getLeagues(){
return leagueRepository.findAll();
}
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public League getLeagueById(@PathVariable("id") ObjectId _id) {
return leagueRepository.findBy_id(_id);
}
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public League modifyLeagueById(@PathVariable("id") ObjectId _id, @Valid @RequestBody League league) {
league.set_id(_id);
leagueRepository.save(league);
return league;
}
@RequestMapping(value="/", method=RequestMethod.POST)
public League createLeague(@Valid @RequestBody League league) {
league.set_id(ObjectId.get());
leagueRepository.save(league);
return league;
}
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public void deleteLeague(@PathVariable ObjectId _id) {
leagueRepository.delete(leagueRepository.findBy_id(_id));
}
}
//Repository LeagueRepository
package com.intraedge.project.prok.repositories;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.intraedge.project.prok.domain.League;
public interface LeagueRepository extends MongoRepository<League, String> {
League findBy_id(ObjectId _id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment