diff --git a/internal/handler/staticHelperHendlers.go b/internal/handler/staticHelperHendlers.go index 4432806..6a7d5c9 100644 --- a/internal/handler/staticHelperHendlers.go +++ b/internal/handler/staticHelperHendlers.go @@ -114,10 +114,18 @@ func (m *Repository) PersonaCabinetHandler(w http.ResponseWriter, r *http.Reques personalInfo.UserName = user.UserName personalInfo.Type = user.Type //will show type of user in personal cabinet totalPosts, _ := m.DB.GetTotalPostsAmmountByUserID(personalInfo.ID) + + receivedPMS, err := m.DB.GetPMbyReceiverUserID(sessionUserID) + if err != nil { + setErrorAndRedirect(w, r, "Could not get received PMS"+err.Error(), "/error-page") + return + } + data := make(map[string]interface{}) data["personal"] = personalInfo data["totalPosts"] = totalPosts data["loggedAsID"] = sessionUserID + data["receivedPMS"] = receivedPMS renderer.RendererTemplate(w, "personal.page.html", &models.TemplateData{ Data: data, @@ -130,6 +138,11 @@ func (m *Repository) PersonaCabinetHandler(w http.ResponseWriter, r *http.Reques // GetAllThreadsForUserHandler gets all threads from user (user id) func (m *Repository) GetAllThreadsForUserHandler(w http.ResponseWriter, r *http.Request) { + sessionUserID := m.GetLoggedUser(w, r) + if sessionUserID == 0 { + setErrorAndRedirect(w, r, "unautorized", "/error-page") + return + } if r.Method == http.MethodGet { userID, _ := strconv.Atoi(r.URL.Query().Get("userID")) user, errUser := m.DB.GetUserByID(userID) @@ -163,6 +176,7 @@ func (m *Repository) GetAllThreadsForUserHandler(w http.ResponseWriter, r *http. info.Subject = thread.Subject info.Created = thread.Created.Format("2006-01-02 15:04:05") info.Category = thread.Category + info.UserID = thread.UserID info.PictureUserWhoCreatedThread = user.Picture info.UserNameWhoCreatedThread = user.UserName @@ -195,6 +209,7 @@ func (m *Repository) GetAllThreadsForUserHandler(w http.ResponseWriter, r *http. data["games"] = m.App.GamesList data["threads"] = threadsInfo + data["loggedAsID"] = sessionUserID renderer.RendererTemplate(w, "home.page.html", &models.TemplateData{ Data: data, @@ -207,6 +222,12 @@ func (m *Repository) GetAllThreadsForUserHandler(w http.ResponseWriter, r *http. // GetAllPostsForUserHandler gets all posts from user (user id) func (m *Repository) GetAllPostsForUserHandler(w http.ResponseWriter, r *http.Request) { + sessionUserID := m.GetLoggedUser(w, r) + if sessionUserID == 0 { + setErrorAndRedirect(w, r, "unautorized", "/error-page") + return + } + if r.Method == http.MethodGet { userID, _ := strconv.Atoi(r.URL.Query().Get("userID")) user, errUser := m.DB.GetUserByID(userID) @@ -262,6 +283,7 @@ func (m *Repository) GetAllPostsForUserHandler(w http.ResponseWriter, r *http.Re data["posts"] = postsInfo data["games"] = m.App.GamesList + data["loggedAsID"] = sessionUserID renderer.RendererTemplate(w, "theme.page.html", &models.TemplateData{ Data: data, @@ -270,6 +292,12 @@ func (m *Repository) GetAllPostsForUserHandler(w http.ResponseWriter, r *http.Re } func (m *Repository) GetAllLikedPostsByUserIDHandler(w http.ResponseWriter, r *http.Request) { + sessionUserID := m.GetLoggedUser(w, r) + if sessionUserID == 0 { + setErrorAndRedirect(w, r, "unautorized", "/error-page") + return + } + if r.Method == http.MethodGet { userID, _ := strconv.Atoi(r.URL.Query().Get("userID")) user, errUser := m.DB.GetUserByID(userID) @@ -325,6 +353,7 @@ func (m *Repository) GetAllLikedPostsByUserIDHandler(w http.ResponseWriter, r *h data["posts"] = postsInfo data["games"] = m.App.GamesList + data["loggedAsID"] = sessionUserID renderer.RendererTemplate(w, "theme.page.html", &models.TemplateData{ Data: data, diff --git a/internal/handler/themeHandler.go b/internal/handler/themeHandler.go index 16f0865..b6a8861 100644 --- a/internal/handler/themeHandler.go +++ b/internal/handler/themeHandler.go @@ -292,6 +292,7 @@ func prepareDataForThemePage(m *Repository, w http.ResponseWriter, r *http.Reque data["creatorPostsAmount"] = creatorPostsAmount data["creatorImg"] = creator.Picture data["mainThreadName"] = mainThread.Subject + data["mainThreadCategory"] = mainThread.Category data["mainThreadID"] = mainThread.ID data["mainThreadCreatedTime"] = mainThread.Created.Format("2006-01-02 15:04:05") data["games"] = m.App.GamesList diff --git a/internal/repository/dbrepo/sqllite.go b/internal/repository/dbrepo/sqllite.go index 1daddd1..d33a893 100644 --- a/internal/repository/dbrepo/sqllite.go +++ b/internal/repository/dbrepo/sqllite.go @@ -865,3 +865,63 @@ func (m *SqliteBDRepo) DeletePM(pm models.PM) error { } return nil } + +// GetPMbyReceiverUserID insert post into SQLite DB +func (m *SqliteBDRepo) GetPMbyReceiverUserID(userID int) ([]models.PM, error) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := `select * from pm + where receiverUserID = $1 + ` + + rows, err := m.DB.QueryContext(ctx, query, userID) + if err != nil { + return nil, err + } + defer rows.Close() + + var pms []models.PM + + for rows.Next() { + var pm models.PM + err := rows.Scan(&pm.ID, &pm.Content, &pm.Created, &pm.SenderUserID, &pm.ReceiverUserID) + if err != nil { + return nil, err + } + pms = append(pms, pm) + } + + return pms, nil +} + +// GetPMbysenderUserID insert post into SQLite DB +func (m *SqliteBDRepo) GetPMbysenderUserID(userID int) ([]models.PM, error) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := `select * from pm + where senderUserID = $1 + ` + + rows, err := m.DB.QueryContext(ctx, query, userID) + if err != nil { + return nil, err + } + defer rows.Close() + + var pms []models.PM + + for rows.Next() { + var pm models.PM + err := rows.Scan(&pm.ID, &pm.Content, &pm.Created, &pm.SenderUserID, &pm.ReceiverUserID) + if err != nil { + return nil, err + } + pms = append(pms, pm) + } + + return pms, nil +} + + diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 7447b7f..3099971 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -38,4 +38,6 @@ type DatabaseInt interface { GetAllThreadsByClassification(classification models.TextClassification) ([]models.Thread, error) CreatePM(pm models.PM) error DeletePM(pm models.PM) error + GetPMbyReceiverUserID(userID int) ([]models.PM, error) + GetPMbysenderUserID(userID int) ([]models.PM, error) } diff --git a/mainDB.db b/mainDB.db deleted file mode 100644 index 51a6320..0000000 Binary files a/mainDB.db and /dev/null differ diff --git a/template/home.page.html b/template/home.page.html index 87e4fe3..63eead5 100644 --- a/template/home.page.html +++ b/template/home.page.html @@ -58,6 +58,7 @@

What this topic will {{$threadinfo := index .Data "threads"}} {{ range $threadinfo }} + {{ if or (eq .UserID $loggedID) (eq .Classification "approved") }} diff --git a/template/theme.page.html b/template/theme.page.html index 013ca19..b0025a9 100644 --- a/template/theme.page.html +++ b/template/theme.page.html @@ -71,6 +71,9 @@

Type your post and cli

Topic:

+

+ {{index .Data "mainThreadCategory"}} +


{{$threadImage := index .Data "threadImg"}} {{ if $threadImage }} @@ -101,6 +104,7 @@

Topic:

{{$postinfo := index .Data "posts"}} {{ range $postinfo }} {{$postCreatorID := .UserIDWhoCreatedPost}} + {{ if or (eq $postCreatorID $loggedID) (eq .Classification "approved") }}