Added a new controller and updated service and repository layer

parent c63b6c70
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.service.ParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* This is the primary controller that will be used
* to fetch the API data requested.
*/
@RestController
public class QueryController {
@Autowired
private ParticipantService participantService;
@RequestMapping(value = "/{surveyId}/participant/{participantId}")
public Participant getParticipantFromSurvey(
@PathVariable("surveyId") Long surveyId,
@PathVariable("participantId") Long participantId) {
return participantService.findByIdAndSurveyId(participantId, surveyId);
}
}
\ No newline at end of file
package no.nsd.websurvey.tromso.controller;
import no.nsd.websurvey.tromso.domain.Participant;
import no.nsd.websurvey.tromso.domain.Survey;
import no.nsd.websurvey.tromso.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -28,4 +29,4 @@ public class SurveyController {
public Survey getSurvey(@PathVariable("id") Long id) {
return surveyService.findById(id);
}
}
}
\ No newline at end of file
......@@ -11,4 +11,6 @@ public interface ParticipantRepository extends CrudRepository<Participant, Long>
public List<Participant> findBySurvey(Survey survey);
public List<Participant> findBySurveyId(Long id);
public Participant findByIdAndSurveyId(Long id, Long surveyId);
}
......@@ -42,5 +42,14 @@ public interface ParticipantService {
* @return {@link java.util.List} of {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public List<Participant> findBySurveyId(Long id);
/**
* Return one specific participant based on both {@link no.nsd.websurvey.tromso.domain.Survey}.id and
* {@link no.nsd.websurvey.tromso.domain.Participant}.id
* @param id of the {@link no.nsd.websurvey.tromso.domain.Participant}
* @param surveyId id of the {@link no.nsd.websurvey.tromso.domain.Survey}
* @return the resulting {@link no.nsd.websurvey.tromso.domain.Participant}
*/
public Participant findByIdAndSurveyId(Long id, Long surveyId);
}
......@@ -41,6 +41,11 @@ public class ParticipantServiceImpl implements ParticipantService {
return toList(participantRepository.findBySurveyId(id));
}
@Override
public Participant findByIdAndSurveyId(Long id, Long surveyId) {
return participantRepository.findByIdAndSurveyId(id, surveyId);
}
/**
* Transform results in a {@link java.lang.Iterable} into an {@link java.util.ArrayList}
* @param iterable {@link java.lang.Iterable} containg {@link no.nsd.websurvey.tromso.domain.Participant} objects.
......
......@@ -136,4 +136,20 @@ public class RestControllerTests {
assertNotNull(participant);
}
}
@Test
public void testGetParticipantsIdAndSurveyIdFromUrl() throws Exception {
Long surveyId = 1L;
Long participantId = 10L;
RestTemplate template = new RestTemplate();
ResponseEntity<Participant> participantEntity = template.getForEntity(
"http://localhost:8080/"+surveyId+"/participant/"+participantId, Participant.class);
Participant participant = participantEntity.getBody();
assertThat(participantEntity.getStatusCode().toString(), is("200"));
assertNotNull(participant);
assertTrue(participant.getId().equals(participantId));
}
}
\ No newline at end of file
......@@ -82,4 +82,15 @@ public class ParticipantServiceTest {
assertTrue(participant.getSurvey().getId().equals(surveyId));
}
}
@Test
public void testFindParticipantByIdAndSurveyId() throws Exception {
Long surveyId = 1L;
Long participantId = 10L;
Participant participant = participantService.findByIdAndSurveyId(participantId, surveyId);
assertNotNull(participant);
assertTrue(participant.getId().equals(participantId));
assertTrue(participant.getSurvey().getId().equals(surveyId));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment