Skip to content

Commit

Permalink
Merge pull request #258 from effectivemade/backendApp/hotfix/fixWorks…
Browse files Browse the repository at this point in the history
…paceDto

[+] add workspace dto for api v0
  • Loading branch information
zavyalov-daniil authored May 3, 2024
2 parents 2517b32 + 1367ae9 commit 0bd2478
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import model.RecurrenceDTO
data class BookingDTO (
val owner: UserDTO,
val participants: List<UserDTO>,
val workspace: WorkspaceDTO,
val workspace: WorkspaceResponseDTO,
val id: String?,
val beginBooking: Long,
val endBooking: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UtilityDTO>,
val zone: WorkspaceZoneDTO? = null,
val tag: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,7 +14,7 @@ val workspaceDiModule = module(createdAtStart = true) {
single { WorkspaceRepository(get(), get()) }
single { WorkspaceService(get()) }
single<IWorkspaceService> { get<WorkspaceService>() }
single { WorkspaceFacadeConverter(get()) }
single { WorkspaceDtoModelConverter(get()) }
single { WorkspaceFacade(get(), get(), get(), get()) }
single { WorkspaceFacadeV1(get(), get(), get(), get(), get()) }
}
Original file line number Diff line number Diff line change
@@ -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.*

/**
Expand All @@ -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]
Expand All @@ -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]
*
Expand Down Expand Up @@ -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]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<WorkspaceDTO> {
fun findAllByTag(tag: String): List<WorkspaceResponseDTO> {
val result = transactionManager.useTransaction({
val workspaceList: List<Workspace> = service.findAllByTag(tag)
workspaceList.map { converter.modelToDto(it) }
workspaceList.map { converter.modelToResponseDto(it) }
})
return result
}
Expand All @@ -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<WorkspaceDTO> {
fun findAllFreeByPeriod(tag: String, beginTimestamp: Long, endTimestamp: Long): List<WorkspaceResponseDTO> {
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)
Expand All @@ -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) }
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -27,9 +27,9 @@ fun SwaggerDocument.returnWorkspaceById(): OpenApiRoute.() -> Unit = {
response {
HttpStatusCode.OK to {
description = "Returns workspace found by id"
body<WorkspaceDTO> {
body<WorkspaceResponseDTO> {
example(
"Workspaces", WorkspaceDTO(
"Workspaces", WorkspaceResponseDTO(
id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting",
utilities = listOf(
UtilityDTO(
Expand Down Expand Up @@ -86,10 +86,10 @@ fun SwaggerDocument.returnWorkspaceByTag(): OpenApiRoute.() -> Unit = {
response {
HttpStatusCode.OK to {
description = "Returns all workspaces found by tag"
body<List<WorkspaceDTO>> {
body<List<WorkspaceResponseDTO>> {
example(
"Workspace", listOf(
WorkspaceDTO(
WorkspaceResponseDTO(
id = "2561471e-2bc6-11ee-be56-0242ac120002", name = "Sun", tag = "meeting",
utilities = listOf(
UtilityDTO(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0bd2478

Please sign in to comment.