Skip to content

Commit

Permalink
Merge pull request #95 from Pomog/yurii_dev
Browse files Browse the repository at this point in the history
Yurii dev
  • Loading branch information
Pomog authored Jan 18, 2024
2 parents d634291 + 3f906a9 commit e9de1e1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/handler/staticHelperHendlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions internal/handler/themeHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


2 changes: 2 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Binary file removed mainDB.db
Binary file not shown.
1 change: 1 addition & 0 deletions template/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ <h1 class="modal-title fs-5" id="createNewTopicModalLabel">What this topic will
{{$threadinfo := index .Data "threads"}}

{{ range $threadinfo }}

{{ if or (eq .UserID $loggedID) (eq .Classification "approved") }}
<tr>
<td>
Expand Down
4 changes: 4 additions & 0 deletions template/theme.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ <h1 class="modal-title fs-5" id="createNewPostModalLabel">Type your post and cli
<th>
<div id="mainTheme">
<h3>Topic:</h3>
<p>
{{index .Data "mainThreadCategory"}}
</p>
<hr>
{{$threadImage := index .Data "threadImg"}}
{{ if $threadImage }}
Expand Down Expand Up @@ -101,6 +104,7 @@ <h3>Topic:</h3>
{{$postinfo := index .Data "posts"}}
{{ range $postinfo }}
{{$postCreatorID := .UserIDWhoCreatedPost}}

{{ if or (eq $postCreatorID $loggedID) (eq .Classification "approved") }}

<tr>
Expand Down

0 comments on commit e9de1e1

Please sign in to comment.