Merge branch 'feature/db-delete-cascade'
This commit is contained in:
commit
94b20b98ad
@ -0,0 +1,19 @@
|
||||
-- ON DELETE -> CASCADE
|
||||
|
||||
ALTER TABLE `Notes`
|
||||
DROP CONSTRAINT `Notes_ibfk_1`;
|
||||
|
||||
ALTER TABLE `Notes`
|
||||
ADD FOREIGN KEY (`user_id`) REFERENCES `Users` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
|
||||
|
||||
ALTER TABLE `Chapters`
|
||||
DROP CONSTRAINT `Chapters_ibfk_1`;
|
||||
|
||||
ALTER TABLE `Chapters`
|
||||
ADD FOREIGN KEY (`note_id`) REFERENCES `Notes` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
|
||||
|
||||
ALTER TABLE `Tags`
|
||||
DROP CONSTRAINT `Tags_ibfk_1`;
|
||||
|
||||
ALTER TABLE `Tags`
|
||||
ADD FOREIGN KEY (`note_id`) REFERENCES `Notes` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
|
||||
@ -109,8 +109,6 @@ class NotesService(override val kodein: Kodein) : KodeinAware {
|
||||
|
||||
fun deleteNote(noteId: Int): Unit =
|
||||
db.useTransaction {
|
||||
db.delete(Tags) { it.noteId eq noteId }
|
||||
db.delete(Chapters) { it.noteId eq noteId }
|
||||
db.delete(Notes) { it.id eq noteId }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package be.vandewalleh.services
|
||||
|
||||
import be.vandewalleh.entities.User
|
||||
import be.vandewalleh.tables.Notes
|
||||
import be.vandewalleh.tables.Users
|
||||
import me.liuwj.ktorm.database.*
|
||||
import me.liuwj.ktorm.dsl.*
|
||||
@ -16,7 +15,6 @@ import java.time.LocalDateTime
|
||||
*/
|
||||
class UserService(override val kodein: Kodein) : KodeinAware {
|
||||
private val db by instance<Database>()
|
||||
private val notesService by instance<NotesService>()
|
||||
|
||||
/**
|
||||
* returns a user ID if present or null
|
||||
@ -69,19 +67,9 @@ class UserService(override val kodein: Kodein) : KodeinAware {
|
||||
|
||||
fun deleteUser(userId: Int) {
|
||||
db.useTransaction {
|
||||
val notesId = db.from(Notes)
|
||||
.select(Notes.id)
|
||||
.where { Notes.userId eq userId }
|
||||
.map { it[Notes.userId]!! }
|
||||
|
||||
notesId.forEach { noteId ->
|
||||
notesService.deleteNote(noteId)
|
||||
}
|
||||
db.delete(Users) { it.id eq userId }
|
||||
}
|
||||
|
||||
db.delete(Users) { it.id eq userId }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
data class UserRegistrationDto(val username: String, val email: String, val password: String)
|
||||
Loading…
x
Reference in New Issue
Block a user