Skip to content

Commit

Permalink
Merge branch 'main' into dev3
Browse files Browse the repository at this point in the history
Signed-off-by: 陳柏睿 <[email protected]>
  • Loading branch information
rakechen-0307 authored May 11, 2024
2 parents 9e1b0b8 + 9e26aab commit 1ccb642
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 91 deletions.
33 changes: 33 additions & 0 deletions Taipei-City-Dashboard-BE/app/controllers/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,36 @@ func DeleteIncident(c *gin.Context) {
}


func CreateIncidentType(c *gin.Context) {
var incidentType models.IncidentType

if err := c.ShouldBindJSON(&incidentType); (err != nil) || (incidentType.Type == "") {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()})
return
}
tmpIncident, err := models.CreateIncidentType(incidentType.Type)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()})
return
}
if tmpIncident.IsEmpty() {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": "Incident type " + incidentType.Type + " already exists!!"})
return
}
c.JSON(http.StatusCreated, gin.H{"status": "success", "data": tmpIncident})
}

func UpdateIncidentType(c *gin.Context) {
var incident models.Incident

if err := c.ShouldBindJSON(&incident); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()})
return
}
tmpIncident, err := models.UpdateIncidentType(incident.Type)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"status": "success", "data": tmpIncident})
}
43 changes: 0 additions & 43 deletions Taipei-City-Dashboard-BE/app/controllers/incident_type.go

This file was deleted.

1 change: 1 addition & 0 deletions Taipei-City-Dashboard-BE/app/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func MigrateManagerSchema() {
DBManager.AutoMigrate(&AuthUserGroupRole{})
DBManager.AutoMigrate(&Component{}, &ComponentChart{}, &ComponentMap{})
DBManager.AutoMigrate(&Dashboard{}, &DashboardGroup{}, &Issue{})
DBManager.AutoMigrate(&Incident{}, &IncidentType{})

// 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
45 changes: 45 additions & 0 deletions Taipei-City-Dashboard-BE/app/models/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ type Incident struct {
Time int64 `json:"reportTime"`
}

type IncidentType struct {
ID uint `gorm:"primaryKey"`
Type string `json:"type" gorm:"not null"`
Count int `json:"count"`
}

func (m IncidentType) IsEmpty() bool {
return m.Type == "" && m.Count == 0
}


func GetAllIncident(pageSize int, pageNum int, sort string, order string) (incidents []Incident, totalIncidents int64, resultNum int64, err error) {
tempDB := DBManager.Table("incidents")

Expand Down Expand Up @@ -79,3 +90,37 @@ func DeleteIncident(id uint) (incident Incident, err error) {
err = DBManager.Delete(&incident).Error
return incident, err
}

func CreateIncidentType(incidentType string) (newType IncidentType, err error){
newType = IncidentType{
Type: incidentType,
Count: 0,
}
if err := DBManager.Where("type = ?", incidentType).Error; err == nil {
fmt.Printf("Incident type " + incidentType + " already exists!!")
return IncidentType{}, nil
}

tempDB := DBManager.Table("incident_types")
err = tempDB.Create(&newType).Error
return newType, err
}

func UpdateIncidentType(incidentType string) (updType IncidentType, err error){
if err := DBManager.Where("type = ?", incidentType).First(&updType, 1).Error; err != nil {
// Handle error (e.g., incident 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 {
// Handle error
fmt.Printf("Failed to update incident type " + incidentType)
return IncidentType{}, err
}
return updType, err
}


47 changes: 0 additions & 47 deletions Taipei-City-Dashboard-BE/app/models/incident_type.go

This file was deleted.

2 changes: 1 addition & 1 deletion Taipei-City-Dashboard-FE/public/mapData/incident.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@
}
],
"type": "FeatureCollection"
}
}

0 comments on commit 1ccb642

Please sign in to comment.