package com.betriebsratkanzlei.legalconsenthub.comment import com.betriebsratkanzlei.legalconsenthub_api.api.CommentApi import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormCommentCountsDto import com.betriebsratkanzlei.legalconsenthub_api.model.CommentDto import com.betriebsratkanzlei.legalconsenthub_api.model.CreateCommentDto import com.betriebsratkanzlei.legalconsenthub_api.model.CursorPagedCommentDto import org.springframework.http.ResponseEntity import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.RestController import java.time.Instant import java.util.UUID @RestController class CommentController( val commentService: CommentService, val commentMapper: CommentMapper, ) : CommentApi { @PreAuthorize( "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')", ) override fun createComment( applicationFormId: UUID, formElementId: UUID, createCommentDto: CreateCommentDto, ): ResponseEntity = ResponseEntity.ok( commentMapper.toCommentDto( commentService.createComment(applicationFormId, formElementId, createCommentDto), ), ) @PreAuthorize( "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')", ) override fun getGroupedCommentCountByApplicationFromId( applicationFormId: UUID, ): ResponseEntity { val counts = commentService.getGroupedCommentCountByApplicationFromId(applicationFormId) return ResponseEntity.ok( ApplicationFormCommentCountsDto( counts = counts.mapKeys { it.key.toString() }, ), ) } @PreAuthorize( "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')", ) override fun getCommentsByApplicationFormId( applicationFormId: UUID, formElementId: UUID?, cursorCreatedAt: Instant?, limit: Int, ): ResponseEntity { val page = commentService.getComments(applicationFormId, formElementId, cursorCreatedAt, limit) return ResponseEntity.ok( CursorPagedCommentDto( content = page.comments.map { commentMapper.toCommentDto(it) }, nextCursorCreatedAt = page.nextCursorCreatedAt, hasMore = page.hasMore, ), ) } @PreAuthorize( "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')", ) override fun updateComment( id: UUID, commentDto: CommentDto, ): ResponseEntity = ResponseEntity.ok( commentMapper.toCommentDto( commentService.updateComment(id, commentDto), ), ) @PreAuthorize( "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')", ) override fun deleteComment(id: UUID): ResponseEntity { commentService.deleteCommentByID(id) return ResponseEntity.noContent().build() } }