-
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
Showing
54 changed files
with
1,192 additions
and
110 deletions.
There are no files selected for viewing
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,31 @@ | ||
package replace.dto | ||
|
||
import kotlinx.serialization.Serializable | ||
import replace.model.File | ||
|
||
@Serializable | ||
class FileDto( | ||
override val id: String? = null, | ||
val name: String, | ||
val path: String, | ||
val extension: String, | ||
val sizeInBytes: Long, | ||
val mime: String? = null, | ||
) : Dto | ||
|
||
fun File.toDto() = FileDto( | ||
id = id?.toHexString(), | ||
name = name, | ||
path = path, | ||
extension = extension, | ||
sizeInBytes = sizeInBytes, | ||
mime = mime, | ||
) | ||
|
||
fun FileDto.toModel() = File( | ||
name = name, | ||
path = path, | ||
extension = extension, | ||
sizeInBytes = sizeInBytes, | ||
mime = mime, | ||
) |
37 changes: 37 additions & 0 deletions
37
application/src/jvmMain/kotlin/replace/dto/FileUploadDto.kt
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,37 @@ | ||
package replace.dto | ||
|
||
import kotlinx.serialization.Serializable | ||
import replace.datastore.FileRepository | ||
import replace.datastore.FileStorage | ||
import replace.datastore.TemporaryFileRepository | ||
import replace.usecase.temporaryfileupload.SaveTemporaryFileUploadPersistentUseCase | ||
|
||
@Serializable | ||
class FileUploadDto( | ||
override val id: String, | ||
val temporary: Boolean, | ||
) : Dto | ||
|
||
suspend fun FileUploadDto.save( | ||
temporaryFileRepository: TemporaryFileRepository, | ||
fileRepository: FileRepository, | ||
fileStorage: FileStorage, | ||
): FileUploadDto { | ||
if (!temporary) { | ||
return this | ||
} | ||
|
||
val file = SaveTemporaryFileUploadPersistentUseCase.execute( | ||
id, | ||
temporaryFileRepository, | ||
fileRepository, | ||
fileStorage, | ||
) | ||
|
||
checkNotNull(file.id) { "Could not save file" } | ||
|
||
return FileUploadDto( | ||
id = file.id, | ||
temporary = false, | ||
) | ||
} |
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
35 changes: 35 additions & 0 deletions
35
application/src/jvmMain/kotlin/replace/dto/TemporaryFileUploadDto.kt
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,35 @@ | ||
package replace.dto | ||
|
||
import kotlinx.serialization.Serializable | ||
import replace.model.TemporaryFile | ||
import java.time.LocalDateTime | ||
|
||
@Serializable | ||
class TemporaryFileUploadDto( | ||
override val id: String? = null, | ||
val name: String, | ||
val path: String, | ||
val mime: String? = null, | ||
val extension: String, | ||
val sizeInBytes: Long, | ||
val createdAt: String, | ||
) : Dto | ||
|
||
fun TemporaryFile.toDto() = TemporaryFileUploadDto( | ||
id = id?.toHexString(), | ||
name = name, | ||
path = path, | ||
mime = mime, | ||
extension = extension, | ||
sizeInBytes = sizeInBytes, | ||
createdAt = createdAt.toString() | ||
) | ||
|
||
fun TemporaryFileUploadDto.toModel() = TemporaryFile( | ||
name = name, | ||
path = path, | ||
extension = extension, | ||
mime = mime, | ||
sizeInBytes = sizeInBytes, | ||
createdAt = LocalDateTime.parse(createdAt) | ||
) |
47 changes: 47 additions & 0 deletions
47
application/src/jvmMain/kotlin/replace/usecase/file/CreateFileUseCase.kt
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,47 @@ | ||
package replace.usecase.file | ||
|
||
import org.bson.types.ObjectId | ||
import replace.datastore.FileRepository | ||
import replace.datastore.FileStorage | ||
import replace.datastore.TemporaryFileRepository | ||
import replace.dto.FileDto | ||
import replace.dto.toDto | ||
import replace.model.File | ||
import java.util.UUID | ||
|
||
object CreateFileUseCase { | ||
|
||
suspend fun execute( | ||
temporaryFileUploadId: String, | ||
temporaryFileRepository: TemporaryFileRepository, | ||
fileRepository: FileRepository, | ||
fileStorage: FileStorage, | ||
): FileDto { | ||
if (!ObjectId.isValid(temporaryFileUploadId)) { | ||
throw IllegalArgumentException("Id $temporaryFileUploadId is not a valid ObjectId") | ||
} | ||
|
||
val temporaryFileUpload = temporaryFileRepository.findOneById(ObjectId(temporaryFileUploadId)) | ||
?: throw IllegalArgumentException("Temporary file upload with id $temporaryFileUploadId not found") | ||
|
||
val newFilePath = "uploads/${UUID.randomUUID()}/base.${temporaryFileUpload.extension}" | ||
|
||
if (!fileStorage.copyFile(temporaryFileUpload.path, newFilePath)) { | ||
throw IllegalStateException("Could not copy file from ${temporaryFileUpload.path} to $newFilePath") | ||
} | ||
|
||
val file = File( | ||
name = temporaryFileUpload.name, | ||
path = newFilePath, | ||
mime = temporaryFileUpload.mime, | ||
extension = temporaryFileUpload.extension, | ||
sizeInBytes = fileStorage.getFileSize(newFilePath), | ||
) | ||
|
||
val insertedFile = fileRepository.insertOne(file) | ||
|
||
checkNotNull(insertedFile) { "Could not insert File into Database" } | ||
|
||
return insertedFile.toDto() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
application/src/jvmMain/kotlin/replace/usecase/file/DeleteFileUseCase.kt
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,27 @@ | ||
package replace.usecase.file | ||
|
||
import org.bson.types.ObjectId | ||
import replace.datastore.FileRepository | ||
import replace.datastore.FileStorage | ||
|
||
object DeleteFileUseCase { | ||
|
||
suspend fun execute( | ||
fileId: String, | ||
fileRepository: FileRepository, | ||
fileStorage: FileStorage, | ||
) { | ||
if (!ObjectId.isValid(fileId)) { | ||
throw IllegalArgumentException("Id $fileId: is not a valid ObjectId") | ||
} | ||
|
||
val file = fileRepository.findOneById(ObjectId(fileId)) | ||
?: throw IllegalArgumentException("File with id $fileId not found") | ||
|
||
if (fileStorage.deleteFile(file.path)) { | ||
fileRepository.deleteOneById(ObjectId(fileId)) | ||
} else { | ||
throw IllegalStateException("Could not delete file with id $fileId") | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
application/src/jvmMain/kotlin/replace/usecase/floor/SaveFloorPlanFileUseCase.kt
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,34 @@ | ||
package replace.usecase.floor | ||
|
||
import org.bson.types.ObjectId | ||
import replace.datastore.FileRepository | ||
import replace.datastore.FileStorage | ||
import replace.datastore.FloorRepository | ||
import replace.datastore.TemporaryFileRepository | ||
import replace.dto.FloorDto | ||
import replace.dto.saveFiles | ||
import replace.usecase.file.DeleteFileUseCase | ||
|
||
object SaveFloorPlanFileUseCase { | ||
suspend fun execute( | ||
floorDto: FloorDto, | ||
floorRepository: FloorRepository, | ||
temporaryFileRepository: TemporaryFileRepository, | ||
fileRepository: FileRepository, | ||
fileStorage: FileStorage, | ||
): FloorDto { | ||
val oldPlanFileId = floorDto.id?.let { floorRepository.findOneById(ObjectId(it)) }?.planFileId?.toHexString() | ||
|
||
val saved = floorDto.saveFiles( | ||
temporaryFileRepository = temporaryFileRepository, | ||
fileRepository = fileRepository, | ||
fileStorage = fileStorage, | ||
) | ||
|
||
if (oldPlanFileId != saved.planFile?.id) { | ||
oldPlanFileId?.let { DeleteFileUseCase.execute(it, fileRepository, fileStorage) } | ||
} | ||
|
||
return saved | ||
} | ||
} |
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
Oops, something went wrong.