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

feature-1789/back-end-user-input-previous-employment #1877

Draft
wants to merge 17 commits into
base: onboard
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 9 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
@@ -0,0 +1,162 @@
package com.objectcomputing.checkins.services.employmenthistory;

import io.micronaut.data.annotation.AutoPopulated;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.TypeDef;
import io.micronaut.data.jdbc.annotation.ColumnTransformer;
import io.micronaut.data.model.DataType;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.persistence.Entity;
import javax.persistence.Table;

import java.time.LocalDate;
import java.util.Objects;
import java.util.UUID;

import javax.persistence.Column;
import javax.validation.constraints.NotBlank;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.Nullable;

@Entity
@Introspected
@Table(name = "employment_history")
public class EmploymentHistory {

@Id
@Column(name = "id")
@AutoPopulated
@TypeDef(type = DataType.STRING)
@Schema(description = "private key id")
private UUID id;

@NotBlank
@Column(name = "company")
@ColumnTransformer(read = "pgp_sym_decrypt(company::bytea,'${aes.key}')", write = "pgp_sym_encrypt(?,'${aes.key}') ")
@Schema(description = "name of previous employer")
private String company;

@NotBlank
@Column(name = "companyaddress")
@ColumnTransformer(read = "pgp_sym_decrypt(companyAddress::bytea,'${aes.key}')", write = "pgp_sym_encrypt(?,'${aes.key}') ")
@Schema(description = "address of previous employer")
private String companyAddress;

@NotBlank
@Column(name = "jobtitle")
@ColumnTransformer(read = "pgp_sym_decrypt(jobTitle::bytea,'${aes.key}')", write = "pgp_sym_encrypt(?,'${aes.key}') ")
@Schema(description = "job title")
private String jobTitle;

@NotBlank
@Column(name = "startdate")
@ColumnTransformer(read = "pgp_sym_decrypt(startDate::bytea,'${aes.key}')", write = "pgp_sym_encrypt(?,'${aes.key}') ")
@Schema(description = "start date of job")
private LocalDate startDate;

@NotBlank
@Column(name = "enddate")
@ColumnTransformer(read = "pgp_sym_decrypt(endDate::bytea,'${aes.key}')", write = "pgp_sym_encrypt(?,'${aes.key}') ")
@Schema(description = "end date of job")
private LocalDate endDate;

@Nullable
@Column(name = "reason")
@ColumnTransformer(read = "pgp_sym_decrypt(reason::bytea,'${aes.key}')", write = "pgp_sym_encrypt(?,'${aes.key}') ")
@Schema(description = "reason for leaving")
private String reason;

public EmploymentHistory(UUID id, String company, String companyAddress, String jobTitle, LocalDate startDate, LocalDate endDate, @Nullable String reason) {
this.id = id;
this.company = company;
this.companyAddress = companyAddress;
this.jobTitle = jobTitle;
this.startDate = startDate;
this.endDate = endDate;
this.reason = reason;
}

public EmploymentHistory(String company, String companyAddress, String jobTitle, LocalDate startDate, LocalDate endDate, @Nullable String reason) {
this.company = company;
this.companyAddress = companyAddress;
this.jobTitle = jobTitle;
this.startDate = startDate;
this.endDate = endDate;
this.reason = reason;
}

public EmploymentHistory(){}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

public String getCompanyAddress() {
return companyAddress;
}

public void setCompanyAddress(String companyAddress) {
this.companyAddress = companyAddress;
}

@Nullable
public String getJobTitle() {
return jobTitle;
}

public void setJobTitle(@Nullable String jobTitle) {
this.jobTitle = jobTitle;
}

@Nullable
public LocalDate getStartDate() {
return startDate;
}

public void setStartDate(@Nullable LocalDate startDate) {
this.startDate = startDate;
}

public LocalDate getEndDate() {
return endDate;
}

public void setEndDate(LocalDate endDate) {
this.endDate = endDate;
}

@Nullable
public String getReason() {
return reason;
}

public void setReason(@Nullable String reason) {
this.reason = reason;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EmploymentHistory that = (EmploymentHistory) o;
return Objects.equals(id, that.id) && Objects.equals(company, that.company) && Objects.equals(companyAddress, that.companyAddress) && Objects.equals(jobTitle, that.jobTitle) && Objects.equals(startDate, that.startDate) && Objects.equals(endDate, that.endDate) && Objects.equals(reason, that.reason);
}

@Override
public int hashCode() {
return Objects.hash(id, company, companyAddress, jobTitle, startDate, endDate, reason);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.objectcomputing.checkins.services.employmenthistory;

import java.net.URI;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Delete;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.annotation.Put;
import io.micronaut.http.MediaType;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import io.netty.channel.EventLoopGroup;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.inject.Named;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

@Controller("/services/employment-history")
@Secured(SecurityRule.IS_AUTHENTICATED)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "employment history")
public class EmploymentHistoryController {
private final EmploymentHistoryServices employmentHistoryServices;
private final EventLoopGroup eventLoopGroup;
private final Scheduler scheduler;

public EmploymentHistoryController(EmploymentHistoryServices employmentHistoryServices,
EventLoopGroup eventLoopGroup,
@Named(TaskExecutors.IO) ExecutorService ioExecutorService) {
this.employmentHistoryServices = employmentHistoryServices;
this.eventLoopGroup = eventLoopGroup;
this.scheduler = Schedulers.fromExecutorService(ioExecutorService);
}

@Get("/{id}")
public Mono<HttpResponse<EmploymentHistoryDTO>> getById(UUID id) {
return Mono.fromCallable(() -> employmentHistoryServices.getById(id))
.publishOn(Schedulers.fromExecutor(eventLoopGroup))
.map(profile -> (HttpResponse<EmploymentHistoryDTO>) HttpResponse.ok(fromEntity(profile))
.headers(headers -> headers.location(location(profile.getId()))))
.subscribeOn(scheduler);
}

@Get("/{?id,company,companyAddress,jobTitle,startDate,endDate,reason}")
public Mono<HttpResponse<List<EmploymentHistoryDTO>>> findByValue(@Nullable UUID id,
@Nullable String company,
@Nullable String companyAddress,
@Nullable String jobTitle,
@Nullable LocalDate startDate,
@Nullable LocalDate endDate,
@Nullable String reason) {
return Mono.fromCallable(() -> employmentHistoryServices.findByValues(id, company, companyAddress, jobTitle,
startDate, endDate, reason))
.publishOn(Schedulers.fromExecutor(eventLoopGroup))
.map(Employmenthistory -> {
List<EmploymentHistoryDTO> dtoList = Employmenthistory.stream().map(this::fromEntity)
.collect(Collectors.toList());
return (HttpResponse<List<EmploymentHistoryDTO>>) HttpResponse.ok(dtoList);
}).subscribeOn(scheduler);
}

@Post()
public Mono<HttpResponse<EmploymentHistoryDTO>> save(@Body @Valid EmploymentHistoryCreateDTO employmentHistory) {
return Mono.fromCallable(() -> employmentHistoryServices.saveHistory(fromDTO(employmentHistory)))
.publishOn(Schedulers.fromExecutor(eventLoopGroup))
.map(savedHistory -> (HttpResponse<EmploymentHistoryDTO>) HttpResponse
.created(fromEntity(savedHistory))
.headers(headers -> headers.location(location(savedHistory.getId()))))
.subscribeOn(scheduler);
}

@Put()
public Mono<HttpResponse<EmploymentHistoryDTO>> update(@Body @Valid EmploymentHistoryDTO employmentHistory) {
return Mono.fromCallable(() -> employmentHistoryServices.saveHistory(fromDTO(employmentHistory)))
.publishOn(Schedulers.fromExecutor(eventLoopGroup))
.map(savedHistory -> {
EmploymentHistoryDTO updatedEmploymentHistory = fromEntity(savedHistory);
return (HttpResponse<EmploymentHistoryDTO>) HttpResponse
.ok()
.headers(headers -> headers.location(location(updatedEmploymentHistory.getId())))
.body(updatedEmploymentHistory);
})
.subscribeOn(scheduler);

}

@Delete("/{id}")
public Mono<? extends HttpResponse<?>> delete(@NotNull UUID id) {
return Mono.fromCallable(() -> employmentHistoryServices.deleteHistory(id))
.publishOn(Schedulers.fromExecutor(eventLoopGroup))
.map(successFlag -> (HttpResponse<?>) HttpResponse.ok())
.subscribeOn(scheduler);
}

protected URI location(UUID id) {
return URI.create("/employment-history/" + id);
}

private EmploymentHistoryDTO fromEntity(EmploymentHistory entity) {
EmploymentHistoryDTO dto = new EmploymentHistoryDTO();
dto.setId(entity.getId());
dto.setCompany(entity.getCompany());
dto.setCompanyAddress(entity.getCompanyAddress());
dto.setJobTitle(entity.getJobTitle());
dto.setStartDate(entity.getStartDate());
dto.setEndDate(entity.getEndDate());
dto.setReason(entity.getReason());
return dto;
}

private EmploymentHistory fromDTO(EmploymentHistoryDTO dto) {
return new EmploymentHistory(dto.getId(), dto.getCompany(), dto.getCompanyAddress(), dto.getJobTitle(),
dto.getStartDate(), dto.getEndDate(), dto.getReason());
}

private EmploymentHistory fromDTO(EmploymentHistoryCreateDTO dto) {
return new EmploymentHistory(dto.getCompany(), dto.getCompanyAddress(), dto.getJobTitle(),
dto.getStartDate(), dto.getEndDate(), dto.getReason());
}
}
Loading