1
1
package com.github.nenadjakic.eav.controller
2
2
3
+ import com.github.nenadjakic.eav.dto.AttributeValueAddRequest
3
4
import com.github.nenadjakic.eav.dto.AttributeValueResponse
5
+ import com.github.nenadjakic.eav.entity.AttributeValue
4
6
import com.github.nenadjakic.eav.extension.collectionMap
5
7
import com.github.nenadjakic.eav.service.AttributeValueService
8
+ import io.swagger.v3.oas.annotations.Operation
9
+ import io.swagger.v3.oas.annotations.responses.ApiResponse
10
+ import io.swagger.v3.oas.annotations.responses.ApiResponses
6
11
import io.swagger.v3.oas.annotations.tags.Tag
12
+ import jakarta.validation.Valid
7
13
import org.modelmapper.ModelMapper
8
14
import org.springframework.http.ResponseEntity
15
+ import org.springframework.validation.annotation.Validated
16
+ import org.springframework.web.bind.annotation.DeleteMapping
9
17
import org.springframework.web.bind.annotation.GetMapping
10
18
import org.springframework.web.bind.annotation.PathVariable
19
+ import org.springframework.web.bind.annotation.PostMapping
20
+ import org.springframework.web.bind.annotation.PutMapping
21
+ import org.springframework.web.bind.annotation.RequestBody
11
22
import org.springframework.web.bind.annotation.RequestMapping
12
23
import org.springframework.web.bind.annotation.RestController
24
+ import org.springframework.web.servlet.support.ServletUriComponentsBuilder
13
25
14
26
@Tag(name = " Attribute-value controller" , description = " API endpoints for managing values of attributes." )
15
27
@RestController
16
28
@RequestMapping(" /attribute-value" )
29
+ @Validated
17
30
open class AttributeValueController (
18
31
val modelMapper : ModelMapper ,
19
32
val attributeValueService : AttributeValueService
20
33
) {
21
34
35
+ @Operation(
36
+ operationId = " findAttributeValueById" ,
37
+ summary = " Get attribute-value by id." ,
38
+ description = " Returns an attribute-value with the specified id (entityId-attributeId)."
39
+ )
40
+ @ApiResponses(
41
+ value = [
42
+ ApiResponse (responseCode = " 200" , description = " Successfully retrieved attribute-value." ),
43
+ ApiResponse (responseCode = " 404" , description = " Attribute-value not found." )
44
+ ]
45
+ )
46
+ @GetMapping(" /{entityId}-{attributeId}" )
47
+ open fun findById (@PathVariable entityId : Long , @PathVariable attributeId : Long ): ResponseEntity <AttributeValueResponse > {
48
+ val attributeValue = attributeValueService.findById(entityId, attributeId)
49
+ val response = attributeValue?.let { modelMapper.map(attributeValue, AttributeValueResponse ::class .java) }
50
+
51
+ return ResponseEntity .ofNullable(response)
52
+ }
53
+
54
+ @Operation(
55
+ operationId = " findAttributeValueByEntityId" ,
56
+ summary = " Get all attribute-value by entitiyId." ,
57
+ description = " Returns all attribute-values with the specified entityId."
58
+ )
59
+ @ApiResponses(
60
+ value = [
61
+ ApiResponse (responseCode = " 200" , description = " Successfully retrieved attribute-value." ),
62
+ ApiResponse (responseCode = " 404" , description = " Attribute-value not found." )
63
+ ]
64
+ )
22
65
@GetMapping(" entity/{entityId}" )
23
66
open fun findByEntityId (@PathVariable entityId : Long ): ResponseEntity <List <AttributeValueResponse >> {
24
67
val attributeValues = attributeValueService.findByEntityId(entityId)
25
68
val response = modelMapper.collectionMap(attributeValues, AttributeValueResponse ::class .java)
26
69
return ResponseEntity .ok(response)
27
70
}
28
71
72
+ @Operation(
73
+ operationId = " createAttributeValue" ,
74
+ summary = " Create attribute-vale." ,
75
+ description = " Creates a new attribute-value based on the provided model."
76
+ )
77
+ @ApiResponses(
78
+ value = [
79
+ ApiResponse (responseCode = " 201" , description = " Attribute-value created successfully." ),
80
+ ApiResponse (responseCode = " 400" , description = " Invalid request data." )
81
+ ]
82
+ )
83
+ @PostMapping
84
+ open fun create (@RequestBody @Valid attributeValueAddRequest : AttributeValueAddRequest ): ResponseEntity <Void > {
85
+ val attributeValue = modelMapper.map(attributeValueAddRequest, AttributeValue ::class .java)
86
+ val createdAttributeValue = attributeValueService.create(attributeValue)
87
+
88
+ val location = ServletUriComponentsBuilder
89
+ .fromCurrentRequest()
90
+ .path(" /{entityId}-{attributeId}" )
91
+ .buildAndExpand(createdAttributeValue.entity.id, createdAttributeValue.attribute.id)
92
+ .toUri()
93
+
94
+ return ResponseEntity .created(location).build()
95
+ }
96
+
97
+ @Operation(
98
+ operationId = " updateAttributeValue" ,
99
+ summary = " Update attribute-value." ,
100
+ description = " Updates an existing attribute-value based on the provided model."
101
+ )
102
+ @ApiResponses(
103
+ value = [
104
+ ApiResponse (responseCode = " 204" , description = " Attribute-value updated successfully." ),
105
+ ApiResponse (responseCode = " 400" , description = " Invalid request data." )
106
+ ]
107
+ )
108
+ @PutMapping
109
+ open fun update (@RequestBody @Valid attributeValueAddRequest : AttributeValueAddRequest ): ResponseEntity <Void > {
110
+ val attributeValue = modelMapper.map(attributeValueAddRequest, AttributeValue ::class .java)
111
+ attributeValueService.update(attributeValue)
112
+ return ResponseEntity .noContent().build()
113
+ }
114
+
115
+ @Operation(
116
+ operationId = " deleteAttributeValueById" ,
117
+ summary = " Delete attribute-value by id." ,
118
+ description = " Deletes an attribute-value with the specified id."
119
+ )
120
+ @ApiResponses(
121
+ value = [
122
+ ApiResponse (responseCode = " 204" , description = " Attribute-value deleted successfully" )
123
+ ]
124
+ )
125
+ @DeleteMapping
126
+ open fun deleteById (@PathVariable entityId : Long , @PathVariable attributeId : Long ): ResponseEntity <Void > {
127
+ attributeValueService.deleteById(entityId, attributeId)
128
+ return ResponseEntity .noContent().build()
129
+ }
29
130
}
0 commit comments