Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Den dev #74

Merged
merged 14 commits into from
Dec 24, 2023
Merged
4 changes: 4 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func main() {

app.InfoLog.Printf("Server starting on port %s\n", Port)

app.PostLen = 2500 //post and topic size

app.FileSize = 2 //here we set 2mb of file size

srv := &http.Server{
Addr: Port,
Handler: routes(&app),
Expand Down
6 changes: 5 additions & 1 deletion cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ func routes(a *config.AppConfig) http.Handler {
mux.HandleFunc("/help", handler.Repo.HelpHandler)
mux.HandleFunc("/privat_policy", handler.Repo.PrivatPolicyHandler)
mux.HandleFunc("/personal_cabinet", handler.Repo.PersonaCabinetHandler)
//handlers to edit delete inidvidual posts
mux.HandleFunc("/edit_post", handler.Repo.EditPostHandler)
mux.HandleFunc("/edit_post_result", handler.Repo.EditPostResultHandler)
mux.HandleFunc("/delete_post_result", handler.Repo.DeletePostHandler)
//handlers to edit delete whole topics
mux.HandleFunc("/edit_topic", handler.Repo.EditTopicHandler)
mux.HandleFunc("/edit_topic_result", handler.Repo.EditTopicResultHandler)
mux.HandleFunc("/delete_topic_result", handler.Repo.DeleteTopicHandler)

return mux
}
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ type AppConfig struct {
ErrorLog *log.Logger
UserLogin uuid.UUID // This field can hold a UUID value
ServerEmail string
PostLen int //this parameter limits post and topic size
FileSize int64
}
139 changes: 139 additions & 0 deletions internal/handler/editDeletePostHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package handler

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

"github.com/Pomog/ForumFFF/internal/models"
"github.com/Pomog/ForumFFF/internal/renderer"
)

// RegisterHandler handles both GET and POST requests for the registration page.
func (m *Repository) EditPostHandler(w http.ResponseWriter, r *http.Request) {
sessionUserID := m.GetLoggedUser(w, r)
user, _ := m.DB.GetUserByID(sessionUserID)

if r.Method == http.MethodPost {

var initialFormData models.Post

postID, err1 := strconv.Atoi(r.URL.Query().Get("postID"))
if err1 != nil {
setErrorAndRedirect(w, r, "Could not convert postID into integer: "+err1.Error(), "/error-page")
return
}
post, err2 := m.DB.GetPostByID(postID)
if err2 != nil {
setErrorAndRedirect(w, r, "Could not get post from GetPostByID: "+err2.Error(), "/error-page")
return
}

if user.UserName == "guest" || user.UserName == "" {
setErrorAndRedirect(w, r, "Guests can not edit/delete posts", "/error-page")
return
} else if user.ID != post.UserID {
setErrorAndRedirect(w, r, "Only Admin or Creator of the Post can Edit / Delete it", "/error-page")
return
}

initialFormData.Content = post.Content
initialFormData.Subject = post.Subject
initialFormData.UserID = post.UserID
initialFormData.ThreadId = post.ThreadId
initialFormData.ID = post.ID
data := make(map[string]interface{})
data["content"] = initialFormData
data["creator"] = user.UserName
renderer.RendererTemplate(w, "edit_post.page.html", &models.TemplateData{
Data: data,
})

} else {
http.Error(w, "No such method", http.StatusMethodNotAllowed)
}

}

func (m *Repository) EditPostResultHandler(w http.ResponseWriter, r *http.Request) {

if r.Method == http.MethodPost {
if strings.TrimSpace(r.FormValue("post-text")) == "" || len(r.FormValue("post-text")) > m.App.PostLen {
setErrorAndRedirect(w, r, "The post is empty or too long", "/error-page")
return
}

postID, err1 := strconv.Atoi(r.URL.Query().Get("postID"))
if err1 != nil {
setErrorAndRedirect(w, r, "Could not convert postID into integer: "+err1.Error(), "/error-page")
return
}
post, err2 := m.DB.GetPostByID(postID)

if err2 != nil {
setErrorAndRedirect(w, r, "Could not get post from GetPostByID: "+err2.Error(), "/error-page")
return
}
post.Content = r.FormValue("post-text")
err3 := m.DB.EditPost(post)

if err3 != nil {
setErrorAndRedirect(w, r, "Could not edit post using EditPost(post): "+err3.Error(), "/error-page")
return
}

data := make(map[string]interface{})
data["post"] = post.Content
data["threadID"] = post.ThreadId

renderer.RendererTemplate(w, "edit_topic_result.page.html", &models.TemplateData{
Data: data,
})

} else {
http.Error(w, "No such method", http.StatusMethodNotAllowed)
}

}

func (m *Repository) DeletePostHandler(w http.ResponseWriter, r *http.Request) {
sessionUserID := m.GetLoggedUser(w, r)
user, _ := m.DB.GetUserByID(sessionUserID)

if r.Method == http.MethodPost {
postID, err1 := strconv.Atoi(r.URL.Query().Get("postID"))
if err1 != nil {
setErrorAndRedirect(w, r, "Could not convert postID into integer: "+err1.Error(), "/error-page")
return
}
post, err2 := m.DB.GetPostByID(postID)
if err2 != nil {
setErrorAndRedirect(w, r, "Could not get post from GetPostByID: "+err2.Error(), "/error-page")
return
}
post.Content = r.FormValue("post-text")
err3 := m.DB.DeletePost(post)

if err3 != nil {
setErrorAndRedirect(w, r, "Could not m.DB.DeletePost(post): "+err3.Error(), "/error-page")
return
}

message := fmt.Sprintf("Post ID - %v deleted by User %s with email %s", post.ID, user.UserName, user.Email)
fmt.Println(message)
// helper.SendEmail(m.App.ServerEmail, message)

data := make(map[string]interface{})
data["post"] = post.Content
data["threadID"] = post.ThreadId

renderer.RendererTemplate(w, "edit_post_result.page.html", &models.TemplateData{
Data: data,
})

} else {
http.Error(w, "No such method", http.StatusMethodNotAllowed)
}

}
116 changes: 28 additions & 88 deletions internal/handler/editTopicHandler.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,48 @@
package handler

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

"github.com/Pomog/ForumFFF/internal/models"
"github.com/Pomog/ForumFFF/internal/renderer"
"github.com/google/uuid"
)

func (m *Repository) GetLoggedUser(w http.ResponseWriter, r *http.Request) int {
var UserID int
loginUUID := m.App.UserLogin

if loginUUID == uuid.Nil {
m.App.InfoLog.Println("Could not get loginUUID in HomeHandler")
http.Redirect(w, r, "/login", http.StatusSeeOther)
}

for _, cookie := range r.Cookies() {
if cookie.Value == loginUUID.String() {
userID, _ := m.DB.GetUserIDForSessionID(cookie.Value)
if UserID = userID; UserID != 0 {
break
}
}
}

if UserID == 0 {
setErrorAndRedirect(w, r, "Could not verify User, Please LogIN", "/error-page")
return 0
}
return UserID
}

// RegisterHandler handles both GET and POST requests for the registration page.
func (m *Repository) EditTopicHandler(w http.ResponseWriter, r *http.Request) {
sessionUserID := m.GetLoggedUser(w, r)
user, _ := m.DB.GetUserByID(sessionUserID)

if r.Method == http.MethodPost {

var initialFormData models.Post
var initialFormData models.Thread

postID, err1 := strconv.Atoi(r.URL.Query().Get("postID"))
topicID, err1 := strconv.Atoi(r.URL.Query().Get("topicID"))
if err1 != nil {
setErrorAndRedirect(w, r, "Could not convert postID into integer: "+err1.Error(), "/error-page")
setErrorAndRedirect(w, r, "Could not convert topicID into integer: "+err1.Error(), "/error-page")
return
}
post, err2 := m.DB.GetPostByID(postID)
topic, err2 := m.DB.GetThreadByID(topicID)
if err2 != nil {
setErrorAndRedirect(w, r, "Could not get post from GetPostByID: "+err2.Error(), "/error-page")
setErrorAndRedirect(w, r, "Could not get topic from m.DB.GetThreadByID(topicID): "+err2.Error(), "/error-page")
return
}


if user.UserName == "guest" || user.UserName == "" {
setErrorAndRedirect(w, r, "Guests can not edit/delete posts", "/error-page")
} else if user.ID != post.UserID {
setErrorAndRedirect(w, r, "Only Admin or Creator of the Post can Edit / Delete it", "/error-page")
setErrorAndRedirect(w, r, "Guests can not edit/delete topics", "/error-page")
return

} else if user.ID != topic.UserID {
setErrorAndRedirect(w, r, "Only Admin or Creator of the Topic can Edit / Delete it", "/error-page")
return
}
initialFormData.Content = post.Content
initialFormData.Subject = post.Subject
initialFormData.UserID = post.UserID
initialFormData.ThreadId = post.ThreadId
initialFormData.ID = post.ID
initialFormData.Subject = topic.Subject
initialFormData.UserID = topic.UserID
initialFormData.ID = topic.ID
data := make(map[string]interface{})
data["content"] = initialFormData
data["creatorName"] = user.UserName
renderer.RendererTemplate(w, "edit_topic.page.html", &models.TemplateData{
Data: data,
})
Expand All @@ -80,67 +56,32 @@ func (m *Repository) EditTopicHandler(w http.ResponseWriter, r *http.Request) {
func (m *Repository) EditTopicResultHandler(w http.ResponseWriter, r *http.Request) {

if r.Method == http.MethodPost {
if strings.TrimSpace(r.FormValue("post-text")) == "" || len(r.FormValue("post-text")) > 500 {
if strings.TrimSpace(r.FormValue("post-text")) == "" || len(r.FormValue("post-text")) > m.App.PostLen {
setErrorAndRedirect(w, r, "The post is empty or too long", "/error-page")
return
}

postID, err1 := strconv.Atoi(r.URL.Query().Get("postID"))
topicID, err1 := strconv.Atoi(r.URL.Query().Get("topicID"))
if err1 != nil {
setErrorAndRedirect(w, r, "Could not convert postID into integer: "+err1.Error(), "/error-page")
setErrorAndRedirect(w, r, "Could not convert topicID into integer: "+err1.Error(), "/error-page")
return
}
post, err2 := m.DB.GetPostByID(postID)
topic, err2 := m.DB.GetThreadByID(topicID)
if err2 != nil {
setErrorAndRedirect(w, r, "Could not get post from GetPostByID: "+err2.Error(), "/error-page")
return
}
post.Content = r.FormValue("post-text")
err3 := m.DB.EditPost(post)
topic.Subject = r.FormValue("post-text")
err3 := m.DB.EditTopic(topic)

if err3 != nil {
setErrorAndRedirect(w, r, "Could not edit post using EditPost(post): "+err3.Error(), "/error-page")
return
}

data := make(map[string]interface{})
data["post"] = post.Content
data["threadID"] = post.ThreadId

renderer.RendererTemplate(w, "edit_topic_result.page.html", &models.TemplateData{
Data: data,
})

} else {
http.Error(w, "No such method", http.StatusMethodNotAllowed)
}

}

func (m *Repository) DeleteTopicHandler(w http.ResponseWriter, r *http.Request) {
sessionUserID := m.GetLoggedUser(w, r)
user, _ := m.DB.GetUserByID(sessionUserID)

if r.Method == http.MethodPost {
postID, err1 := strconv.Atoi(r.URL.Query().Get("postID"))
if err1 != nil {
setErrorAndRedirect(w, r, "Could not convert postID into integer: "+err1.Error(), "/error-page")
}
post, err2 := m.DB.GetPostByID(postID)
if err2 != nil {
setErrorAndRedirect(w, r, "Could not get post from GetPostByID: "+err2.Error(), "/error-page")
}
post.Content = r.FormValue("post-text")
err3 := m.DB.DeletePost(post)

if err3 != nil {
setErrorAndRedirect(w, r, "Could not m.DB.DeletePost(post): "+err3.Error(), "/error-page")
}

message := fmt.Sprintf("Post ID - %v deleted by User %s with email %s", post.ID, user.UserName, user.Email)
fmt.Println(message)
// helper.SendEmail(m.App.ServerEmail, message)

data := make(map[string]interface{})
data["post"] = post.Content
data["threadID"] = post.ThreadId
data["topic"] = topic.Subject
data["threadID"] = topic.ID

renderer.RendererTemplate(w, "edit_topic_result.page.html", &models.TemplateData{
Data: data,
Expand All @@ -149,5 +90,4 @@ func (m *Repository) DeleteTopicHandler(w http.ResponseWriter, r *http.Request)
} else {
http.Error(w, "No such method", http.StatusMethodNotAllowed)
}

}
Loading