From 70699ed51cce5c22ef98d2299d03b1bcf7187dcf Mon Sep 17 00:00:00 2001
From: "Dominik.Sigmund" <dominik.sigmund@br.de>
Date: Thu, 11 Jul 2024 12:02:29 +0200
Subject: [PATCH] Small fix for the basic add

---
 README.md       | 10 +++++++++-
 commands/add.go | 10 +++++-----
 main.go         |  8 +++++++-
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index b1c9dce..1d24627 100644
--- a/README.md
+++ b/README.md
@@ -32,7 +32,15 @@ ds init
 ### Add a new table
 
 ```bash
-ds add table "Table Name"
+ds add
+```
+
+```bash
+ds add "Table Name"
+```
+
+```bash
+ds add users --model '{"fields":[{"name":"id","type":"INT","primary_key":true,"auto_increment":true,"not_null":true},{"name":"name","type":"VARCHAR(255)","unique":true,"not_null":true},{"name":"email","type":"VARCHAR(255)","unique":true,"not_null":true},{"name":"created_at","type":"DATETIME","not_null":true,"default_value":"CURRENT_TIMESTAMP"}]}'
 ```
 
 ## Contributing
diff --git a/commands/add.go b/commands/add.go
index c8b5c29..71c8c64 100644
--- a/commands/add.go
+++ b/commands/add.go
@@ -299,18 +299,18 @@ func promptForTableModel() TableModel {
 		utils.PromptIfEmpty(&field.Name, "Enter field name: ")
 		field.Type = promptForFieldType()
 
-		field.PrimaryKey = utils.PromptForBool("Is this a primary key? (y/n): ")
+		field.PrimaryKey = utils.PromptForBool("Is this a primary key? (y/n): ") // TODO: use SelectMenu
 		if !field.PrimaryKey {
 			utils.PromptIfEmpty(&field.ForeignKey, "Enter foreign key (leave empty if not applicable): ")
 		}
-		field.Unique = utils.PromptForBool("Is this field unique? (y/n): ")
-		field.NotNull = utils.PromptForBool("Is this field not null? (y/n): ")
-		field.AutoIncrement = utils.PromptForBool("Is this field auto-increment? (y/n): ")
+		field.Unique = utils.PromptForBool("Is this field unique? (y/n): ") // TODO: use SelectMenu
+		field.NotNull = utils.PromptForBool("Is this field not null? (y/n): ") // TODO: use SelectMenu
+		field.AutoIncrement = utils.PromptForBool("Is this field auto-increment? (y/n): ") // TODO: use SelectMenu
 		field.DefaultValue = utils.PromptForString("Enter default value (leave empty if not applicable): ")
 
 		fields = append(fields, field)
 
-		if !utils.PromptForBool("Do you want to add another field? (y/n): ") {
+		if !utils.PromptForBool("Do you want to add another field? (y/n): ") { // TODO: use SelectMenu
 			break
 		}
 	}
diff --git a/main.go b/main.go
index eeb0757..c580459 100644
--- a/main.go
+++ b/main.go
@@ -36,13 +36,19 @@ func main() {
 	case "add":
 		var tableName, model string
 		args := os.Args[2:]
-		for i, arg := range args {
+		for i := 0; i < len(args); i++ {
+			arg := args[i]
 			if arg == "--model" && i+1 < len(args) {
 				model = args[i+1]
+				i++ // Skip the next argument since it's the model value
 			} else if !strings.HasPrefix(arg, "--") {
 				tableName = arg
 			}
 		}
+		if tableName == "" {
+			fmt.Println("Usage: ds add <tablename> [--model <json_model>]")
+			return
+		}
 		commands.Add(tableName, model)
 	case "version":
 		commands.Version(version)
-- 
GitLab