feat: add threads mock, make template work

This commit is contained in:
Yanislav Igonin 2021-08-28 18:41:34 +03:00
parent 157a8108d5
commit a7f31e48c2
6 changed files with 114 additions and 4 deletions

2
.gitignore vendored
View File

@ -18,3 +18,5 @@
tmp/
micrach
uploads/

View File

@ -4,10 +4,13 @@ import (
"net/http"
"github.com/gin-gonic/gin"
Repositories "micrach/repositories"
)
func GetThreads(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
threads := Repositories.Threads.Get()
c.HTML(http.StatusOK, "index.html", threads)
}
func GetThread(c *gin.Context) {

View File

@ -21,6 +21,7 @@ func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*.html")
router.Static("/uploads", "./uploads")
router.GET("/", Controllers.GetThreads)
router.POST("/", Controllers.CreateThread)
router.GET("/:threadId", Controllers.GetThread)

29
repositories/structs.go Normal file
View File

@ -0,0 +1,29 @@
package repositories
import "time"
type Thread struct {
ID int `json:"id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Posts []Post `json:"posts"`
}
type Post struct {
ID int `json:"id"`
ThreadID int `json:"-"`
Title string `json:"title"`
Text string `json:"text"`
IsSage bool `json:"isSage"`
Files []File `json:"files"`
CreatedAt time.Time `json:"createdAt"`
}
type File struct {
ID int `json:"-"`
PostID int `json:"-"`
CreatedAt time.Time `json:"-"`
Name string `json:"name"`
Ext string `json:"-"`
Size int `json:"size"`
}

View File

@ -0,0 +1,55 @@
package repositories
import "time"
func getFile(id, postId int, name string) File {
return File{
ID: id,
PostID: postId,
Name: name,
Ext: "image/jpeg",
Size: 10000,
CreatedAt: time.Now(),
}
}
func getPost(id, threadID int) Post {
return Post{
ID: id,
ThreadID: threadID,
Title: "Basic Title",
Text: "Basic Text",
IsSage: false,
Files: []File{
getFile(2, id, "Screenshot 2020-08-14 at 23.17.29.png"),
getFile(1, id, "maxresdefault.jpg"),
},
CreatedAt: time.Now(),
}
}
func getThread(id int) Thread {
return Thread{
ID: id,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Posts: []Post{
getPost(1, id),
getPost(1, id),
},
}
}
var ThreadsDb = []Thread{
getThread(1),
getThread(2),
getThread(3),
}
type ThreadsRepository struct{}
var Threads ThreadsRepository
func (r *ThreadsRepository) Get() []Thread {
return ThreadsDb
}

View File

@ -17,8 +17,28 @@
</head>
<body>
<div>
INDEX
<h1 class="display-1 text-center">Welcome to Micrach</h1>
<div class="container">
{{range $Thread := .}}
{{$FirstPost := index $Thread.Posts 0}}
<div class="card" style="width: 18rem;">
{{$FirstFile := index $FirstPost.Files 0}}
<img
src="/uploads/{{$Thread.ID}}/{{$FirstFile.Name}}"
class="card-img-top"
alt="Uploaded picture"
>
<div class="card-body">
<h5 class="card-title">{{$FirstPost.Title}}</h5>
<p class="card-text">{{$FirstPost.Text}}</p>
<a href="/{{$Thread.ID}}" class="btn btn-primary">Open</a>
</div>
</div>
{{end}}
</div>
</body>