From a8a3d77dfa74b055639dc689780de83b3777d315 Mon Sep 17 00:00:00 2001 From: dverves Date: Sat, 6 Jan 2024 22:58:17 +0200 Subject: [PATCH] continued working with moderatro panel. Done thread change category --- internal/handler/homeHandler.go | 3 +++ internal/handler/moderPanelHanlder.go | 33 ++++++++++++++++++++++++++ internal/models/models.go | 1 + internal/repository/dbrepo/sqllite.go | 5 ++-- mainDB.db | Bin 327680 -> 327680 bytes template/home.page.html | 8 +++++-- template/moderMain.page.html | 28 +++++++++++++++++++--- 7 files changed, 70 insertions(+), 8 deletions(-) diff --git a/internal/handler/homeHandler.go b/internal/handler/homeHandler.go index c35787e..d6be3f5 100644 --- a/internal/handler/homeHandler.go +++ b/internal/handler/homeHandler.go @@ -104,6 +104,9 @@ func processThreadInfo(m *Repository, thread models.Thread) (models.ThreadDataFo info.Subject = thread.Subject info.Created = thread.Created.Format("2006-01-02 15:04:05") info.Category = thread.Category + info.Classification = thread.Classification + info.UserID = user.ID + info.PictureUserWhoCreatedThread = user.Picture info.UserNameWhoCreatedThread = user.UserName diff --git a/internal/handler/moderPanelHanlder.go b/internal/handler/moderPanelHanlder.go index 82961f5..befc4e6 100644 --- a/internal/handler/moderPanelHanlder.go +++ b/internal/handler/moderPanelHanlder.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "strconv" "github.com/Pomog/ForumFFF/internal/models" "github.com/Pomog/ForumFFF/internal/renderer" @@ -25,9 +26,41 @@ func (m *Repository) ModerPanelHandler(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { handleGetRequestModerPage(w, r, m, sessionUserID) + } else if r.Method == http.MethodPost { + handlePostRequestModerPage(w, r, m, sessionUserID) } } +func handlePostRequestModerPage(w http.ResponseWriter, r *http.Request, m *Repository, sessionUserID int) { + err := r.ParseForm() + if err != nil { + setErrorAndRedirect(w, r, "Could not parse form "+err.Error(), "/error-page") + return + } + + selectedCategory := r.FormValue("btnradio") + topicID, err := strconv.Atoi(r.FormValue("topicID")) + if err != nil { + setErrorAndRedirect(w, r, "Could not convert string into int "+err.Error(), "/error-page") + return + } + topic, err := m.DB.GetThreadByID(topicID) + if err != nil { + setErrorAndRedirect(w, r, "Could not get topic by id "+err.Error(), "/error-page") + return + } + cat := models.TextClassification(selectedCategory) + + err = m.DB.EditTopicClassification(topic, cat) + if err != nil { + setErrorAndRedirect(w, r, "Could not edit topic classification "+err.Error(), "/error-page") + return + } + + // Redirect back to the previous page (referer) + http.Redirect(w, r, r.Header.Get("Referer"), http.StatusFound) +} + // handleGetRequest handles GET requests for the home page. func handleGetRequestModerPage(w http.ResponseWriter, r *http.Request, m *Repository, sessionUserID int) { topicCat := r.URL.Query().Get("topic") diff --git a/internal/models/models.go b/internal/models/models.go index 4fe6602..bce9ee0 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -39,6 +39,7 @@ type ThreadDataForMainPage struct { Image string Category string Classification string + UserID int } type PostDataForThemePage struct { diff --git a/internal/repository/dbrepo/sqllite.go b/internal/repository/dbrepo/sqllite.go index e9c08d3..a50437e 100644 --- a/internal/repository/dbrepo/sqllite.go +++ b/internal/repository/dbrepo/sqllite.go @@ -794,9 +794,8 @@ func (m *SqliteBDRepo) EditTopicClassification(topic models.Thread, classificati SET classification = $1 WHERE id = $2; ` - _, err := m.DB.ExecContext(ctx, stmt, - topic.Classification, + classification, topic.ID, ) @@ -817,7 +816,7 @@ func (m *SqliteBDRepo) EditPostClassification(post models.Post, classification m ` _, err := m.DB.ExecContext(ctx, stmt, - post.Classification, + classification, post.ID, ) diff --git a/mainDB.db b/mainDB.db index 4d9e339a770db46714d66f066c50951248fdf490..738ec7374dec486503aee7ff499c34c5eacca8f1 100644 GIT binary patch delta 267 zcmZo@5NT)-nIO$Lb)t+j}J2Uo0vpk`dX@)I2k&Mfim*4o8wkwG4a4;7#NtgY(BH9oQVa< y+B?~Ottn3{BMT=(CnwMu!m^ty)q<|W|5Tg+ delta 227 zcmZo@5NT)-nIO%WJyFJ)F?(Y|K)fnjVsfI1rIDqnZnCj~xvq(kfvIj{N|L3nfpJQz zWva2MVPdM;=Bx3B0wSjwcni7u8BTMo;V@$R!J5S)!nA*4;|k8^Dh^JDwt8d6%?d?d z8M%T4I2c;AjTxO)H=ir;V%#iJ@>ZLdfq{XUBbb3BnAe%>#Adz)Wvr8VS8e8IU|^ZZ z&=GIU=qNvVn_=K)5xwn4!m;_xs&XdoRx4%>hAu&% b4#CY6mTqEd+R(Ovv26oW+Xm)k8(0DWxl%>e diff --git a/template/home.page.html b/template/home.page.html index 65bb86a..7439f9d 100644 --- a/template/home.page.html +++ b/template/home.page.html @@ -53,8 +53,9 @@

What this topic will {{$threadinfo := index .Data "threads"}} + {{$loggedID := index .Data "loggedAsID"}} {{ range $threadinfo }} - + {{ if or (eq .UserID $loggedID) (eq .Classification "approved") }} @@ -63,6 +64,9 @@

What this topic will
Created by: {{ .UserNameWhoCreatedThread}}
Topic created: {{ .Created}}
Category: {{ .Category}} + {{ if eq .UserID $loggedID}} +
Classification: {{ .Classification}} + {{ end }}
{{ .Subject }}
@@ -86,7 +90,7 @@

What this topic will {{ $numOfPosts}} messages - + {{end}} {{ end }} diff --git a/template/moderMain.page.html b/template/moderMain.page.html index 1e3271a..424500e 100644 --- a/template/moderMain.page.html +++ b/template/moderMain.page.html @@ -29,19 +29,41 @@

Select Thread Category:

{{ range $topics }} + {{$topicCategory := .Classification}} + {{$topicID := .ID}}
Created by user with ID: {{ .UserID}}
Topic created: {{convertTimeTopic . }}
Category: {{ .Category}} -
Classification: {{ .Classification}}
{{ .Subject }} -
+ + + + + + +
+ {{range $index, $category := $categories}} +
+ + + +
+ {{end}} + +
- {{ end }} + + +
+ + {{ end }}
{{ end }}