add init db data, fix bugs, refactor
This commit is contained in:
@@ -64,34 +64,34 @@ func (m FoundryStateModel) Insert(state *FoundryState) error {
|
||||
}
|
||||
|
||||
err = m.InsertOptions(&state.Options, state.Id)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range state.Modules {
|
||||
err = m.InsertModule(&state.Modules[i], state.Id)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range state.Systems {
|
||||
err = m.InsertSystem(&state.Systems[i], state.Id)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range state.Worlds {
|
||||
err = m.InsertWorld(&state.Worlds[i], state.Id)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for i := range state.Users {
|
||||
err = m.InsertUser(&state.Users[i], state.Id)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -258,3 +258,49 @@ func (m FoundryStateModel) Delete(id int64) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) DeleteAll() error {
|
||||
query := `
|
||||
DELETE FROM foundry_state`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) DeleteAllSeq() error {
|
||||
query := `
|
||||
DELETE FROM sqlite_sequence`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func (m FoundryStateModel) InsertModule(module *Module, stateId int64) error {
|
||||
}
|
||||
for i := range module.Languages {
|
||||
err = m.InsertModuleLanguages(&module.Languages[i], module.Id)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func (m FoundryStateModel) InsertModule(module *Module, stateId int64) error {
|
||||
|
||||
func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibility, moduleId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (module_id, minumum, verified, maximum)
|
||||
INSERT INTO modules_compatibility (module_id, minimum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
@@ -68,12 +68,16 @@ func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibilit
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (module_id, language, name, path)
|
||||
INSERT INTO modules_languages (module_id, language, name, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
@@ -82,7 +86,11 @@ func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int64)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&lang.Id)
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&lang.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetModules(idState int64) (Modules, error) {
|
||||
|
||||
@@ -27,23 +27,24 @@ func (systems Systems) GetById(id int) *System {
|
||||
func (m FoundryStateModel) InsertSystem(system *System, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems (state_id, text_id, title, description, url, download)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, created_at`
|
||||
|
||||
args := []any{stateId, system.TextId, system.Title, system.Description, system.Url, system.Download}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&system.Id)
|
||||
if err != nil {
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&system.Id, &system.CreatedAt)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return m.InsertModuleCompatibility(&system.Compatibility, system.Id)
|
||||
return m.InsertSystemCompatibility(&system.Compatibility, system.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int64) error {
|
||||
func (m FoundryStateModel) InsertSystemCompatibility(compatibility *Compatibility, systemId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems_compatibility (system_id, minumum, verified, maximum)
|
||||
INSERT INTO systems_compatibility (system_id, minimum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)`
|
||||
|
||||
args := []any{systemId, compatibility.Minimum, compatibility.Verified, compatibility.Maximum}
|
||||
@@ -51,7 +52,11 @@ func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibilit
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetSystems(idState int64) (Systems, error) {
|
||||
|
||||
@@ -72,7 +72,7 @@ func (m FoundryStateModel) InsertUserHotbar(key string, value string, userId int
|
||||
defer cancel()
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -40,7 +40,7 @@ func (m FoundryStateModel) InsertWorld(world *World, stateId int64) error {
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&world.Id, &world.CreatedAt)
|
||||
if err != nil {
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return m.InsertWorldCompatibility(&world.Compatibility, world.Id)
|
||||
@@ -48,7 +48,7 @@ func (m FoundryStateModel) InsertWorld(world *World, stateId int64) error {
|
||||
|
||||
func (m FoundryStateModel) InsertWorldCompatibility(compatibility *Compatibility, worldId int64) error {
|
||||
query := `
|
||||
INSERT INTO worlds_compatibility (world_id, minumum, verified, maximum)
|
||||
INSERT INTO worlds_compatibility (world_id, minimum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
@@ -57,7 +57,11 @@ func (m FoundryStateModel) InsertWorldCompatibility(compatibility *Compatibility
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetWorlds(idState int64) (Worlds, error) {
|
||||
|
||||
Reference in New Issue
Block a user