Add room type images list endpoint
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s

This commit is contained in:
androidlover5842
2026-01-27 23:34:37 +05:30
parent e4c38a699f
commit df351204ed
4 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
package com.android.trisolarisserver.controller
import com.android.trisolarisserver.controller.dto.RoomImageResponse
import com.android.trisolarisserver.models.room.RoomImage
import com.android.trisolarisserver.models.room.RoomImageTag
import com.android.trisolarisserver.repo.RoomImageRepo
import com.android.trisolarisserver.repo.RoomTypeRepo
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
import java.nio.file.Files
import java.nio.file.Paths
import java.util.UUID
@RestController
@RequestMapping("/properties/{propertyId}/room-types/{roomTypeCode}/images")
class RoomTypeImages(
private val roomTypeRepo: RoomTypeRepo,
private val roomImageRepo: RoomImageRepo,
@org.springframework.beans.factory.annotation.Value("\${storage.rooms.publicBaseUrl}")
private val publicBaseUrl: String
) {
@GetMapping
fun listByRoomType(
@PathVariable propertyId: UUID,
@PathVariable roomTypeCode: String
): List<RoomImageResponse> {
val roomType = roomTypeRepo.findByPropertyIdAndCodeIgnoreCase(propertyId, roomTypeCode)
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Room type not found")
val images = roomImageRepo.findByPropertyIdAndRoomTypeCodeOrdered(propertyId, roomType.code).toMutableList()
if (images.isEmpty()) return emptyList()
val missing = mutableListOf<RoomImage>()
val present = mutableListOf<RoomImage>()
for (img in images) {
val originalExists = Files.exists(Paths.get(img.originalPath))
if (!originalExists) {
missing.add(img)
try {
Files.deleteIfExists(Paths.get(img.thumbnailPath))
} catch (_: Exception) {
}
} else {
present.add(img)
}
}
if (missing.isNotEmpty()) {
roomImageRepo.deleteAll(missing)
}
return present.map { it.toResponse(publicBaseUrl) }
}
}
private fun RoomImage.toResponse(baseUrl: String): RoomImageResponse {
val id = id ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Image id missing")
return RoomImageResponse(
id = id,
propertyId = property.id!!,
roomId = room.id!!,
roomTypeCode = roomTypeCode,
url = "$baseUrl/properties/${property.id}/rooms/${room.id}/images/$id/file",
thumbnailUrl = "$baseUrl/properties/${property.id}/rooms/${room.id}/images/$id/file?size=thumb",
contentType = contentType,
sizeBytes = sizeBytes,
tags = tags.map { it.toResponse() }.toSet(),
roomSortOrder = roomSortOrder,
roomTypeSortOrder = roomTypeSortOrder,
createdAt = createdAt.toString()
)
}
private fun RoomImageTag.toResponse(): com.android.trisolarisserver.controller.dto.RoomImageTagResponse {
val id = id ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Tag id missing")
return com.android.trisolarisserver.controller.dto.RoomImageTagResponse(
id = id,
name = name
)
}