diff --git a/internal/handler/homeHandler.go b/internal/handler/homeHandler.go index d6be3f5..8d814e9 100644 --- a/internal/handler/homeHandler.go +++ b/internal/handler/homeHandler.go @@ -1,6 +1,7 @@ package handler import ( + "errors" "fmt" "log" "net/http" @@ -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 + } } } @@ -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) @@ -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)) @@ -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 { diff --git a/internal/handler/moderPanelHanlder.go b/internal/handler/moderPanelHanlder.go index 52f1110..36db4e2 100644 --- a/internal/handler/moderPanelHanlder.go +++ b/internal/handler/moderPanelHanlder.go @@ -1,6 +1,7 @@ package handler import ( + "fmt" "net/http" "strconv" @@ -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") @@ -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 { diff --git a/internal/handler/staticHelperHendlers.go b/internal/handler/staticHelperHendlers.go index 6a7d5c9..1a3787e 100644 --- a/internal/handler/staticHelperHendlers.go +++ b/internal/handler/staticHelperHendlers.go @@ -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" ) diff --git a/internal/handler/themeHandler.go b/internal/handler/themeHandler.go index b6a8861..c84ba8d 100644 --- a/internal/handler/themeHandler.go +++ b/internal/handler/themeHandler.go @@ -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. @@ -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") @@ -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) @@ -343,5 +339,5 @@ func AttachFile(m *Repository, w http.ResponseWriter, r *http.Request, post *mod } } - //_______________________________________ + return nil } diff --git a/static/post_images/0ca4b78c-8874-47d4-bc99-b84bb1285217image.png b/static/post_images/0ca4b78c-8874-47d4-bc99-b84bb1285217image.png new file mode 100644 index 0000000..e25d076 Binary files /dev/null and b/static/post_images/0ca4b78c-8874-47d4-bc99-b84bb1285217image.png differ diff --git a/template/moderMain.page.html b/template/moderMain.page.html index 0eb13c0..8f5aa87 100644 --- a/template/moderMain.page.html +++ b/template/moderMain.page.html @@ -43,42 +43,41 @@