update templates, structs and controllers to support captcha

This commit is contained in:
Yanislav Igonin 2021-10-04 11:40:53 +03:00
parent e0d44aa0b6
commit 77096ef867
5 changed files with 54 additions and 17 deletions

View File

@ -51,17 +51,20 @@ func GetThreads(c *gin.Context) {
c.HTML(http.StatusNotFound, "404.html", nil) c.HTML(http.StatusNotFound, "404.html", nil)
return return
} }
data := Repositories.IndexPageData{
Threads: threads,
PagesCount: pagesCount,
Page: page,
}
csrfToken := csrf.GetToken(c) csrfToken := csrf.GetToken(c)
c.SetCookie("csrf", csrfToken, 60, "/", "", true, true) c.SetCookie("csrf", csrfToken, 60, "/", "", true, true)
captchaID := captcha.New() captchaID := captcha.New()
log.Println(captchaID) htmlData := Repositories.GetThreadsHtmlData{
c.HTML(http.StatusOK, "index.html", data) Threads: threads,
PagesCount: pagesCount,
Page: page,
FormData: Repositories.HtmlFormData{
CaptchaID: captchaID,
},
}
c.HTML(http.StatusOK, "index.html", htmlData)
} }
func GetThread(c *gin.Context) { func GetThread(c *gin.Context) {
@ -84,7 +87,17 @@ func GetThread(c *gin.Context) {
csrfToken := csrf.GetToken(c) csrfToken := csrf.GetToken(c)
c.SetCookie("csrf", csrfToken, 60, "/", "", true, true) c.SetCookie("csrf", csrfToken, 60, "/", "", true, true)
c.HTML(http.StatusOK, "thread.html", thread)
firstPost := thread[0]
captchaID := captcha.New()
htmlData := Repositories.GetThreadHtmlData{
Thread: thread,
FormData: Repositories.HtmlFormData{
FirstPostID: firstPost.ID,
CaptchaID: captchaID,
},
}
c.HTML(http.StatusOK, "thread.html", htmlData)
} }
func CreateThread(c *gin.Context) { func CreateThread(c *gin.Context) {

View File

@ -2,6 +2,10 @@ package repositories
import "time" import "time"
// DB Structs
// DB Structs
// DB Structs
type Post struct { type Post struct {
ID int `json:"id"` ID int `json:"id"`
IsParent bool `json:"-"` IsParent bool `json:"-"`
@ -24,8 +28,23 @@ type File struct {
Size int `json:"size"` Size int `json:"size"`
} }
type IndexPageData struct { // HTML Templates Structs
// HTML Templates Structs
// HTML Templates Structs
type HtmlFormData struct {
FirstPostID int
CaptchaID string
}
type GetThreadHtmlData struct {
Thread []Post
FormData HtmlFormData
}
type GetThreadsHtmlData struct {
Threads []Post Threads []Post
PagesCount int PagesCount int
Page int Page int
FormData HtmlFormData
} }

View File

@ -13,7 +13,7 @@
<h1 class="display-1 text-center">Welcome to Micrach</h1> <h1 class="display-1 text-center">Welcome to Micrach</h1>
<div class="container"> <div class="container">
{{ template "post-form" }} {{ template "post-form" .FormData }}
<div id="" class="row row-cols-auto gy-4 mb-4"> <div id="" class="row row-cols-auto gy-4 mb-4">
{{ range $Post := .Threads }} {{ range $Post := .Threads }}

View File

@ -65,15 +65,20 @@
{{ define "post-form" }} {{ define "post-form" }}
<div class="col col-12"> <div class="col col-12">
<form id="postForm" action="/{{ . }}" method="POST" enctype="multipart/form-data"> {{ if eq .FirstPostID 0 }}
{{ if eq . nil }} <form id="postForm" action="/" method="POST" enctype="multipart/form-data">
{{ else }}
<form id="postForm" action="/{{ .FirstPostID }}" method="POST" enctype="multipart/form-data">
{{ end }}
{{ if eq .FirstPostID 0 }}
<input class="form-control" id="postTitle" placeholder="Title" name="title"> <input class="form-control" id="postTitle" placeholder="Title" name="title">
{{ end }} {{ end }}
<textarea class="form-control" id="postText" rows="5" placeholder="Text" name="text"></textarea> <textarea class="form-control" id="postText" rows="5" placeholder="Text" name="text"></textarea>
<input class="form-control" type="file" id="postFiles" multiple name="files"> <input class="form-control" type="file" id="postFiles" multiple name="files">
<div class="row"> <div class="row">
{{ if ne . nil }} {{ if ne .FirstPostID 0 }}
<div class="col"> <div class="col">
<input class="form-check-input" type="checkbox" value="" id="postSage" name="sage"> <input class="form-check-input" type="checkbox" value="" id="postSage" name="sage">
<label class="form-check-label" for="postSage"> <label class="form-check-label" for="postSage">
@ -84,7 +89,7 @@
<div class="col text-end"> <div class="col text-end">
<button class="col btn btn-outline-primary" type="submit" > <button class="col btn btn-outline-primary" type="submit" >
{{ if ne . nil }} {{ if ne .FirstPostID 0 }}
Send Send
{{ else }} {{ else }}
Create thread Create thread

View File

@ -1,4 +1,4 @@
{{ $FirstPost:= index . 0 }} {{ $FirstPost:= index .Thread 0 }}
<html lang="en"> <html lang="en">
<head> <head>
@ -20,10 +20,10 @@
</h1> </h1>
<div class="container"> <div class="container">
{{ template "post-form" $FirstPost.ID }} {{ template "post-form" .FormData }}
<div id="postsСontainer"> <div id="postsСontainer">
{{range $Post := .}} {{ range $Post := .Thread }}
<div class="post-container mb-4"> <div class="post-container mb-4">
{{ $filesLength := len $Post.Files }} {{ $filesLength := len $Post.Files }}