-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to persistent CloudKit database (#208)
Co-authored-by: hfhbd <[email protected]>
- Loading branch information
Showing
20 changed files
with
226 additions
and
227 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
52 changes: 44 additions & 8 deletions
52
backend/src/jvmMain/kotlin/app/softwork/composetodo/DTO.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 |
---|---|---|
@@ -1,16 +1,52 @@ | ||
package app.softwork.composetodo | ||
|
||
import app.softwork.composetodo.dao.Todo | ||
import app.softwork.composetodo.dao.User | ||
import kotlinx.datetime.toKotlinLocalDateTime | ||
import app.softwork.cloudkitclient.values.* | ||
import app.softwork.composetodo.dao.* | ||
import kotlinx.uuid.* | ||
|
||
fun Todo.toDTO() = app.softwork.composetodo.dto.Todo( | ||
id = id.value, | ||
title = title, | ||
until = until?.toKotlinLocalDateTime(), | ||
finished = finished | ||
id = recordName.toUUID(), | ||
title = fields.title.value, | ||
until = fields.until?.value, | ||
finished = fields.finished.value.toBoolean(), | ||
recordChangeTag = recordChangeTag | ||
) | ||
|
||
fun app.softwork.composetodo.dto.Todo.toDAO(user: User) = Todo( | ||
recordName = id.toString(), | ||
fields = Todo.Fields( | ||
title = Value.String(title), | ||
finished = Value.String(finished.toString()), | ||
until = until?.let { Value.DateTime(it) }, | ||
user = Value.Reference(user) | ||
), | ||
recordChangeTag = recordChangeTag | ||
) | ||
|
||
fun User.toDTO() = app.softwork.composetodo.dto.User( | ||
id = id.value, firstName = firstName, lastName = lastName | ||
username = recordName, | ||
firstName = fields.firstName.value, | ||
lastName = fields.lastName.value, | ||
recordChangeTag = recordChangeTag!! | ||
) | ||
|
||
fun app.softwork.composetodo.dto.User.New.toDAO() = User( | ||
recordName = username, | ||
|
||
fields = User.Fields( | ||
firstName = Value.String(firstName), | ||
lastName = Value.String(lastName), | ||
password = Value.String(password) | ||
), | ||
recordChangeTag = null | ||
) | ||
|
||
fun app.softwork.composetodo.dto.User.toDAO() = User( | ||
recordName = username, | ||
fields = User.Fields( | ||
firstName = Value.String(firstName), | ||
lastName = Value.String(lastName), | ||
password = null | ||
), | ||
recordChangeTag = recordChangeTag | ||
) |
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
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
12 changes: 5 additions & 7 deletions
12
backend/src/jvmMain/kotlin/app/softwork/composetodo/controller/AdminController.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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package app.softwork.composetodo.controller | ||
|
||
import app.softwork.composetodo.dao.User | ||
import app.softwork.composetodo.toDTO | ||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction | ||
import app.softwork.cloudkitclient.* | ||
import app.softwork.composetodo.* | ||
import app.softwork.composetodo.dao.* | ||
|
||
object AdminController { | ||
suspend fun allUsers() = newSuspendedTransaction { | ||
User.all().map { it.toDTO() } | ||
} | ||
class AdminController(private val db: Client.Database) { | ||
suspend fun allUsers() = db.query(User).map { it.toDTO() } | ||
} |
59 changes: 21 additions & 38 deletions
59
backend/src/jvmMain/kotlin/app/softwork/composetodo/controller/TodoController.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 |
---|---|---|
@@ -1,53 +1,36 @@ | ||
package app.softwork.composetodo.controller | ||
|
||
import app.softwork.cloudkitclient.* | ||
import app.softwork.composetodo.* | ||
import app.softwork.composetodo.dao.* | ||
import app.softwork.composetodo.definitions.* | ||
import kotlinx.datetime.* | ||
import kotlinx.uuid.* | ||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.transactions.experimental.* | ||
|
||
class TodoController(private val db: Database) { | ||
suspend fun todos(user: User) = newSuspendedTransaction(db = db) { | ||
user.todos.map { it.toDTO() } | ||
} | ||
class TodoController(private val db: Client.Database) { | ||
|
||
suspend fun create(user: User, newTodo: app.softwork.composetodo.dto.Todo) = newSuspendedTransaction(db = db) { | ||
Todo.new(newTodo.id) { | ||
this.user = user | ||
title = newTodo.title | ||
until = newTodo.until?.toJavaLocalDateTime() | ||
finished = newTodo.finished | ||
}.toDTO() | ||
} | ||
suspend fun todos(user: User) = db.query(Todo) { | ||
Todo.Fields::user eq user | ||
}.map { it.toDTO() } | ||
|
||
suspend fun getTodo(user: User, todoID: UUID) = newSuspendedTransaction(db = db) { | ||
Todo.find { | ||
Todos.id eq todoID and (Todos.user eq user.id) | ||
}.first().toDTO() | ||
suspend fun create(newTodo: Todo) = db.create(newTodo, Todo) | ||
|
||
suspend fun getTodo(user: User, todoID: UUID) = db.read(todoID.toString(), Todo)?.takeIf { | ||
it.fields.user.value.recordName == user.recordName | ||
} | ||
|
||
suspend fun delete(user: User, todoID: UUID) = newSuspendedTransaction(db = db) { | ||
Todo.find { | ||
Todos.id eq todoID and (Todos.user eq user.id) | ||
}.first().delete() | ||
suspend fun delete(user: User, todoID: UUID) = getTodo(user, todoID)?.let { | ||
db.delete(it, Todo) | ||
} | ||
|
||
suspend fun update(user: User, todoID: UUID, update: app.softwork.composetodo.dto.Todo) = | ||
newSuspendedTransaction(db = db) { | ||
Todo.find { | ||
Todos.id eq todoID and (Todos.user eq user.id) | ||
}.first().apply { | ||
title = update.title | ||
until = update.until?.toJavaLocalDateTime() | ||
finished = update.finished | ||
}.toDTO() | ||
} | ||
|
||
suspend fun deleteAll(user: User) = newSuspendedTransaction(db = db) { | ||
user.todos.forEach { | ||
it.delete() | ||
} | ||
getTodo(user, todoID)?.toDTO()?.copy( | ||
title = update.title, | ||
until = update.until, | ||
finished = update.finished | ||
)?.toDAO(user)?.let { db.update(it, Todo) } | ||
|
||
suspend fun deleteAll(user: User) = db.query(Todo) { | ||
Todo.Fields::user eq user | ||
}.forEach { | ||
db.delete(it, Todo) | ||
} | ||
} |
Oops, something went wrong.