|
| 1 | +package repoimpl |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + migrationPath = "migration" |
| 13 | + repositoryPath = "repository" |
| 14 | + testPath = "test" |
| 15 | + postgresPath = "postgres" |
| 16 | +) |
| 17 | + |
| 18 | +// Implement migration SQL queries for all models |
| 19 | +// Parameters: |
| 20 | +// - model... : implementation of models with table name and fields |
| 21 | +// |
| 22 | +// Example: |
| 23 | +// - NewDBTable(models.User{}) |
| 24 | +// |
| 25 | +// Returns: |
| 26 | +// - error: error if something went wrong |
| 27 | +func NewProject(datas ...any) error { |
| 28 | + |
| 29 | + log.Println("Starting implement...") |
| 30 | + |
| 31 | + err := NewMigration(datas...) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + err = NewRepository(datas...) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + err = NewTest(datas...) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func NewRepository(datas ...any) error { |
| 49 | + |
| 50 | + var ( |
| 51 | + storage strings.Builder |
| 52 | + interfaces strings.Builder |
| 53 | + ) |
| 54 | + err := os.Mkdir(repositoryPath, 0755) |
| 55 | + if err != nil { |
| 56 | + if strings.Contains(err.Error(), "exists") { |
| 57 | + fmt.Println("repository folder already exist, If you want to create new repository, click something else `q` or `Q`") |
| 58 | + var answer string |
| 59 | + fmt.Scanln(&answer) |
| 60 | + if answer == "q" || answer == "Q" { |
| 61 | + return nil |
| 62 | + } |
| 63 | + err := os.RemoveAll(repositoryPath) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + } |
| 68 | + } else { |
| 69 | + err := os.Mkdir(repositoryPath+"/"+postgresPath, 0755) |
| 70 | + if err != nil { |
| 71 | + if strings.Contains(err.Error(), "exists") { |
| 72 | + fmt.Println("repository/postgres folder already exist, If you want to create new repository/postgres, click something else `q` or `Q`") |
| 73 | + var answer string |
| 74 | + fmt.Scanln(&answer) |
| 75 | + if answer == "q" || answer == "Q" { |
| 76 | + return nil |
| 77 | + } |
| 78 | + err := os.RemoveAll(repositoryPath + "/" + postgresPath) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + storage.WriteString(storageHeader()) |
| 86 | + |
| 87 | + for _, model := range datas { |
| 88 | + |
| 89 | + lowerNameOfModel := fieldToDefault(reflect.TypeOf(model).Name()) |
| 90 | + upperNameOfModel := reflect.TypeOf(model).Name() |
| 91 | + valueOfModel := reflect.ValueOf(model) |
| 92 | + typeOfModel := valueOfModel.Type() |
| 93 | + numberOfFields := valueOfModel.NumField() |
| 94 | + |
| 95 | + fieldsOfModel := fields(typeOfModel, numberOfFields) |
| 96 | + |
| 97 | + log.Println("Starting repository...") |
| 98 | + err = repositoryFiles(createRepository(upperNameOfModel, lowerNameOfModel, fieldsOfModel), lowerNameOfModel) |
| 99 | + if err != nil { |
| 100 | + return clear(err) |
| 101 | + } |
| 102 | + interfaceName, interfaceMethods := storageInterface(upperNameOfModel, lowerNameOfModel, fieldsOfModel) |
| 103 | + storage.WriteString(interfaceName) |
| 104 | + interfaces.WriteString(interfaceMethods) |
| 105 | + log.Println("Successful repository implemented") |
| 106 | + } |
| 107 | + storage.WriteString(storageFooter()) |
| 108 | + storage.WriteString(interfaces.String()) |
| 109 | + storageFile(storage.String()) |
| 110 | + log.Println("Succesfull implemented...") |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func NewMigration(datas ...any) error { |
| 115 | + err := os.Mkdir(migrationPath, 0755) |
| 116 | + if err != nil { |
| 117 | + if strings.Contains(err.Error(), "exists") { |
| 118 | + fmt.Println("migration folder already exist, If you want to create new migration, click something else `q` or `Q`") |
| 119 | + var answer string |
| 120 | + fmt.Scanln(&answer) |
| 121 | + if answer == "q" || answer == "Q" { |
| 122 | + return nil |
| 123 | + } |
| 124 | + err := os.RemoveAll(migrationPath) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + } |
| 129 | + } else { |
| 130 | + err := os.Mkdir(migrationPath+"/"+postgresPath, 0755) |
| 131 | + if err != nil { |
| 132 | + if strings.Contains(err.Error(), "exists") { |
| 133 | + fmt.Println("migration/postgres folder already exist, If you want to create new migration/postgres, click something else `q` or `Q`") |
| 134 | + var answer string |
| 135 | + fmt.Scanln(&answer) |
| 136 | + if answer == "q" || answer == "Q" { |
| 137 | + return nil |
| 138 | + } |
| 139 | + err := os.RemoveAll(migrationPath + "/" + postgresPath) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + for _, model := range datas { |
| 148 | + |
| 149 | + lowerNameOfModel := fieldToDefault(reflect.TypeOf(model).Name()) |
| 150 | + // upperNameOfModel := reflect.TypeOf(model).Name() |
| 151 | + valueOfModel := reflect.ValueOf(model) |
| 152 | + typeOfModel := valueOfModel.Type() |
| 153 | + numberOfFields := valueOfModel.NumField() |
| 154 | + |
| 155 | + fieldsOfModel := fields(typeOfModel, numberOfFields) |
| 156 | + |
| 157 | + log.Println("Starting migration...") |
| 158 | + init, drop := createDbQuery(lowerNameOfModel, fieldsOfModel) |
| 159 | + err := migrationFiles(lowerNameOfModel, init, drop) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + log.Println("Successful migration implemented") |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | +func NewTest(datas ...any) error { |
| 170 | + err := os.Mkdir(testPath, 0755) |
| 171 | + if err != nil { |
| 172 | + if strings.Contains(err.Error(), "exists") { |
| 173 | + fmt.Println("test folder already exist, If you want to create new test, click something else `q` or `Q`") |
| 174 | + var answer string |
| 175 | + fmt.Scanln(&answer) |
| 176 | + if answer == "q" || answer == "Q" { |
| 177 | + return nil |
| 178 | + } |
| 179 | + err := os.RemoveAll(testPath) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + } |
| 184 | + } else { |
| 185 | + err := os.Mkdir(testPath+"/"+postgresPath, 0755) |
| 186 | + if err != nil { |
| 187 | + if strings.Contains(err.Error(), "exists") { |
| 188 | + fmt.Println("test/postgres folder already exist, If you want to create new test/postgres, click something else `q` or `Q`") |
| 189 | + var answer string |
| 190 | + fmt.Scanln(&answer) |
| 191 | + if answer == "q" || answer == "Q" { |
| 192 | + return nil |
| 193 | + } |
| 194 | + err := os.RemoveAll(testPath + "/" + postgresPath) |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + for _, model := range datas { |
| 202 | + |
| 203 | + lowerNameOfModel := fieldToDefault(reflect.TypeOf(model).Name()) |
| 204 | + // upperNameOfModel := reflect.TypeOf(model).Name() |
| 205 | + valueOfModel := reflect.ValueOf(model) |
| 206 | + typeOfModel := valueOfModel.Type() |
| 207 | + numberOfFields := valueOfModel.NumField() |
| 208 | + |
| 209 | + fieldsOfModel := fields(typeOfModel, numberOfFields) |
| 210 | + |
| 211 | + log.Println("Starting test...") |
| 212 | + err = testFiles(createTest(lowerNameOfModel, fieldsOfModel), lowerNameOfModel) |
| 213 | + if err != nil { |
| 214 | + return clear(err) |
| 215 | + } |
| 216 | + |
| 217 | + log.Println("Successful test implemented") |
| 218 | + } |
| 219 | + |
| 220 | + return nil |
| 221 | +} |
| 222 | + |
| 223 | +func clear(response error) error { |
| 224 | + |
| 225 | + if _, err := os.Stat(migrationPath + "/" + postgresPath); os.IsExist(err) { |
| 226 | + err = os.RemoveAll(migrationPath + "/" + postgresPath) |
| 227 | + if err != nil { |
| 228 | + log.Printf("Error: %v", err) |
| 229 | + } |
| 230 | + } else { |
| 231 | + log.Printf("Error: %v", err) |
| 232 | + } |
| 233 | + if _, err := os.Stat(repositoryPath + "/" + postgresPath); os.IsExist(err) { |
| 234 | + err = os.RemoveAll(repositoryPath + "/" + postgresPath) |
| 235 | + if err != nil { |
| 236 | + log.Printf("Error: %v", err) |
| 237 | + } |
| 238 | + } else { |
| 239 | + log.Printf("Error: %v", err) |
| 240 | + } |
| 241 | + |
| 242 | + if _, err := os.Stat(testPath + "/" + postgresPath); os.IsExist(err) { |
| 243 | + err = os.RemoveAll(testPath + "/" + postgresPath) |
| 244 | + if err != nil { |
| 245 | + log.Printf("Error: %v", err) |
| 246 | + } |
| 247 | + } else { |
| 248 | + log.Printf("Error: %v", err) |
| 249 | + } |
| 250 | + |
| 251 | + return response |
| 252 | +} |
0 commit comments