Skip to content

Commit 267f54f

Browse files
committed
Refactor DatabasesContent to set default database type to MySQL and streamline database input handling
1 parent 29e4fb4 commit 267f54f

3 files changed

Lines changed: 106 additions & 145 deletions

File tree

src/main/kotlin/com/github/cnrture/quickbackendwizard/generators/DatabaseType.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ data class DatabaseType(
66
val description: String,
77
) {
88
companion object {
9-
val H2 = DatabaseType("h2", "H2 (In-Memory)", "Lightweight in-memory database for development and testing.")
109
val MYSQL = DatabaseType("mysql", "MySQL", "Popular open-source relational database management system.")
10+
val H2 = DatabaseType("h2", "H2 (In-Memory)", "Lightweight in-memory database for development and testing.")
1111
val MARIADB = DatabaseType("mariadb", "MariaDB", "Community-developed fork of MySQL with enhanced features.")
1212
val POSTGRESQL = DatabaseType(
1313
"postgresql",
1414
"PostgreSQL",
1515
"Advanced open-source relational database with strong standards compliance."
1616
)
17-
val ALL = listOf(H2, MYSQL, MARIADB, POSTGRESQL)
17+
val ALL = listOf(MYSQL, H2, MARIADB, POSTGRESQL)
1818
}
1919
}
Lines changed: 96 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.github.cnrture.quickbackendwizard.generators
22

3-
import androidx.compose.foundation.background
43
import androidx.compose.foundation.layout.*
54
import androidx.compose.foundation.rememberScrollState
65
import androidx.compose.foundation.verticalScroll
76
import androidx.compose.material.Divider
8-
import androidx.compose.material.Switch
9-
import androidx.compose.material.SwitchDefaults
107
import androidx.compose.runtime.*
118
import androidx.compose.ui.Alignment
129
import androidx.compose.ui.Modifier
@@ -22,8 +19,7 @@ import com.github.cnrture.quickbackendwizard.theme.QBWTheme
2219
@OptIn(ExperimentalLayoutApi::class)
2320
@Composable
2421
fun DatabasesContent(moduleBuilder: QBWSpringBootModuleBuilder) {
25-
var selectedDatabase by remember { mutableStateOf(DatabaseType.ALL.first().type) }
26-
var isDatabaseEnabled by remember { mutableStateOf(false) }
22+
var selectedDatabase by remember { mutableStateOf(DatabaseType.MYSQL.type) }
2723
var dbName by remember { mutableStateOf("") }
2824
var dbUsername by remember { mutableStateOf("") }
2925
var dbPassword by remember { mutableStateOf("") }
@@ -47,22 +43,6 @@ fun DatabasesContent(moduleBuilder: QBWSpringBootModuleBuilder) {
4743
),
4844
)
4945
Spacer(modifier = Modifier.size(8.dp))
50-
Switch(
51-
checked = isDatabaseEnabled,
52-
onCheckedChange = {
53-
isDatabaseEnabled = it
54-
if (!it) {
55-
selectedDatabase = ""
56-
moduleBuilder.selectedDatabase = ""
57-
}
58-
},
59-
colors = SwitchDefaults.colors(
60-
checkedThumbColor = QBWTheme.colors.red,
61-
uncheckedThumbColor = QBWTheme.colors.lightGray,
62-
checkedTrackColor = QBWTheme.colors.red,
63-
uncheckedTrackColor = QBWTheme.colors.lightGray.copy(alpha = 0.5f),
64-
),
65-
)
6646
}
6747
QBWText(
6848
text = "Select the database you want to use in your project.",
@@ -73,125 +53,106 @@ fun DatabasesContent(moduleBuilder: QBWSpringBootModuleBuilder) {
7353
modifier = Modifier.padding(vertical = 8.dp),
7454
color = QBWTheme.colors.lightGray,
7555
)
76-
Box(
77-
modifier = Modifier.fillMaxWidth(),
78-
) {
79-
Column {
80-
QBWText(
81-
text = "Database Type",
82-
color = QBWTheme.colors.red,
83-
style = TextStyle(
84-
fontSize = 16.sp,
85-
fontWeight = FontWeight.SemiBold,
86-
),
87-
)
88-
Spacer(modifier = Modifier.height(12.dp))
89-
FlowRow(
90-
modifier = Modifier.fillMaxWidth(),
91-
maxItemsInEachRow = 2,
56+
Column {
57+
QBWText(
58+
text = "Database Type",
59+
color = QBWTheme.colors.red,
60+
style = TextStyle(
61+
fontSize = 16.sp,
62+
fontWeight = FontWeight.SemiBold,
63+
),
64+
)
65+
Spacer(modifier = Modifier.height(12.dp))
66+
FlowRow(
67+
modifier = Modifier.fillMaxWidth(),
68+
maxItemsInEachRow = 2,
69+
) {
70+
DatabaseType.ALL.forEach { it ->
71+
DatabaseItem(
72+
modifier = Modifier.weight(1f),
73+
name = it.name,
74+
description = it.description,
75+
type = it.type,
76+
selectedType = selectedDatabase,
77+
onSelect = { type ->
78+
selectedDatabase = type
79+
moduleBuilder.selectedDatabase = type
80+
},
81+
)
82+
}
83+
}
84+
Spacer(modifier = Modifier.height(24.dp))
85+
Row(
86+
modifier = Modifier.fillMaxWidth(),
87+
horizontalArrangement = Arrangement.spacedBy(16.dp),
88+
) {
89+
Column(
90+
modifier = Modifier.weight(1f),
9291
) {
93-
DatabaseType.ALL.forEach { it ->
94-
DatabaseItem(
95-
modifier = Modifier.weight(1f),
96-
name = it.name,
97-
description = it.description,
98-
type = it.type,
99-
selectedType = selectedDatabase,
100-
isEnabled = isDatabaseEnabled,
101-
onSelect = { type ->
102-
selectedDatabase = type
103-
moduleBuilder.selectedDatabase = type
104-
},
105-
)
106-
}
92+
QBWText(
93+
text = "Database Name",
94+
color = QBWTheme.colors.red,
95+
style = TextStyle(
96+
fontSize = 16.sp,
97+
fontWeight = FontWeight.SemiBold,
98+
),
99+
)
100+
Spacer(modifier = Modifier.height(12.dp))
101+
QBWTextField(
102+
modifier = Modifier.fillMaxWidth(),
103+
placeholder = "(e.g., testdb)",
104+
value = dbName,
105+
onValueChange = {
106+
dbName = it
107+
moduleBuilder.dbName = it
108+
},
109+
)
107110
}
108-
Spacer(modifier = Modifier.height(24.dp))
109-
Row(
110-
modifier = Modifier.fillMaxWidth(),
111-
horizontalArrangement = Arrangement.spacedBy(16.dp),
111+
Column(
112+
modifier = Modifier.weight(1f),
112113
) {
113-
Column(
114-
modifier = Modifier.weight(1f),
115-
) {
116-
QBWText(
117-
text = "Database Name",
118-
color = QBWTheme.colors.red,
119-
style = TextStyle(
120-
fontSize = 16.sp,
121-
fontWeight = FontWeight.SemiBold,
122-
),
123-
)
124-
Spacer(modifier = Modifier.height(12.dp))
125-
QBWTextField(
126-
modifier = Modifier.fillMaxWidth(),
127-
placeholder = "(e.g., testdb)",
128-
value = dbName,
129-
isEnabled = isDatabaseEnabled,
130-
onValueChange = {
131-
dbName = it
132-
moduleBuilder.dbName = it
133-
},
134-
)
135-
}
136-
Column(
137-
modifier = Modifier.weight(1f),
138-
) {
139-
QBWText(
140-
text = "Database Username",
141-
color = QBWTheme.colors.red,
142-
style = TextStyle(
143-
fontSize = 16.sp,
144-
fontWeight = FontWeight.SemiBold,
145-
),
146-
)
147-
Spacer(modifier = Modifier.height(12.dp))
148-
QBWTextField(
149-
modifier = Modifier.fillMaxWidth(),
150-
placeholder = "(e.g., root)",
151-
value = dbUsername,
152-
isEnabled = isDatabaseEnabled,
153-
onValueChange = {
154-
dbUsername = it
155-
moduleBuilder.dbUsername = it
156-
},
157-
)
158-
}
159-
Column(
160-
modifier = Modifier.weight(1f),
161-
) {
162-
QBWText(
163-
text = "Database Password",
164-
color = QBWTheme.colors.red,
165-
style = TextStyle(
166-
fontSize = 16.sp,
167-
fontWeight = FontWeight.SemiBold,
168-
),
169-
)
170-
Spacer(modifier = Modifier.height(12.dp))
171-
QBWTextField(
172-
modifier = Modifier.fillMaxWidth(),
173-
placeholder = "(e.g., password)",
174-
value = dbPassword,
175-
isEnabled = isDatabaseEnabled,
176-
onValueChange = {
177-
dbPassword = it
178-
moduleBuilder.dbPassword = it
179-
},
180-
)
181-
}
114+
QBWText(
115+
text = "Database Username",
116+
color = QBWTheme.colors.red,
117+
style = TextStyle(
118+
fontSize = 16.sp,
119+
fontWeight = FontWeight.SemiBold,
120+
),
121+
)
122+
Spacer(modifier = Modifier.height(12.dp))
123+
QBWTextField(
124+
modifier = Modifier.fillMaxWidth(),
125+
placeholder = "(e.g., root)",
126+
value = dbUsername,
127+
onValueChange = {
128+
dbUsername = it
129+
moduleBuilder.dbUsername = it
130+
},
131+
)
182132
}
183-
}
184-
Box(
185-
modifier = Modifier
186-
.matchParentSize()
187-
.background(
188-
color = if (isDatabaseEnabled) {
189-
QBWTheme.colors.gray.copy(alpha = 0.0f)
190-
} else {
191-
QBWTheme.colors.gray.copy(alpha = 0.6f)
133+
Column(
134+
modifier = Modifier.weight(1f),
135+
) {
136+
QBWText(
137+
text = "Database Password",
138+
color = QBWTheme.colors.red,
139+
style = TextStyle(
140+
fontSize = 16.sp,
141+
fontWeight = FontWeight.SemiBold,
142+
),
143+
)
144+
Spacer(modifier = Modifier.height(12.dp))
145+
QBWTextField(
146+
modifier = Modifier.fillMaxWidth(),
147+
placeholder = "(e.g., password)",
148+
value = dbPassword,
149+
onValueChange = {
150+
dbPassword = it
151+
moduleBuilder.dbPassword = it
192152
},
193153
)
194-
)
154+
}
155+
}
195156
}
196157
}
197158
}
@@ -203,15 +164,13 @@ private fun DatabaseItem(
203164
description: String,
204165
type: String,
205166
selectedType: String,
206-
isEnabled: Boolean,
207167
onSelect: (String) -> Unit,
208168
) {
209169
QBWRadioButton(
210170
modifier = modifier,
211171
text = name,
212172
description = description,
213-
isEnabled = isEnabled,
214-
selected = selectedType == type && isEnabled,
173+
selected = selectedType == type,
215174
onClick = { onSelect(type) },
216175
)
217176
}

src/main/kotlin/com/github/cnrture/quickbackendwizard/generators/QBWSpringBootModuleBuilder.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.roots.ModifiableRootModel
1414
import com.intellij.openapi.roots.ui.configuration.ModulesProvider
1515
import com.intellij.openapi.vfs.VirtualFile
16+
import fleet.kernel.db
1617
import java.io.IOException
1718

1819
class QBWSpringBootModuleBuilder : JavaModuleBuilder() {
@@ -27,7 +28,7 @@ class QBWSpringBootModuleBuilder : JavaModuleBuilder() {
2728
var includeSpringSecurity: Boolean = false
2829
var includeValidation: Boolean = false
2930
var isAddGradleTasks: Boolean = false
30-
var selectedDatabase: String = ""
31+
var selectedDatabase: String = DatabaseType.MYSQL.type
3132
var dbName: String = ""
3233
var dbUsername: String = ""
3334
var dbPassword: String = ""
@@ -116,10 +117,8 @@ class QBWSpringBootModuleBuilder : JavaModuleBuilder() {
116117
createApplicationProperties(root)
117118
createWebConfigFile(root)
118119

119-
if (selectedDatabase.isNotEmpty()) {
120-
createDatabaseFiles(root)
121-
createEnvFile(root, dbName, dbUsername, dbPassword)
122-
}
120+
createDatabaseFiles(root)
121+
createEnvFile(root, dbName, dbUsername, dbPassword)
123122

124123
endpoints.forEach { endpoint ->
125124
createEntityFile(root, endpoint)
@@ -233,7 +232,7 @@ class QBWSpringBootModuleBuilder : JavaModuleBuilder() {
233232
}
234233

235234
private fun createDatabaseFiles(root: VirtualFile) {
236-
if (selectedDatabase != "none" && endpoints.isNotEmpty()) {
235+
if (endpoints.isNotEmpty()) {
237236
val sqlScript = getSqlCreateTableScript(endpoints, selectedDatabase, projectName)
238237
if (sqlScript.isNotEmpty()) {
239238
createFile(root, "create_tables.sql", sqlScript)
@@ -324,6 +323,9 @@ class QBWSpringBootModuleBuilder : JavaModuleBuilder() {
324323
}
325324

326325
private fun createEnvFile(root: VirtualFile, dbName: String, dbUsername: String, dbPassword: String) {
326+
val dbName = dbName.ifEmpty { "testdb" }
327+
val dbUsername = dbUsername.ifEmpty { "root" }
328+
val dbPassword = dbPassword.ifEmpty { "password" }
327329
val content = getEnvironmentContent(dbName, dbUsername, dbPassword)
328330
createFile(root, ".env", content)
329331
}

0 commit comments

Comments
 (0)