forked from taipei-doit/Taipei-City-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish incident routes, modify AdminDisaster.vue (#15)
- Loading branch information
1 parent
45902ae
commit 9a64fd7
Showing
8 changed files
with
406 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"TaipeiCityDashboardBE/app/models" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func GetIncident(c *gin.Context) { | ||
type incidentQuery struct { | ||
PageSize int `form:"pagesize"` | ||
PageNum int `form:"pagenum"` | ||
Sort string `form:"sort"` | ||
Order string `form:"order"` | ||
} | ||
|
||
// Get query parameters | ||
var query incidentQuery | ||
c.ShouldBindQuery(&query) | ||
|
||
incidents, totalIncidents, resultNum, err := models.GetAllIncident(query.PageSize, query.PageNum, query.Sort, query.Order) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "success", "total": totalIncidents, "results": resultNum, "data": incidents}) | ||
} | ||
|
||
func CreateIncident(c *gin.Context) { | ||
var incident models.Incident | ||
// var buf bytes.Buffer | ||
// _, err := io.Copy(&buf, c.Request.Body) | ||
// if err != nil { | ||
// // Handle error | ||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"}) | ||
// return | ||
// } | ||
|
||
// Convert buffer to string | ||
// requestBody := buf.String() | ||
// fmt.Println(requestBody) | ||
|
||
// Bind the issue data | ||
if err := c.ShouldBindJSON(&incident); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
if incident.Type == "" || incident.Description == "" || incident.Distance == 0 { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "title, description, distance info is required"}) | ||
return | ||
} | ||
|
||
tmpIncident, err := models.CreateIncident(incident.Type, incident.Description, incident.Distance, incident.Latitude, incident.Longitude, incident.Time) | ||
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}) | ||
} | ||
|
||
func DeleteIncident(c *gin.Context) { | ||
var incident models.Incident | ||
|
||
// Bind the issue data | ||
if err := c.ShouldBindJSON(&incident); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
// if incident.Type == "" || incident.Description == "" || incident.Distance == 0 { | ||
// c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "title, description, distance info is required"}) | ||
// return | ||
// } | ||
|
||
tmpIncident, err := models.DeleteIncident(incident.ID) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "success", "data": tmpIncident}) | ||
} | ||
|
||
|
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,43 @@ | ||
package controllers | ||
|
||
import ( | ||
"TaipeiCityDashboardBE/app/models" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
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}) | ||
} |
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,81 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type Incident struct { | ||
ID uint `gorm:"primaryKey"` | ||
Type string `json:"inctype"` | ||
Description string `json:"description"` | ||
Distance float64 `json:"distance"` | ||
Latitude float64 `json:"latitude"` | ||
Longitude float64 `json:"longitude"` | ||
Time int64 `json:"reportTime"` | ||
} | ||
|
||
func GetAllIncident(pageSize int, pageNum int, sort string, order string) (incidents []Incident, totalIncidents int64, resultNum int64, err error) { | ||
tempDB := DBManager.Table("incidents") | ||
|
||
// Count the total amount of incidents | ||
tempDB.Count(&totalIncidents) | ||
|
||
tempDB.Count(&resultNum) | ||
|
||
// Sort the issues | ||
if sort != "" { | ||
tempDB = tempDB.Order(sort + " " + order) | ||
} | ||
|
||
// Paginate the issues | ||
if pageSize > 0 { | ||
tempDB = tempDB.Limit(pageSize) | ||
if pageNum > 0 { | ||
tempDB = tempDB.Offset((pageNum - 1) * pageSize) | ||
} | ||
} | ||
|
||
// Get the incidents | ||
err = tempDB.Find(&incidents).Error | ||
|
||
return incidents, totalIncidents, resultNum, err | ||
} | ||
|
||
func CreateIncident(incidentType, description string, distance, latitude, longitude float64, incidentTime int64) (incident Incident, err error) { | ||
|
||
// Create an example incident | ||
// incident = Incident{ | ||
// Type: "Accident", | ||
// Description: "Car crash on the highway", | ||
// Distance: 2.5, | ||
// Latitude: 40.7128, | ||
// Longitude: -74.0060, | ||
// Time: time.Now().Unix(), | ||
// } | ||
incident = Incident { | ||
Type: incidentType, | ||
Description: description, | ||
Distance: distance, | ||
Latitude: latitude, | ||
Longitude: longitude, | ||
Time: incidentTime, | ||
} | ||
|
||
// Insert the incident into the database | ||
err = DBManager.Create(&incident).Error | ||
|
||
return incident, err | ||
} | ||
|
||
func DeleteIncident(id uint) (incident Incident, err error) { | ||
|
||
if err := DBManager.Where("ID = ?", id).First(&incident, 1).Error; err != nil { | ||
// Handle error (e.g., incident not found) | ||
fmt.Printf("Incident not found") | ||
return Incident{}, err | ||
} | ||
|
||
// Delete the incident | ||
err = DBManager.Delete(&incident).Error | ||
return incident, err | ||
} |
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,47 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
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 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 | ||
} |
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
Oops, something went wrong.