Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RI-139: [admin/job] permet l'archivage de plusieurs offres #184

Merged
merged 8 commits into from
Nov 22, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public JobPostingDto toDto(JobPosting model) {
.keywords(model.getKeywords())
.contractType(model.getContractType())
.createdAt(model.getCreatedAt())
.archive(model.isArchive())
.build();
}

Expand All @@ -46,6 +47,7 @@ public JobPosting toModel(JobPostingDto dto) {
.keywords(dto.getKeywords())
.contractType(dto.getContractType())
.createdAt(dto.getCreatedAt())
.archive(dto.isArchive())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.*;

@RestController
Expand Down Expand Up @@ -63,4 +65,10 @@ public ResponseEntity<JobPostingDto> toggleArchive(@PathVariable Long jobId) {
JobPostingDto updated = jobPostingService.toggleArchiveJobPosting(jobId);
return ResponseEntity.ok(updated);
}

@PatchMapping(ARCHIVE_JOBS_URI)
public ResponseEntity<List<JobPostingDto>> toggleArchiveJobs(@RequestBody final List<Long> jobsId) {
List<JobPostingDto> updated = jobPostingService.toggleArchiveAll(jobsId);
return ResponseEntity.ok(updated);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
Expand Down Expand Up @@ -58,9 +60,18 @@ public void deleteJobPosting(Long jobPostingId) {
public JobPostingDto toggleArchiveJobPosting(Long jobPostingId) {
JobPosting jobPosting = jobPostingRepository.findById(jobPostingId)
.orElseThrow(() -> new NotFoundException(String.format(Message.JOB_NOT_FOUND_MESSAGE, jobPostingId)));
jobPosting.setArchive(jobPosting.isArchive());
jobPosting.setArchive(!jobPosting.isArchive());
final JobPosting save = jobPostingRepository.save(jobPosting);
return jobPostingAdapter.toDto(save);
}

@Transactional
public List<JobPostingDto> toggleArchiveAll(List<Long> jobsId) {
List<JobPosting> jobs = jobPostingRepository.findAllById(jobsId);
jobs.forEach(job -> job.setArchive(!job.isArchive()));
return jobPostingRepository.saveAll(jobs)
.stream()
.map(jobPostingAdapter::toDto)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ApiUrls {
public static final String GET_JOB_URI = "/{jobId}";
public static final String DELETE_JOB_URI = "/{jobId}";
public static final String ARCHIVE_JOB_URI = "/{jobId}/archive";
public static final String ARCHIVE_JOBS_URI = "/archive";

// POSTS
public static final String POSTS_BASE_URI = "/api/posts";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.*;
Expand Down Expand Up @@ -154,6 +155,44 @@ void archiveJobShouldChangeStatus() throws Exception {
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}

@Test
@WithMockUser
void archiveJobShouldChangeListStatus() throws Exception {
// given
JobPostingDto job = JobPostingDto.builder()
.id(1L)
.archive(false)
.build();

List<Long> givenDto = Collections.singletonList(job.getId());

List<JobPostingDto> expectedDto = Collections.singletonList(
JobPostingDto.builder()
.id(1L)
.archive(true)
.build()
);

String json = gson.toJson(givenDto);

given(jobPostingService.toggleArchiveAll(anyList())).willReturn(expectedDto);

// when
final ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOBS_URI).contentType(APPLICATION_JSON).content(json));

// then
resultActions.andDo(print())
.andExpect(status().isOk());

for (int i = 0; i < givenDto.size(); i++) {
final String contentPath = String.format("$[%d]", i);
resultActions
.andExpect(jsonPath(contentPath + ".id", is(job.getId().intValue())))
.andExpect(jsonPath(contentPath + ".archive", is(!job.isArchive())));
verify(jobPostingService, times(1)).toggleArchiveAll(givenDto);
}
}
mathusha-sdv marked this conversation as resolved.
Show resolved Hide resolved

@Test
@WithMockUser
void archiveJobPostingWhithUnexistingIdShouldThrowException() throws Exception {
Expand Down Expand Up @@ -183,4 +222,24 @@ void archiveJobByIdWithoutJwtShouldBeForbidden() throws Exception {
.andExpect(status().isForbidden());
verify(jobPostingService, never()).toggleArchiveJobPosting(anyLong());
}

@Test
void archiveJobListWithoutJwtShouldBeForbidden() throws Exception {
// given
JobPostingDto job = JobPostingDto.builder()
.id(1L)
.archive(false)
.build();

List<Long> givenDto = Collections.singletonList(job.getId());
String json = gson.toJson(givenDto);

// when
ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOBS_URI).contentType(APPLICATION_JSON).content(json));

// then
resultActions.andDo(print())
.andExpect(status().isForbidden());
verify(jobPostingService, never()).toggleArchiveAll(Collections.singletonList(anyLong()));
}
}
5 changes: 4 additions & 1 deletion data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,7 @@ VALUES (15, 'Ivalua', 'Alternance', '2022-09-30', 'Ingénieur informaticien c#',
'En tant que chef d\'étage à moustaches, ma tâche consistait essentiellement à motiver le personnel en instaurant des moments privilégiés à la cantine autour d\'un céleri rémoulade ou d\'un couscous de la mer.',
'2021-09-10', 'Chef d\'étage à moustaches', '2021-02-28', 15);

-- 2021-11-22 06:09:06

ALTER TABLE `job_posting`
ADD `short_description` longtext NULL AFTER `description`;