|
1 | 1 | package com.github.nenadjakic.eav.controller
|
2 | 2 |
|
3 |
| -import collectionMap |
4 |
| -import com.github.nenadjakic.eav.util.RestUtil |
5 |
| -import com.github.nenadjakic.eav.dto.AttributeAddResponse |
6 | 3 | import com.github.nenadjakic.eav.dto.AttributeAddRequest
|
| 4 | +import com.github.nenadjakic.eav.dto.AttributeResponse |
7 | 5 | import com.github.nenadjakic.eav.dto.AttributeUpdateRequest
|
8 | 6 | import com.github.nenadjakic.eav.entity.Attribute
|
| 7 | +import com.github.nenadjakic.eav.extension.collectionMap |
9 | 8 | import com.github.nenadjakic.eav.service.AttributeService
|
| 9 | +import com.github.nenadjakic.eav.util.RestUtil |
| 10 | +import io.swagger.v3.oas.annotations.Operation |
| 11 | +import io.swagger.v3.oas.annotations.responses.ApiResponse |
| 12 | +import io.swagger.v3.oas.annotations.responses.ApiResponses |
| 13 | +import io.swagger.v3.oas.annotations.tags.Tag |
10 | 14 | import org.modelmapper.ModelMapper
|
11 | 15 | import org.springframework.data.domain.Page
|
12 | 16 | import org.springframework.http.ResponseEntity
|
13 | 17 | import org.springframework.web.bind.annotation.RequestMapping
|
14 | 18 | import org.springframework.web.bind.annotation.RestController
|
15 | 19 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder
|
16 | 20 |
|
| 21 | +@Tag(name = "Attribute controller", description = "API endpoints for managing attributes.") |
17 | 22 | @RestController
|
18 | 23 | @RequestMapping("/attribute")
|
19 | 24 | open class AttributeController(
|
20 | 25 | private val modelMapper: ModelMapper,
|
21 | 26 | private val attributeService: AttributeService
|
22 |
| -) : EavReadController<AttributeAddResponse>, EavWriteController<AttributeAddRequest, AttributeUpdateRequest> { |
| 27 | +) : EavReadController<AttributeResponse>, EavWriteController<AttributeAddRequest, AttributeUpdateRequest> { |
23 | 28 |
|
24 |
| - open override fun findAll(): ResponseEntity<List<AttributeAddResponse>> { |
| 29 | + @Operation( |
| 30 | + operationId = "findAllAttributes", |
| 31 | + summary = "Get all attributes", |
| 32 | + description = "Returns a list of all attributes.") |
| 33 | + @ApiResponses( |
| 34 | + value = [ |
| 35 | + ApiResponse(responseCode = "200", description = "Successful operation") |
| 36 | + ] |
| 37 | + ) |
| 38 | + open override fun findAll(): ResponseEntity<List<AttributeResponse>> { |
25 | 39 | val attributes = attributeService.findAll()
|
26 |
| - val response = modelMapper.collectionMap(attributes, AttributeAddResponse::class.java) |
| 40 | + val response = modelMapper.collectionMap(attributes, AttributeResponse::class.java) |
27 | 41 | return ResponseEntity.ok(response)
|
28 | 42 | }
|
29 | 43 |
|
30 |
| - open override fun findPage(pageNumber: Int, pageSize: Int?): ResponseEntity<Page<AttributeAddResponse>> { |
| 44 | + @Operation( |
| 45 | + operationId = "findPageWithAttributes", |
| 46 | + summary = "Get attributes by page.", |
| 47 | + description = "Returns a page of attributes based on page number and page size.") |
| 48 | + @ApiResponses( |
| 49 | + value = [ |
| 50 | + ApiResponse(responseCode = "200", description = "Successfully retrieved page of attributes.") |
| 51 | + ] |
| 52 | + ) |
| 53 | + open override fun findPage(pageNumber: Int, pageSize: Int?): ResponseEntity<Page<AttributeResponse>> { |
31 | 54 | val page = attributeService.findPage(RestUtil.getPager(pageNumber, pageSize))
|
32 |
| - val response = page.map { modelMapper.map(it, AttributeAddResponse::class.java) } |
| 55 | + val response = page.map { modelMapper.map(it, AttributeResponse::class.java) } |
33 | 56 | return ResponseEntity.ok(response)
|
34 | 57 | }
|
35 | 58 |
|
36 |
| - open override fun findById(id: Long): ResponseEntity<AttributeAddResponse> { |
| 59 | + @Operation( |
| 60 | + operationId = "findAttributeById", |
| 61 | + summary = "Get attribute by id.", |
| 62 | + description = "Returns an attribute with the specified id." |
| 63 | + ) |
| 64 | + @ApiResponses( |
| 65 | + value = [ |
| 66 | + ApiResponse(responseCode = "200", description = "Successfully retrieved attribute."), |
| 67 | + ApiResponse(responseCode = "404", description = "Attribute not found.") |
| 68 | + ] |
| 69 | + ) |
| 70 | + open override fun findById(id: Long): ResponseEntity<AttributeResponse> { |
37 | 71 | val attribute = attributeService.findById(id)
|
38 |
| - val response: AttributeAddResponse? = attribute?.let { modelMapper.map(attribute, AttributeAddResponse::class.java) } |
| 72 | + val response: AttributeResponse? = attribute?.let { modelMapper.map(attribute, AttributeResponse::class.java) } |
39 | 73 | return ResponseEntity.ofNullable(response)
|
40 |
| - |
41 | 74 | }
|
42 | 75 |
|
| 76 | + @Operation( |
| 77 | + operationId = "deleteAttributeById", |
| 78 | + summary = "Delete attribute by id.", |
| 79 | + description = "Deletes an attribute with the specified id." |
| 80 | + ) |
| 81 | + @ApiResponses( |
| 82 | + value = [ |
| 83 | + ApiResponse(responseCode = "204", description = "Entity deleted successfully") |
| 84 | + ] |
| 85 | + ) |
43 | 86 | open override fun deleteById(id: Long): ResponseEntity<Void> {
|
44 | 87 | attributeService.deleteById(id)
|
45 | 88 | return ResponseEntity.noContent().build()
|
46 | 89 | }
|
47 | 90 |
|
| 91 | + @Operation( |
| 92 | + operationId = "updateAttribute", |
| 93 | + summary = "Update attribute.", |
| 94 | + description = "Updates an existing attribute based on the provided model." |
| 95 | + ) |
| 96 | + @ApiResponses( |
| 97 | + value = [ |
| 98 | + ApiResponse(responseCode = "204", description = "Attribute updated successfully."), |
| 99 | + ApiResponse(responseCode = "400", description = "Invalid request data.") |
| 100 | + ] |
| 101 | + ) |
48 | 102 | open override fun update(model: AttributeUpdateRequest): ResponseEntity<Void> {
|
49 | 103 | val entity = modelMapper.map(model, Attribute::class.java)
|
50 | 104 | attributeService.update(entity)
|
51 | 105 | return ResponseEntity.noContent().build()
|
52 | 106 | }
|
53 | 107 |
|
| 108 | + @Operation( |
| 109 | + operationId = "createAttribute", |
| 110 | + summary = "Create attribute.", |
| 111 | + description = "Creates a new attribute based on the provided model." |
| 112 | + ) |
| 113 | + @ApiResponses( |
| 114 | + value = [ |
| 115 | + ApiResponse(responseCode = "201", description = "Attribute created successfully."), |
| 116 | + ApiResponse(responseCode = "400", description = "Invalid request data.") |
| 117 | + ] |
| 118 | + ) |
54 | 119 | open override fun create(model: AttributeAddRequest): ResponseEntity<Void> {
|
55 | 120 | val entity = modelMapper.map(model, Attribute::class.java)
|
56 | 121 | val createdEntity = attributeService.create(entity)
|
|
0 commit comments