Skip to content

Commit 5ecc6a1

Browse files
authored
Merge pull request #28 from navikt/comments_v2
Add delete and modify for comments.
2 parents de302aa + 2c07aee commit 5ecc6a1

File tree

6 files changed

+254
-93
lines changed

6 files changed

+254
-93
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package no.nav.klage.document.api
2+
3+
import io.swagger.v3.oas.annotations.Operation
4+
import io.swagger.v3.oas.annotations.tags.Tag
5+
import no.nav.klage.document.api.views.CommentInput
6+
import no.nav.klage.document.api.views.CommentView
7+
import no.nav.klage.document.api.views.ModifyCommentInput
8+
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
9+
import no.nav.klage.document.domain.Comment
10+
import no.nav.klage.document.service.CommentService
11+
import no.nav.klage.document.util.getLogger
12+
import no.nav.klage.document.util.getSecureLogger
13+
import no.nav.security.token.support.core.api.ProtectedWithClaims
14+
import no.nav.security.token.support.core.context.TokenValidationContextHolder
15+
import org.springframework.web.bind.annotation.*
16+
import java.util.*
17+
18+
@RestController
19+
@ProtectedWithClaims(issuer = ISSUER_AAD)
20+
@Tag(name = "kabal-smart-editor-api")
21+
@RequestMapping("/documents/{documentId}/comments")
22+
class CommentsController(
23+
private val commentService: CommentService,
24+
private val tokenValidationContextHolder: TokenValidationContextHolder
25+
) {
26+
27+
companion object {
28+
@Suppress("JAVA_CLASS_ON_COMPANION")
29+
private val logger = getLogger(javaClass.enclosingClass)
30+
private val secureLogger = getSecureLogger()
31+
}
32+
33+
@Operation(
34+
summary = "Create comment for a given document",
35+
description = "Create comment for a given document"
36+
)
37+
@PostMapping
38+
fun createComment(
39+
@PathVariable("documentId") documentId: UUID,
40+
@RequestBody commentInput: CommentInput
41+
): CommentView {
42+
log("createComment called with id $documentId")
43+
return mapCommentToView(
44+
commentService.createComment(
45+
documentId = documentId,
46+
text = commentInput.text,
47+
authorName = commentInput.author.name,
48+
authorIdent = commentInput.author.ident
49+
)
50+
)
51+
}
52+
53+
@Operation(
54+
summary = "Get all comments for a given document",
55+
description = "Get all comments for a given document"
56+
)
57+
@GetMapping
58+
fun getAllCommentsWithPossibleThreads(
59+
@PathVariable("documentId") documentId: UUID
60+
): List<CommentView> {
61+
log("getAllCommentsWithPossibleThreads called with id $documentId")
62+
return commentService.getComments(documentId).map { mapCommentToView(it) }
63+
}
64+
65+
@Operation(
66+
summary = "Reply to a given comment",
67+
description = "Reply to a given comment"
68+
)
69+
@PostMapping("/{commentId}/replies")
70+
fun replyToComment(
71+
@PathVariable("documentId") documentId: UUID,
72+
@PathVariable("commentId") commentId: UUID,
73+
@RequestBody commentInput: CommentInput,
74+
): CommentView {
75+
log("replyToComment called with id $documentId and commentId $commentId")
76+
return mapCommentToView(
77+
commentService.replyToComment(
78+
documentId = documentId,
79+
parentCommentId = commentId,
80+
text = commentInput.text,
81+
authorName = commentInput.author.name,
82+
authorIdent = commentInput.author.ident
83+
)
84+
)
85+
}
86+
87+
@Operation(
88+
summary = "Modify a given comment",
89+
description = "Modify a given comment"
90+
)
91+
@PatchMapping("/{commentId}")
92+
fun modifyComment(
93+
@PathVariable("documentId") documentId: UUID,
94+
@PathVariable("commentId") commentId: UUID,
95+
@RequestBody modifyCommentInput: ModifyCommentInput,
96+
): CommentView {
97+
log("modifyComment called with id $documentId and commentId $commentId")
98+
return mapCommentToView(
99+
commentService.setCommentText(
100+
commentId = commentId,
101+
text = modifyCommentInput.text,
102+
loggedInIdent = getIdent()!!
103+
)
104+
)
105+
}
106+
107+
@Operation(
108+
summary = "Get a given comment",
109+
description = "Get a given comment"
110+
)
111+
@GetMapping("/{commentId}")
112+
fun getCommentWithPossibleThread(
113+
@PathVariable("documentId") documentId: UUID,
114+
@PathVariable("commentId") commentId: UUID
115+
): CommentView {
116+
log("getCommentWithPossibleThread called with id $documentId and commentId $commentId")
117+
return mapCommentToView(commentService.getComment(commentId = commentId))
118+
}
119+
120+
@Operation(
121+
summary = "Delete a given comment (includes possible thread)",
122+
description = "Delete a given comment (includes possible thread)"
123+
)
124+
@DeleteMapping("/{commentId}")
125+
fun deleteCommentWithPossibleThread(
126+
@PathVariable("documentId") documentId: UUID,
127+
@PathVariable("commentId") commentId: UUID
128+
) {
129+
log("deleteCommentWithPossibleThread called with id $documentId and commentId $commentId")
130+
commentService.deleteComment(commentId = commentId, loggedInIdent = getIdent()!!)
131+
}
132+
133+
private fun mapCommentToView(comment: Comment): CommentView =
134+
CommentView(
135+
id = comment.id,
136+
text = comment.text,
137+
author = CommentView.Author(
138+
name = comment.authorName,
139+
ident = comment.authorIdent
140+
),
141+
comments = comment.comments.map { mapCommentToView(it) },
142+
created = comment.created,
143+
modified = comment.modified
144+
)
145+
146+
private fun log(message: String) {
147+
logger.debug(message)
148+
secureLogger.debug("{}. On-behalf-of: {}", message, getIdent())
149+
}
150+
151+
fun getIdent(): String? =
152+
tokenValidationContextHolder.tokenValidationContext.getJwtToken(ISSUER_AAD)
153+
.jwtTokenClaims?.get("NAVident")?.toString()
154+
155+
}

src/main/kotlin/no/nav/klage/document/api/DocumentController.kt

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package no.nav.klage.document.api
22

33
import io.swagger.v3.oas.annotations.Operation
44
import io.swagger.v3.oas.annotations.tags.Tag
5-
import no.nav.klage.document.api.views.CommentInput
6-
import no.nav.klage.document.api.views.CommentView
75
import no.nav.klage.document.api.views.DocumentView
86
import no.nav.klage.document.config.SecurityConfiguration.Companion.ISSUER_AAD
9-
import no.nav.klage.document.domain.Comment
107
import no.nav.klage.document.domain.Document
11-
import no.nav.klage.document.service.CommentService
128
import no.nav.klage.document.service.DocumentService
139
import no.nav.klage.document.util.getLogger
1410
import no.nav.klage.document.util.getSecureLogger
@@ -27,7 +23,6 @@ import java.util.*
2723
@RequestMapping("/documents")
2824
class DocumentController(
2925
private val documentService: DocumentService,
30-
private val commentService: CommentService,
3126
private val tokenValidationContextHolder: TokenValidationContextHolder
3227
) {
3328

@@ -84,73 +79,6 @@ class DocumentController(
8479
documentService.deleteDocument(documentId)
8580
}
8681

87-
@Operation(
88-
summary = "Create comment for a given document",
89-
description = "Create comment for a given document"
90-
)
91-
@PostMapping("/{documentId}/comments")
92-
fun createComment(
93-
@PathVariable("documentId") documentId: UUID,
94-
@RequestBody commentInput: CommentInput
95-
): CommentView {
96-
log("createComment called with id $documentId")
97-
return mapCommentToView(
98-
commentService.createComment(
99-
documentId = documentId,
100-
text = commentInput.text,
101-
authorName = commentInput.author.name,
102-
authorIdent = commentInput.author.ident
103-
)
104-
)
105-
}
106-
107-
@Operation(
108-
summary = "Get all comments for a given document",
109-
description = "Get all comments for a given document"
110-
)
111-
@GetMapping("/{documentId}/comments")
112-
fun getAllCommentsWithPossibleThreads(
113-
@PathVariable("documentId") documentId: UUID
114-
): List<CommentView> {
115-
log("getAllCommentsWithPossibleThreads called with id $documentId")
116-
return commentService.getComments(documentId).map { mapCommentToView(it) }
117-
}
118-
119-
@Operation(
120-
summary = "Reply to a given comment",
121-
description = "Reply to a given comment"
122-
)
123-
@PostMapping("/{documentId}/comments/{commentId}/replies")
124-
fun replyToComment(
125-
@PathVariable("documentId") documentId: UUID,
126-
@PathVariable("commentId") commentId: UUID,
127-
@RequestBody commentInput: CommentInput,
128-
): CommentView {
129-
log("replyToComment called with id $documentId and commentId $commentId")
130-
return mapCommentToView(
131-
commentService.replyToComment(
132-
documentId = documentId,
133-
parentCommentId = commentId,
134-
text = commentInput.text,
135-
authorName = commentInput.author.name,
136-
authorIdent = commentInput.author.ident
137-
)
138-
)
139-
}
140-
141-
@Operation(
142-
summary = "Get a given comment",
143-
description = "Get a given comment"
144-
)
145-
@GetMapping("/{documentId}/comments/{commentId}")
146-
fun getCommentWithPossibleThread(
147-
@PathVariable("documentId") documentId: UUID,
148-
@PathVariable("commentId") commentId: UUID
149-
): CommentView {
150-
log("getCommentWithPossibleThread called with id $documentId and commentId $commentId")
151-
return mapCommentToView(commentService.getComment(commentId = commentId))
152-
}
153-
15482
@Operation(
15583
summary = "Generer PDF",
15684
description = "Generer PDF"
@@ -182,19 +110,6 @@ class DocumentController(
182110
modified = document.modified
183111
)
184112

185-
private fun mapCommentToView(comment: Comment): CommentView =
186-
CommentView(
187-
id = comment.id,
188-
text = comment.text,
189-
author = CommentView.Author(
190-
name = comment.authorName,
191-
ident = comment.authorIdent
192-
),
193-
comments = comment.comments.map { mapCommentToView(it) },
194-
created = comment.created,
195-
modified = comment.modified
196-
)
197-
198113
private fun log(message: String) {
199114
logger.debug(message)
200115
secureLogger.debug("{}. On-behalf-of: {}", message, getIdent())

src/main/kotlin/no/nav/klage/document/api/views/CommentInput.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ data class CommentInput(
99
val ident: String
1010
)
1111
}
12+
13+
data class ModifyCommentInput(
14+
val text: String,
15+
)

src/main/kotlin/no/nav/klage/document/service/CommentService.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,25 @@ class CommentService(private val commentRepository: CommentRepository) {
5151
}
5252

5353
fun getComment(commentId: UUID): Comment {
54-
return commentRepository.getById(commentId)
54+
return commentRepository.getReferenceById(commentId)
55+
}
56+
57+
fun setCommentText(commentId: UUID, text: String, loggedInIdent: String): Comment {
58+
val comment = commentRepository.getReferenceById(commentId)
59+
if (comment.authorIdent != loggedInIdent) {
60+
throw RuntimeException("Not allowed to modify others comment")
61+
}
62+
comment.text = text
63+
comment.modified = LocalDateTime.now()
64+
return comment
65+
}
66+
67+
fun deleteComment(commentId: UUID, loggedInIdent: String) {
68+
val comment = commentRepository.getReferenceById(commentId)
69+
if (comment.authorIdent != loggedInIdent) {
70+
throw RuntimeException("Not allowed to delete others comment")
71+
}
72+
commentRepository.delete(comment)
5573
}
5674

5775
}

src/main/kotlin/no/nav/klage/document/service/DocumentService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ class DocumentService(
3030
}
3131

3232
fun updateDocument(documentId: UUID, json: String): Document {
33-
val document = documentRepository.getById(documentId)
33+
val document = documentRepository.getReferenceById(documentId)
3434
document.json = json
3535
document.modified = LocalDateTime.now()
3636
return document
3737
}
3838

3939
fun getDocument(documentId: UUID): Document {
40-
return documentRepository.getById(documentId)
40+
return documentRepository.getReferenceById(documentId)
4141
}
4242

4343
fun getDocumentAsPDF(documentId: UUID): PDFDocument {
44-
return kabalJsonToPdfClient.getPDFDocument(documentRepository.getById(documentId).json)
44+
return kabalJsonToPdfClient.getPDFDocument(documentRepository.getReferenceById(documentId).json)
4545
}
4646

4747
fun deleteDocument(documentId: UUID) {

0 commit comments

Comments
 (0)