Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit a2c2204

Browse files
committed
merge sendemail to entity Type
1 parent 9e4db08 commit a2c2204

File tree

6 files changed

+26
-38
lines changed

6 files changed

+26
-38
lines changed

gutils/tasks.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ func init() {
2828
}
2929
}
3030

31-
func composeEmail(ent models.Entity) string {
32-
key, err := ent.K.MarshalJSON()
33-
if err != nil {
34-
key = []byte("unrecognizable key")
35-
}
36-
return fmt.Sprintf(`Link to the website: %s
37-
Name: %s
38-
XPATH: %s
39-
Key: %s`, ent.URL, ent.Name, ent.XPATH, key)
40-
}
41-
4231
// Refresh refreshes prices from datastore
4332
func Refresh() {
4433
log.Println("INFO: Refresh started")
@@ -61,10 +50,10 @@ func Refresh() {
6150
}
6251
if ent.Options.AlertType == "onChange" && content != last.Price {
6352
subject := fmt.Sprintf("[%s] <%s> Alert: price changes to %s!", email.Identity, ent.Name, content)
64-
email.Send(composeEmail(ent), subject, ent.Options.Email)
53+
ent.SendEmail(&subject)
6554
} else if ent.Options.AlertType == "threshold" && ent.Options.Threshold >= float32(thisP) {
6655
subject := fmt.Sprintf("[%s] <%s> Alert: price drops to %s!", email.Identity, ent.Name, content)
67-
email.Send(composeEmail(ent), subject, ent.Options.Email)
56+
ent.SendEmail(&subject)
6857
}
6958

7059
// update history & save entity
@@ -88,7 +77,7 @@ func Refresh() {
8877
key, _ := ent.K.MarshalJSON()
8978
log.Printf("URL: %s\nXPATH: %s\nKey: %s", ent.URL, ent.XPATH, key)
9079
subject := fmt.Sprintf("[%s] <%s> Alert: failed to fetch price for reason `%s`!", email.Identity, ent.Name, content)
91-
email.Send(composeEmail(ent), subject, ent.Options.Email)
80+
ent.SendEmail(&subject)
9281
}
9382
}
9483
}

handlers/create.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/labstack/echo"
11+
"github.com/xiahongze/pricetracker/email"
1112
"github.com/xiahongze/pricetracker/gutils"
1213
"github.com/xiahongze/pricetracker/models"
1314
"github.com/xiahongze/pricetracker/trackers"
@@ -59,7 +60,8 @@ func Create(c echo.Context) error {
5960
return err
6061
}
6162

62-
sendEmail(&entity, "Created")
63+
subject := fmt.Sprintf("[%s] <%s> INFO: %s!", email.Identity, entity.Name, "Created")
64+
entity.SendEmail(&subject)
6365

6466
return c.JSON(http.StatusCreated, entity)
6567
}

handlers/delete.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package handlers
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67

78
"github.com/labstack/echo"
9+
"github.com/xiahongze/pricetracker/email"
810
"github.com/xiahongze/pricetracker/gutils"
911
"github.com/xiahongze/pricetracker/models"
1012
)
@@ -28,7 +30,8 @@ func Delete(c echo.Context) error {
2830
return err
2931
}
3032

31-
sendEmail(entity, "Deleted")
33+
subject := fmt.Sprintf("[%s] <%s> INFO: %s!", email.Identity, entity.Name, "Deleted")
34+
entity.SendEmail(&subject)
3235

3336
return c.JSON(http.StatusOK, entity)
3437
}

handlers/sendemail.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

handlers/update.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package handlers
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67

78
"github.com/labstack/echo"
9+
"github.com/xiahongze/pricetracker/email"
810
"github.com/xiahongze/pricetracker/gutils"
911
"github.com/xiahongze/pricetracker/models"
1012
)
@@ -50,7 +52,8 @@ func Update(c echo.Context) error {
5052
return err
5153
}
5254

53-
sendEmail(entity, "Updated")
55+
subject := fmt.Sprintf("[%s] <%s> INFO: %s!", email.Identity, entity.Name, "Updated")
56+
entity.SendEmail(&subject)
5457

5558
return c.JSON(http.StatusOK, entity)
5659
}

models/entity.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"cloud.google.com/go/datastore"
10+
"github.com/xiahongze/pricetracker/email"
1011
)
1112

1213
type (
@@ -55,3 +56,14 @@ func (entity *Entity) Save(ctx context.Context, entTypName string, dsClient *dat
5556
}
5657
return nil
5758
}
59+
60+
// SendEmail does what the name says
61+
func (entity *Entity) SendEmail(subject *string) {
62+
if b, err := json.MarshalIndent(entity, "", " "); err == nil {
63+
if err := email.Send(string(b), *subject, entity.Options.Email); err != nil {
64+
log.Print("failed to send email", err)
65+
}
66+
} else {
67+
log.Print("failed to marshal entity", err)
68+
}
69+
}

0 commit comments

Comments
 (0)