Skip to content

Commit

Permalink
Merge pull request #96 from Pomog/yurii_dev
Browse files Browse the repository at this point in the history
Select Category fixed
  • Loading branch information
TartuDen committed Jan 18, 2024
2 parents e9de1e1 + 33fb1cd commit 94114ad
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 70 deletions.
34 changes: 19 additions & 15 deletions internal/handler/homeHandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"errors"
"fmt"
"log"
"net/http"
Expand All @@ -25,7 +26,11 @@ func (m *Repository) HomeHandler(w http.ResponseWriter, r *http.Request) {
handleGetRequest(w, r, m, sessionUserID)

} else if r.Method == http.MethodPost {
handlePostRequest(w, r, m, sessionUserID)
err := handlePostRequest(w, r, m, sessionUserID)
if err != nil {
setErrorAndRedirect(w, r, "Could not create Post"+err.Error(), "/error-page")
return
}
}
}

Expand Down Expand Up @@ -107,7 +112,6 @@ func processThreadInfo(m *Repository, thread models.Thread) (models.ThreadDataFo
info.Classification = thread.Classification
info.UserID = user.ID


info.PictureUserWhoCreatedThread = user.Picture
info.UserNameWhoCreatedThread = user.UserName
posts, err := m.DB.GetAllPostsFromThread(thread.ID)
Expand Down Expand Up @@ -150,20 +154,21 @@ func prepareDataForTemplate(w http.ResponseWriter, r *http.Request, m *Repositor
}

// handlePostRequest handles POST requests for creating new threads.
func handlePostRequest(w http.ResponseWriter, r *http.Request, m *Repository, sessionUserID int) {
func handlePostRequest(w http.ResponseWriter, r *http.Request, m *Repository, sessionUserID int) error {
loggedUser, err := m.DB.GetUserByID(sessionUserID)
if err != nil {
setErrorAndRedirect(w, r, "Could not get user by ID: m.DB.GetUserByID(sessionUserID)", "/error-page")
return
return err
}

userName := loggedUser.UserName
if userName == "guest" || strings.TrimSpace(userName) == "" {
setErrorAndRedirect(w, r, guestRestiction, "/error-page")
return
return errors.New("guestRestiction")
}

thread := createThreadFromRequest(m, w, r, sessionUserID)
thread, err := createThreadFromRequest(m, w, r, sessionUserID)
if err != nil {
return err
}
thread.Category = strings.TrimSpace(helper.CorrectPunctuationsSpaces(thread.Category))
thread.Subject = strings.TrimSpace(helper.CorrectPunctuationsSpaces(thread.Subject))

Expand All @@ -183,28 +188,27 @@ func handlePostRequest(w http.ResponseWriter, r *http.Request, m *Repository, se
for _, err := range validationsErrors {
errorMsg += err.Error() + "\n"
}
setErrorAndRedirect(w, r, errorMsg, "/error-page")
return
return err
}

id, err := m.DB.CreateThread(thread)
if err != nil {
setErrorAndRedirect(w, r, "Could not create thread: m.DB.CreateThread(thread)"+err.Error(), "/error-page")
return
return err
}

http.Redirect(w, r, fmt.Sprintf("/theme?threadID=%d", id), http.StatusSeeOther)
return nil
}

// createThreadFromRequest creates a thread from the HTTP request.
func createThreadFromRequest(m *Repository, w http.ResponseWriter, r *http.Request, sessionUserID int) models.Thread {
func createThreadFromRequest(m *Repository, w http.ResponseWriter, r *http.Request, sessionUserID int) (models.Thread, error) {
thread := models.Thread{
Subject: r.FormValue("message-text"),
Category: r.FormValue("category-text"),
UserID: sessionUserID,
}
AttachFile(m, w, r, nil, &thread)
return thread
err := AttachFile(m, w, r, nil, &thread)
return thread, err
}

func getUserThatCreatedLastPost(posts []models.Post) int {
Expand Down
8 changes: 7 additions & 1 deletion internal/handler/moderPanelHanlder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"fmt"
"net/http"
"strconv"

Expand Down Expand Up @@ -38,9 +39,13 @@ func handlePostRequestModerPage(w http.ResponseWriter, r *http.Request, m *Repos
return
}

selectedCategory := r.FormValue("btnradio")
topicID := r.FormValue("topicID")
selectedCategory := r.FormValue("btnradio" + topicID)

if r.FormValue("postID") != "" {
postIDF := r.FormValue("postID")
selectedCategory := r.FormValue("btnradio" + postIDF)

postID, err := strconv.Atoi(r.FormValue("postID"))
if err != nil {
setErrorAndRedirect(w, r, "Could not convert string into int "+err.Error(), "/error-page")
Expand Down Expand Up @@ -72,6 +77,7 @@ func handlePostRequestModerPage(w http.ResponseWriter, r *http.Request, m *Repos
return
}
cat := models.TextClassification(selectedCategory)
fmt.Println("selectedCategory", cat)

err = m.DB.EditTopicClassification(topic, cat)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/handler/staticHelperHendlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type Repository struct {
}

const (
dbErrorUserPresent = "DB Error func UserPresent"
userAlreadyExistsMsg = "User Already Exists"
dbErrorCreateUser = "DB Error func CreateUser"
dbErrorUserPresent = "db Error func UserPresent"
userAlreadyExistsMsg = "user Already Exists"
dbErrorCreateUser = "bd Error func CreateUser"
fileReceivingErrorMsg = "file receiving error"
fileCreatingErrorMsg = "Unable to create file"
fileSavingErrorMsg = "Unable to save file"
fileCreatingErrorMsg = "unable to create file"
fileSavingErrorMsg = "unable to save file"
guestRestiction = "guests can not create Themes and Posts, Like or Dislike posts, please log in or register"
)

Expand Down
20 changes: 8 additions & 12 deletions internal/handler/themeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ func createPostFromRequest(m *Repository, w http.ResponseWriter, r *http.Request
return post, errors.New(errorMsg)
}

AttachFile(m, w, r, &post, nil)
return post, nil
err:= AttachFile(m, w, r, &post, nil)
return post, err
}

// getPostsInfo retrieves information for rendering posts.
Expand Down Expand Up @@ -301,7 +301,7 @@ func prepareDataForThemePage(m *Repository, w http.ResponseWriter, r *http.Reque
}

// AttachFile attaches a file to a post or thread.
func AttachFile(m *Repository, w http.ResponseWriter, r *http.Request, post *models.Post, thread *models.Thread) {
func AttachFile(m *Repository, w http.ResponseWriter, r *http.Request, post *models.Post, thread *models.Thread) error {
// ADD IMAGE TO STATIC_________________________
// Get the file from the form data
file, handler, errFileGet := r.FormFile("image")
Expand All @@ -310,31 +310,27 @@ func AttachFile(m *Repository, w http.ResponseWriter, r *http.Request, post *mod

// Validate file size (2 MB limit)
if handler.Size > m.App.FileSize<<20 {
setErrorAndRedirect(w, r, "File size should be below 2 MB", "/error-page")
return
return errors.New("file size should be below 2 MB")
}

// Validate file type (must be an image)
contentType := handler.Header.Get("Content-Type")
if contentType != "image/jpeg" && contentType != "image/png" && contentType != "image/gif" {
setErrorAndRedirect(w, r, "Wrong File Formate, allowed jpeg, png, gif ", "/error-page")
return
return errors.New("wrong File Formate, allowed jpeg, png, gif")
}

// Create a new file in the "static/post_images" directory
newFilePath := filepath.Join("static/post_images", handler.Filename)
newFile, errFileCreate := os.Create(newFilePath)
if errFileCreate != nil {
setErrorAndRedirect(w, r, fileCreatingErrorMsg, "/error-page")
return
return errors.New(fileCreatingErrorMsg)
}
defer newFile.Close()

// Copy the uploaded file to the new file
_, err := io.Copy(newFile, file)
if err != nil {
setErrorAndRedirect(w, r, fileSavingErrorMsg, "/error-page")
return
return errors.New(fileSavingErrorMsg)
}
if post != nil {
post.Image = path.Join("/", newFilePath)
Expand All @@ -343,5 +339,5 @@ func AttachFile(m *Repository, w http.ResponseWriter, r *http.Request, post *mod
}

}
//_______________________________________
return nil
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 36 additions & 37 deletions template/moderMain.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,41 @@ <h2>Select Posts Category:</h2>

{{if $posts}}
<div>
<tbody class="containerBody">
{{ range $posts }}
{{$postsCategory := .Classification}}
{{$postID := .ID}}
<tr>
<td>
<br>Created by user with ID: {{ .UserID}}
<br>Topic created: {{convertTime . }}
<br>Category: {{ .Classification}}
<br><em>{{ .Subject }} </em>
<br><strong>{{ .Content }} </strong>
</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 $postsCategory }}
checked {{end}}>
<label class="btn btn-outline-primary" for="btnradio{{$index}}"> {{$category}} </label>
<input type="hidden" name="postID" value="{{ $postID }}">
</div>
{{end}}
<input type="submit" value="Submit">
</form>

</td>
</tr>
<tbody class="containerBody">
{{ range $posts }}
{{$postsCategory := .Classification}}
{{$postID := .ID}}
<tr>
<td>
<br>Created by user with ID: {{ .UserID}}
<br>Topic created: {{convertTime . }}
<br>Category: {{ .Classification}}
<br><em>{{ .Subject }} </em>
<br><strong>{{ .Content }} </strong>
</td>
</tr>
<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{{$postID}}" id="btnradio{{$postID}}{{$index}}"
autocomplete="off" value="{{$category}}" {{ if eq $category $postsCategory }}
checked {{end}}>
<label class="btn btn-outline-primary" for="btnradio{{$postID}}{{$index}}"> {{$category}} </label>
</div>
{{end}}
<input type="hidden" name="postID" value="{{ $postID }}">
<input type="submit" value="Submit">
</form>
</td>
</tr>
<tr>
<td><hr></td>
</tr>
{{ end }}
</tbody>

</tbody>
<hr>
</tr>
{{ end }}
</tbody>

{{ end }}

Expand All @@ -104,10 +103,10 @@ <h2>Select Posts Category:</h2>
<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}}"
<input type="radio" class="btn-check" name="btnradio{{$topicID}}" id="btnradio{{$topicID}}{{$index}}"
autocomplete="off" value="{{$category}}" {{ if eq $category $topicCategory }}
checked {{end}}>
<label class="btn btn-outline-primary" for="btnradio{{$index}}"> {{$category}} </label>
<label class="btn btn-outline-primary" for="btnradio{{$topicID}}{{$index}}"> {{$category}} </label>
<input type="hidden" name="topicID" value="{{ $topicID }}">
</div>
{{end}}
Expand Down

0 comments on commit 94114ad

Please sign in to comment.