-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5908f3c
commit 89bad92
Showing
11 changed files
with
432 additions
and
7 deletions.
There are no files selected for viewing
12 changes: 8 additions & 4 deletions
12
api/src/main/java/ca/bc/gov/educ/eas/api/constants/v1/URL.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 |
---|---|---|
@@ -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"; | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
api/src/main/java/ca/bc/gov/educ/eas/api/controller/v1/SessionController.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,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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
api/src/main/java/ca/bc/gov/educ/eas/api/endpoint/v1/SessionEndpoint.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,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); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
api/src/main/java/ca/bc/gov/educ/eas/api/mappers/SessionMapper.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,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); | ||
|
||
} |
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
14 changes: 14 additions & 0 deletions
14
api/src/main/java/ca/bc/gov/educ/eas/api/repository/v1/SessionRepository.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,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> { | ||
} |
62 changes: 62 additions & 0 deletions
62
api/src/main/java/ca/bc/gov/educ/eas/api/service/v1/SessionService.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,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
41
api/src/main/java/ca/bc/gov/educ/eas/api/struct/v1/Session.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,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
26
api/src/main/java/ca/bc/gov/educ/eas/api/util/UUIDUtil.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,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); | ||
} | ||
} |
Oops, something went wrong.