Ma Suhyeon

Implement post call, post message, post app analysis

......@@ -63,10 +63,21 @@ func NewApp(config Config) *App {
app.echo.POST("/extractions", app.PostExtractions, auth)
extraction := app.echo.Group("/extractions/:no")
extraction.GET("/calls", app.GetCalls)
extraction.POST("/calls", app.PostCall)
extraction.DELETE("/calls/:id", app.DeleteCall)
extraction.GET("/messages", app.GetMessages)
extraction.POST("/messages", app.PostMessage)
extraction.DELETE("/messages/:id", app.DeleteMessage)
extraction.GET("/calls/analyses", app.GetCallsAnalyses)
extraction.GET("/apps/analyses", app.GetAppsAnalyses)
extraction.POST("/apps/analyses", app.PostAppAnalysis)
extraction.DELETE("/apps/analyses/:package", app.DeleteAppAnalysis)
extraction.GET("/messages/analyses", app.GetMessagesAnalyses)
extraction.GET("/processes", app.GetProcesses)
extraction.GET("/alarms", app.GetAlarms)
......
......@@ -33,6 +33,37 @@ func (app *App) GetCalls(c echo.Context) error {
return c.JSON(http.StatusOK, calls)
}
func (app *App) PostCall(c echo.Context) error {
call := Call{}
if err := c.Bind(&call); err != nil {
return err
}
query := "INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?)"
_, err := app.db.Exec(query, c.Param("no"), call.ID, call.Type, call.Name, call.Number, call.Duration, call.Date)
if err != nil {
return nil
}
return c.NoContent(http.StatusNoContent)
}
func (app *App) DeleteCall(c echo.Context) error {
query := "DELETE FROM calls WHERE `extraction_no`=? AND `id`=?"
res, err := app.db.Exec(query, c.Param("no"), c.Param("id"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the call")
} else {
return c.NoContent(http.StatusNoContent)
}
}
type CallStats struct {
Number string `json:"number" db:"number"`
Name *string `json:"name" db:"name"`
......@@ -73,6 +104,41 @@ func (app *App) GetAppsAnalyses(c echo.Context) error {
return c.JSON(http.StatusOK, apps)
}
func (app *App) PostAppAnalysis(c echo.Context) error {
info := AppInfo{}
if err := c.Bind(&info); err != nil {
return err
}
query := "INSERT INTO apps VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
_, err := app.db.Exec(
query,
c.Param("no"), info.PackageName, info.Name, info.Version,
info.WifiUsage, info.CellularUsage, info.LastUsed, info.ForegroundTime,
)
if err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
func (app *App) DeleteAppAnalysis(c echo.Context) error {
query := "DELETE FROM apps WHERE `extraction_no`=? AND `package_name`=?"
res, err := app.db.Exec(query, c.Param("no"), c.Param("package"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the app")
} else {
return c.NoContent(http.StatusNoContent)
}
}
type Message struct {
ID int `json:"id" db:"id"`
Type int `json:"type" db:"type"`
......@@ -114,6 +180,38 @@ func (app *App) GetMessages(c echo.Context) error {
return c.JSON(http.StatusOK, messages)
}
func (app *App) PostMessage(c echo.Context) error {
message := Message{}
if err := c.Bind(&message); err != nil {
return err
}
query := "INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)"
_, err := app.db.Exec(query, c.Param("no"), message.ID, message.Type, message.Address, message.Body, message.Date)
if err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
}
func (app *App) DeleteMessage(c echo.Context) error {
query := "DELETE FROM messages WHERE `extraction_no`=? AND `id`=?"
res, err := app.db.Exec(query, c.Param("no"), c.Param("id"))
if err != nil {
return err
}
if rows, err := res.RowsAffected(); err != nil {
return err
} else if rows == 0 {
return echo.NewHTTPError(http.StatusNotFound, "Can not find the message")
} else {
return c.NoContent(http.StatusNoContent)
}
}
type MessageStats struct {
Address string `json:"address" db:"address"`
Receive int `json:"receive" db:"receive"`
......