diff --git a/.gitignore b/.gitignore index 17adbe7..266f6cd 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,6 @@ .vscode/ tmp/ -micrach \ No newline at end of file +micrach + +uploads/ \ No newline at end of file diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 479471f..b8188a8 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -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) { diff --git a/main.go b/main.go index a3281ad..861d2dd 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/repositories/structs.go b/repositories/structs.go new file mode 100644 index 0000000..42011f1 --- /dev/null +++ b/repositories/structs.go @@ -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"` +} diff --git a/repositories/threads_repository.go b/repositories/threads_repository.go new file mode 100644 index 0000000..71537e6 --- /dev/null +++ b/repositories/threads_repository.go @@ -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 +} diff --git a/templates/index.html b/templates/index.html index 24e9d10..422376a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,8 +17,28 @@ -
-INDEX +

Welcome to Micrach

+ +
+ {{range $Thread := .}} + {{$FirstPost := index $Thread.Posts 0}} + +
+ {{$FirstFile := index $FirstPost.Files 0}} + + Uploaded picture +
+
{{$FirstPost.Title}}
+

{{$FirstPost.Text}}

+ Open +
+
+ + {{end}}