Skip to content

Commit b6cf233

Browse files
committed
Add dates to user posts
User posts now include the date it was submitted. The layout for each post has also been changed to account for the date. Changes made drumstick.sql file - added created_on column of type date to posts table to automatically time stamp a date for a post when created. Changes made to posts.go file Changes made to the post struct - Added createdOn field of type time.Time to map to the the same field in the posts table - Added the date string field to store the date as a formatted string. This allows the date to be viewed via the posts template file - Changes made to usernameByID func - added logic to format the date in usernameByID func to store the date as a string in the post.Date field - Changes made to CreatePosts func - updated fields to inlcude the createdOn field to map to created_on column in the posts table Changes made to posts.tmpl file - looping through posts now shows the date - added link to new drumstick.css file for better layout of posts elements Changes made to main.go file - added the echo.Static func to include the root directory path to the static file drumstick.css file to shorten the import path for posts.tmpl.
1 parent c8301e8 commit b6cf233

5 files changed

Lines changed: 77 additions & 35 deletions

File tree

drumstick.sql

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- PostgreSQL database dump
33
--
44

5-
\restrict xIB8oa6OL1g4W2kgqB6ttDmSQdalyoWow5wgfGhof14LWv70TjhknIVbzwgYgcH
5+
\restrict kjat48JEwxUStXA4GnmmegyFEUjW5UWBb3apRFcL9rp6CJIGB2HyW9kbVVQuulT
66

77
-- Dumped from database version 16.10 (Homebrew)
88
-- Dumped by pg_dump version 16.10 (Homebrew)
@@ -32,6 +32,7 @@ CREATE TABLE public.posts (
3232
thread_id integer NOT NULL,
3333
username character varying(30) NOT NULL,
3434
content character varying(350) NOT NULL,
35+
created_on date DEFAULT CURRENT_DATE NOT NULL,
3536
CONSTRAINT non_empty_column CHECK ((length(TRIM(BOTH FROM content)) > 0))
3637
);
3738

@@ -159,7 +160,7 @@ ALTER TABLE ONLY public.user_profile ALTER COLUMN id SET DEFAULT nextval('public
159160
-- Data for Name: posts; Type: TABLE DATA; Schema: public; Owner: <username>
160161
--
161162

162-
COPY public.posts (user_id, parent_id, thread_id, username, content) FROM stdin;
163+
COPY public.posts (user_id, parent_id, thread_id, username, content, created_on) FROM stdin;
163164
\.
164165

165166

@@ -200,14 +201,6 @@ SELECT pg_catalog.setval('public.user_account_id_seq', 1, false);
200201
SELECT pg_catalog.setval('public.user_profile_id_seq', 1, false);
201202

202203

203-
--
204-
-- Name: posts posts_user_id_parent_id_thread_id_username_content_key; Type: CONSTRAINT; Schema: public; Owner: <username>
205-
--
206-
207-
ALTER TABLE ONLY public.posts
208-
ADD CONSTRAINT posts_user_id_parent_id_thread_id_username_content_key UNIQUE (user_id, parent_id, thread_id, username, content);
209-
210-
211204
--
212205
-- Name: user_account user_account_email_key; Type: CONSTRAINT; Schema: public; Owner: <username>
213206
--
@@ -300,5 +293,5 @@ ALTER TABLE ONLY public.user_profile
300293
-- PostgreSQL database dump complete
301294
--
302295

303-
\unrestrict xIB8oa6OL1g4W2kgqB6ttDmSQdalyoWow5wgfGhof14LWv70TjhknIVbzwgYgcH
296+
\unrestrict kjat48JEwxUStXA4GnmmegyFEUjW5UWBb3apRFcL9rp6CJIGB2HyW9kbVVQuulT
304297

internal/posts/posts.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import (
55
"fmt"
66
"scriptmang/drumstick/internal/accts"
77
"scriptmang/drumstick/internal/backend"
8+
"time"
89

910
"github.com/georgysavva/scany/v2/pgxscan"
1011
"github.com/jackc/pgx/v5"
1112
"github.com/jackc/pgx/v5/pgconn"
1213
)
1314

1415
type Post struct {
15-
UserID int `json:"user_id" form:"user_id"`
16-
ParentID int `json:"parent_id"`
17-
ThreadID int `json:"thread_id"`
18-
Username string `json:"username" form:"username"`
19-
Content string `json:"content" form:"content"`
16+
UserID int `json:"user_id" form:"user_id"`
17+
ParentID int `json:"parent_id"`
18+
ThreadID int `json:"thread_id"`
19+
Username string `json:"username" form:"username"`
20+
Content string `json:"content" form:"content"`
21+
CreatedOn time.Time `json:"created_on"`
22+
Date string
2023
}
2124

2225
func usernameByID(id int) (string, error) {
@@ -41,6 +44,11 @@ func UserPostsByID(uid int) ([]*Post, error) {
4144
if err != nil {
4245
return nil, fmt.Errorf("error: %w", err)
4346
}
47+
48+
for _, pst := range userPosts {
49+
pst.Date = pst.CreatedOn.Format("01/02/2006")
50+
}
51+
4452
return userPosts, nil
4553
}
4654

@@ -58,7 +66,7 @@ func CreatePosts(userPost, email string) (*Post, error) {
5866
return nil, fmt.Errorf("error: %w", uidErr)
5967
}
6068

61-
// add posts to posts
69+
// get the user's username
6270
username, userNameErr := usernameByID(uid)
6371
if userNameErr != nil {
6472
return nil, fmt.Errorf("error: %v\n", userNameErr)
@@ -68,7 +76,8 @@ func CreatePosts(userPost, email string) (*Post, error) {
6876
err := db.QueryRow(ctx, `INSERT INTO posts(user_id, parent_id, username, content)`+
6977
` VALUES($1,$2,$3,$4) RETURNING *`, uid, 0, username, userPost).Scan(
7078
&tempPost.UserID, &tempPost.ParentID,
71-
&tempPost.ThreadID, &tempPost.Username, &tempPost.Content,
79+
&tempPost.ThreadID, &tempPost.Username,
80+
&tempPost.Content, &tempPost.CreatedOn,
7281
)
7382

7483
if err != nil {

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ func main() {
233233
router.Use(middleware.Logger())
234234
router.Use(middleware.Recover())
235235

236-
router.Renderer = tm
236+
router.Static("static", "ui/static/")
237237

238+
router.Renderer = tm
238239
router.GET("/", homePage)
239240
router.GET("/signup", signUp)
240241
router.GET("/loginForm", loginForm)

ui/html/pages/posts.tmpl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
<head>
55
<meta charset='utf-8'>
66
<title>Posts</title>
7-
<link rel='stylesheet' href='https://fonts.google.apis.com/css?family=Ubuntu+Mono:400,700'>
7+
<link rel='stylesheet' href="static/drumstick.css">
88
</head>
9-
<body>
9+
<body>
10+
1011
<header>
1112
<h1>View Posts</h1>
1213
</header>
@@ -22,24 +23,26 @@
2223

2324

2425
<h3> My Feed</h3>
25-
<hr>
26-
26+
<hr class="post-separator">
2727
{{range .Posts}}
28-
<div name="post-container" style="margin-bottom: 5rem">
29-
30-
<div name="post-top-div">
31-
<p><strong>{{.Username}}</strong></p>
32-
</div>
33-
34-
<div name="post-center-div" style="margin-bototm: 4rem; margin-left: 1rem">
35-
<p>{{.Content}}</p>
36-
</div>
37-
28+
<div class="posts">
29+
<div class="posts-row">
30+
<div class="posts-item">
31+
<p><strong>{{.Username}}</strong></p>
32+
</div>
33+
34+
<div class="posts-item">
35+
<p><strong>{{.Date}}</strong></p>
36+
</div>
37+
</div>
38+
39+
<div class="posts-body">
40+
<p>{{.Content}}</p>
41+
</div>
3842
</div>
39-
<hr>
43+
<hr class="posts-separator bottom-separator">
4044
{{end}}
4145

42-
4346
</body>
4447
</html>
4548
{{end}}

ui/static/drumstick.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.post-container {
2+
margin-bottom: 0;
3+
padding: 0;
4+
}
5+
6+
.posts-row {
7+
display: flex;
8+
justify-content: space-between;
9+
gap: 5px;
10+
margin-bottom: 0.25rem;
11+
}
12+
13+
.posts-body {
14+
display: block;
15+
padding: 0;
16+
margin-left: 1rem;
17+
}
18+
19+
.posts-item {
20+
padding: 0;
21+
}
22+
23+
.posts-row p, .posts-body p {
24+
margin: 0;
25+
line-height: 1.2;
26+
}
27+
28+
.posts-separator {
29+
margin: 0.5rem 0;
30+
border: none;
31+
border-top: 1px solid black;
32+
}
33+
34+
.bottom-separator {
35+
margin-top: 4rem;
36+
}

0 commit comments

Comments
 (0)