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-136: archive une offre #167

Merged
merged 4 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.dynonuggets.refonteimplicaction.dto.CompanyDto;
import com.dynonuggets.refonteimplicaction.dto.ContractTypeDto;
import com.dynonuggets.refonteimplicaction.dto.JobPostingDto;
import com.dynonuggets.refonteimplicaction.dto.StatusDto;
import com.dynonuggets.refonteimplicaction.model.Company;
import com.dynonuggets.refonteimplicaction.model.ContractType;
import com.dynonuggets.refonteimplicaction.model.JobPosting;
import com.dynonuggets.refonteimplicaction.model.Status;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -17,13 +15,11 @@
public class JobPostingAdapter {
private final CompanyAdapter companyAdapter;
private final ContractTypeAdapter contractAdapter;
private final StatusAdapter statusAdapter;

public JobPostingDto toDto(JobPosting model) {

CompanyDto companyDto = companyAdapter.toDto(model.getCompany());
ContractTypeDto contractTypeDto = contractAdapter.toDto(model.getContractType());
StatusDto statusDto = statusAdapter.toDto(model.getStatus());

return JobPostingDto.builder()
.id(model.getId())
Expand All @@ -35,7 +31,6 @@ public JobPostingDto toDto(JobPosting model) {
.salary(model.getSalary())
.keywords(model.getKeywords())
.contractType(contractTypeDto)
.status(statusDto)
.createdAt(model.getCreatedAt())
.build();
}
Expand All @@ -44,7 +39,6 @@ public JobPosting toModel(JobPostingDto dto) {

Company company = companyAdapter.toModel(dto.getCompany());
ContractType contractType = contractAdapter.toModel(dto.getContractType());
Status status = statusAdapter.toModel(dto.getStatus());

return JobPosting.builder()
.id(dto.getId())
Expand All @@ -56,7 +50,6 @@ public JobPosting toModel(JobPostingDto dto) {
.salary(dto.getSalary())
.keywords(dto.getKeywords())
.contractType(contractType)
.status(status)
.createdAt(dto.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ public ResponseEntity<Void> delete(@PathVariable Long jobId) {
jobPostingService.deleteJobPosting(jobId);
return ResponseEntity.noContent().build();
}

@PatchMapping(path = ARCHIVE_JOB_URI)
public ResponseEntity<JobPostingDto> toggleArchive(@PathVariable Long jobId) {
JobPostingDto updated = jobPostingService.toggleArchiveJobPosting(jobId);
return ResponseEntity.ok(updated);
}
mathusha-sdv marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class JobPostingDto {
private String salary;
private String keywords;
private ContractTypeDto contractType;
private StatusDto status;
private Instant createdAt;
private boolean archive;

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public class JobPosting {
@JoinColumn(name = "contract_type_id", nullable = false)
private ContractType contractType;

@ManyToOne
@JoinColumn(name = "status_id", nullable = false)
private Status status;

@Column(name = "created_at")
private Instant createdAt;

@Column(name = "archive")
private boolean archive;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ public void deleteJobPosting(Long jobPostingId) {
.orElseThrow(() -> new NotFoundException("Impossible de supprimer l'offre, " + jobPostingId + " n'existe pas."));
jobPostingRepository.delete(jobPosting);
}

@Transactional
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());
final JobPosting save = jobPostingRepository.save(jobPosting);
return jobPostingAdapter.toDto(save);
}
mathusha-sdv marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class ApiUrls {
public static final String JOBS_BASE_URI = "/api/job-postings";
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";

// POSTS
public static final String POSTS_BASE_URI = "/api/posts";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.dynonuggets.refonteimplicaction.dto.CompanyDto;
import com.dynonuggets.refonteimplicaction.dto.ContractTypeDto;
import com.dynonuggets.refonteimplicaction.dto.JobPostingDto;
import com.dynonuggets.refonteimplicaction.dto.StatusDto;
import com.dynonuggets.refonteimplicaction.model.Company;
import com.dynonuggets.refonteimplicaction.model.ContractType;
import com.dynonuggets.refonteimplicaction.model.JobPosting;
import com.dynonuggets.refonteimplicaction.model.Status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -17,28 +15,22 @@

class JobPostingAdapterTest {
CompanyDto companyDto;
StatusDto statusDto;
ContractTypeDto contractTypeDto;
Company company;
Status status;
ContractType contractType;
JobPosting jobPosting;
JobPostingAdapter jobPostingAdapter;
CompanyAdapter companyAdapter;
StatusAdapter statusAdapter;
ContractTypeAdapter contractTypeAdapter;

@BeforeEach
public void setUp() {
companyAdapter = new CompanyAdapter();
statusAdapter = new StatusAdapter();
contractTypeAdapter = new ContractTypeAdapter();
jobPostingAdapter = new JobPostingAdapter(companyAdapter, contractTypeAdapter, statusAdapter);
jobPostingAdapter = new JobPostingAdapter(companyAdapter, contractTypeAdapter);
company = new Company(1L, "urlModel", "logo", "name", "description");
status = new Status(2L, "label", "type");
contractType = new ContractType(3L, "label", "code");
companyDto = new CompanyDto(1L, "urlModel", "logo", "name", "description");
statusDto = new StatusDto(2L, "label", "type");
contractTypeDto = new ContractTypeDto(3L, "label", "code");

jobPosting = JobPosting.builder()
Expand All @@ -51,7 +43,6 @@ public void setUp() {
.salary("salary")
.keywords("keywords")
.contractType(contractType)
.status(status)
.createdAt(Instant.now())
.build();
}
Expand All @@ -68,7 +59,6 @@ void toDtoTest() {
assertThat(jobPostingDto.getLocation()).isEqualTo(jobPosting.getLocation());
assertThat(jobPostingDto.getSalary()).isEqualTo(jobPosting.getSalary());
assertThat(jobPostingDto.getKeywords()).isEqualTo(jobPosting.getKeywords());
assertThat(jobPostingDto.getStatus()).isEqualTo(statusDto);
assertThat(jobPostingDto.getContractType()).isEqualTo(contractTypeDto);
assertThat(jobPostingDto.getCreatedAt()).isEqualTo(jobPosting.getCreatedAt());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import java.util.Arrays;
import java.util.List;

import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.GET_JOB_URI;
import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.JOBS_BASE_URI;
import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.*;
import static com.dynonuggets.refonteimplicaction.utils.Message.JOB_NOT_FOUND_MESSAGE;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -126,4 +126,61 @@ void getJobByIdWithoutJwtShouldBeForbidden() throws Exception {

verify(jobPostingService, times(0)).getJobById(anyLong());
}

@Test
@WithMockUser
void archiveJobShouldChangeStatus() throws Exception {
mathusha-sdv marked this conversation as resolved.
Show resolved Hide resolved
// given
JobPostingDto givenDto = JobPostingDto.builder()
.id(1L)
.archive(false)
.build();

JobPostingDto expectedDto = JobPostingDto.builder()
.id(1L)
.archive(true)
.build();

given(jobPostingService.toggleArchiveJobPosting(anyLong())).willReturn(expectedDto);

// when
final ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, givenDto.getId()).contentType(APPLICATION_JSON));

// then
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is((givenDto.getId().intValue()))))
.andExpect(jsonPath("$.archive", is(!givenDto.isArchive())));
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}

@Test
@WithMockUser
void archiveJobPostingWhithUnexistingIdShouldThrowException() throws Exception {
// given
final long jobId = 123L;
final NotFoundException exception = new NotFoundException(String.format(JOB_NOT_FOUND_MESSAGE, jobId));
given(jobPostingService.toggleArchiveJobPosting(anyLong())).willThrow(exception);

// when
ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobId));

// then
resultActions.andExpect(status().isNotFound());
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}

@Test
void archiveJobByIdWithoutJwtShouldBeForbidden() throws Exception {
// given
final long jobId = 123L;

// when
ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobId).contentType(APPLICATION_JSON));

// then
resultActions.andDo(print())
.andExpect(status().isForbidden());
verify(jobPostingService, never()).toggleArchiveJobPosting(anyLong());
}
}
2 changes: 1 addition & 1 deletion data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,4 @@ CREATE TABLE `work_experience`

ALTER TABLE `job_posting`
ADD `short_description` longtext NULL AFTER `description`;
-- 2021-09-25 14:15:20
-- 2021-09-25 14:15:20
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
type="button"
>
<i
[ngClass]="job.isArchived ? 'fa-box-open border-warning text-warning' : 'fa-box border-info text-info'"
[ngClass]="job.archive ? 'fa-box-open border-warning text-warning' : 'fa-box border-info text-info'"
class="fas border rounded-3 px-2 py-1"
>
</i>
Expand Down