From d4c7138081c30e04df19b89f04ff2911be8d533f Mon Sep 17 00:00:00 2001 From: akuma06 Date: Wed, 10 May 2017 04:02:23 +0200 Subject: [PATCH] Mod Page (WIP) --- router/modpanel.go | 57 ++++++++++++++++++++++++++++++++++++++ service/comment/comment.go | 17 ++++++++++++ service/torrent/torrent.go | 11 ++++++++ service/user/user.go | 11 ++++++++ 4 files changed, 96 insertions(+) create mode 100644 router/modpanel.go create mode 100644 service/comment/comment.go diff --git a/router/modpanel.go b/router/modpanel.go new file mode 100644 index 00000000..fbc0e951 --- /dev/null +++ b/router/modpanel.go @@ -0,0 +1,57 @@ +// hurry mod panel to get it faaaaaaaaaaaast + +package router + +import ( + "fmt" + "net/http" + "strconv" + "html/template" + + "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/service/user" + "github.com/ewhal/nyaa/service/user/permission" + // "github.com/ewhal/nyaa/util/languages" + // "github.com/ewhal/nyaa/util/modelHelper" + "github.com/gorilla/mux" +) + +var panelCommentList *template.Template +func init() { + panelCommentList = template.Must(template.New("commentlist").Funcs(FuncMap).ParseFiles(filepath.Join(TemplateDir, "admin_index.html"), filepath.Join(TemplateDir, "admin/commentlist.html"))) +} + +func IndexModPanel(w http.ResponseWriter, r *http.Request) {} +func TorrentsListPanel(w http.ResponseWriter, r *http.Request) {} +func UsersListPanel(w http.ResponseWriter, r *http.Request) {} +func CommentsListPanel(w http.ResponseWriter, r *http.Request) { + page,_ := strconv.Atoi(r.URL.Query().Get("p")) + offset := 100 + + comments := commentService.GetAllTorrents(page*offset, offset) + languages.SetTranslationFromRequest(panelCommentList, r, "en-us") + htv := PanelCommentListVbs{comments} + err := panelCommentList.ExecuteTemplate(w, "index.html", htv) + +} +func TorrentEditModPanel(w http.ResponseWriter, r *http.Request) { + // Todo +} +func CommentDeleteModPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if (HasAdmin(currentUser)) { + err := form.NewErrors() + _, _ := userService.DeleteComment(id) + url, _ := Router.Get("mod_comment_list") + http.Redirect(w, r, url.String()+"?deleted", http.StatusSeeOther) + } +} +func TorrentDeleteModPanel(w http.ResponseWriter, r *http.Request) { + currentUser := GetUser(r) + if (HasAdmin(currentUser)) { + err := form.NewErrors() + _, _ := torrentService.DeleteTorrent(id) + url, _ := Router.Get("mod_torrent_list") + http.Redirect(w, r, url.String()+"?deleted", http.StatusSeeOther) + } +} \ No newline at end of file diff --git a/service/comment/comment.go b/service/comment/comment.go new file mode 100644 index 00000000..336b7cb2 --- /dev/null +++ b/service/comment/comment.go @@ -0,0 +1,17 @@ +package commentService + +import ( + "net/http" + + "github.com/ewhal/nyaa/db" + "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/service/user/permission" + "github.com/ewhal/nyaa/util/modelHelper" + +) + +func GetAllComments(limit int, offset int) { + var comments []model.Comment + db.ORM.Limit(limit).Offset(offset).Preload("Uploader").Find(&comments) + return comments +} diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index 02ac59dc..6a83451e 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -176,3 +176,14 @@ func CreateWhereParams(conditions string, params ...string) WhereParams { } return whereParams } + +func DeleteTorrent(id string) { + var torrent model.Torrents + if db.ORM.First(&torrent, id).RecordNotFound() { + return http.StatusNotFound, errors.New("Torrent is not found.") + } + if db.ORM.Delete(&torrent).Error != nil { + return http.StatusInternalServerError, errors.New("Torrent is not deleted.") + } + return http.StatusOK, nil +} \ No newline at end of file diff --git a/service/user/user.go b/service/user/user.go index b2579339..7c6b8399 100644 --- a/service/user/user.go +++ b/service/user/user.go @@ -305,4 +305,15 @@ func RemoveFollow(user *model.User, follower *model.User) { var userFollows = model.UserFollows{UserID: user.Id, FollowerID: follower.Id} db.ORM.Delete(&userFollows) } +} + +func DeleteComment(id string) { + var comment model.Comment + if db.ORM.First(&comment, id).RecordNotFound() { + return http.StatusNotFound, errors.New("Comment is not found.") + } + if db.ORM.Delete(&comment).Error != nil { + return http.StatusInternalServerError, errors.New("Comment is not deleted.") + } + return http.StatusOK, nil } \ No newline at end of file