Skip to content

Commit

Permalink
Move IncidentType to DBDashboard (rakechen-0307#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
dienruei123 committed May 26, 2024
1 parent a569bf8 commit 640ae9a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions Taipei-City-Dashboard-BE/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func MigrateManagerSchema() {

func InsertDashbaordSampleData() {
models.ConnectToDatabases("DASHBOARD")
models.DBDashboard.AutoMigrate(&models.IncidentType{})
initial.InitSampleCityData()
models.CloseConnects("DASHBOARD")
}
7 changes: 7 additions & 0 deletions Taipei-City-Dashboard-BE/app/controllers/incident.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"net/http"

"TaipeiCityDashboardBE/app/models"
Expand Down Expand Up @@ -113,6 +114,12 @@ func UpdateIncidentType(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()})
return
}
var tmpinc models.Incident
if err := models.DBManager.Table("incidents").Where("id = ?", incident.ID).First(&tmpinc, 1).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()})
return
}
fmt.Println(incident.Type)
tmpIncident, err := models.UpdateIncidentType(incident.Type)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()})
Expand Down
2 changes: 1 addition & 1 deletion Taipei-City-Dashboard-BE/app/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func MigrateManagerSchema() {
DBManager.AutoMigrate(&Component{}, &ComponentChart{}, &ComponentMap{})
DBManager.AutoMigrate(&Contributor{})
DBManager.AutoMigrate(&Dashboard{}, &DashboardGroup{}, &Issue{})
DBManager.AutoMigrate(&Incident{}, &IncidentType{})
DBManager.AutoMigrate(&Incident{})

// All users beneath the public group do not need to be added to the public group
// DBManager.Exec("ALTER TABLE auth_user_group_roles ADD CONSTRAINT check_group_id CHECK (group_id > 1);")
Expand Down
21 changes: 10 additions & 11 deletions Taipei-City-Dashboard-BE/app/models/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,25 @@ func CreateIncidentType(incidentType string) (newType IncidentType, err error){
Type: incidentType,
Count: 0,
}
if err := DBManager.Where("type = ?", incidentType).Error; err == nil {
fmt.Printf("Incident type " + incidentType + " already exists!!")
return IncidentType{}, nil
var tmpType IncidentType
if err := DBDashboard.Where("type = ?", incidentType).First(&tmpType).Error; err != nil {
err = DBDashboard.Create(&newType).Error
return newType, err
}

tempDB := DBManager.Table("incident_types")
err = tempDB.Create(&newType).Error
return newType, err
// Handle error (e.g., incident not found)
fmt.Printf("Incident type " + incidentType + " already exist!!")
return IncidentType{}, err
}

func UpdateIncidentType(incidentType string) (updType IncidentType, err error){
if err := DBManager.Where("type = ?", incidentType).First(&updType, 1).Error; err != nil {
if err := DBDashboard.Where("type = ?", incidentType).First(&updType).Error; err != nil {
// Handle error (e.g., incident not found)
fmt.Printf("Incident type" + incidentType + " not found")
fmt.Printf("Incident type " + incidentType + " not found")
return IncidentType{}, err
}

updType.Count += 1
tempDB := DBManager.Table("incident_types")
if err := tempDB.Save(&updType).Error; err != nil {
if err := DBDashboard.Save(&updType).Error; err != nil {
// Handle error
fmt.Printf("Failed to update incident type " + incidentType)
return IncidentType{}, err
Expand Down

0 comments on commit 640ae9a

Please sign in to comment.