Skip to content

Commit

Permalink
RI-203: validation des groupes et des offres
Browse files Browse the repository at this point in the history
* code review
  • Loading branch information
nekorpeche committed Dec 11, 2021
1 parent 51138d6 commit 1cdc060
Show file tree
Hide file tree
Showing 29 changed files with 148 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public JobPostingDto toDto(JobPosting model) {
.contractType(model.getContractType())
.createdAt(model.getCreatedAt())
.archive(model.isArchive())
.active(model.isActive())
.valid(model.isValid())
.build();
}

Expand All @@ -49,7 +49,7 @@ public JobPosting toModel(JobPostingDto dto) {
.contractType(dto.getContractType())
.createdAt(dto.getCreatedAt())
.archive(dto.isArchive())
.active(dto.isActive())
.valid(dto.isValid())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Group toModel(GroupDto dto, User user) {
.name(dto.getName())
.description(dto.getDescription())
.createdAt(dto.getCreatedAt())
.active(dto.isActive())
.valid(dto.isValid())
.user(user)
.build();
}
Expand All @@ -38,7 +38,7 @@ public GroupDto toDto(Group model) {
.description(model.getDescription())
.createdAt(model.getCreatedAt())
.imageUrl(imageUrl)
.active(model.isActive())
.valid(model.isValid())
.username(model.getUser().getUsername())
.userId(model.getUser().getId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ResponseEntity<Page<GroupDto>> getAllActiveGroups(
@RequestParam(value = "sortOrder", defaultValue = "ASC") String sortOrder
) {
Pageable pageable = PageRequest.of(page, rows, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
Page<GroupDto> subredditDtos = groupService.getAllActiveGroups(pageable);
Page<GroupDto> subredditDtos = groupService.getAllValidGroups(pageable);
return ResponseEntity.ok(subredditDtos);
}

Expand All @@ -67,13 +67,13 @@ public ResponseEntity<Page<GroupDto>> getAllPendingGroups(
@RequestParam(value = "sortOrder", defaultValue = "ASC") String sortOrder
) {
Pageable pageable = PageRequest.of(page, rows, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
Page<GroupDto> pendingGroups = groupService.getAllPendingActivationGroups(pageable);
Page<GroupDto> pendingGroups = groupService.getAllPendingGroups(pageable);
return ResponseEntity.ok(pendingGroups);
}

@PatchMapping(ACTIVATE_GROUP_URI)
public ResponseEntity<Void> activateJob(@RequestBody final Group group) {
groupService.activateGroup(group);
@PatchMapping(VALIDATE_GROUP_URI)
public ResponseEntity<Void> validateGroup(@RequestBody final Group group) {
groupService.validateGroup(group);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@ public ResponseEntity<Page<JobPostingDto>> getAllByCriteria(
Pageable pageable = PageRequest.of(page, rows, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
final boolean applyCheck = Boolean.parseBoolean(checkApplyAsString);
final Boolean isArchive = StringUtils.isNotBlank(archiveAsString) ? Boolean.parseBoolean(archiveAsString) : null;
Page<JobPostingDto> jobPostingDtos = jobPostingService.getAllWithCriteria(pageable, search, contractType, isArchive, applyCheck);
Page<JobPostingDto> jobPostingDtos = jobPostingService.getAllWithCriteria(pageable, search, contractType, isArchive, applyCheck, null);
return ResponseEntity.ok(jobPostingDtos);
}

@GetMapping(ACTIVE_JOBS)
public ResponseEntity<Page<JobPostingDto>> getAllActiveByCriteria(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "rows", defaultValue = "10") int rows,
@RequestParam(value = "sortBy", defaultValue = "id") String sortBy,
@RequestParam(value = "sortOrder", defaultValue = "ASC") String sortOrder,
@RequestParam(value = "search", defaultValue = "") String search,
@RequestParam(value = "contractType", required = false) String contractType,
@RequestParam(value = "archive", required = false) String archiveAsString
) {
Pageable pageable = PageRequest.of(page, rows, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
final Boolean isArchive = StringUtils.isNotBlank(archiveAsString) ? Boolean.parseBoolean(archiveAsString) : null;
Page<JobPostingDto> jobPostingDtos = jobPostingService.getAllActiveWithCriteria(pageable, search, contractType, isArchive);
return ResponseEntity.ok(jobPostingDtos);
}

Expand Down Expand Up @@ -84,13 +100,13 @@ public ResponseEntity<Page<JobPostingDto>> getAllPendingJobs(
@RequestParam(value = "rows", defaultValue = "10") int rows
) {
Pageable pageable = PageRequest.of(page, rows);
Page<JobPostingDto> pendingJobs = jobPostingService.getAllPendingActivationJobs(pageable);
Page<JobPostingDto> pendingJobs = jobPostingService.getAllPendingJobs(pageable);
return ResponseEntity.ok(pendingJobs);
}

@PatchMapping(ACTIVATE_JOB_URI)
public ResponseEntity<Void> activateJob(@RequestBody final JobPosting job) {
jobPostingService.activateJob(job);
@PatchMapping(VALIDATE_JOB_URI)
public ResponseEntity<Void> validateJob(@RequestBody final JobPosting job) {
jobPostingService.validateJob(job);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GroupDto {
private Integer numberOfPosts;
private String imageUrl;
private Instant createdAt;
private boolean active;
private boolean valid;
private String username;
private Long userId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public class JobPostingDto {
private Instant createdAt;
private boolean archive;
private boolean apply;
private boolean active;
private boolean valid;

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public class Group {
@ManyToMany(fetch = FetchType.LAZY, cascade = ALL, mappedBy = "groups")
private List<User> users;

@Column(name = "active")
private boolean active;
@Column(name = "valid")
private boolean valid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public class JobPosting {
@Column(columnDefinition = "boolean default false")
private boolean archive;

@Column(name = "active")
private boolean active;
@Column(name = "valid")
private boolean valid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

@Repository
public interface JobPostingRepository extends JpaRepository<JobPosting, Long>, JobPostingRepositoryCustom {
Page<JobPosting> findAllByActiveIsFalse(Pageable pageable);
Page<JobPosting> findAllByValidIsFalse(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public interface JobPostingRepositoryCustom {
* @param pageable l'objet de pagination
* @param search la chaîne de caratères à rechercher dans les champs title, description, keywords
* @param contractType le type de contrat à rechercher (CDD, CDI)
* @param active
* @return la liste de résultats paginée des JobPostings correspondant aux critères
*/
Page<JobPosting> findAllWithCriteria(final Pageable pageable, final String search, final String contractType, final Boolean archive);
Page<JobPosting> findAllWithCriteria(final Pageable pageable, final String search, final String contractType, final Boolean archive, Boolean active);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface SubredditRepository extends JpaRepository<Group, Long> {

Optional<Group> findByName(String name);

Page<Group> findAllByActiveIsFalse(Pageable pageable);
Page<Group> findAllByValidIsFalse(Pageable pageable);

Page<Group> findAllByActiveIsTrue(Pageable pageable);
Page<Group> findAllByValidIsTrue(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class JobPostingRepositoryImpl implements JobPostingRepositoryCustom {
private final EntityManager entityManager;

@Override
public Page<JobPosting> findAllWithCriteria(Pageable pageable, String search, String contractType, Boolean archive) {
public Page<JobPosting> findAllWithCriteria(Pageable pageable, String search, String contractType, Boolean archive, Boolean active) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<JobPosting> query = criteriaBuilder.createQuery(JobPosting.class);
Root<JobPosting> queryRoot = query.from(JobPosting.class);
Expand Down Expand Up @@ -52,6 +52,11 @@ public Page<JobPosting> findAllWithCriteria(Pageable pageable, String search, St
if (archive != null) {
predicates.add(criteriaBuilder.equal(queryRoot.get("archive"), archive));
}

if (active != null) {
predicates.add(criteriaBuilder.equal(queryRoot.get("active"), active));
}

// combinaison des différents prédicats
Predicate finalPredicate = criteriaBuilder.and(predicates.toArray(new Predicate[0]));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public GroupDto save(GroupDto groupDto) {
}

@Transactional(readOnly = true)
public Page<GroupDto> getAllActiveGroups(Pageable pageable) {
final Page<Group> subreddits = subredditRepository.findAllByActiveIsTrue(pageable);
public Page<GroupDto> getAllValidGroups(Pageable pageable) {
final Page<Group> subreddits = subredditRepository.findAllByValidIsTrue(pageable);
return subreddits.map(subredditAdapter::toDto);
}

Expand All @@ -77,14 +77,14 @@ public List<GroupDto> getAllByTopPosting(int limit) {
}

@Transactional
public Page<GroupDto> getAllPendingActivationGroups(Pageable pageable) {
return subredditRepository.findAllByActiveIsFalse(pageable)
public Page<GroupDto> getAllPendingGroups(Pageable pageable) {
return subredditRepository.findAllByValidIsFalse(pageable)
.map(subredditAdapter::toDto);
}

@Transactional
public void activateGroup(Group group) {
group.setActive(true);
public void validateGroup(Group group) {
group.setValid(true);
subredditRepository.save(group);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public JobPostingDto getJobById(Long jobId) {
return jobDto;
}

public Page<JobPostingDto> getAllWithCriteria(Pageable pageable, String search, String contractType, Boolean archive, boolean applyCheck) {
public Page<JobPostingDto> getAllWithCriteria(Pageable pageable, String search, String contractType, Boolean archive, boolean applyCheck, Boolean active) {
// récupération des jobs
final Page<JobPosting> jobs = jobPostingRepository.findAllWithCriteria(pageable, search, contractType, archive);
final Page<JobPosting> jobs = jobPostingRepository.findAllWithCriteria(pageable, search, contractType, archive, active);
if (applyCheck) {
final List<Long> jobIds = jobs.stream().map(JobPosting::getId).collect(toList());
final List<Long> jobAppliesIds = getAllAppliesWithJobIdsIn(jobIds, authService.getCurrentUser().getId());
Expand Down Expand Up @@ -105,15 +105,18 @@ public List<JobPostingDto> toggleArchiveAll(List<Long> jobsId) {
}

@Transactional
public Page<JobPostingDto> getAllPendingActivationJobs(Pageable pageable) {
return jobPostingRepository.findAllByActiveIsFalse(pageable)
public Page<JobPostingDto> getAllPendingJobs(Pageable pageable) {
return jobPostingRepository.findAllByValidIsFalse(pageable)
.map(jobPostingAdapter::toDto);
}

@Transactional
public void activateJob(JobPosting job) {
job.setActive(true);
public void validateJob(JobPosting job) {
job.setValid(true);
jobPostingRepository.save(job);
}

public Page<JobPostingDto> getAllActiveWithCriteria(Pageable pageable, String search, String contractType, Boolean isArchive) {
return this.getAllWithCriteria(pageable, search, contractType, isArchive, true, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class ApiUrls {
public static final String ARCHIVE_JOB_URI = "/{jobId}/archive";
public static final String ARCHIVE_JOBS_URI = "/archive";
public static final String GET_PENDING_JOB_URI = "/pending";
public static final String ACTIVATE_JOB_URI = "/activate";
public static final String VALIDATE_JOB_URI = "/validate";
public static final String ACTIVE_JOBS = "/active";

// POSTS
public static final String POSTS_BASE_URI = "/api/posts";
Expand All @@ -31,7 +32,7 @@ public class ApiUrls {
public static final String GROUPS_BASE_URI = "/api/groups";
public static final String GET_ALL_BY_TOP_POSTING_URI = "/top-posting";
public static final String GET_PENDING_GROUP_URI = "/pending";
public static final String ACTIVATE_GROUP_URI = "/activate";
public static final String VALIDATE_GROUP_URI = "/validate";
public static final String ACTIVE_GROUPS = "/active";

// COMMENTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ void should_map_to_model() {
@Test
void should_map_to_dto_with_count_when_model_has_posts() {
// given
User user = User.builder()
.id(1L)
.username("test")
.build();

Group expectedModel = Group.builder()
.id(123L)
.description("blablabla")
.name("blabla")
.posts(Arrays.asList(new Post(), new Post(), new Post(), new Post()))
.createdAt(Instant.now())
.user(user)
.build();

// when
Expand All @@ -84,12 +90,18 @@ void should_map_to_dto_with_count_when_model_has_posts() {
@Test
void should_map_to_dto_with_image_url_when_model_has_image() {
// given
User user = User.builder()
.id(1L)
.username("test")
.build();

Group expectedModel = Group.builder()
.id(123L)
.description("blablabla")
.name("blabla")
.image(FileModel.builder().url("http://url.com").objectKey("blablabla").build())
.createdAt(Instant.now())
.user(user)
.build();

String expectedUrl = "http://url/objectKey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void should_list_all_subreddit_with_no_authentication() throws Exception {
.username("test")
.build();

given(groupService.getAllActiveGroups(DEFAULT_PAGEABLE)).willReturn(subreddits);
given(groupService.getAllValidGroups(DEFAULT_PAGEABLE)).willReturn(subreddits);
given(userRepository.findById(any())).willReturn(Optional.of(user));

// when
Expand All @@ -105,7 +105,7 @@ void should_list_all_subreddit_with_no_authentication() throws Exception {
}
resultActions.andReturn();

verify(groupService, times(1)).getAllActiveGroups(any());
verify(groupService, times(1)).getAllValidGroups(any());
}

@Test
Expand All @@ -116,7 +116,7 @@ void should_response_forbidden_when_listing_all_subreddit_whith_no_authenticatio
// then
resultActions.andDo(print()).andExpect(status().isForbidden());

verify(groupService, never()).getAllActiveGroups(any());
verify(groupService, never()).getAllValidGroups(any());
}

@Test
Expand Down Expand Up @@ -177,12 +177,12 @@ void should_response_forbidden_when_top_posting_and_not_authenticated() throws E
void should_get_all_pending_groups_when_authenticated() throws Exception {
//given
List<GroupDto> groupDtos = Arrays.asList(
GroupDto.builder().id(1L).active(false).build(),
GroupDto.builder().id(2L).active(false).build(),
GroupDto.builder().id(3L).active(false).build()
GroupDto.builder().id(1L).valid(false).build(),
GroupDto.builder().id(2L).valid(false).build(),
GroupDto.builder().id(3L).valid(false).build()
);
Page<GroupDto> groupPageMockResponse = new PageImpl<>(groupDtos);
given(groupService.getAllPendingActivationGroups(any())).willReturn(groupPageMockResponse);
given(groupService.getAllPendingGroups(any())).willReturn(groupPageMockResponse);

// when
ResultActions resultActions = mvc.perform(
Expand All @@ -195,10 +195,10 @@ void should_get_all_pending_groups_when_authenticated() throws Exception {
for (int i = 0; i < groupDtos.size(); i++) {
final String contentPath = String.format("$.content[%d]", i);
resultActions.andExpect(jsonPath(contentPath + ".id", is(Math.toIntExact(groupDtos.get(i).getId()))));
resultActions.andExpect(jsonPath(contentPath + ".active", is(groupDtos.get(i).isActive())));
resultActions.andExpect(jsonPath(contentPath + ".valid", is(groupDtos.get(i).isValid())));
}

verify(groupService, times(1)).getAllPendingActivationGroups(any());
verify(groupService, times(1)).getAllPendingGroups(any());
}

@Test
Expand All @@ -210,6 +210,6 @@ void should_response_forbidden_when_pending_groups_and_not_authenticated() throw
// then
resultActions.andDo(print()).andExpect(status().isForbidden());

verify(groupService, never()).getAllPendingActivationGroups(any());
verify(groupService, never()).getAllPendingGroups(any());
}
}
Loading

0 comments on commit 1cdc060

Please sign in to comment.