Skip to content

Commit

Permalink
continued working with moderatro panel. Done thread change category
Browse files Browse the repository at this point in the history
  • Loading branch information
TartuDen committed Jan 6, 2024
1 parent 3d8c765 commit 2171b24
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
3 changes: 3 additions & 0 deletions internal/handler/homeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions internal/handler/moderPanelHanlder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"net/http"
"strconv"

"github.com/Pomog/ForumFFF/internal/models"
"github.com/Pomog/ForumFFF/internal/renderer"
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ThreadDataForMainPage struct {
Image string
Category string
Classification string
UserID int
}

type PostDataForThemePage struct {
Expand Down
5 changes: 2 additions & 3 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -817,7 +816,7 @@ func (m *SqliteBDRepo) EditPostClassification(post models.Post, classification m
`

_, err := m.DB.ExecContext(ctx, stmt,
post.Classification,
classification,
post.ID,
)

Expand Down
Binary file modified mainDB.db
Binary file not shown.
8 changes: 6 additions & 2 deletions template/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ <h1 class="modal-title fs-5" id="createNewTopicModalLabel">What this topic will
</thead>
<tbody class="containerBody">
{{$threadinfo := index .Data "threads"}}
{{$loggedID := index .Data "loggedAsID"}}
{{ range $threadinfo }}

{{ if or (eq .UserID $loggedID) (eq .Classification "approved") }}
<tr>
<td>
<!-- 1column -->
Expand All @@ -63,6 +64,9 @@ <h1 class="modal-title fs-5" id="createNewTopicModalLabel">What this topic will
<br>Created by: {{ .UserNameWhoCreatedThread}}
<br>Topic created: {{ .Created}}
<br>Category: {{ .Category}}
{{ if eq .UserID $loggedID}}
<br>Classification: {{ .Classification}}
{{ end }}
<br><strong>{{ .Subject }} </strong><hr>
</a>
</td>
Expand All @@ -86,7 +90,7 @@ <h1 class="modal-title fs-5" id="createNewTopicModalLabel">What this topic will
<td><!-- 3column -->{{ $numOfPosts}} messages</td>

</tr>

{{end}}
{{ end }}
</tbody>
</table>
Expand Down
28 changes: 25 additions & 3 deletions template/moderMain.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,41 @@ <h2>Select Thread Category:</h2>
<div>
<tbody class="containerBody">
{{ range $topics }}
{{$topicCategory := .Classification}}
{{$topicID := .ID}}
<tr>
<td>

<br>Created by user with ID: {{ .UserID}}
<br>Topic created: {{convertTimeTopic . }}
<br>Category: {{ .Category}}
<br>Classification: {{ .Classification}}
<br><strong>{{ .Subject }} </strong>
<hr>


</td>
<tbody class="containerBody">
<tr>
<td>
<form method="post" action="">
{{range $index, $category := $categories}}
<div class="btn-group" role="group" aria-label="Basic radio toggle button group">
<input type="radio" class="btn-check" name="btnradio" id="btnradio{{$index}}"
autocomplete="off" value="{{$category}}" {{ if eq $category $topicCategory }}
checked {{end}}>
<label class="btn btn-outline-primary" for="btnradio{{$index}}"> {{$category}} </label>
<input type="hidden" name="topicID" value="{{ $topicID }}">
</div>
{{end}}
<input type="submit" value="Submit">
</form>

</td>
</tr>
{{ end }}

</tbody>
<hr>
</tr>
{{ end }}
</tbody>
</div>
{{ end }}
Expand Down

0 comments on commit 2171b24

Please sign in to comment.