Skip to content

Commit 179999b

Browse files
Migrated files page.
1 parent 85acd5a commit 179999b

File tree

10 files changed

+138
-77
lines changed

10 files changed

+138
-77
lines changed

pkg/handlers/auth.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import (
1919
"github.com/mikestefanello/pagoda/pkg/ui"
2020
)
2121

22-
type (
23-
Auth struct {
24-
auth *services.AuthClient
25-
mail *services.MailClient
26-
orm *ent.Client
27-
}
28-
)
22+
type Auth struct {
23+
auth *services.AuthClient
24+
mail *services.MailClient
25+
orm *ent.Client
26+
}
2927

3028
func init() {
3129
Register(new(Auth))
@@ -81,7 +79,7 @@ func (h *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
8179
return err
8280
}
8381

84-
// Attempt to load the user
82+
// Attempt to load the user.
8583
u, err := h.orm.User.
8684
Query().
8785
Where(user.Email(strings.ToLower(input.Email))).
@@ -95,7 +93,7 @@ func (h *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
9593
return fail(err, "error querying user during forgot password")
9694
}
9795

98-
// Generate the token
96+
// Generate the token.
9997
token, pt, err := h.auth.GeneratePasswordResetToken(ctx, u.ID)
10098
if err != nil {
10199
return fail(err, "error generating password reset token")
@@ -105,7 +103,7 @@ func (h *Auth) ForgotPasswordSubmit(ctx echo.Context) error {
105103
"user_id", u.ID,
106104
)
107105

108-
// Email the user
106+
// Email the user.
109107
url := ctx.Echo().Reverse(routenames.ResetPassword, u.ID, pt.ID, token)
110108
err = h.mail.
111109
Compose().
@@ -145,7 +143,7 @@ func (h *Auth) LoginSubmit(ctx echo.Context) error {
145143
return err
146144
}
147145

148-
// Attempt to load the user
146+
// Attempt to load the user.
149147
u, err := h.orm.User.
150148
Query().
151149
Where(user.Email(strings.ToLower(input.Email))).
@@ -159,13 +157,13 @@ func (h *Auth) LoginSubmit(ctx echo.Context) error {
159157
return fail(err, "error querying user during login")
160158
}
161159

162-
// Check if the password is correct
160+
// Check if the password is correct.
163161
err = h.auth.CheckPassword(input.Password, u.Password)
164162
if err != nil {
165163
return authFailed()
166164
}
167165

168-
// Log the user in
166+
// Log the user in.
169167
err = h.auth.Login(ctx, u.ID)
170168
if err != nil {
171169
return fail(err, "unable to log in user")
@@ -206,13 +204,13 @@ func (h *Auth) RegisterSubmit(ctx echo.Context) error {
206204
return err
207205
}
208206

209-
// Hash the password
207+
// Hash the password.
210208
pwHash, err := h.auth.HashPassword(input.Password)
211209
if err != nil {
212210
return fail(err, "unable to hash password")
213211
}
214212

215-
// Attempt creating the user
213+
// Attempt creating the user.
216214
u, err := h.orm.User.
217215
Create().
218216
SetName(input.Name).
@@ -235,7 +233,7 @@ func (h *Auth) RegisterSubmit(ctx echo.Context) error {
235233
return fail(err, "unable to create user")
236234
}
237235

238-
// Log the user in
236+
// Log the user in.
239237
err = h.auth.Login(ctx, u.ID)
240238
if err != nil {
241239
log.Ctx(ctx).Error("unable to log user in",
@@ -250,7 +248,7 @@ func (h *Auth) RegisterSubmit(ctx echo.Context) error {
250248

251249
msg.Success(ctx, "Your account has been created. You are now logged in.")
252250

253-
// Send the verification email
251+
// Send the verification email.
254252
h.sendVerificationEmail(ctx, u)
255253

256254
return redirect.New(ctx).
@@ -259,7 +257,7 @@ func (h *Auth) RegisterSubmit(ctx echo.Context) error {
259257
}
260258

261259
func (h *Auth) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
262-
// Generate a token
260+
// Generate a token.
263261
token, err := h.auth.GenerateEmailVerificationToken(usr.Email)
264262
if err != nil {
265263
log.Ctx(ctx).Error("unable to generate email verification token",
@@ -269,7 +267,7 @@ func (h *Auth) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
269267
return
270268
}
271269

272-
// Send the email
270+
// Send the email.
273271
url := ctx.Echo().Reverse(routenames.VerifyEmail, token)
274272
err = h.mail.
275273
Compose().
@@ -306,16 +304,16 @@ func (h *Auth) ResetPasswordSubmit(ctx echo.Context) error {
306304
return err
307305
}
308306

309-
// Hash the new password
307+
// Hash the new password.
310308
hash, err := h.auth.HashPassword(input.Password)
311309
if err != nil {
312310
return fail(err, "unable to hash password")
313311
}
314312

315-
// Get the requesting user
313+
// Get the requesting user.
316314
usr := ctx.Get(context.UserKey).(*ent.User)
317315

318-
// Update the user
316+
// Update the user.
319317
_, err = usr.
320318
Update().
321319
SetPassword(hash).
@@ -325,7 +323,7 @@ func (h *Auth) ResetPasswordSubmit(ctx echo.Context) error {
325323
return fail(err, "unable to update password")
326324
}
327325

328-
// Delete all password tokens for this user
326+
// Delete all password tokens for this user.
329327
err = h.auth.DeletePasswordTokens(ctx, usr.ID)
330328
if err != nil {
331329
return fail(err, "unable to delete password tokens")
@@ -340,7 +338,7 @@ func (h *Auth) ResetPasswordSubmit(ctx echo.Context) error {
340338
func (h *Auth) VerifyEmail(ctx echo.Context) error {
341339
var usr *ent.User
342340

343-
// Validate the token
341+
// Validate the token.
344342
token := ctx.Param("token")
345343
email, err := h.auth.ValidateEmailVerificationToken(token)
346344
if err != nil {
@@ -350,7 +348,7 @@ func (h *Auth) VerifyEmail(ctx echo.Context) error {
350348
Go()
351349
}
352350

353-
// Check if it matches the authenticated user
351+
// Check if it matches the authenticated user.
354352
if u := ctx.Get(context.AuthenticatedUserKey); u != nil {
355353
authUser := u.(*ent.User)
356354

@@ -359,7 +357,7 @@ func (h *Auth) VerifyEmail(ctx echo.Context) error {
359357
}
360358
}
361359

362-
// Query to find a matching user, if needed
360+
// Query to find a matching user, if needed.
363361
if usr == nil {
364362
usr, err = h.orm.User.
365363
Query().
@@ -371,7 +369,7 @@ func (h *Auth) VerifyEmail(ctx echo.Context) error {
371369
}
372370
}
373371

374-
// Verify the user, if needed
372+
// Verify the user, if needed.
375373
if !usr.Verified {
376374
usr, err = usr.
377375
Update().

pkg/handlers/cache.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import (
1111
"github.com/mikestefanello/pagoda/pkg/ui"
1212
)
1313

14-
type (
15-
Cache struct {
16-
cache *services.CacheClient
17-
}
18-
)
14+
type Cache struct {
15+
cache *services.CacheClient
16+
}
1917

2018
func init() {
2119
Register(new(Cache))
@@ -59,7 +57,7 @@ func (h *Cache) Submit(ctx echo.Context) error {
5957
return err
6058
}
6159

62-
// Set the cache
60+
// Set the cache.
6361
err := h.cache.
6462
Set().
6563
Key("page_cache_example").

pkg/handlers/files.go

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,47 @@ import (
77

88
"github.com/labstack/echo/v4"
99
"github.com/mikestefanello/pagoda/pkg/msg"
10-
"github.com/mikestefanello/pagoda/pkg/page"
10+
"github.com/mikestefanello/pagoda/pkg/routenames"
1111
"github.com/mikestefanello/pagoda/pkg/services"
12-
"github.com/mikestefanello/pagoda/templates"
12+
"github.com/mikestefanello/pagoda/pkg/ui"
1313
"github.com/spf13/afero"
1414
)
1515

16-
const (
17-
routeNameFiles = "files"
18-
routeNameFilesSubmit = "files.submit"
19-
)
20-
21-
type (
22-
Files struct {
23-
files afero.Fs
24-
*services.TemplateRenderer
25-
}
26-
27-
File struct {
28-
Name string
29-
Size int64
30-
Modified string
31-
}
32-
)
16+
type Files struct {
17+
files afero.Fs
18+
}
3319

3420
func init() {
3521
Register(new(Files))
3622
}
3723

3824
func (h *Files) Init(c *services.Container) error {
39-
h.TemplateRenderer = c.TemplateRenderer
4025
h.files = c.Files
4126
return nil
4227
}
4328

4429
func (h *Files) Routes(g *echo.Group) {
45-
g.GET("/files", h.Page).Name = routeNameFiles
46-
g.POST("/files", h.Submit).Name = routeNameFilesSubmit
30+
g.GET("/files", h.Page).Name = routenames.Files
31+
g.POST("/files", h.Submit).Name = routenames.FilesSubmit
4732
}
4833

4934
func (h *Files) Page(ctx echo.Context) error {
50-
p := page.New(ctx)
51-
p.Layout = templates.LayoutMain
52-
p.Name = templates.PageFiles
53-
p.Title = "Upload a file"
54-
55-
// Send a list of all uploaded files to the template to be rendered.
35+
// Compile a list of all uploaded files to be rendered.
5636
info, err := afero.ReadDir(h.files, "")
5737
if err != nil {
5838
return err
5939
}
6040

61-
files := make([]File, 0)
41+
files := make([]*ui.File, 0)
6242
for _, file := range info {
63-
files = append(files, File{
43+
files = append(files, &ui.File{
6444
Name: file.Name(),
6545
Size: file.Size(),
6646
Modified: file.ModTime().Format(time.DateTime),
6747
})
6848
}
6949

70-
p.Data = files
71-
72-
return h.RenderPage(ctx, p)
50+
return ui.UploadFile(ctx, files)
7351
}
7452

7553
func (h *Files) Submit(ctx echo.Context) error {

pkg/handlers/pages.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ func (h *Pages) HomeOld(ctx echo.Context) error {
6262

6363
func (h *Pages) Home(ctx echo.Context) error {
6464
pgr := page.NewPager(ctx, 4)
65-
p := h.fetchPosts(&pgr)
6665

6766
return ui.Home(ctx, ui.Posts{
68-
Posts: p,
67+
Posts: h.fetchPosts(&pgr),
6968
Pager: pgr,
7069
})
7170
}

pkg/log/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"github.com/mikestefanello/pagoda/pkg/context"
88
)
99

10-
// Set sets a logger in the context
10+
// Set sets a logger in the context.
1111
func Set(ctx echo.Context, logger *slog.Logger) {
1212
ctx.Set(context.LoggerKey, logger)
1313
}
1414

15-
// Ctx returns the logger stored in context, or provides the default logger if one is not present
15+
// Ctx returns the logger stored in context, or provides the default logger if one is not present.
1616
func Ctx(ctx echo.Context) *slog.Logger {
1717
if l, ok := ctx.Get(context.LoggerKey).(*slog.Logger); ok {
1818
return l
@@ -21,7 +21,7 @@ func Ctx(ctx echo.Context) *slog.Logger {
2121
return Default()
2222
}
2323

24-
// Default returns the default logger
24+
// Default returns the default logger.
2525
func Default() *slog.Logger {
2626
return slog.Default()
2727
}

pkg/routenames/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ const (
1919
TaskSubmit = "task.submit"
2020
Cache = "cache"
2121
CacheSubmit = "cache.submit"
22+
Files = "files"
23+
FilesSubmit = "files.submit"
2224
)

pkg/services/mail.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ func NewMailClient(cfg *config.Config) (*MailClient, error) {
3939
}, nil
4040
}
4141

42-
// Compose creates a new email
42+
// Compose creates a new email.
4343
func (m *MailClient) Compose() *mail {
4444
return &mail{
4545
client: m,
4646
from: m.config.Mail.FromAddress,
4747
}
4848
}
4949

50-
// skipSend determines if mail sending should be skipped
50+
// skipSend determines if mail sending should be skipped.
5151
func (m *MailClient) skipSend() bool {
5252
return m.config.App.Environment != config.EnvProduction
5353
}
5454

55-
// send attempts to send the email
55+
// send attempts to send the email.
5656
func (m *MailClient) send(email *mail, ctx echo.Context) error {
5757
switch {
5858
case email.to == "":
@@ -73,15 +73,15 @@ func (m *MailClient) send(email *mail, ctx echo.Context) error {
7373
email.body = buf.String()
7474
}
7575

76-
// Check if mail sending should be skipped
76+
// Check if mail sending should be skipped.
7777
if m.skipSend() {
7878
log.Ctx(ctx).Debug("skipping email delivery",
7979
"to", email.to,
8080
)
8181
return nil
8282
}
8383

84-
// TODO: Finish based on your mail sender of choice!
84+
// TODO: Finish based on your mail sender of choice or stop logging below!
8585
log.Ctx(ctx).Info("sending email",
8686
"to", email.to,
8787
"subject", email.subject,
@@ -121,7 +121,7 @@ func (m *mail) Component(component gomponents.Node) *mail {
121121
return m
122122
}
123123

124-
// Send attempts to send the email
124+
// Send attempts to send the email.
125125
func (m *mail) Send(ctx echo.Context) error {
126126
return m.client.send(m, ctx)
127127
}

0 commit comments

Comments
 (0)