File size: 12,345 Bytes
673d3dc
4382eea
b25a91d
4382eea
9aa39c6
4382eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ae84ae
 
 
58b80d0
3ae84ae
 
 
433cbba
 
 
 
3ae84ae
 
 
58b80d0
b65a903
 
 
9aa39c6
b65a903
 
 
53c2ef3
58b80d0
 
 
3ae84ae
 
 
 
 
 
1447a4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9aa39c6
3ae84ae
 
1447a4b
4382eea
 
 
3903985
433cbba
 
 
9aa39c6
 
 
 
 
019bfa0
044c6f9
019bfa0
 
 
 
 
 
9aa39c6
282d72a
 
9aa39c6
019bfa0
 
282d72a
019bfa0
9aa39c6
3ae84ae
282d72a
019bfa0
282d72a
 
019bfa0
 
 
282d72a
019bfa0
282d72a
019bfa0
282d72a
 
019bfa0
 
 
 
 
 
 
 
 
 
3ae84ae
282d72a
 
 
 
 
019bfa0
 
 
282d72a
 
019bfa0
 
 
282d72a
 
019bfa0
 
10d74a5
 
6f1cf67
10d74a5
6f1cf67
 
 
 
 
 
 
 
10d74a5
6f1cf67
019bfa0
4b0a220
044c6f9
4b0a220
 
 
 
019bfa0
 
4b0a220
 
 
 
019bfa0
4b0a220
 
 
 
 
 
 
 
 
 
 
 
019bfa0
 
044c6f9
4b0a220
 
 
 
019bfa0
 
 
4b0a220
 
 
 
 
019bfa0
4b0a220
 
 
 
 
 
 
 
 
 
 
 
019bfa0
4b0a220
019bfa0
10d74a5
c57b456
019bfa0
673d3dc
019bfa0
 
 
 
3ae84ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
document.addEventListener('DOMContentLoaded', function() {
    // Initialize tabs
    const tabButtons = document.querySelectorAll('.tab-btn');
    const tabContents = document.querySelectorAll('.tab-content');
    
    tabButtons.forEach(button => {
        button.addEventListener('click', () => {
            // Remove active class from all buttons and contents
            tabButtons.forEach(btn => btn.classList.remove('active', 'text-primary-500'));
            tabButtons.forEach(btn => btn.classList.add('text-gray-400'));
            tabContents.forEach(content => content.classList.add('hidden'));
            
            // Add active class to clicked button and corresponding content
            button.classList.add('active', 'text-primary-500');
            button.classList.remove('text-gray-400');
            const tabId = button.getAttribute('data-tab');
            document.getElementById(tabId).classList.remove('hidden');
        });
    });

    // Add input field
    const addInputBtn = document.getElementById('add-input');
    const inputContainer = document.getElementById('input-container');
    
    addInputBtn.addEventListener('click', () => {
        const inputGroup = document.createElement('div');
        inputGroup.className = 'input-group';
        inputGroup.innerHTML = `
            <div class="flex gap-2">
                <input type="text" class="w-1/2 bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Field Name">
                <input type="text" class="w-1/2 bg-gray-700 border border-gray-600 rounded-lg px-4 py-2 text-gray-100 focus:border-primary-500 focus:ring-1 focus:ring-primary-500" placeholder="Field Type">
            </div>
            <button class="remove-input mt-1 text-red-400 hover:text-red-300 text-sm flex items-center">
                <i data-feather="trash-2" class="w-3 h-3 mr-1"></i> Remove
            </button>
`;
        inputContainer.appendChild(inputGroup);
        feather.replace();
        
        // Add event listener to new remove button
        const removeBtn = inputGroup.querySelector('.remove-input');
        removeBtn.addEventListener('click', () => {
            inputGroup.remove();
        });
    });

    // Remove input field
    document.addEventListener('click', function(e) {
        if (e.target.classList.contains('remove-input') || e.target.closest('.remove-input')) {
            const btn = e.target.classList.contains('remove-input') ? e.target : e.target.closest('.remove-input');
            btn.parentElement.remove();
        }
    });
// Download all files
    const downloadBtn = document.getElementById('download-btn');
    downloadBtn.addEventListener('click', () => {
        // First generate the output if not already generated
        document.getElementById('generate-btn').click();
        
        // Get all code contents
        const className = document.getElementById('class-name').value || 'MyClass';
        const entityCode = document.querySelector('#entity textarea').value;
        const repositoryCode = document.querySelector('#repository textarea').value;
        const serviceCode = document.querySelector('#service textarea').value;
        const controllerCode = document.querySelector('#controller textarea').value;
        
        // Function to create and trigger download
        function download(filename, text) {
            const element = document.createElement('a');
            element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
            element.setAttribute('download', filename);
            element.style.display = 'none';
            document.body.appendChild(element);
            element.click();
            document.body.removeChild(element);
        }
        
        // Download all files
        download(`${className}.java`, entityCode);
        download(`${className}Repository.java`, repositoryCode);
        download(`${className}Service.java`, serviceCode);
        download(`${className}Controller.java`, controllerCode);
    });

    // Generate output
    const generateBtn = document.getElementById('generate-btn');
const outputTextarea = document.querySelector('#output textarea');
    const previewContent = document.querySelector('.preview-content');
    const rawContent = document.querySelector('.raw-content');
    generateBtn.addEventListener('click', () => {
        const className = document.getElementById('class-name').value || 'MyClass';
        const inputGroups = Array.from(document.querySelectorAll('.input-group'));
        const fields = inputGroups.map(group => {
            const inputs = group.querySelectorAll('input');
            return {
                name: inputs[0].value || 'field',
                type: inputs[1].value || 'String'
            };
        });
        // Generate Entity
        let entityCode = `package com.example.entity;\n\n`;
        entityCode += `import jakarta.persistence.Entity;\n`;
        entityCode += `import jakarta.persistence.GeneratedValue;\n`;
        entityCode += `import jakarta.persistence.GenerationType;\n`;
        entityCode += `import jakarta.persistence.Id;\n\n`;
        entityCode += `@Entity\n`;
        entityCode += `public class ${className} {\n\n`;

        // Add fields with annotations if needed
        fields.forEach(field => {
            if (field.name.toLowerCase() === 'id') {
                entityCode += `    @Id\n`;
                entityCode += `    @GeneratedValue(strategy=GenerationType.IDENTITY)\n`;
            }
            entityCode += `    private ${field.type} ${field.name};\n`;
        });
        
        // Add default constructor
        entityCode += `\n    protected ${className}() {}\n\n`;
        
        // Add parameterized constructor
        entityCode += `    public ${className}(`;
        entityCode += fields.map(f => `${f.type} ${f.name}`).join(', ');
        entityCode += `) {\n`;
        fields.forEach(f => {
            entityCode += `        this.${f.name} = ${f.name};\n`;
        });
        entityCode += `    }\n\n`;

        // Add toString()
        entityCode += `    @Override\n`;
        entityCode += `    public String toString() {\n`;
        entityCode += `        return String.format(\n`;
        entityCode += `                "${className}[`;
        entityCode += fields.map(f => `${f.name}=%s`).join(', ');
        entityCode += `]",\n`;
        entityCode += `                `;
        entityCode += fields.map(f => `${f.name}`).join(', ');
        entityCode += `);\n`;
        entityCode += `    }\n\n`;

        // Add getters and setters
        fields.forEach(field => {
            // Getter
            const getterName = field.type === 'boolean' ? `is${field.name.charAt(0).toUpperCase() + field.name.slice(1)}` 
                : `get${field.name.charAt(0).toUpperCase() + field.name.slice(1)}`;
            entityCode += `    public ${field.type} ${getterName}() {\n`;
            entityCode += `        return ${field.name};\n`;
            entityCode += `    }\n\n`;
            
            // Setter
            entityCode += `    public void set${field.name.charAt(0).toUpperCase() + field.name.slice(1)}(${field.type} ${field.name}) {\n`;
            entityCode += `        this.${field.name} = ${field.name};\n`;
            entityCode += `    }\n\n`;
        });

        entityCode += `}\n`;
        // Generate Repository
        let repositoryCode = `package com.example.repository;\n\n`;
        repositoryCode += `import com.example.entity.${className};\n`;
        repositoryCode += `import org.springframework.data.repository.CrudRepository;\n`;
        repositoryCode += `import org.springframework.stereotype.Repository;\n\n`;
        
        // Find ID type (default to Long if not found)
        let idType = 'Long';
        const idField = fields.find(f => f.name.toLowerCase() === 'id');
        if (idField) {
            idType = idField.type;
        }
        
        repositoryCode += `@Repository\n`;
        repositoryCode += `public interface ${className}Repository extends CrudRepository<${className}, ${idType}> {\n\n`;
        repositoryCode += `}\n`;
        // Generate Service
        let serviceCode = `package com.example.service;\n\n`;
        serviceCode += `import org.springframework.beans.factory.annotation.Autowired;\n`;
        serviceCode += `import org.springframework.stereotype.Service;\n`;
        serviceCode += `import java.util.Optional;\n`;
        serviceCode += `import java.util.List;\n\n`;
        serviceCode += `@Service\n`;
        serviceCode += `public class ${className}Service {\n\n`;
        serviceCode += `    @Autowired\n`;
        serviceCode += `    private ${className}Repository ${className.toLowerCase()}Repository;\n\n`;
        serviceCode += `    public List<${className}> findAll() {\n`;
        serviceCode += `        return (List<${className}>) ${className.toLowerCase()}Repository.findAll();\n`;
        serviceCode += `    }\n\n`;
        serviceCode += `    public Optional<${className}> findById(Long id) {\n`;
        serviceCode += `        return ${className.toLowerCase()}Repository.findById(id);\n`;
        serviceCode += `    }\n\n`;
        serviceCode += `    public ${className} save(${className} ${className.toLowerCase()}) {\n`;
        serviceCode += `        return ${className.toLowerCase()}Repository.save(${className.toLowerCase()});\n`;
        serviceCode += `    }\n\n`;
        serviceCode += `    public void delete(${className} ${className.toLowerCase()}) {\n`;
        serviceCode += `        ${className.toLowerCase()}Repository.delete(${className.toLowerCase()});\n`;
        serviceCode += `    }\n\n`;
        serviceCode += `    public void delete(Long id) {\n`;
        serviceCode += `        ${className.toLowerCase()}Repository.deleteById(id);\n`;
        serviceCode += `    }\n`;
        serviceCode += `}\n`;
        // Generate Controller
        let controllerCode = `package com.example.controller;\n\n`;
        controllerCode += `import org.springframework.beans.factory.annotation.Autowired;\n`;
        controllerCode += `import org.springframework.web.bind.annotation.*;\n`;
        controllerCode += `import java.util.List;\n`;
        controllerCode += `import java.util.Optional;\n\n`;
        controllerCode += `@RestController\n`;
        controllerCode += `@RequestMapping("/api/${className.toLowerCase()}s")\n`;
        controllerCode += `public class ${className}Controller {\n\n`;
        controllerCode += `    @Autowired\n`;
        controllerCode += `    private ${className}Service ${className.toLowerCase()}Service;\n\n`;
        controllerCode += `    @GetMapping\n`;
        controllerCode += `    public List<${className}> findAll() {\n`;
        controllerCode += `        return ${className.toLowerCase()}Service.findAll();\n`;
        controllerCode += `    }\n\n`;
        controllerCode += `    @GetMapping("/{id}")\n`;
        controllerCode += `    public Optional<${className}> findById(@PathVariable Long id) {\n`;
        controllerCode += `        return ${className.toLowerCase()}Service.findById(id);\n`;
        controllerCode += `    }\n\n`;
        controllerCode += `    @PostMapping\n`;
        controllerCode += `    public ${className} save(@RequestBody ${className} ${className.toLowerCase()}) {\n`;
        controllerCode += `        return ${className.toLowerCase()}Service.save(${className.toLowerCase()});\n`;
        controllerCode += `    }\n\n`;
        controllerCode += `    @DeleteMapping("/{id}")\n`;
        controllerCode += `    public void delete(@PathVariable Long id) {\n`;
        controllerCode += `        ${className.toLowerCase()}Service.delete(id);\n`;
        controllerCode += `    }\n`;
        controllerCode += `}\n`;
// Update tabs
        document.querySelector('#entity textarea').value = entityCode;
        document.querySelector('#repository textarea').value = repositoryCode;
        document.querySelector('#service textarea').value = serviceCode;
        document.querySelector('#controller textarea').value = controllerCode;
});
        // Set first tab as active by default
        if (tabButtons.length > 0) {
            tabButtons[0].click();
        }
});