-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: Controller for pvk dokument and updated pvk dokument files
Co-authored-by: andregroseth <[email protected]>
- Loading branch information
1 parent
73c83e6
commit 7a860e1
Showing
5 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
apps/backend/src/main/java/no/nav/data/pvk/pvkdokument/PvkDokumentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package no.nav.data.pvk.pvkdokument; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.nav.data.common.exceptions.ValidationException; | ||
import no.nav.data.common.rest.PageParameters; | ||
import no.nav.data.common.rest.RestResponsePage; | ||
import no.nav.data.pvk.pvkdokument.domain.PvkDokument; | ||
import no.nav.data.pvk.pvkdokument.dto.PvkDokumentRequest; | ||
import no.nav.data.pvk.pvkdokument.dto.PvkDokumentResponse; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/pvkdokument") | ||
@Tag(name = "Pvk Dokument", description = "Pvk Dokument for etterlevelsesdokumentasjon") | ||
public class PvkDokumentController { | ||
|
||
private final PvkDokumentService pvkDokumentService; | ||
|
||
@Operation(summary = "Get All PvkDokument") | ||
@ApiResponse(description = "ok") | ||
@GetMapping | ||
public ResponseEntity<RestResponsePage<PvkDokumentResponse>> getAll( | ||
PageParameters pageParameters | ||
) { | ||
log.info("Get all PvkDokument"); | ||
Page<PvkDokument> page = pvkDokumentService.getAll(pageParameters); | ||
return ResponseEntity.ok(new RestResponsePage<>(page).convert(PvkDokumentResponse::buildFrom)); | ||
} | ||
|
||
@Operation(summary = "Create PvkDocument") | ||
@ApiResponse(responseCode = "201", description = "PvkDokument created") | ||
@PostMapping | ||
public ResponseEntity<PvkDokumentResponse> createPvkDokumente(@RequestBody PvkDokumentRequest request) { | ||
log.info("Create PvkDokument"); | ||
var pvkDokument = pvkDokumentService.save(request.convertToPvkDokument(), false); | ||
|
||
return new ResponseEntity<>(PvkDokumentResponse.buildFrom(pvkDokument), HttpStatus.CREATED); | ||
} | ||
|
||
@Operation(summary = "Update Pvk Document") | ||
@ApiResponse(description = "Pvk Document updated") | ||
@PutMapping("/{id}") | ||
public ResponseEntity<PvkDokumentResponse> updatePvkDokument(@PathVariable UUID id, @Valid @RequestBody PvkDokumentRequest request) { | ||
log.info("Update Pvk Document id={}", id); | ||
|
||
if (!Objects.equals(id, request.getIdAsUUID())) { | ||
throw new ValidationException(String.format("id mismatch in request %s and path %s", request.getId(), id)); | ||
} | ||
|
||
var pvkDokument = pvkDokumentService.save(request.convertToPvkDokument(), true); | ||
return ResponseEntity.ok(PvkDokumentResponse.buildFrom(pvkDokument)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
apps/backend/src/main/java/no/nav/data/pvk/pvkdokument/PvkDokumentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package no.nav.data.pvk.pvkdokument; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.nav.data.common.exceptions.NotFoundException; | ||
import no.nav.data.common.rest.PageParameters; | ||
import no.nav.data.pvk.pvkdokument.domain.PvkDokument; | ||
import no.nav.data.pvk.pvkdokument.domain.PvkDokumentRepo; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class PvkDokumentService { | ||
|
||
private PvkDokumentRepo repo; | ||
|
||
public PvkDokument get(UUID uuid) { | ||
if (uuid == null || !repo.existsById(uuid)) return null; | ||
return getPvkDokument(uuid); | ||
} | ||
|
||
private PvkDokument getPvkDokument(UUID uuid) { | ||
return repo.findById(uuid).orElseThrow(() -> new NotFoundException("Couldn't find Pvk Dokument with id " + uuid)); | ||
} | ||
|
||
@Transactional | ||
public PvkDokument saveTestData(PvkDokument pvkDokument) { | ||
pvkDokument = repo.save(pvkDokument); | ||
repo.flush(); | ||
return pvkDokument; | ||
} | ||
|
||
public Page<PvkDokument> getAll(PageParameters pageParameters) { | ||
return repo.findAll(pageParameters.createPage()); | ||
} | ||
|
||
public Optional<PvkDokument> getByEtterlevelseDokumentasjon(String etterlevelseDokumentasjonId) { | ||
return repo.findByEtterlevelseDokumensjon(etterlevelseDokumentasjonId); | ||
} | ||
|
||
@Transactional(propagation = Propagation.REQUIRED) | ||
public PvkDokument save(PvkDokument pvkDokument, boolean isUpdate) { | ||
|
||
if (!isUpdate) { | ||
if (pvkDokument.getId() == null) { | ||
pvkDokument.setId(UUID.randomUUID()); | ||
} | ||
var existingPvkDokument = repo.findByEtterlevelseDokumensjon(pvkDokument.getEtterlevelseDokumentId()); | ||
if (existingPvkDokument.isPresent()) { | ||
log.warn("Found existing pvk document when trying to create for etterlevelse dokumentation id: {}", pvkDokument.getEtterlevelseDokumentId()); | ||
pvkDokument = existingPvkDokument.get(); | ||
} | ||
} | ||
|
||
return repo.save(pvkDokument); | ||
} | ||
|
||
@Transactional(propagation = Propagation.REQUIRED) | ||
public PvkDokument delete(UUID id) { | ||
var pvkDokumentToDelete = repo.findById(id); | ||
repo.deleteById(id); | ||
return pvkDokumentToDelete.orElse(null); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
apps/backend/src/main/java/no/nav/data/pvk/pvkdokument/domain/PvkDokumentRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package no.nav.data.pvk.pvkdokument.domain; | ||
|
||
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 java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface PvkDokumentRepo extends JpaRepository<PvkDokument, UUID> { | ||
|
||
@Override | ||
@Query(value = "select * from pvk_dokument", | ||
countQuery = "select count(1) from pvk_dokument", | ||
nativeQuery = true) | ||
Page<PvkDokument> findAll(Pageable pageable); | ||
|
||
|
||
@Query(value = "select * from pvk_dokument where etterlevelse_dokumentasjon_id = ?1", nativeQuery = true) | ||
Optional<PvkDokument> findByEtterlevelseDokumensjon(String etterlevelseDokumentasjonId); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters