Skip to content

Commit cfe82c4

Browse files
committed
* added delete vote functionality
* fixed index error * removed setUserId, using session id instead
1 parent 08a4413 commit cfe82c4

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

app/controllers/app.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ func (c *App) Index (arg models.Args) revel.Result {
5555
}
5656

5757
func (c *App) Quote (id int) revel.Result {
58-
e := c.getEntry(id)
59-
return Utf8Result(e[0].Quote)
58+
var quote string
59+
if e := c.getEntry(id); len(e) != 0 {
60+
quote = e[0].Quote
61+
}
62+
return Utf8Result(quote)
6063
}
6164

6265
func (c *App) AdvSearch () revel.Result {

app/controllers/base.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"github.com/robfig/revel"
55
"github.com/PacketFire/goqdb/app/models"
66
"strings"
7-
"encoding/hex"
8-
"crypto/md5"
9-
"io"
107
"time"
118
)
129

@@ -22,16 +19,6 @@ const (
2219
ERR_ID_NOT_FOUND = "id not found"
2320
)
2421

25-
func setUserId (c *revel.Controller) revel.Result {
26-
h := md5.New()
27-
28-
io.WriteString(h, c.Request.RemoteAddr)
29-
io.WriteString(h, c.Request.Header.Get("User-Agent"))
30-
31-
c.Params.Set("userid", hex.EncodeToString(h.Sum(nil)))
32-
return nil
33-
}
34-
3522
func (c *Base) getEntries (arg models.Args) []models.Quote {
3623
var entries []models.Quote
3724

@@ -138,9 +125,7 @@ func (c *Base) getTotal (arg models.Args) int64 {
138125

139126
func (c *Base) insertQuote (quote *models.Quote) {
140127

141-
var userId string
142-
143-
c.Params.Bind(&userId, "userid")
128+
userId := c.Session.Id()
144129

145130
if !c.canPost(userId) {
146131
c.Flash.Error(ERR_POST_THRESHOLD_REACHED)
@@ -223,15 +208,15 @@ func (c *Base) vote (voteId int, voteType string) {
223208
return
224209
}
225210

226-
var userId string
227-
c.Params.Bind(&userId, "userid")
228-
229-
if !c.canVote(voteId, userId) {
230-
c.Flash.Error(ERR_MULTIPLE_VOTE)
231-
return
232-
}
211+
userId := c.Session.Id()
233212

234213
if voteType != models.VOTE_DELETE {
214+
215+
if !c.canVote(voteId, userId) {
216+
c.Flash.Error(ERR_MULTIPLE_VOTE)
217+
return
218+
}
219+
235220
query := `UPDATE QuoteEntry SET Rating = Rating`
236221
switch voteType {
237222
case models.VOTE_UP:
@@ -250,6 +235,21 @@ func (c *Base) vote (voteId int, voteType string) {
250235
c.Flash.Error(ERR_ID_NOT_FOUND)
251236
return
252237
}
238+
} else {
239+
t := time.Now().Truncate(24 * time.Hour).Unix()
240+
res, err := c.Txn.Exec(
241+
`DELETE FROM QuoteEntry ` +
242+
`WHERE QuoteId = ? AND UserId = ? AND Created >= ?`,
243+
voteId, userId, t)
244+
245+
if err != nil {
246+
panic(err)
247+
}
248+
if n, err := res.RowsAffected(); n >= 1 {
249+
return
250+
} else if err != nil {
251+
panic(err)
252+
}
253253
}
254254

255255
err := c.Txn.Insert(&models.VoteEntry{

app/controllers/init.go

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ func init() {
1010
revel.InterceptMethod((*GorpController).Begin, revel.BEFORE)
1111
revel.InterceptMethod((*GorpController).Commit, revel.AFTER)
1212
revel.InterceptMethod((*GorpController).Rollback, revel.FINALLY)
13-
14-
revel.InterceptFunc(setUserId, revel.BEFORE, &Base{})
1513
}
1614

app/views/App/Index.html

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
[{{$entry.Rating}}]
7070
<a href="{{url "App.Vote" $entry.QuoteId "down" $arg}}"
7171
class="btn btn-danger btn-xs btn-vote">-</a>
72+
73+
<a href="{{url "App.Vote" $entry.QuoteId "delete" $arg}}"
74+
class="btn btn-warning btn-xs btn-vote">x</a>
75+
</p>
7276
</p>
7377

7478
<ul class="tags list-inline">

0 commit comments

Comments
 (0)