Skip to content

Commit

Permalink
S28 2404: API PUT /capture-sessions/{id} (#222)
Browse files Browse the repository at this point in the history
Co-authored-by: Swagger V2 bot <[email protected]>
  • Loading branch information
lucas-phillips28 and jasonpaige authored Jan 25, 2024
1 parent e025045 commit e4cbe4b
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 7 deletions.
96 changes: 96 additions & 0 deletions pre-api-stg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ definitions:
properties:
booking:
$ref: '#/definitions/Booking'
deleted:
type: boolean
deletedAt:
format: date-time
type: string
Expand Down Expand Up @@ -204,6 +206,11 @@ definitions:
- AVAILABLE
- FUTURE
type: string
required:
- booking_id
- id
- origin
- status
type: object
Case:
properties:
Expand Down Expand Up @@ -435,6 +442,62 @@ definitions:
- id
- scheduled_for
type: object
CreateCaptureSessionDTO:
description: CreateCaptureSessionDTO
properties:
booking_id:
description: CreateCaptureSessionBookingId
format: uuid
type: string
finished_at:
description: CreateCaptureSessionFinishedAt
format: date-time
type: string
finished_by_user_id:
description: CreateCaptureSessionFinishedByUserId
format: uuid
type: string
id:
description: CreateCaptureSessionId
format: uuid
type: string
ingest_address:
description: CreateCaptureSessionIngestAddress
type: string
live_output_url:
description: CreateCaptureSessionLiveOutputURL
type: string
origin:
description: CreateCaptureSessionOrigin
enum:
- PRE
- VODAFONE
type: string
started_at:
description: CreateCaptureSessionStartedAt
format: date-time
type: string
started_by_user_id:
description: CreateCaptureSessionStartedByUserId
format: uuid
type: string
status:
description: CreateCaptureSessionStatus
enum:
- STANDBY
- INITIALIZATION
- RECORDING
- FINISHED
- PROCESSING
- AVAILABLE
- FUTURE
type: string
required:
- booking_id
- id
- origin
- status
type: object
CreateCaseDTO:
description: CreateCaseDTO
properties:
Expand Down Expand Up @@ -748,6 +811,11 @@ definitions:
- AVAILABLE
- FUTURE
type: string
required:
- booking_id
- id
- origin
- status
type: object
EntityModelCaseDTO:
properties:
Expand Down Expand Up @@ -1820,6 +1888,34 @@ paths:
summary: Get a Capture Session by Id
tags:
- capture-session-controller
put:
consumes:
- application/json
operationId: upsertCaptureSession
parameters:
- format: uuid
in: path
name: captureSessionId
required: true
type: string
- description: The User Id of the User making the request
format: uuid
in: header
name: X-User-Id
required: false
type: string
x-example: 123e4567-e89b-12d3-a456-426614174000
- in: body
name: body
required: true
schema:
$ref: '#/definitions/CreateCaptureSessionDTO'
responses:
'200':
description: OK
summary: Create or Update a Capture Session
tags:
- capture-session-controller
/cases:
get:
operationId: getCases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
Expand All @@ -14,24 +15,27 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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 uk.gov.hmcts.reform.preapi.controllers.base.PreApiController;
import uk.gov.hmcts.reform.preapi.controllers.params.SearchCaptureSessions;
import uk.gov.hmcts.reform.preapi.dto.CaptureSessionDTO;
import uk.gov.hmcts.reform.preapi.dto.CreateCaptureSessionDTO;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;
import uk.gov.hmcts.reform.preapi.exception.PathPayloadMismatchException;
import uk.gov.hmcts.reform.preapi.exception.RequestedPageOutOfRangeException;
import uk.gov.hmcts.reform.preapi.services.CaptureSessionService;

import java.sql.Timestamp;
import java.util.Optional;
import java.util.UUID;

import static org.springframework.http.ResponseEntity.ok;

@RestController
@RequestMapping("/capture-sessions")
public class CaptureSessionController {
public class CaptureSessionController extends PreApiController {

private final CaptureSessionService captureSessionService;

Expand Down Expand Up @@ -108,7 +112,7 @@ public HttpEntity<PagedModel<EntityModel<CaptureSessionDTO>>> searchCaptureSessi
if (pageable.getPageNumber() > resultPage.getTotalPages()) {
throw new RequestedPageOutOfRangeException(pageable.getPageNumber(), resultPage.getTotalPages());
}
return ok(assembler.toModel(resultPage));
return ResponseEntity.ok(assembler.toModel(resultPage));
}

@DeleteMapping("/{captureSessionId}")
Expand All @@ -117,4 +121,19 @@ public ResponseEntity<Void> deleteCaptureSessionById(@PathVariable UUID captureS
captureSessionService.deleteById(captureSessionId);
return ResponseEntity.ok().build();
}

@PutMapping("/{captureSessionId}")
@Operation(operationId = "upsertCaptureSession", summary = "Create or Update a Capture Session")
public ResponseEntity<Void> upsertCaptureSession(
@PathVariable UUID captureSessionId,
@Valid @RequestBody CreateCaptureSessionDTO createCaptureSessionDTO
) {
if (!captureSessionId.equals(createCaptureSessionDTO.getId())) {
throw new PathPayloadMismatchException("id", "createCaptureSessionDTO.id");
}
return getUpsertResponse(
captureSessionService.upsert(createCaptureSessionDTO),
createCaptureSessionDTO.getId()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
import uk.gov.hmcts.reform.preapi.entities.CaptureSession;
Expand All @@ -18,12 +19,15 @@
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class CreateCaptureSessionDTO {
@Schema(description = "CreateCaptureSessionId")
@NotNull(message = "id is required")
private UUID id;

@Schema(description = "CreateCaptureSessionBookingId")
@NotNull(message = "booking_id is required")
private UUID bookingId;

@Schema(description = "CreateCaptureSessionOrigin")
@NotNull(message = "origin is required")
private RecordingOrigin origin;

@Schema(description = "CreateCaptureSessionIngestAddress")
Expand All @@ -45,6 +49,7 @@ public class CreateCaptureSessionDTO {
private UUID finishedByUserId;

@Schema(description = "CreateCaptureSessionStatus")
@NotNull(message = "status is required")
private RecordingStatus status;

public CreateCaptureSessionDTO(CaptureSession captureSession) {
Expand All @@ -54,9 +59,13 @@ public CreateCaptureSessionDTO(CaptureSession captureSession) {
this.ingestAddress = captureSession.getIngestAddress();
this.liveOutputUrl = captureSession.getLiveOutputUrl();
this.startedAt = captureSession.getStartedAt();
this.startedByUserId = captureSession.getStartedByUser().getId();
this.startedByUserId = captureSession.getStartedByUser() != null
? captureSession.getStartedByUser().getId()
: null;
this.finishedAt = captureSession.getFinishedAt();
this.finishedByUserId = captureSession.getFinishedByUser().getId();
this.finishedByUserId = captureSession.getFinishedByUser() != null
? captureSession.getFinishedByUser().getId()
: null;
this.status = captureSession.getStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;
Expand Down Expand Up @@ -60,4 +61,11 @@ public class CaptureSession extends BaseEntity {

@Column(name = "deleted_at")
private Timestamp deletedAt;

@Transient
private boolean deleted;

public boolean isDeleted() {
return deletedAt != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import org.springframework.stereotype.Repository;
import uk.gov.hmcts.reform.preapi.entities.User;

import java.util.Optional;
import java.util.UUID;

@Repository
public interface UserRepository extends SoftDeleteRepository<User, UUID> {
boolean existsByIdAndDeletedAtIsNull(UUID id);

Optional<User> findByIdAndDeletedAtIsNull(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.reform.preapi.dto.CaptureSessionDTO;
import uk.gov.hmcts.reform.preapi.dto.CreateCaptureSessionDTO;
import uk.gov.hmcts.reform.preapi.entities.Booking;
import uk.gov.hmcts.reform.preapi.entities.CaptureSession;
import uk.gov.hmcts.reform.preapi.enums.RecordingOrigin;
import uk.gov.hmcts.reform.preapi.enums.RecordingStatus;
import uk.gov.hmcts.reform.preapi.enums.UpsertResult;
import uk.gov.hmcts.reform.preapi.exception.NotFoundException;
import uk.gov.hmcts.reform.preapi.exception.ResourceInDeletedStateException;
import uk.gov.hmcts.reform.preapi.repositories.BookingRepository;
import uk.gov.hmcts.reform.preapi.repositories.CaptureSessionRepository;
import uk.gov.hmcts.reform.preapi.repositories.UserRepository;

import java.sql.Timestamp;
import java.time.temporal.ChronoUnit;
Expand All @@ -22,12 +28,18 @@ public class CaptureSessionService {

private final RecordingService recordingService;
private final CaptureSessionRepository captureSessionRepository;
private final BookingRepository bookingRepository;
private final UserRepository userRepository;

@Autowired
public CaptureSessionService(RecordingService recordingService,
CaptureSessionRepository captureSessionRepository) {
CaptureSessionRepository captureSessionRepository,
BookingRepository bookingRepository,
UserRepository userRepository) {
this.recordingService = recordingService;
this.captureSessionRepository = captureSessionRepository;
this.bookingRepository = bookingRepository;
this.userRepository = userRepository;
}

@Transactional
Expand Down Expand Up @@ -82,4 +94,49 @@ public void deleteCascade(Booking booking) {
.forEach(recordingService::deleteCascade);
captureSessionRepository.deleteAllByBooking(booking);
}

@Transactional
public UpsertResult upsert(CreateCaptureSessionDTO createCaptureSessionDTO) {
var foundCaptureSession = captureSessionRepository.findById(createCaptureSessionDTO.getId());

if (foundCaptureSession.isPresent() && foundCaptureSession.get().isDeleted()) {
throw new ResourceInDeletedStateException("CaptureSessionDTO", createCaptureSessionDTO.getId().toString());
}

var captureSession = foundCaptureSession.orElse(new CaptureSession());

var booking = bookingRepository
.findByIdAndDeletedAtIsNull(createCaptureSessionDTO.getBookingId())
.orElseThrow(() -> new NotFoundException("Booking: " + createCaptureSessionDTO.getBookingId()));


var startedByUser = createCaptureSessionDTO.getStartedByUserId() != null
? userRepository
.findByIdAndDeletedAtIsNull(createCaptureSessionDTO.getStartedByUserId())
.orElseThrow(() -> new NotFoundException("User: " + createCaptureSessionDTO.getStartedByUserId()))
: null;

var finishedByUser = createCaptureSessionDTO.getFinishedByUserId() != null
? userRepository
.findByIdAndDeletedAtIsNull(createCaptureSessionDTO.getFinishedByUserId())
.orElseThrow(() -> new NotFoundException("User: " + createCaptureSessionDTO.getFinishedByUserId()))
: null;

captureSession.setId(createCaptureSessionDTO.getId());
captureSession.setBooking(booking);
captureSession.setOrigin(createCaptureSessionDTO.getOrigin());
captureSession.setIngestAddress(createCaptureSessionDTO.getIngestAddress());
captureSession.setLiveOutputUrl(createCaptureSessionDTO.getLiveOutputUrl());
captureSession.setStartedAt(createCaptureSessionDTO.getStartedAt());
captureSession.setStartedByUser(startedByUser);
captureSession.setFinishedAt(createCaptureSessionDTO.getFinishedAt());
captureSession.setFinishedByUser(finishedByUser);
captureSession.setStatus(createCaptureSessionDTO.getStatus());

captureSessionRepository.save(captureSession);

var isUpdate = foundCaptureSession.isPresent();

return isUpdate ? UpsertResult.UPDATED : UpsertResult.CREATED;
}
}
Loading

0 comments on commit e4cbe4b

Please sign in to comment.