working creation/editing of watch (with filters/connections)

This commit is contained in:
BroodjeAap 2022-09-25 14:56:15 +00:00
parent 1a67ecc056
commit 81d8a01a65
7 changed files with 94 additions and 116 deletions

163
main.go
View file

@ -38,11 +38,12 @@ func (web Web) canvas(c *gin.Context) {
c.HTML(http.StatusOK, "canvas", gin.H{})
}
func (web Web) newWatch(c *gin.Context) {
c.HTML(http.StatusOK, "newWatch", gin.H{})
func (web Web) watchCreate(c *gin.Context) {
c.HTML(http.StatusOK, "watchCreate", gin.H{})
}
func (web Web) createWatch(c *gin.Context) {
func (web Web) watchCreatePost(c *gin.Context) {
log.Println("TEST")
var watch Watch
errMap, err := bindAndValidateWatch(&watch, c)
if err != nil {
@ -50,7 +51,7 @@ func (web Web) createWatch(c *gin.Context) {
return
}
web.db.Create(&watch)
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/view/%d", watch.ID))
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/%d", watch.ID))
}
func (web Web) deleteWatch(c *gin.Context) {
@ -84,26 +85,51 @@ func (web Web) watchView(c *gin.Context) {
}
func (web Web) watchSave(c *gin.Context) {
var watchId = c.PostForm("watch_id")
var watch Watch
web.db.Model(&Watch{}).First(&watch, watchId)
bindAndValidateWatch(&watch, c)
var filters []Filter
web.db.Save(&watch)
var newFilters []Filter
var filtersJson = c.PostForm("filters")
if err := json.Unmarshal([]byte(filtersJson), &filters); err != nil {
if err := json.Unmarshal([]byte(filtersJson), &newFilters); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
var connections []FilterConnection
var newConnections []FilterConnection
var connectionsJson = c.PostForm("connections")
if err := json.Unmarshal([]byte(connectionsJson), &connections); err != nil {
if err := json.Unmarshal([]byte(connectionsJson), &newConnections); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/view/%d", watch.ID))
var oldFilters []Filter
web.db.Model(&Filter{}).Where("watch_id = ?", watch.ID).Find(&oldFilters)
filterMap := make(map[uint]*Filter)
for i := range newFilters {
filter := &newFilters[i]
filterMap[filter.ID] = filter
filter.ID = 0
}
web.db.Delete(&Filter{}, "watch_id = ?", watch.ID)
if len(newFilters) > 0 {
web.db.Create(&newFilters)
}
web.db.Delete(&FilterConnection{}, "watch_id = ?", watch.ID)
log.Println(len(filterMap))
for i := range newConnections {
connection := &newConnections[i]
connection.OutputID = filterMap[connection.OutputID].ID
connection.InputID = filterMap[connection.InputID].ID
}
if len(newConnections) > 0 {
web.db.Create(&newConnections)
}
c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/%d", watch.ID))
}
/*
@ -167,48 +193,6 @@ func (web Web) viewWatch(c *gin.Context) {
}
*/
func (web Web) createFilter(c *gin.Context) {
var filter Filter
errMap, err := bindAndValidateFilter(&filter, c)
if err != nil {
log.Print(err)
c.HTML(http.StatusBadRequest, "500", errMap)
return
}
web.db.Create(&filter)
c.Redirect(http.StatusSeeOther, "/group/edit")
}
func (web Web) updateFilter(c *gin.Context) {
var filterUpdate FilterUpdate
errMap, err := bindAndValidateFilterUpdate(&filterUpdate, c)
if err != nil {
log.Print(err)
c.HTML(http.StatusBadRequest, "500", errMap)
return
}
var filter Filter
web.db.First(&filter, filterUpdate.ID)
filter.Name = filterUpdate.Name
filter.Type = filterUpdate.Type
filter.Var1 = filterUpdate.From
//filter.Var2 = &filterUpdate.To
web.db.Save(&filter)
c.Redirect(http.StatusSeeOther, "/group/edit/")
}
func (web Web) deleteFilter(c *gin.Context) {
id, err := strconv.Atoi(c.PostForm("filter_id"))
if err != nil {
c.Redirect(http.StatusSeeOther, "/watch/new")
return
}
group_id := c.PostForm("group_id")
web.db.Delete(&Filter{}, id)
c.Redirect(http.StatusSeeOther, "/group/edit/"+group_id)
}
func passiveBot(bot *tgbotapi.BotAPI) {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
@ -253,39 +237,40 @@ func main() {
db, _ := gorm.Open(sqlite.Open(viper.GetString("database.dsn")))
db.AutoMigrate(&Watch{}, &Filter{}, &FilterConnection{})
/*
watch := Watch{
Name: "LG C2 42",
Interval: 60,
}
db.Create(&watch)
watch := Watch{
Name: "LG C2 42",
Interval: 60,
}
db.Create(&watch)
urlFilter := Filter{
WatchID: watch.ID,
Name: "PriceWatch Fetch",
X: 100,
Y: 100,
Type: "url",
Var1: "https://tweakers.net/pricewatch/1799060/lg-c2-42-inch-donkerzilveren-voet-zwart.html",
}
db.Create(&urlFilter)
urlFilter := Filter{
WatchID: watch.ID,
Name: "PriceWatch Fetch",
X: 100,
Y: 100,
Type: "url",
Var1: "https://tweakers.net/pricewatch/1799060/lg-c2-42-inch-donkerzilveren-voet-zwart.html",
}
db.Create(&urlFilter)
xpathFilter := Filter{
WatchID: watch.ID,
Name: "price select",
X: 300,
Y: 300,
Type: "xpath",
Var1: "//td[@class='shop-price']",
}
db.Create(&xpathFilter)
xpathFilter := Filter{
WatchID: watch.ID,
Name: "price select",
X: 300,
Y: 300,
Type: "xpath",
Var1: "//td[@class='shop-price']",
}
db.Create(&xpathFilter)
connection := FilterConnection{
WatchID: watch.ID,
OutputID: urlFilter.ID,
InputID: xpathFilter.ID,
}
db.Create(&connection)
connection := FilterConnection{
WatchID: watch.ID,
OutputID: urlFilter.ID,
InputID: xpathFilter.ID,
}
db.Create(&connection)
*/
//bot, _ := tgbotapi.NewBotAPI(viper.GetString("telegram.token"))
//bot.Debug = true
@ -304,7 +289,7 @@ func main() {
templates := multitemplate.NewRenderer()
templates.AddFromFiles("index", "templates/base.html", "templates/index.html")
templates.AddFromFiles("newWatch", "templates/base.html", "templates/newWatch.html")
templates.AddFromFiles("watchCreate", "templates/base.html", "templates/watchCreate.html")
templates.AddFromFiles("viewWatch", "templates/base.html", "templates/viewWatch.html")
templates.AddFromFiles("editGroup", "templates/base.html", "templates/editGroup.html")
@ -314,13 +299,9 @@ func main() {
router.HTMLRender = templates
router.GET("/", web.index)
router.GET("/watch/new", web.newWatch)
router.POST("/watch/create", web.createWatch)
router.GET("/watch/new", web.watchCreate)
router.POST("/watch/create", web.watchCreatePost)
router.POST("/watch/delete", web.deleteWatch)
//router.GET("/watch/view/:id/", web.viewWatch)
router.POST("/filter/create/", web.createFilter)
router.POST("/filter/update/", web.updateFilter)
router.POST("/filter/delete/", web.deleteFilter)
router.GET("/watch/:id", web.watchView)
router.POST("/watch/save", web.watchSave)

View file

@ -1,17 +1,13 @@
package main
import (
"gorm.io/gorm"
)
type Watch struct {
gorm.Model
ID uint `form:"watch_id" yaml:"watch_id"`
Name string `form:"watch_name" yaml:"watch_name" binding:"required" validate:"min=1"`
Interval int `form:"interval" yaml:"interval" binding:"required"`
}
type Filter struct {
gorm.Model
ID uint `form:"filter_id" yaml:"filter_id" json:"filter_id"`
WatchID uint `form:"filter_watch_id" yaml:"filter_watch_id" json:"filter_watch_id" binding:"required"`
Name string `form:"filter_name" yaml:"filter_name" json:"filter_name" binding:"required" validate:"min=1"`
X int `form:"x" yaml:"x" json:"x" validate:"default=0"`
@ -24,8 +20,8 @@ type Filter struct {
}
type FilterConnection struct {
gorm.Model
WatchID uint `form:"connection_watch_id" yaml:"connection_watch_id" binding:"required"`
ID uint `form:"filter_connection_id" yaml:"filter_connection_id" json:"filter_connection_id"`
WatchID uint `form:"connection_watch_id" yaml:"connection_watch_id" json:"connection_watch_id" binding:"required"`
OutputID uint `form:"filter_output_id" yaml:"filter_output_id" json:"filter_output_id" binding:"required"`
InputID uint `form:"filter_input_id" yaml:"filter_input_id" json:"filter_input_id" binding:"required"`
}

View file

@ -181,11 +181,11 @@ function saveWatch() {
for (var _c = __values(_diagram.nodes.values()), _d = _c.next(); !_d.done; _d = _c.next()) {
var filter = _d.value;
filters.push({
WatchID: watchId,
id: filter.id,
filter_watch_id: watchId,
filter_id: filter.id,
filter_name: filter.label,
x: filter.x,
y: filter.y,
x: Math.round(filter.x),
y: Math.round(filter.y),
// @ts-ignore
filter_type: filter.meta.type,
// @ts-ignore
@ -211,7 +211,7 @@ function saveWatch() {
for (var _e = __values(_diagram.connections), _f = _e.next(); !_f.done; _f = _e.next()) {
var _g = __read(_f.value, 2), output = _g[0], input = _g[1];
connections.push({
WatchID: watchId,
connection_watch_id: watchId,
filter_output_id: output.id,
filter_input_id: input.id
});

View file

@ -176,11 +176,11 @@ function saveWatch(){
let filters = new Array<Object>();
for (let filter of _diagram.nodes.values()){
filters.push({
WatchID: watchId,
id: filter.id,
filter_watch_id: watchId,
filter_id: filter.id,
filter_name: filter.label,
x: filter.x,
y: filter.y,
x: Math.round(filter.x),
y: Math.round(filter.y),
// @ts-ignore
filter_type: filter.meta.type,
// @ts-ignore
@ -197,7 +197,7 @@ function saveWatch(){
let connections = new Array<Object>();
for (let [output, input] of _diagram.connections){
connections.push({
WatchID: watchId,
connection_watch_id: watchId,
filter_output_id: output.id,
filter_input_id: input.id,
})

View file

@ -72,6 +72,11 @@
Edit Watch
</button>
</div>
<div>
<button type="button" id="filterButton" class="btn btn-primary btn-block btn-sm" data-bs-toggle="modal" data-bs-target="#WatchModal">
Save
</button>
</div>
<div class="modal fade" id="WatchModal" tabindex="-1" aria-labelledby="WatchModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
@ -92,20 +97,16 @@
<label for="scheduleInput" class="col-sm-2 col-form-label">Schedule:</label>
<div class="col-sm-10 p-2">
<select id="scheduleInput" class="form-control" name="schedule">
<select id="scheduleInput" class="form-control" name="schedule_id">
<option value=".ID">.Name</option>
</select>
</div>
<label for="proxyInput" class="col-sm-2 col-form-label">Proxy Pool:</label>
<div class="col-sm-10 p-2">
<select id="proxyInput" class="form-control" name="proxy_pool">
<select id="proxyInput" class="form-control" name="proxy_pool_id">
<option value=".ID">.Name</option>
</select>
</div>
<label for="intervalInput" class="col-sm-2 col-form-label">Interval</label>
<div class="col-sm-10 p-2">
<input type="number" class="form-control" name="interval" id="intervalInput" placeholder="">
</div>
<input type="hidden" id="filtersInput" name="filters" value="">
<input type="hidden" id="connectionsInput" name="connections" value="">
</div>

View file

@ -9,7 +9,7 @@
</thead>
<tbody>
{{ range . }}
<tr class="pointer" onclick="window.location='/watch/view/{{ .ID }}'">
<tr class="pointer" onclick="window.location='/watch/{{ .ID }}'">
<td class="h3">{{ .Name }}</td>
<td class="h3">{{ .Interval }}</td>
<td>