Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

initial implementation of making comments

Cette révision appartient à :
Andrew Zhao 2017-05-07 20:34:12 -07:00
Parent 2e3c45cbc7
révision b232018e6b
5 fichiers modifiés avec 48 ajouts et 10 suppressions

Voir le fichier

@ -12,8 +12,8 @@ type Comment struct {
Username string `json:"username"` // this is duplicate but it'll be faster rite?
TorrentId int
// LikingCount int `json:"likingCount"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `json:"deletedAt"`
User User `json:"user"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt""`
User User `json:"user"`
}

Voir le fichier

@ -46,7 +46,8 @@ func init() {
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
Router.Handle("/faq", gzipFaqHandler).Name("faq")
Router.Handle("/feed", gzipRssHandler).Name("feed")
Router.Handle("/view/{id}", gzipViewHandler).Name("view_torrent")
Router.Handle("/view/{id}", gzipViewHandler).Methods("GET").Name("view_torrent")
Router.HandleFunc("/view/{id}", PostCommentHandler).Methods("POST").Name("post_comment")
Router.Handle("/upload", gzipUploadHandler).Name("upload")
Router.Handle("/user/register", gzipUserRegisterFormHandler).Name("user_register").Methods("GET")
Router.Handle("/user/login", gzipUserLoginFormHandler).Name("user_login").Methods("GET")

Voir le fichier

@ -7,6 +7,7 @@ import (
"github.com/ewhal/nyaa/model"
userForms "github.com/ewhal/nyaa/service/user/form"
"github.com/ewhal/nyaa/service/user"
"github.com/ewhal/nyaa/service/captcha"
"github.com/gorilla/mux"
)
@ -34,6 +35,7 @@ type NotFoundTemplateVariables struct {
type ViewTemplateVariables struct {
Torrent model.TorrentsJson
Captcha captcha.Captcha
Search SearchForm
Navigation Navigation
User model.User
@ -137,4 +139,4 @@ func NewSearchForm(params ...string) (searchForm SearchForm) {
func GetUser(r *http.Request) model.User {
user, _ , _ := userService.RetrieveCurrentUser(r)
return user
}
}

Voir le fichier

@ -2,7 +2,11 @@ package router
import (
"net/http"
"strconv"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/captcha"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/log"
"github.com/gorilla/mux"
@ -18,11 +22,32 @@ func ViewHandler(w http.ResponseWriter, r *http.Request) {
return
}
b := torrent.ToJson()
htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)}
htv := ViewTemplateVariables{b, captcha.Captcha{CaptchaID: captcha.GetID()}, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)}
err = viewTemplate.ExecuteTemplate(w, "index.html", htv)
if err != nil {
log.Errorf("ViewHandler(): %s", err)
}
}
func PostCommentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
userCaptcha := captcha.Extract(r)
if !captcha.Authenticate(userCaptcha) {
http.Error(w, "bad captcha", 403)
}
content := r.FormValue("comment")
idNum, err := strconv.Atoi(id)
comment := model.Comment{Username: "れんちょん", Content: content, TorrentId: idNum}
db.ORM.Create(&comment)
url, err := Router.Get("view_torrent").URL("id", id)
if err == nil {
http.Redirect(w, r, url.String(), 302)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

Voir le fichier

@ -30,7 +30,7 @@
<td>Links</td>
<td>
<a aria-label="Magnet Button" href="{{.Magnet}}" type="button" class="btn btn-success download-btn">
<span class="glyphicon glyphicon-magnet" aria-hidden="true"></span> Download!</button>
<span class="glyphicon glyphicon-magnet" aria-hidden="true"></span> Download!
</a>
<a style="padding-left: 0.5em"></a>
<a aria-label="Torrent file" href="http://anicache.com/torrent/{{.Hash}}.torrent" type="button" class="btn btn-success">
@ -54,6 +54,16 @@
</tr>
{{end}}
</table>
{{end}}
{{end}}
<form method="post">
<div class="form-group">
<label for="comment">Submit a Comment</label>
<textarea name="comment" class="form-control" rows="5"></textarea>
</div>
{{with .Captcha}}
{{block "captcha" .}}{{end}}
{{end}}
<input type="submit" value="Submit">
</form>
</div>
{{end}}