Skip to content

Commit

Permalink
Feat searchsurveys campaignspartitioningsbysurvey endpoints (#50)
Browse files Browse the repository at this point in the history
* feat - add get campaigns and partitionings by survey id

* feat - add search surveys endpoints

* fix - integrate pr review modifs

---------

Co-authored-by: Eric Thuaud <eric.thuaud@insee.fr>
  • Loading branch information
EricThuaud and Eric Thuaud authored Feb 13, 2024
1 parent 3aca094 commit 4cd4d27
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>fr.insee.survey</groupId>
<artifactId>platine-management</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
<name>platine-management</name>
<description>REST API for communication between DB and Platine-Management UI and Platine-My-Surveys UI</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ private Constants() {
public static final String API_SOURCES_ID = "/api/sources/{id}";
public static final String API_SOURCES_ID_SURVEYS = "/api/sources/{id}/surveys";
public static final String API_SURVEYS = "/api/surveys";

public static final String API_SURVEYS_SEARCH = "/api/surveys/search";
public static final String API_SURVEYS_ID = "/api/surveys/{id}";
public static final String API_SURVEYS_ID_CAMPAIGNS = "/api/surveys/{id}/campaigns";

public static final String API_SURVEYS_ID_CAMPAIGNS_PARTITIONINGS = "/api/surveys/{id}/campaigns-partitionings";
public static final String API_CAMPAIGNS = "/api/campaigns";
public static final String API_CAMPAIGNS_ID = "/api/campaigns/{id}";
public static final String CAMPAIGNS_ID_ONGOING = "/campaigns/{id}/ongoing";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Survey;
import fr.insee.survey.datacollectionmanagement.metadata.dto.CampaignDto;
import fr.insee.survey.datacollectionmanagement.metadata.dto.CampaignPartitioningsDto;
import fr.insee.survey.datacollectionmanagement.metadata.dto.OnGoingDto;
import fr.insee.survey.datacollectionmanagement.metadata.service.CampaignService;
import fr.insee.survey.datacollectionmanagement.metadata.service.SurveyService;
Expand Down Expand Up @@ -84,6 +85,16 @@ public ResponseEntity<List<CampaignDto>> getCampaignsBySurvey(@PathVariable("id"

}

@Operation(summary = "Search for campaigns and partitionings by the survey id")
@GetMapping(value = Constants.API_SURVEYS_ID_CAMPAIGNS_PARTITIONINGS, produces = "application/json")
public ResponseEntity<List<CampaignPartitioningsDto>> getCampaignsPartitioningsBySurvey(@PathVariable("id") String id) {

Survey survey = surveyService.findById(id);
return ResponseEntity.ok()
.body(survey.getCampaigns().stream().map(this::convertToCampaignPartitioningsDto).toList());

}

@Operation(summary = "Search for a campaign by its id")
@GetMapping(value = Constants.API_CAMPAIGNS_ID, produces = "application/json")
@ApiResponses(value = {
Expand Down Expand Up @@ -178,6 +189,10 @@ private CampaignDto convertToDto(Campaign campaign) {
return modelmapper.map(campaign, CampaignDto.class);
}

private CampaignPartitioningsDto convertToCampaignPartitioningsDto(Campaign campaign) {
return modelmapper.map(campaign, CampaignPartitioningsDto.class);
}

private Campaign convertToEntity(CampaignDto campaignDto) {
return modelmapper.map(campaignDto, Campaign.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ public ResponseEntity<SurveyController.SurveyPage> getSurveys(
List<SurveyDto> listSurveys = pageSurvey.stream().map(this::convertToDto).toList();
return ResponseEntity.ok().body(new SurveyController.SurveyPage(listSurveys, pageable, pageSurvey.getTotalElements()));
}

@Operation(summary = "Search for surveys, paginated")
@GetMapping(value = Constants.API_SURVEYS_SEARCH, produces = "application/json")
public ResponseEntity<SurveyController.SurveyPage> getSurveys(
@RequestParam(required = false) String idSource,
@RequestParam(required = false) Integer year,
@RequestParam(required = false) String periodicity,
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "20") Integer size,
@RequestParam(defaultValue = "id") String sort) {
Pageable pageable = PageRequest.of(page, size, Sort.by(sort));
Page<Survey> pageSurvey = surveyService.findBySourceIdYearPeriodicity(pageable, idSource, year, periodicity);
List<SurveyDto> listSurveys = pageSurvey.stream().map(this::convertToDto).toList();
return ResponseEntity.ok().body(new SurveyController.SurveyPage(listSurveys, pageable, pageSurvey.getTotalElements()));
}
@Operation(summary = "Search for surveys by the source id")
@GetMapping(value = Constants.API_SOURCES_ID_SURVEYS, produces = "application/json")
public ResponseEntity<List<SurveyDto>> getSurveysBySource(@PathVariable("id") String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import fr.insee.survey.datacollectionmanagement.metadata.util.PeriodEnum;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;

import java.util.List;

@Data
public class CampaignPartitioningsDto {

@NotBlank
private String id;
private String surveyId;
private int year;
private String campaignWording;
private PeriodEnum period;
private List<PartitioningDto> partitionings;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package fr.insee.survey.datacollectionmanagement.metadata.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import fr.insee.survey.datacollectionmanagement.metadata.domain.Survey;

public interface SurveyRepository extends JpaRepository<Survey, String>, PagingAndSortingRepository<Survey, String> {

List<Survey> findByYear(int year);

static final String QUERY_FIND_SURVEY =
"""
select
su.*
from
survey su
join source so
on (so.id=su.source_id)
where
(:sourceId is null or UPPER(su.source_id) = UPPER(cast( :sourceId as text)))
and (:periodicity is null or UPPER(so.periodicity) = UPPER(cast( :periodicity as text)))
and (:year is null or su.year_value = :year)
""";
@Query(nativeQuery = true, value = QUERY_FIND_SURVEY)
Page<Survey> findBySourceIdYearPeriodicity(Pageable pageable, String sourceId, Integer year, String periodicity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface SurveyService {

List<Survey> findByYear(int year);
Page<Survey> findBySourceIdYearPeriodicity(Pageable pageable, String sourceId, Integer year, String periodicity);

Survey findById(String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class SurveyServiceImpl implements SurveyService {
private final SurveyRepository surveyRepository;

@Override
public List<Survey> findByYear(int year) {
return surveyRepository.findByYear(year);
public Page<Survey> findBySourceIdYearPeriodicity(Pageable pageable, String sourceId, Integer year, String periodicity) {
return surveyRepository.findBySourceIdYearPeriodicity(pageable,sourceId, year, periodicity);
}

@Override
Expand Down

0 comments on commit 4cd4d27

Please sign in to comment.