Skip to content

Commit

Permalink
Merge pull request #39 from Pomog/den_dev
Browse files Browse the repository at this point in the history
created "create topic" modal
  • Loading branch information
Pomog authored Dec 7, 2023
2 parents 014ff6c + 1bed01d commit d31dd48
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 70 deletions.
126 changes: 98 additions & 28 deletions internal/handler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,41 +202,61 @@ func (m *Repository) RegisterHandler(w http.ResponseWriter, r *http.Request) {
// MainHandler is a method of the Repository struct that handles requests to the main page.
// It renders the "home.page.html" template to the provided HTTP response writer.
func (m *Repository) HomeHandler(w http.ResponseWriter, r *http.Request) {
threads, err := m.DB.GetAllThreads()
if err != nil {
log.Fatal(err)
}
if r.Method == http.MethodGet {
threads, err := m.DB.GetAllThreads()
if err != nil {
log.Fatal(err)
}

var threadsInfo []models.ThreadDataForMainPage
for _, thread := range threads {
var user models.User
user, _ = m.DB.GetUserByID(thread.UserID)
var info models.ThreadDataForMainPage
info.Subject = thread.Subject
info.Created = thread.Created.Format("2006-01-02 15:04:05")
var threadsInfo []models.ThreadDataForMainPage
for _, thread := range threads {
var user models.User
user, _ = m.DB.GetUserByID(thread.UserID)
var info models.ThreadDataForMainPage
info.Subject = thread.Subject
info.Created = thread.Created.Format("2006-01-02 15:04:05")

info.PictureUserWhoCreatedThread = user.Picture
info.UserNameWhoCreatedThread = user.UserName
info.PictureUserWhoCreatedThread = user.Picture
info.UserNameWhoCreatedThread = user.UserName

posts, err := m.DB.GetAllPostsFromThread(thread.ID)
if err != nil {
log.Fatal(err)
posts, err := m.DB.GetAllPostsFromThread(thread.ID)
if err != nil {
log.Fatal(err)
}
info.Posts = posts
userWhoCreatedLastPost, _ := m.DB.GetUserByID(getUserThatCreatedLastPost(posts))
info.PictureUserWhoCreatedLastPost = userWhoCreatedLastPost.Picture
info.UserNameWhoCreatedLastPost = userWhoCreatedLastPost.UserName
threadsInfo = append(threadsInfo, info)
}
info.Posts = posts
userWhoCreatedLastPost, _ := m.DB.GetUserByID(getUserThatCreatedLastPost(posts))
info.PictureUserWhoCreatedLastPost = userWhoCreatedLastPost.Picture
info.UserNameWhoCreatedLastPost = userWhoCreatedLastPost.UserName
threadsInfo = append(threadsInfo, info)
}

data := make(map[string]interface{})
data := make(map[string]interface{})

data["threads"] = threadsInfo

data["threads"] = threadsInfo
renderer.RendererTemplate(w, "home.page.html", &models.TemplateData{
Data: data,
})
} else if r.Method == http.MethodPost {
thread := models.Thread{
Subject: r.FormValue("message-text"),
UserID: getUserFromCookies(r),
}

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

}

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

func getUserFromCookies(r *http.Request) int {
beaves := models.User{
ID: 4,
}
return beaves.ID
}

func getUserThatCreatedLastPost(posts []models.Post) int {
Expand All @@ -252,10 +272,60 @@ func getUserThatCreatedLastPost(posts []models.Post) int {
return id
}

func getThreadIDFromCookies(r *http.Request) int {
return 2
}

// MainHandler is a method of the Repository struct that handles requests to the main page.
// It renders the "home.page.html" template to the provided HTTP response writer.
func (m *Repository) ThemeHandler(w http.ResponseWriter, r *http.Request) {
renderer.RendererTemplate(w, "theme.page.html", &models.TemplateData{})
if r.Method == http.MethodGet {
threadID := getThreadIDFromCookies(r)
posts, err := m.DB.GetAllPostsFromThread(threadID)
if err != nil {
setErrorAndRedirect(w, r, "Could not get all posts from thread", "/error-page")
}

var postsInfo []models.PostDataForThemePage

for _, post := range posts {
var user models.User
user, err = m.DB.GetUserByID(post.UserID)
if err != nil {
setErrorAndRedirect(w, r, "Could not get user by id", "/error-page")
}
var info models.PostDataForThemePage
info.Subject = post.Subject
info.Created = post.Created.Format("2006-01-02 15:04:05")
info.Content = post.Content
info.PictureUserWhoCreatedPost = user.Picture
info.UserNameWhoCreatedPost = user.UserName
postsInfo = append(postsInfo, info)
}

data := make(map[string]interface{})

data["posts"] = postsInfo

mainThread, err := m.DB.GetThreadByID(getThreadIDFromCookies(r))
if err != nil {
setErrorAndRedirect(w, r, "Could not get thread by id", "/error-page")
}

creator, err := m.DB.GetUserByID(mainThread.UserID)
if err != nil {
setErrorAndRedirect(w, r, "Could not get user as creator", "/error-page")
}

data["creatorName"] = creator.UserName
data["creatorImg"] = creator.Picture
data["mainThreadName"] = mainThread.Subject
data["mainThreadCreatedTime"] = mainThread.Created.Format("2006-01-02 15:04:05")

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

// ErrorPage handles the "/error-page" route
Expand Down
22 changes: 15 additions & 7 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ type Thread struct {
}

type ThreadDataForMainPage struct {
Subject string
Created string
UserNameWhoCreatedThread string
UserNameWhoCreatedLastPost string
PictureUserWhoCreatedThread string
PictureUserWhoCreatedLastPost string
Posts []Post
Subject string
Created string
UserNameWhoCreatedThread string
UserNameWhoCreatedLastPost string
PictureUserWhoCreatedThread string
PictureUserWhoCreatedLastPost string
Posts []Post
}

type PostDataForThemePage struct {
Subject string
Content string
Created string
UserNameWhoCreatedPost string
PictureUserWhoCreatedPost string
}

type Post struct {
Expand Down
25 changes: 22 additions & 3 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ func (m *SqliteBDRepo) GetUserByID(ID int) (models.User, error) {
return user, nil
}

func (m *SqliteBDRepo) GetThreadByID(ID int) (models.Thread, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

query := `select *
from thread
where id = $1
`
var thread models.Thread

row := m.DB.QueryRowContext(ctx, query, ID)

err := row.Scan(&thread.ID, &thread.Subject, &thread.Created, &thread.UserID)
if err != nil {
return thread, err
}
return thread, nil
}

func (m *SqliteBDRepo) CreateUser(r models.User) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
Expand All @@ -96,18 +115,18 @@ func (m *SqliteBDRepo) CreateUser(r models.User) error {
return nil
}

func (m *SqliteBDRepo) CreateThread(userID int, thread models.Thread) error {
func (m *SqliteBDRepo) CreateThread(thread models.Thread) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

stmt := `insert into thread
(subject, userID)
values ($1, $2, )
values ($1, $2)
`

_, err := m.DB.ExecContext(ctx, stmt,
thread.Subject,
userID,
thread.UserID,
)

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ type DatabaseInt interface {
UserPresent(userName, email string) (bool, error)
UserPresentLogin(email, password string) (bool, error)
CreateUser(r models.User) error
CreateThread(userID int, thread models.Thread) error
CreateThread(thread models.Thread) error
CreatePost(post models.Post) error
IsThreadExist(thread models.Thread) (bool, error)
GetAllPostsFromThread(threadID int) ([]models.Post, error)
GetUserByID(ID int) (models.User, error)
GetAllThreads() ([]models.Thread, error)
GetThreadByID(ID int) (models.Thread, error)
}
5 changes: 4 additions & 1 deletion static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,7 @@ section {
a:hover {
color: rgb(255, 255, 255); /* Color on hover */
}


.modal-content {
background-color: rgb(54, 50, 74);
}
29 changes: 28 additions & 1 deletion template/home.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@
<table class="table-borderless forumTable whiteLink mt-3">
<thead class="containerHead">
<tr >
<th class="forumFirstCol" scope="col">Forums</th>
<!-- modal starts -->
<th class="forumFirstCol" scope="col">
Forums<br>
<button type="button" class="btn btn-light" data-bs-toggle="modal" data-bs-target="#createNewTopicModal" data-bs-whatever="@getbootstrap">Create New Topic</button>
</th>
<div class="modal fade text-bg-dark p-3" id="createNewTopicModal" tabindex="-1" aria-labelledby="createNewTopicModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="createNewTopicModalLabel">What this topic will be about?</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post" action="/home">
<div class="mb-3">
<label for="message-text" class="col-form-label">Type your topic here:</label>
<textarea class="form-control" id="message-text" name="message-text"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-light">Publish new topic</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- modal ends -->
<th scope="col">Latest Message</th>
<th scope="col">Total Messages</th>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion template/login.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>Login</h1>
{{end}}
<input class="form-control" type="password" name="passwordLogIn" id="passwordLogIn" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Login</button>
<button type="submit" class="btn btn-light">Login</button>
</form>
</div>
</div>
Expand Down
28 changes: 3 additions & 25 deletions template/main.layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,16 @@
<button class="btn btn-outline-light" type="submit">Search</button>
</form>
<div>
<a class="btn login nav-link active" href="/login">Loging</a>
<a class="btn register nav-link active" href="/registration">Register</a>
<a class="btn btn-light login" href="/login">Loging</a>
<a class="btn btn-light register" href="/registration">Register</a>
</div>
</div>
</div>
</nav>

</head>
<body>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

<div class="container custom-container">
<table class="table-borderless forumTable whiteLink mt-3">
<thead class="containerHead">
Expand Down
2 changes: 1 addition & 1 deletion template/register.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h1>Register</h1>

<input type="file" class="form-control" name="avatar" id="avatar">
</div>
<button type="submit" class="btn btn-primary">Register</button>
<button type="submit" class="btn btn-light">Register</button>
</form>
</div>
</div>
Expand Down
8 changes: 6 additions & 2 deletions template/theme.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@
<table class="table-borderless forumTable whiteLink mt-3">
<thead class="containerHead">
<tr >

<th class="forumFirstColTheme" scope="col" colspan="2">Project TL</th>
{{/* info related to the main(mother)Thread */}}
<th class="forumFirstColTheme" scope="col" colspan="2">
{{index .Data "mainThreadName"}} <br>
Created: {{index .Data "mainThreadCreatedTime"}}
</th>

<th></th>

</tr>
</thead>
<tbody class="containerBody">
Expand Down

0 comments on commit d31dd48

Please sign in to comment.