-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
444aeb1
commit 3ed9b00
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package initializers | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"main/models" | ||
"os" | ||
) | ||
|
||
func SeedDB() { | ||
LoadStatusesFromJSON("./seeds/statuses.json") | ||
LoadOfficesFromJSON("./seeds/offices.json") | ||
} | ||
|
||
func LoadStatusesFromJSON(filename string) { | ||
var statuses []models.Status | ||
|
||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
log.Panicf("Error reading JSON file: %s", err) | ||
} | ||
|
||
err = json.Unmarshal(data, &statuses) | ||
if err != nil { | ||
log.Panicf("Error unmarshalling JSON: %s", err) | ||
} | ||
|
||
for _, status := range statuses { | ||
if result := DB.FirstOrCreate(&status, models.Status{ID: status.ID}); result.Error != nil { | ||
log.Printf("Error inserting/updating status [%d] %s: %s", status.ID, status.Name, result.Error) | ||
} else { | ||
log.Printf("Successfully inserted/updated status [%d] %s", status.ID, status.Name) | ||
} | ||
} | ||
} | ||
|
||
func LoadAddressesFromJSON(filename string) { | ||
var addresses []models.Address | ||
|
||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
log.Panicf("Error reading JSON file: %s", err) | ||
} | ||
|
||
err = json.Unmarshal(data, &addresses) | ||
if err != nil { | ||
log.Panicf("Error unmarshalling JSON: %s", err) | ||
} | ||
|
||
for _, address := range addresses { | ||
if result := DB.FirstOrCreate(&address, models.Address{ID: address.ID}); result.Error != nil { | ||
log.Printf("Error inserting/updating address [%d] %s: %s", address.ID, address.City, result.Error) | ||
} else { | ||
log.Printf("Successfully inserted/updated address [%d] %s", address.ID, address.City) | ||
} | ||
} | ||
} | ||
|
||
func LoadOfficesFromJSON(filename string) { | ||
var offices []models.Office | ||
|
||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
log.Panicf("Error reading JSON file: %s", err) | ||
} | ||
|
||
err = json.Unmarshal(data, &offices) | ||
if err != nil { | ||
log.Panicf("Error unmarshalling JSON: %s", err) | ||
} | ||
|
||
for _, office := range offices { | ||
if result := DB.FirstOrCreate(&office, models.Office{ID: office.ID}); result.Error != nil { | ||
log.Printf("Error inserting/updating office [%d] %s: %s", office.ID, office.Name, result.Error) | ||
} else { | ||
log.Printf("Successfully inserted/updated office [%d] %s", office.ID, office.Name) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"name": "Wydział Kształtowania Środowiska", | ||
"addressId": 1, | ||
"address": { | ||
"id": 1, | ||
"city": "Kraków", | ||
"street": "Piastowska", | ||
"number": "10", | ||
"zipCode": "31-085" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ "id": 1, "name": "Nowa inwestycja" }, | ||
{ "id": 2, "name": "Inwentaryzacja terenowa" }, | ||
{ "id": 3, "name": "Inwentaryzacja kameralna" }, | ||
{ "id": 4, "name": "Złożenie wniosku" }, | ||
{ "id": 5, "name": "Uzupełnienie braków" }, | ||
{ "id": 6, "name": "Oględziny" }, | ||
{ "id": 7, "name": "Decyzja" }, | ||
{ "id": 8, "name": "Wycinka" }, | ||
{ "id": 8, "name": "Nasadzenia" }, | ||
{ "id": 8, "name": "Inwestycja zakończona" } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters