1
1
package pl .commit .craft .template ;
2
2
3
3
import io .swagger .v3 .oas .annotations .Operation ;
4
+ import io .swagger .v3 .oas .annotations .media .Content ;
5
+ import io .swagger .v3 .oas .annotations .media .Schema ;
6
+ import io .swagger .v3 .oas .annotations .parameters .RequestBody ;
4
7
import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
8
+ import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
9
+ import io .swagger .v3 .oas .annotations .tags .Tag ;
5
10
import lombok .RequiredArgsConstructor ;
11
+ import org .springframework .http .HttpStatus ;
6
12
import org .springframework .http .ResponseEntity ;
7
13
import org .springframework .web .bind .annotation .*;
8
14
import java .io .IOException ;
15
+ import java .util .Collections ;
9
16
import java .util .List ;
10
17
import java .util .Map ;
11
18
12
19
@ RestController
13
20
@ RequestMapping ("/api/v1/craft-template" )
14
21
@ RequiredArgsConstructor
22
+ @ Tag (name = "Commit Template Controller" , description = "Management template commit model" )
15
23
public class CommitCraftTemplateController {
16
24
17
25
private final CommitTemplateService commitTemplateService ;
@@ -20,6 +28,11 @@ public class CommitCraftTemplateController {
20
28
summary = "Get all commit templates" ,
21
29
description = "Fetches a list of all available commit craft templates."
22
30
)
31
+ @ ApiResponses ({
32
+ @ ApiResponse (responseCode = "200" , description = "Successfully fetched templates" ,
33
+ content = @ Content (mediaType = "application/json" ,
34
+ schema = @ Schema (implementation = CommitCraftTemplate .class )))
35
+ })
23
36
@ GetMapping ("/all" )
24
37
public ResponseEntity <List <CommitCraftTemplate >> getAllTemplates () throws IOException {
25
38
List <CommitCraftTemplate > templates = commitTemplateService .getAllTemplates ();
@@ -28,29 +41,40 @@ public ResponseEntity<List<CommitCraftTemplate>> getAllTemplates() throws IOExce
28
41
29
42
@ Operation (
30
43
summary = "Create a dedicated commit template" ,
31
- description = "Creates a new dedicated commit template if the pattern and model scope are valid." ,
32
- responses = {
33
- @ ApiResponse (responseCode = "200" , description = "Template added successfully" ),
34
- @ ApiResponse (responseCode = "400" , description = "Template already exists" )
35
- }
44
+ description = "Creates a new dedicated commit template if the pattern and model scope are valid."
36
45
)
46
+ @ ApiResponses ({
47
+ @ ApiResponse (responseCode = "201" , description = "Template created successfully" ,
48
+ content = @ Content (mediaType = "application/json" )),
49
+ @ ApiResponse (responseCode = "400" , description = "Invalid template format or template already exists" ,
50
+ content = @ Content (mediaType = "application/json" ))
51
+ })
37
52
@ PostMapping ("/dedicated" )
38
- public ResponseEntity <String > createDedicatedTemplate (@ RequestBody CommitCraftTemplate template ) throws IOException {
39
- boolean patternAndModelScope = CommitDedicatedTemplateValidator .validatePatternAndModelScope (template );
40
- if (patternAndModelScope ) {
41
- commitTemplateService .createDedicatedTemplate (template );
42
- return ResponseEntity .ok ("Template added successfully." );
43
- }
44
- return ResponseEntity .badRequest ().body ("Template already exists." );
53
+ public ResponseEntity <Map <String , String >> createDedicatedTemplate (
54
+ @ RequestBody (description = "Commit template data" , required = true ,
55
+ content = @ Content (schema = @ Schema (implementation = CommitCraftTemplate .class )))
56
+ @ org .springframework .web .bind .annotation .RequestBody CommitCraftTemplate template ) throws IOException {
57
+ TemplateOperationResult result = commitTemplateService .createDedicatedTemplate (template );
58
+
59
+ if (result .success ()) {
60
+ return ResponseEntity
61
+ .status (HttpStatus .CREATED )
62
+ .body (Collections .singletonMap ("message" , result .message ()));
63
+ }
64
+
65
+ return ResponseEntity
66
+ .status (HttpStatus .BAD_REQUEST )
67
+ .body (Collections .singletonMap ("error" , result .message ()));
45
68
}
46
69
47
70
@ Operation (
48
71
summary = "Remove a commit template" ,
49
- description = "Removes a dedicated commit template by name." ,
50
- responses = {
51
- @ ApiResponse (responseCode = "200" , description = "Template removed successfully" )
52
- }
72
+ description = "Removes a dedicated commit template by name."
53
73
)
74
+ @ ApiResponses ({
75
+ @ ApiResponse (responseCode = "200" , description = "Template removed successfully" ,
76
+ content = @ Content (mediaType = "text/plain" ))
77
+ })
54
78
@ DeleteMapping ("/removed/{name}" )
55
79
public ResponseEntity <String > addTemplate (@ PathVariable ("name" ) String name ) throws IOException {
56
80
commitTemplateService .removeDedicatedTemplate (name );
@@ -59,11 +83,12 @@ public ResponseEntity<String> addTemplate(@PathVariable("name") String name) thr
59
83
60
84
@ Operation (
61
85
summary = "Generate commit template JSON" ,
62
- description = "Generates a JSON representation of the commit template based on its name." ,
63
- responses = {
64
- @ ApiResponse (responseCode = "200" , description = "JSON generated successfully" )
65
- }
86
+ description = "Generates a JSON representation of the commit template based on its name."
66
87
)
88
+ @ ApiResponses ({
89
+ @ ApiResponse (responseCode = "200" , description = "JSON generated successfully" ,
90
+ content = @ Content (mediaType = "application/json" ))
91
+ })
67
92
@ PostMapping ("/generate-json/{name}" )
68
93
public Map <String , Object > generateJson (@ PathVariable String name ) throws IOException {
69
94
CommitCraftJson commitCraftJson = commitTemplateService .prepareJsonByModel (name );
0 commit comments