Add possibility to share notes

This commit is contained in:
2020-08-25 06:18:18 +02:00
parent 1cb6c731d8
commit c5f9a1d6e0
11 changed files with 128 additions and 21 deletions
@@ -121,7 +121,8 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
markdown = note.markdown,
html = note.html,
updatedAt = now,
uuid = uuid
uuid = uuid,
public = false, // TODO
)
}
}
@@ -199,6 +200,32 @@ internal class NoteRepositoryImpl(private val db: Database) : NoteRepository {
}
}
override fun makePublic(userId: Int, uuid: UUID) = db.update(Notes) {
it.public to true
where { Notes.userId eq userId and (Notes.uuid eq uuid) and (it.deleted eq false) }
} == 1
override fun makePrivate(userId: Int, uuid: UUID) = db.update(Notes) {
it.public to false
where { Notes.userId eq userId and (Notes.uuid eq uuid) and (it.deleted eq false) }
} == 1
override fun findPublic(uuid: UUID): PersistedNote? {
val note = db.notes
.filterColumns { it.columns - it.userId }
.filter { it.uuid eq uuid }
.filter { it.public eq true }
.find { it.deleted eq false }
?: return null
val tags = db.from(Tags)
.select(Tags.name)
.where { Tags.noteUuid eq uuid }
.map { it[Tags.name]!! }
return note.toPersistedNote(tags)
}
private fun List<NoteEntity>.tagsByUuid(): Map<UUID, List<String>> {
return if (isEmpty()) emptyMap()
else db.tags
+4 -1
View File
@@ -27,6 +27,7 @@ internal open class Notes(alias: String?) : Table<NoteEntity>("Notes", alias) {
val userId = int("user_id").references(Users) { it.user }
val updatedAt = datetime("updated_at").bindTo { it.updatedAt }
val deleted = boolean("deleted").bindTo { it.deleted }
val public = boolean("public").bindTo { it.public }
val user get() = userId.referenceTable as Users
}
@@ -41,6 +42,7 @@ internal interface NoteEntity : Entity<NoteEntity> {
var html: String
var updatedAt: LocalDateTime
var deleted: Boolean
var public: Boolean
var user: UserEntity
}
@@ -48,7 +50,7 @@ internal interface NoteEntity : Entity<NoteEntity> {
internal fun NoteEntity.toPersistedMetadata(tags: List<String>) = PersistedNoteMetadata(title, tags, updatedAt, uuid)
internal fun NoteEntity.toPersistedNote(tags: List<String>) =
PersistedNote(NoteMetadata(title, tags), markdown, html, updatedAt, uuid)
PersistedNote(NoteMetadata(title, tags), markdown, html, updatedAt, uuid, public)
internal fun Note.toEntity(uuid: UUID, userId: Int): NoteEntity {
val note = this
@@ -58,6 +60,7 @@ internal fun Note.toEntity(uuid: UUID, userId: Int): NoteEntity {
this.html = note.html
this.uuid = uuid
this.deleted = false
this.public = false
this.user["id"] = userId
}
}
@@ -0,0 +1,2 @@
alter table Notes
add column public bool not null default false
@@ -0,0 +1,2 @@
alter table Notes
add column public bool not null default false