Skip to content

Commit

Permalink
Datenbankmigration (#67)
Browse files Browse the repository at this point in the history
* add liquibase

* add model with some hacks test

* wip

* update Backend

* update backend

* update timeformat in db

* update build gradle

* update file urls

* Added basic database model

* fix frontend + migrations

* update readme

* remove andrena plans

Co-authored-by: Daniel Thoma <[email protected]>
  • Loading branch information
PhilippFaber and djfhe authored Jan 20, 2023
1 parent c1987c0 commit d6e7a74
Show file tree
Hide file tree
Showing 135 changed files with 1,983 additions and 1,109 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Technologien

- Backend: [Ktor](https://ktor.io/)
- Datatabase: [MongoDB](https://www.mongodb.com)
- Datatabase: [PostgreSQL](https://www.postgresql.org/)
- Frontend:
- [Angular](https://angular.io/)
- [Angular Material](https://material.angular.io/)
Expand All @@ -25,19 +25,6 @@
- In `/infrastructure/src/jvmMain/resources/application.conf` configure your database settings
- If you want to contribute to the project run `./gradlew addKtlintCheckGitPreCommitHook` beforehand
- execute `yarn install` in `/web/src/angular` to install frontend packages
- add a `user` collection in your database and add an initial user. Copy-Paste Example:

```json
{
"_id": {
"$oid": "6391c928a8d80e8729177c7f"
},
"username": "user",
"password": "password",
"firstName": "Max",
"lastName": "Mustermann"
}
````

### Run

Expand All @@ -46,8 +33,9 @@

### Notes

- When running ktor in Dev Mode, it will attempt to seed a dev user. See Console output for more information.
- Frontend runs on port 4200, backend on port 8000. Frontend requests are getting proxied by angular to port 8000, configure in `/web/src/angular/proxy.conf.json`
- Frontend hot reloads, backends needs to get restartet.
- Frontend hot reloads, backends needs to get restarted.
- For faster starting time of the backend, comment out `commonMainRuntimeOnly(project(":replace-web"))` in `/build.gradle.kts`.
- Should look like this:

Expand All @@ -61,5 +49,5 @@

```

- frontend won't be compiled and served anymore by Ktor, which is irrelevant in Dev, since we are using the angular dev server
- frontend won't be compiled and served anymore by Ktor, which is irrelevant in Dev, since we are using the angular dev server
- You can execute `./gradlew ktlintCheck` and `./gradlew ktlintFormat` to check for backend formatting (alternatively ctrl + ctrl in intellij ot execute gradle commands)
6 changes: 5 additions & 1 deletion application/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ dependencies {
jvmMainImplementation(libs.logging.impl)
jvmMainImplementation(libs.logging.core)
jvmMainImplementation(libs.kotlinx.serialization)
commonMainImplementation(libs.kotlinx.datetime)
jvmMainImplementation(libs.exposed.core)
jvmMainImplementation(libs.exposed.dao)
jvmMainImplementation(libs.exposed.jdbc)
jvmMainImplementation(libs.exposed.java.time)
jvmMainImplementation(libs.kotlinx.datetime)
}
61 changes: 42 additions & 19 deletions application/src/jvmMain/kotlin/replace/dto/BookableEntityDto.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
package replace.dto

import kotlinx.serialization.Serializable
import org.bson.types.ObjectId
import replace.model.BookableEntity
import kotlin.reflect.KProperty1

@Serializable
data class BookableEntityDto(
override val id: String? = null,
override val id: String,

val name: String,

val floorId: String,

val parentId: String? = null,
val type: BookableEntityTypeDto? = null
) : Dto

fun BookableEntity.toDto() = BookableEntityDto(
id = id?.toHexString(),
name = name,
floorId = floorId.toHexString(),
parentId = parentId?.toHexString(),
type = type?.toDto()
)

fun BookableEntityDto.toModel() =
BookableEntity(
name,
type?.toModel(),
ObjectId(floorId),
if (parentId != null) ObjectId(parentId) else null

val typeId: String? = null,

val floor: FloorDto? = null,
val parent: BookableEntityDto? = null,
val type: BookableEntityTypeDto? = null,
) : ModelDto

fun BookableEntity.toDto(with: List<KProperty1<BookableEntity, *>> = emptyList()): BookableEntityDto {
val floor = if (with.contains(BookableEntity::floor)) {
floor.toDto()
} else {
null
}

val parent = if (with.contains(BookableEntity::parent)) {
parent?.toDto()
} else {
null
}

val type = if (with.contains(BookableEntity::type)) {
type?.toDto()
} else {
null
}

return BookableEntityDto(
id = id.value,
name = name,
floorId = floorId.value,
parentId = parentId?.value,
typeId = typeId?.value,
floor = floor,
parent = parent,
type = type,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ package replace.dto

import kotlinx.serialization.Serializable
import replace.model.BookableEntityType
import kotlin.reflect.KProperty1

@Serializable
data class BookableEntityTypeDto(
override val id: String? = null,
override val id: String,
val name: String,
) : Dto
val bookableEntities: List<BookableEntityDto>? = null,
) : ModelDto

fun BookableEntityType.toDto() = BookableEntityTypeDto(
id = id?.toHexString(),
name = name,
)
fun BookableEntityType.toDto(with: List<KProperty1<BookableEntityType, *>> = emptyList()): BookableEntityTypeDto {
val bookableEntities = if (with.contains(BookableEntityType::bookableEntities)) {
bookableEntities.map { it.toDto() }
} else {
null
}

fun BookableEntityTypeDto.toModel() = BookableEntityType(name)
return BookableEntityTypeDto(
id = id.value,
name = name,
bookableEntities = bookableEntities,
)
}
44 changes: 30 additions & 14 deletions application/src/jvmMain/kotlin/replace/dto/BookingDto.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
package replace.dto

import kotlinx.datetime.Instant
import kotlinx.serialization.Serializable
import org.bson.types.ObjectId
import replace.model.Booking
import kotlin.reflect.KProperty1

@Serializable
data class BookingDto(
override val id: String? = null,
val bookedEntities: List<String>,
val startDateTime: String,
val endDateTime: String,
) : Dto
override val id: String,
val userId: String,
val user: UserDto? = null,
val bookedEntities: List<BookableEntityDto>? = null,
val start: String,
val end: String
) : ModelDto

fun Booking.toDto() = BookingDto(
id = id?.toHexString(),
bookedEntities = bookedEntities.map { it.toHexString() },
startDateTime = startDateTime.toString(),
endDateTime = endDateTime.toString(),
)
fun Booking.toDto(with: List<KProperty1<Booking, *>> = emptyList()): BookingDto {

fun BookingDto.toModel() = Booking(bookedEntities.map { ObjectId(it) }, Instant.parse(startDateTime), Instant.parse(endDateTime))
val bookedEntities = if (with.contains(Booking::bookedEntities)) {
bookedEntities.map { it.toDto() }
} else {
null
}

val user = if (with.contains(Booking::user)) {
user.toDto()
} else {
null
}

return BookingDto(
id = id.value,
userId = userId.value,
start = start.toString(),
end = end.toString(),
bookedEntities = bookedEntities,
user = user,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package replace.dto

import kotlinx.serialization.Serializable

@Serializable
data class CreateBookableEntityDto(
val name: String,

val floorId: String,

val parentId: String? = null,

val typeId: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package replace.model
package replace.dto

import kotlinx.serialization.Serializable

@Serializable
data class BookableEntityType(
data class CreateBookableEntityTypeDto(
val name: String,
) : ObjectWithId()
)
10 changes: 10 additions & 0 deletions application/src/jvmMain/kotlin/replace/dto/CreateBookingDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package replace.dto

import kotlinx.serialization.Serializable

@Serializable
data class CreateBookingDto(
val bookedEntityIds: List<String>,
val start: String,
val end: String
)
10 changes: 10 additions & 0 deletions application/src/jvmMain/kotlin/replace/dto/CreateFloorDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package replace.dto

import kotlinx.serialization.Serializable

@Serializable
data class CreateFloorDto(
val name: String,
val siteId: String,
val planFile: FileUploadDto? = null,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package replace.model
package replace.dto

import kotlinx.serialization.Serializable

@Serializable
data class Site(
class CreateSiteDto(
val name: String,
) : ObjectWithId()
)
4 changes: 2 additions & 2 deletions application/src/jvmMain/kotlin/replace/dto/Dto.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package replace.dto

interface Dto {
val id: String?
abstract class Dto {
fun validate() {}
}
18 changes: 6 additions & 12 deletions application/src/jvmMain/kotlin/replace/dto/FileDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@ import kotlinx.serialization.Serializable
import replace.model.File

@Serializable
class FileDto(
override val id: String? = null,
data class FileDto(
override val id: String,
val name: String,
val path: String,
val extension: String,
val sizeInBytes: Long,
val mime: String? = null,
) : Dto
val url: String,
) : ModelDto

fun File.toDto() = FileDto(
id = id?.toHexString(),
name = name,
path = path,
extension = extension,
sizeInBytes = sizeInBytes,
mime = mime,
)

fun FileDto.toModel() = File(
id = id.value,
name = name,
path = path,
extension = extension,
sizeInBytes = sizeInBytes,
mime = mime,
url = "/api/file/$id"
)
16 changes: 5 additions & 11 deletions application/src/jvmMain/kotlin/replace/dto/FileUploadDto.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
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,
data class FileUploadDto(
val fileId: 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,
fileId,
fileStorage,
)

checkNotNull(file.id) { "Could not save file" }

return FileUploadDto(
id = file.id,
fileId = file.id,
temporary = false,
)
}
Loading

0 comments on commit d6e7a74

Please sign in to comment.