From 1367ae9c5780c45c7d81918981e3fe0bd08628fa Mon Sep 17 00:00:00 2001 From: Daniil Zavyalov Date: Fri, 3 May 2024 15:34:02 +0600 Subject: [PATCH] [+] add dto for api v0 --- .../kotlin/office/effective/dto/BookingDTO.kt | 2 +- .../effective/dto/WorkspaceResponseDTO.kt | 19 ++++++ .../converters/BookingDtoModelConverter.kt | 10 ++-- .../booking/routes/swagger/BookingSwagger.kt | 12 ++-- .../workspace/DI/WorkspaceDiModule.kt | 4 +- ...erter.kt => WorkspaceDtoModelConverter.kt} | 58 +++++++++++++++++-- .../workspace/facade/WorkspaceFacade.kt | 36 +++++++----- .../workspace/facade/WorkspaceFacadeV1.kt | 6 +- .../routes/swagger/WorkspaceSwagger.kt | 12 ++-- .../workspace/WorkspaceFacadeTest.kt | 4 +- 10 files changed, 115 insertions(+), 48 deletions(-) create mode 100644 effectiveOfficeBackend/src/main/kotlin/office/effective/dto/WorkspaceResponseDTO.kt rename effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/{WorkspaceFacadeConverter.kt => WorkspaceDtoModelConverter.kt} (64%) diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/BookingDTO.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/BookingDTO.kt index 6ffcddc08..9520e0f86 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/BookingDTO.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/BookingDTO.kt @@ -14,7 +14,7 @@ import model.RecurrenceDTO data class BookingDTO ( val owner: UserDTO, val participants: List, - val workspace: WorkspaceDTO, + val workspace: WorkspaceResponseDTO, val id: String?, val beginBooking: Long, val endBooking: Long, diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/WorkspaceResponseDTO.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/WorkspaceResponseDTO.kt new file mode 100644 index 000000000..9f9bc4d73 --- /dev/null +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/dto/WorkspaceResponseDTO.kt @@ -0,0 +1,19 @@ +package office.effective.dto + +import kotlinx.serialization.Serializable + +@Serializable +@Deprecated( + message = "Deprecated since 1.0 api version", + replaceWith = ReplaceWith( + expression = "WorkspaceDTO", + imports = ["office.effective.dto.WorkspaceDTO"] + ) +) +data class WorkspaceResponseDTO( + val id: String, + val name: String, + val utilities: List, + val zone: WorkspaceZoneDTO? = null, + val tag: String +) diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/converters/BookingDtoModelConverter.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/converters/BookingDtoModelConverter.kt index 0d69d6f2e..3386305d1 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/converters/BookingDtoModelConverter.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/converters/BookingDtoModelConverter.kt @@ -10,7 +10,7 @@ import office.effective.dto.BookingResponseDTO import office.effective.dto.UserDTO import office.effective.features.user.converters.UserDTOModelConverter import office.effective.features.user.repository.UserRepository -import office.effective.features.workspace.converters.WorkspaceFacadeConverter +import office.effective.features.workspace.converters.WorkspaceDtoModelConverter import office.effective.features.workspace.repository.WorkspaceRepository import office.effective.model.* import org.slf4j.LoggerFactory @@ -19,11 +19,11 @@ import java.time.Instant /** * Converts between [BookingDTO] and [Booking] * - * Uses [UserDTOModelConverter] and [WorkspaceFacadeConverter] to convert contained users and workspaces + * Uses [UserDTOModelConverter] and [WorkspaceDtoModelConverter] to convert contained users and workspaces */ class BookingDtoModelConverter( private val userConverter: UserDTOModelConverter, - private val workspaceConverter: WorkspaceFacadeConverter, + private val workspaceConverter: WorkspaceDtoModelConverter, private val userRepository: UserRepository, private val workspaceRepository: WorkspaceRepository, private val uuidValidator: UuidValidator @@ -51,7 +51,7 @@ class BookingDtoModelConverter( owner = booking.owner?.let { userConverter.modelToDTO(it) } ?: UserDTO("", "", true, "", "", listOf(), "", ""), participants = booking.participants.map { userConverter.modelToDTO(it) }, - workspace = workspaceConverter.modelToDto(booking.workspace), + workspace = workspaceConverter.modelToResponseDto(booking.workspace), id = booking.id.toString(), beginBooking = booking.beginBooking.toEpochMilli(), endBooking = booking.endBooking.toEpochMilli(), @@ -111,7 +111,7 @@ class BookingDtoModelConverter( return Booking( owner = userConverter.dTOToModel(bookingDTO.owner), participants = bookingDTO.participants.map { userConverter.dTOToModel(it) }, - workspace = workspaceConverter.dtoToModel(bookingDTO.workspace), + workspace = workspaceConverter.responseDtoToModel(bookingDTO.workspace), id = bookingDTO.id, beginBooking = Instant.ofEpochMilli(bookingDTO.beginBooking), endBooking = Instant.ofEpochMilli(bookingDTO.endBooking), diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/swagger/BookingSwagger.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/swagger/BookingSwagger.kt index 823c5546c..4b855b011 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/swagger/BookingSwagger.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/booking/routes/swagger/BookingSwagger.kt @@ -7,11 +7,7 @@ import io.github.smiley4.ktorswaggerui.dsl.OpenApiRoute import io.ktor.http.* import office.effective.common.constants.BookingConstants import office.effective.common.swagger.SwaggerDocument -import office.effective.dto.BookingDTO -import office.effective.dto.IntegrationDTO -import office.effective.dto.UserDTO -import office.effective.dto.UtilityDTO -import office.effective.dto.WorkspaceDTO +import office.effective.dto.* import java.time.Instant /** @@ -149,7 +145,7 @@ fun SwaggerDocument.postBooking(): OpenApiRoute.() -> Unit = { tag = "employee" ) ), - workspace = WorkspaceDTO( + workspace = WorkspaceResponseDTO( id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting", utilities = listOf( UtilityDTO( @@ -301,7 +297,7 @@ private val bookingExample1 = BookingDTO( tag = "employee" ) ), - workspace = WorkspaceDTO( + workspace = WorkspaceResponseDTO( id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting", utilities = listOf( UtilityDTO( @@ -361,7 +357,7 @@ private val bookingExample2 = BookingDTO( tag = "employee" ) ), - workspace = WorkspaceDTO( + workspace = WorkspaceResponseDTO( id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting", utilities = listOf( UtilityDTO( diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/DI/WorkspaceDiModule.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/DI/WorkspaceDiModule.kt index ead428bf3..f1de69170 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/DI/WorkspaceDiModule.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/DI/WorkspaceDiModule.kt @@ -1,6 +1,6 @@ package office.effective.features.workspace.DI -import office.effective.features.workspace.converters.WorkspaceFacadeConverter +import office.effective.features.workspace.converters.WorkspaceDtoModelConverter import office.effective.features.workspace.converters.WorkspaceRepositoryConverter import office.effective.features.workspace.facade.WorkspaceFacade import office.effective.features.workspace.facade.WorkspaceFacadeV1 @@ -14,7 +14,7 @@ val workspaceDiModule = module(createdAtStart = true) { single { WorkspaceRepository(get(), get()) } single { WorkspaceService(get()) } single { get() } - single { WorkspaceFacadeConverter(get()) } + single { WorkspaceDtoModelConverter(get()) } single { WorkspaceFacade(get(), get(), get(), get()) } single { WorkspaceFacadeV1(get(), get(), get(), get(), get()) } } diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/WorkspaceFacadeConverter.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/WorkspaceDtoModelConverter.kt similarity index 64% rename from effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/WorkspaceFacadeConverter.kt rename to effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/WorkspaceDtoModelConverter.kt index 240f214d2..c53cf73bd 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/WorkspaceFacadeConverter.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/converters/WorkspaceDtoModelConverter.kt @@ -1,14 +1,10 @@ package office.effective.features.workspace.converters import office.effective.common.utils.UuidValidator -import office.effective.dto.BookingResponseDTO -import office.effective.dto.UtilityDTO -import office.effective.dto.WorkspaceDTO -import office.effective.dto.WorkspaceZoneDTO +import office.effective.dto.* import office.effective.model.Utility import office.effective.model.Workspace import office.effective.model.WorkspaceZone -import org.slf4j.LoggerFactory import java.util.* /** @@ -17,7 +13,7 @@ import java.util.* * * This converter helps in the transformation of data between the DTO and model representations of a workspace. */ -class WorkspaceFacadeConverter(private val uuidValidator: UuidValidator) { +class WorkspaceDtoModelConverter(private val uuidValidator: UuidValidator) { /** * Converts [Workspace] with [WorkspaceZone] and [Utilities][Utility] @@ -39,6 +35,28 @@ class WorkspaceFacadeConverter(private val uuidValidator: UuidValidator) { ) } + /** + * Converts [Workspace] with [WorkspaceZone] and [Utilities][Utility] + * to [WorkspaceDTO] with [WorkspaceZoneDTO] and [UtilityDTO]s + * @param model The [Workspace] object to be converted + * @return The resulting [WorkspaceDTO] object + * @author Daniil Zavyalov + */ + @Deprecated( + message = "Deprecated since 1.0 api version", + replaceWith = ReplaceWith("modelToResponseDto(model)") + ) + fun modelToResponseDto(model: Workspace): WorkspaceResponseDTO { + val utilities = model.utilities.map { utilityModelToDto(it) } + return WorkspaceResponseDTO( + id = model.id.toString(), + name = model.name, + utilities = utilities, + zone = model.zone?.let { zoneModelToDto(it) }, + tag = model.tag, + ) + } + /** * Converts [Utility] to [UtilityDTO] * @@ -85,6 +103,34 @@ class WorkspaceFacadeConverter(private val uuidValidator: UuidValidator) { ) } + /** + * Converts [Workspace] with [WorkspaceZone] and [Utilities][Utility] + * to [WorkspaceResponseDTO] with [WorkspaceZoneDTO] and [UtilityDTO]s. + * Uses [UuidValidator] to convert workspace id to UUID, but if [WorkspaceResponseDTO.id]=="null" [Workspace.id] will be null + * @param dto The WorkspaceDTO object to be converted + * @return The resulting Workspace object + * @author Daniil Zavyalov, Danil Kiselev + */ + @Deprecated( + message = "Deprecated since 1.0 api version", + replaceWith = ReplaceWith("responseDtoToModel(model)") + ) + fun responseDtoToModel(dto: WorkspaceResponseDTO): Workspace { + var workspaceId: UUID? = null + if (dto.id != "null") { + workspaceId = uuidValidator.uuidFromString(dto.id) + } + + val utilities = dto.utilities.map { utilityDtoToModel(it) } + return Workspace( + id = workspaceId, + name = dto.name, + tag = dto.tag, + utilities = utilities, + zone = dto.zone?.let { zoneDtoToModel(it) } + ) + } + /** * Converts [UtilityDTO] to [Utility] * diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacade.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacade.kt index c5c5e2a2d..651235908 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacade.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacade.kt @@ -4,8 +4,9 @@ import office.effective.common.exception.InstanceNotFoundException import office.effective.common.exception.ValidationException import office.effective.common.utils.DatabaseTransactionManager import office.effective.common.utils.UuidValidator -import office.effective.features.workspace.converters.WorkspaceFacadeConverter +import office.effective.features.workspace.converters.WorkspaceDtoModelConverter import office.effective.dto.WorkspaceDTO +import office.effective.dto.WorkspaceResponseDTO import office.effective.dto.WorkspaceZoneDTO import office.effective.model.Workspace import office.effective.serviceapi.IWorkspaceService @@ -17,42 +18,49 @@ import java.time.Instant * * In case of an error, the database transaction will be rolled back. */ +@Deprecated( + message = "Deprecated since 1.0 api version", + replaceWith = ReplaceWith( + expression = "WorkspaceFacadeV1", + imports = ["office.effective.features.workspace.facade.WorkspaceFacadeV1"] + ) +) class WorkspaceFacade(private val service: IWorkspaceService, - private val converter: WorkspaceFacadeConverter, + private val converter: WorkspaceDtoModelConverter, private val transactionManager: DatabaseTransactionManager, private val uuidValidator: UuidValidator) { /** - * Retrieves a [WorkspaceDTO] by its id + * Retrieves a [WorkspaceResponseDTO] by its id * * @param id id of requested workspace. Should be valid UUID - * @return [WorkspaceDTO] with the given [id] + * @return [WorkspaceResponseDTO] with the given [id] * @throws InstanceNotFoundException if workspace with the given id doesn't exist in database * @author Daniil Zavyalov */ - fun findById(id: String): WorkspaceDTO { + fun findById(id: String): WorkspaceResponseDTO { val uuid = uuidValidator.uuidFromString(id) - val workspaceDTO: WorkspaceDTO = transactionManager.useTransaction({ + val workspaceDTO: WorkspaceResponseDTO = transactionManager.useTransaction({ val workspace = service.findById(uuid) ?: throw InstanceNotFoundException(Workspace::class, "Workspace with id $id not found", uuid) - workspace.let { converter.modelToDto(it) } + workspace.let { converter.modelToResponseDto(it) } }) return workspaceDTO } /** - * Returns all [WorkspaceDTO] with the given tag + * Returns all [WorkspaceResponseDTO] with the given tag * * @param tag tag name of requested workspaces - * @return List of [WorkspaceDTO] with the given [tag] + * @return List of [WorkspaceResponseDTO] with the given [tag] * @author Daniil Zavyalov */ - fun findAllByTag(tag: String): List { + fun findAllByTag(tag: String): List { val result = transactionManager.useTransaction({ val workspaceList: List = service.findAllByTag(tag) - workspaceList.map { converter.modelToDto(it) } + workspaceList.map { converter.modelToResponseDto(it) } }) return result } @@ -63,12 +71,12 @@ class WorkspaceFacade(private val service: IWorkspaceService, * @param tag tag name of requested workspaces * @param beginTimestamp period start time * @param endTimestamp period end time - * @return List of [WorkspaceDTO] with the given [tag] + * @return List of [WorkspaceResponseDTO] with the given [tag] * @throws ValidationException if begin or end timestamp less than 0, greater than max timestamp * or if end timestamp less than or equal to begin timestamp * @author Daniil Zavyalov */ - fun findAllFreeByPeriod(tag: String, beginTimestamp: Long, endTimestamp: Long): List { + fun findAllFreeByPeriod(tag: String, beginTimestamp: Long, endTimestamp: Long): List { if (beginTimestamp < 0L || beginTimestamp >= 2147483647000L) throw ValidationException("Begin timestamp should be non-negative and less than timestamp max value") else if (endTimestamp < 0L || endTimestamp >= 2147483647000L) @@ -83,7 +91,7 @@ class WorkspaceFacade(private val service: IWorkspaceService, Instant.ofEpochMilli(beginTimestamp), Instant.ofEpochMilli(endTimestamp) ) - modelList.map { converter.modelToDto(it) } + modelList.map { converter.modelToResponseDto(it) } }) } diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacadeV1.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacadeV1.kt index 491bb8f28..2a105f80d 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacadeV1.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/facade/WorkspaceFacadeV1.kt @@ -1,15 +1,13 @@ package office.effective.features.workspace.facade -import office.effective.common.constants.BookingConstants import office.effective.common.exception.InstanceNotFoundException import office.effective.common.exception.ValidationException import office.effective.common.utils.DatabaseTransactionManager import office.effective.common.utils.UuidValidator -import office.effective.features.workspace.converters.WorkspaceFacadeConverter +import office.effective.features.workspace.converters.WorkspaceDtoModelConverter import office.effective.dto.WorkspaceDTO import office.effective.dto.WorkspaceZoneDTO import office.effective.features.booking.facade.BookingFacadeV1 -import office.effective.features.booking.service.BookingService import office.effective.model.Workspace import office.effective.serviceapi.IWorkspaceService import java.time.Instant @@ -22,7 +20,7 @@ import java.time.Instant */ class WorkspaceFacadeV1( private val service: IWorkspaceService, - private val converter: WorkspaceFacadeConverter, + private val converter: WorkspaceDtoModelConverter, private val transactionManager: DatabaseTransactionManager, private val uuidValidator: UuidValidator, private val bookingFacade: BookingFacadeV1 diff --git a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/routes/swagger/WorkspaceSwagger.kt b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/routes/swagger/WorkspaceSwagger.kt index 2a3ba4662..6a472503d 100644 --- a/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/routes/swagger/WorkspaceSwagger.kt +++ b/effectiveOfficeBackend/src/main/kotlin/office/effective/features/workspace/routes/swagger/WorkspaceSwagger.kt @@ -7,7 +7,7 @@ import io.github.smiley4.ktorswaggerui.dsl.OpenApiRoute import io.ktor.http.* import office.effective.common.swagger.SwaggerDocument import office.effective.dto.UtilityDTO -import office.effective.dto.WorkspaceDTO +import office.effective.dto.WorkspaceResponseDTO import office.effective.dto.WorkspaceZoneDTO /** @@ -27,9 +27,9 @@ fun SwaggerDocument.returnWorkspaceById(): OpenApiRoute.() -> Unit = { response { HttpStatusCode.OK to { description = "Returns workspace found by id" - body { + body { example( - "Workspaces", WorkspaceDTO( + "Workspaces", WorkspaceResponseDTO( id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting", utilities = listOf( UtilityDTO( @@ -86,10 +86,10 @@ fun SwaggerDocument.returnWorkspaceByTag(): OpenApiRoute.() -> Unit = { response { HttpStatusCode.OK to { description = "Returns all workspaces found by tag" - body> { + body> { example( "Workspace", listOf( - WorkspaceDTO( + WorkspaceResponseDTO( id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting", utilities = listOf( UtilityDTO( @@ -104,7 +104,7 @@ fun SwaggerDocument.returnWorkspaceByTag(): OpenApiRoute.() -> Unit = { count = 1 ) ) - ), WorkspaceDTO( + ), WorkspaceResponseDTO( id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Moon", tag = "meeting", utilities = listOf( UtilityDTO( diff --git a/effectiveOfficeBackend/src/test/kotlin/office/effective/workspace/WorkspaceFacadeTest.kt b/effectiveOfficeBackend/src/test/kotlin/office/effective/workspace/WorkspaceFacadeTest.kt index 77e6c35ee..b14970cb9 100644 --- a/effectiveOfficeBackend/src/test/kotlin/office/effective/workspace/WorkspaceFacadeTest.kt +++ b/effectiveOfficeBackend/src/test/kotlin/office/effective/workspace/WorkspaceFacadeTest.kt @@ -4,7 +4,7 @@ import junit.framework.TestCase.assertEquals import office.effective.common.exception.InstanceNotFoundException import office.effective.common.utils.impl.DatabaseTransactionManagerImpl import office.effective.common.utils.UuidValidator -import office.effective.features.workspace.converters.WorkspaceFacadeConverter +import office.effective.features.workspace.converters.WorkspaceDtoModelConverter import office.effective.dto.WorkspaceDTO import office.effective.features.workspace.facade.WorkspaceFacade import office.effective.features.workspace.service.WorkspaceService @@ -26,7 +26,7 @@ class WorkspaceFacadeTest { @Mock private lateinit var mockService: WorkspaceService @Mock - private lateinit var mockConverter: WorkspaceFacadeConverter + private lateinit var mockConverter: WorkspaceDtoModelConverter @Mock private lateinit var mockTransactionManager: DatabaseTransactionManagerImpl @Mock