feat(backend): Add applicationFormTemplate, add isTemplate flag, refactoring

This commit is contained in:
2025-03-08 17:35:09 +01:00
parent 40e295937d
commit 59a8c5d900
12 changed files with 262 additions and 10 deletions

View File

@@ -124,6 +124,119 @@ paths:
"503":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable"
/application-form-templates:
get:
summary: Get all ApplicationFormTemplates
operationId: getAllApplicationFormTemplates
tags:
- application-form-template
responses:
"200":
description: Paged list of application form templates
content:
application/json:
schema:
$ref: "#/components/schemas/PagedApplicationFormDto"
"500":
description: Internal server error
post:
summary: Create a new ApplicationFormTemplate
operationId: createApplicationFormTemplate
tags:
- application-form-template
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/CreateApplicationFormDto"
responses:
"201":
description: Successfully created application form template
content:
application/json:
schema:
$ref: "#/components/schemas/ApplicationFormDto"
"400":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/BadRequest"
"401":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/Unauthorized"
"500":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServerError"
"503":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable"
/application-form-templates/{id}:
parameters:
- name: id
in: path
required: true
schema:
type: string
format: uuid
get:
summary: Get a specific ApplicationFormTemplate
operationId: getApplicationFormTemplateById
tags:
- application-form-template
responses:
"200":
description: Get application form template by ID
content:
application/json:
schema:
$ref: "#/components/schemas/ApplicationFormDto"
"400":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/BadRequest"
"401":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/Unauthorized"
"500":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServerError"
"503":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable"
put:
summary: Updates a ApplicationFormTemplate
operationId: updateApplicationFormTemplate
tags:
- application-form-template
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ApplicationFormDto"
responses:
"200":
description: Successfully updated application form template
content:
application/json:
schema:
$ref: "#/components/schemas/ApplicationFormDto"
"400":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/BadRequest"
"401":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/Unauthorized"
"500":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServerError"
"503":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable"
delete:
summary: Delete a ApplicationFormTemplate
operationId: deleteApplicationFormTemplate
tags:
- application-form-template
responses:
"204":
description: Application Form Template successfully deleted
"400":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/BadRequest"
"401":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/Unauthorized"
"500":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServerError"
"503":
$ref: "https://api.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0#/components/responses/ServiceUnavailable"
/users:
get:
summary: Get all users
@@ -563,6 +676,7 @@ components:
required:
- id
- formElements
- isTemplate
- createdAt
- modifiedAt
properties:
@@ -573,6 +687,8 @@ components:
type: array
items:
$ref: "#/components/schemas/FormElementDto"
isTemplate:
type: boolean
createdAt:
type: string
format: date-time
@@ -583,12 +699,16 @@ components:
CreateApplicationFormDto:
required:
- formElements
- isTemplate
type: object
properties:
formElements:
type: array
items:
$ref: "#/components/schemas/CreateFormElementDto"
isTemplate:
type: boolean
default: false
PagedApplicationFormDto:
type: object

View File

@@ -1,6 +1,6 @@
package com.betriebsratkanzlei.legalconsenthub.application_form
import com.betriebsratkanzlei.legalconsenthub.formelement.FormElement
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElement
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
@@ -12,8 +12,6 @@ import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.util.Date
import java.util.UUID
@Entity
@@ -26,6 +24,9 @@ class ApplicationForm(
@OneToMany(mappedBy = "applicationForm", cascade = [CascadeType.ALL], orphanRemoval = true)
var formElements: MutableList<FormElement> = mutableListOf(),
@Column(nullable = false)
var isTemplate: Boolean,
@CreatedDate
@Column(nullable = false)
var createdAt: LocalDateTime? = null,

View File

@@ -11,6 +11,7 @@ class ApplicationFormMapper(private val formElementMapper: FormElementMapper) {
return ApplicationFormDto(
id = applicationForm.id ?: throw IllegalStateException("ApplicationForm ID must not be null!"),
formElements = applicationForm.formElements.map { formElementMapper.toFormElementDto(it) },
isTemplate = applicationForm.isTemplate,
createdAt = applicationForm.createdAt ?: LocalDateTime.now(),
modifiedAt = applicationForm.modifiedAt ?: LocalDateTime.now()
)
@@ -20,13 +21,14 @@ class ApplicationFormMapper(private val formElementMapper: FormElementMapper) {
return ApplicationForm(
id = applicationForm.id,
formElements = applicationForm.formElements.map { formElementMapper.toFormElement(it) }.toMutableList(),
isTemplate = applicationForm.isTemplate,
createdAt = applicationForm.createdAt,
modifiedAt = applicationForm.modifiedAt
)
}
fun toApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm {
val applicationForm = ApplicationForm()
val applicationForm = ApplicationForm(isTemplate = createApplicationFormDto.isTemplate)
applicationForm.formElements = createApplicationFormDto.formElements
.map { formElementMapper.toFormElement(it, applicationForm) }
.toMutableList()

View File

@@ -1,8 +1,13 @@
package com.betriebsratkanzlei.legalconsenthub.application_form
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import java.util.UUID
@Repository
interface ApplicationFormRepository : JpaRepository<ApplicationForm, UUID>
interface ApplicationFormRepository : JpaRepository<ApplicationForm, UUID> {
fun findAllByIsTemplateTrue(page: Pageable): Page<ApplicationForm>
fun findAllByIsTemplateFalse(page: Pageable): Page<ApplicationForm>
}

View File

@@ -35,7 +35,7 @@ class ApplicationFormService(
fun getApplicationForms(): Page<ApplicationForm> {
val pageable = PageRequest.of(0, 10)
return applicationFormRepository.findAll(pageable)
return applicationFormRepository.findAllByIsTemplateFalse(pageable)
}
fun updateApplicationForm(applicationFormDto: ApplicationFormDto): ApplicationForm {

View File

@@ -1,7 +1,7 @@
package com.betriebsratkanzlei.legalconsenthub.application_form
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
import com.betriebsratkanzlei.legalconsenthub.formelement.FormElement
import com.betriebsratkanzlei.legalconsenthub.form_element.FormElement
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateFormElementDto
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementDto
import org.springframework.stereotype.Component

View File

@@ -1,6 +1,6 @@
package com.betriebsratkanzlei.legalconsenthub.application_form
import com.betriebsratkanzlei.legalconsenthub.formelement.FormOption
import com.betriebsratkanzlei.legalconsenthub.form_element.FormOption
import com.betriebsratkanzlei.legalconsenthub_api.model.FormOptionDto
import org.springframework.stereotype.Component

View File

@@ -0,0 +1,59 @@
package com.betriebsratkanzlei.legalconsenthub.application_form_template
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationFormMapper
import com.betriebsratkanzlei.legalconsenthub.application_form.PagedApplicationFormMapper
import com.betriebsratkanzlei.legalconsenthub_api.api.ApplicationFormTemplateApi
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.PagedApplicationFormDto
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
import java.util.UUID
@RestController
class ApplicationFormTemplateController(
val applicationFormTemplateService: ApplicationFormTemplateService,
val pagedApplicationFormMapper: PagedApplicationFormMapper,
val applicationFormMapper: ApplicationFormMapper,
) : ApplicationFormTemplateApi {
override fun createApplicationFormTemplate(createApplicationFormDto: CreateApplicationFormDto): ResponseEntity<ApplicationFormDto> {
return ResponseEntity.ok(
applicationFormMapper.toApplicationFormDto(
applicationFormTemplateService.createApplicationFormTemplate(createApplicationFormDto)
)
)
}
override fun getAllApplicationFormTemplates(): ResponseEntity<PagedApplicationFormDto> {
return ResponseEntity.ok(
pagedApplicationFormMapper.toPagedApplicationFormDto(
applicationFormTemplateService.getApplicationFormTemplates()
)
)
}
override fun getApplicationFormTemplateById(id: UUID): ResponseEntity<ApplicationFormDto> {
return ResponseEntity.ok(
applicationFormMapper.toApplicationFormDto(
applicationFormTemplateService.getApplicationFormTemplateById(id)
)
)
}
override fun updateApplicationFormTemplate(
id: UUID,
applicationFormDto: ApplicationFormDto
): ResponseEntity<ApplicationFormDto> {
return ResponseEntity.ok(
applicationFormMapper.toApplicationFormDto(
applicationFormTemplateService.updateApplicationFormTemplate(applicationFormDto)
)
)
}
override fun deleteApplicationFormTemplate(id: UUID): ResponseEntity<Unit> {
applicationFormTemplateService.deleteApplicationFormTemplateByID(id)
return ResponseEntity.noContent().build()
}
}

View File

@@ -0,0 +1,64 @@
package com.betriebsratkanzlei.legalconsenthub.application_form_template
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationForm
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationFormMapper
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationFormRepository
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotCreatedException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotDeletedException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotFoundException
import com.betriebsratkanzlei.legalconsenthub.error.ApplicationFormNotUpdatedException
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service
import java.util.UUID
@Service
class ApplicationFormTemplateService(
private val applicationFormRepository: ApplicationFormRepository,
private val applicationFormMapper: ApplicationFormMapper
) {
fun createApplicationFormTemplate(createApplicationFormDto: CreateApplicationFormDto): ApplicationForm {
val applicationForm = applicationFormMapper.toApplicationForm(createApplicationFormDto)
val savedApplicationForm: ApplicationForm
try {
savedApplicationForm = applicationFormRepository.save(applicationForm)
} catch (e: Exception) {
throw ApplicationFormNotCreatedException(e)
}
return savedApplicationForm
}
fun getApplicationFormTemplateById(id: UUID): ApplicationForm {
return applicationFormRepository.findById(id).orElseThrow { ApplicationFormNotFoundException(id) }
}
fun getApplicationFormTemplates(): Page<ApplicationForm> {
val pageable = PageRequest.of(0, 10)
return applicationFormRepository.findAllByIsTemplateTrue(pageable)
}
fun updateApplicationFormTemplate(applicationFormDto: ApplicationFormDto): ApplicationForm {
val applicationForm = applicationFormMapper.toApplicationForm(applicationFormDto)
val updatedApplicationForm: ApplicationForm
try {
updatedApplicationForm = applicationFormRepository.save(applicationForm)
} catch (e: Exception) {
throw ApplicationFormNotUpdatedException(e, applicationFormDto.id)
}
return updatedApplicationForm
}
fun deleteApplicationFormTemplateByID(id: UUID) {
try {
applicationFormRepository.deleteById(id)
} catch (e: Exception) {
throw ApplicationFormNotDeletedException(e)
}
}
}

View File

@@ -1,4 +1,4 @@
package com.betriebsratkanzlei.legalconsenthub.formelement;
package com.betriebsratkanzlei.legalconsenthub.form_element;
import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationForm
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementType

View File

@@ -1,4 +1,4 @@
package com.betriebsratkanzlei.legalconsenthub.formelement;
package com.betriebsratkanzlei.legalconsenthub.form_element;
import com.betriebsratkanzlei.legalconsenthub_api.model.EmployeeDataCategory
import com.betriebsratkanzlei.legalconsenthub_api.model.ProcessingPurpose;

View File

@@ -1,5 +1,6 @@
create table application_form
(
is_template boolean not null,
created_at timestamp(6) not null,
modified_at timestamp(6) not null,
id uuid not null,