Skip to content

Commit

Permalink
First commit for tasks: EAC-10.
Browse files Browse the repository at this point in the history
  • Loading branch information
sumathi-thirumani committed Oct 8, 2024
1 parent 5908f3c commit 89bad92
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 7 deletions.
12 changes: 8 additions & 4 deletions api/src/main/java/ca/bc/gov/educ/eas/api/constants/v1/URL.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package ca.bc.gov.educ.eas.api.constants.v1;

public final class URL {
import lombok.NoArgsConstructor;

private URL(){
}
/**
* This class consolidates all the URL exposed by EAS service.
*/
@NoArgsConstructor
public final class URL {

public static final String BASE_URL="/api/v1/eas";
public static final String BASE_URL = "/api/v1/eas";
public static final String SESSIONS_URL = BASE_URL + "/sessions";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ca.bc.gov.educ.eas.api.controller.v1;

import ca.bc.gov.educ.eas.api.endpoint.v1.SessionEndpoint;
import ca.bc.gov.educ.eas.api.mappers.SessionMapper;
import ca.bc.gov.educ.eas.api.model.v1.SessionEntity;
import ca.bc.gov.educ.eas.api.service.v1.SessionService;
import ca.bc.gov.educ.eas.api.struct.v1.Session;
import ca.bc.gov.educ.eas.api.util.RequestUtil;
import ca.bc.gov.educ.eas.api.util.UUIDUtil;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

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

/**
* Controller for assessment session management
*/
@RestController
@Slf4j
public class SessionController implements SessionEndpoint {

private static final SessionMapper mapper = SessionMapper.mapper;
@Getter(AccessLevel.PRIVATE)
private final SessionService sessionService;

@Autowired
SessionController(SessionService sessionService) {
this.sessionService = sessionService;
}

/**
* Retrieves all assessment sessions.
* @return List of sessions
*/
@Override
public List<Session> getAllSessions() {
List<SessionEntity> test = getSessionService().getAllSessions();
return getSessionService().getAllSessions().stream().map(mapper::toStructure).collect(Collectors.toList());
}

/**
* Updates the assessment session.
* @param assessmentSessionID Identifier for assessment session
* @param session Modified session
* @return Updated session
*/
@Override
public Session updateSession(String assessmentSessionID, Session session) {
RequestUtil.setAuditColumnsForUpdate(session);
SessionEntity sessionEntity = getSessionService().updateSession(UUIDUtil.fromString(assessmentSessionID), mapper.toEntity(session));
return mapper.toStructure(sessionEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ca.bc.gov.educ.eas.api.endpoint.v1;

import ca.bc.gov.educ.eas.api.constants.v1.URL;
import ca.bc.gov.educ.eas.api.struct.v1.Session;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Definition for assessment session management
*/
@RequestMapping(URL.SESSIONS_URL)
public interface SessionEndpoint {

/**
* Retrieves all assessment sessions maintained in data store
* @return List of sessions
*/
@PreAuthorize("hasAuthority('SCOPE_READ_EAS_SESSIONS')")
@GetMapping
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
List<Session> getAllSessions();

/**
* Updates the assessment session
* @param assessmentSessionID Identifier for assessment session
* @param session Modified session
* @return Updated session
*/
@PreAuthorize("hasAuthority('SCOPE_WRITE_EAS_SESSIONS')")
@PutMapping("/{assessmentSessionID}")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "NOT FOUND."), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
Session updateSession(@PathVariable String assessmentSessionID, @Validated @RequestBody Session session);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ca.bc.gov.educ.eas.api.mappers;

import ca.bc.gov.educ.eas.api.model.v1.SessionEntity;
import ca.bc.gov.educ.eas.api.struct.v1.Session;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.factory.Mappers;

/**
* Mapper for converting between SessionEntity and Session DTO.
*/
@Mapper(uses = {UUIDMapper.class, LocalDateTimeMapper.class})
public interface SessionMapper {

SessionMapper mapper = Mappers.getMapper(SessionMapper.class);

/**
* Conversion from Session Entity to DTO.
* @param entity SessionEntity
* @return Session
*/
@Mapping(source = "courseSession", target = "courseSession")
@Mapping(source = "statusCode", target = "status")
Session toStructure(SessionEntity entity);

/**
* Conversion from Session DTO to Entity.
* @param session Session
* @return SessionEntity
*/
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
SessionEntity toEntity(Session session);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class SessionEntity {
private UUID assessmentSessionID;

@Column(name = "COURSE_SESSION", nullable = false, length = 6)
private int courseSession;
private Integer courseSession;

@Column(name = "COURSE_YEAR", nullable = false, length = 4)
private int courseYear;
private Integer courseYear;

@Column(name = "COURSE_MONTH", nullable = false, length = 2)
private int courseMonth;
private Integer courseMonth;

@Column(name = "STATUS_CODE", nullable = false, length = 10)
private String statusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ca.bc.gov.educ.eas.api.repository.v1;

import ca.bc.gov.educ.eas.api.model.v1.SessionEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

/**
* Repository definition for assessment session management.
*/
@Repository
public interface SessionRepository extends JpaRepository<SessionEntity, UUID> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ca.bc.gov.educ.eas.api.service.v1;

import ca.bc.gov.educ.eas.api.exception.EntityNotFoundException;
import ca.bc.gov.educ.eas.api.model.v1.SessionEntity;
import ca.bc.gov.educ.eas.api.repository.v1.SessionRepository;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
* This class encapsulates assessment session functionalities.
* Supported CRUD operations: SELECT ALL and UPDATE.
*/
@Service
@Slf4j
public class SessionService {

@Getter(AccessLevel.PRIVATE)
private final SessionRepository sessionRepository;

@Autowired
public SessionService(final SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}

/**
* Retrieves all managed assessment sessions.
* @return List of SessionEntity
*/
public List<SessionEntity> getAllSessions() {
return this.getSessionRepository().findAll(Sort.by(Sort.Direction.DESC, "courseSession"));
}

/**
* Updates the assessment session.
* @param assessmentSessionId Assessment SessionId
* @param updatedSessionEntity Updated session object
* @return Persisted SessionEntity
*/
@Transactional(propagation = Propagation.REQUIRES_NEW)
public SessionEntity updateSession(UUID assessmentSessionId, SessionEntity updatedSessionEntity) {
val optionalSession = getSessionRepository().findById(assessmentSessionId);
if (optionalSession.isPresent()) {
SessionEntity sessionEntity = optionalSession.get();
log.debug("Assessment session update, current :: {}, new :: {}", sessionEntity, updatedSessionEntity);
sessionEntity.setActiveFromDate(updatedSessionEntity.getActiveFromDate());
sessionEntity.setActiveUntilDate(updatedSessionEntity.getActiveUntilDate());
return getSessionRepository().save(sessionEntity);
} else {
throw new EntityNotFoundException(SessionEntity.class, "SessionEntity", assessmentSessionId.toString());
}
}
}
41 changes: 41 additions & 0 deletions api/src/main/java/ca/bc/gov/educ/eas/api/struct/v1/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ca.bc.gov.educ.eas.api.struct.v1;

import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.ReadOnlyProperty;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
* DTO for Session entity.
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class Session extends BaseRequest implements Serializable {

private static final long serialVersionUID = 1L;

@ReadOnlyProperty
private String assessmentSessionID;

@ReadOnlyProperty
private Integer courseSession;

@ReadOnlyProperty
private Integer courseMonth;

@ReadOnlyProperty
private Integer courseYear;

@ReadOnlyProperty
private String status;

@NotNull(message = "Open Date cannot be null")
private LocalDateTime activeFromDate;

@NotNull(message = "Close Date cannot be null")
private LocalDateTime activeUntilDate;

}
26 changes: 26 additions & 0 deletions api/src/main/java/ca/bc/gov/educ/eas/api/util/UUIDUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ca.bc.gov.educ.eas.api.util;

import java.nio.ByteBuffer;
import java.util.UUID;

/**
* Utility class for handling datatype UUID.
*/
public final class UUIDUtil {
private UUIDUtil() {
}

public static UUID asUuid(byte[] bytes) {
ByteBuffer bb = ByteBuffer.wrap(bytes);
long firstLong = bb.getLong();
long secondLong = bb.getLong();
return new UUID(firstLong, secondLong);
}

public static UUID fromString(String uuid) {
if (uuid == null) {
return null;
}
return UUID.fromString(uuid);
}
}
Loading

0 comments on commit 89bad92

Please sign in to comment.