Skip to content

Commit

Permalink
add some logging into subscribe handler
Browse files Browse the repository at this point in the history
  • Loading branch information
quantonganh committed Aug 24, 2023
1 parent 6f88616 commit 18fe362
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
5 changes: 3 additions & 2 deletions bolt/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ func (ss *subscribeService) Insert(s *blog.Subscribe) error {
return nil
}

// UpdateStatus updates subscribe status
func (ss *subscribeService) UpdateStatus(email string) error {
// Update updates subscribe status and new token
func (ss *subscribeService) Update(email, token string) error {
s, err := ss.FindByEmail(email)
if err != nil {
return err
}

s.Status = blog.StatusPending
s.Token = token
if err := ss.db.stormDB.Save(s); err != nil {
return errors.Errorf("failed to save: %v", err)
}
Expand Down
14 changes: 13 additions & 1 deletion http/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/asdine/storm/v3"
"github.com/pkg/errors"
"github.com/rs/zerolog/hlog"

"github.com/quantonganh/blog"
)
Expand All @@ -22,24 +23,29 @@ func (s *Server) subscribeHandler(w http.ResponseWriter, r *http.Request) error
token := s.SMTPService.GenerateNewUUID()
newSubscriber := blog.NewSubscribe(email, token, blog.StatusPending)

logger := hlog.FromRequest(r)
subscribe, err := s.SubscribeService.FindByEmail(email)
if err != nil {
if errors.Is(err, storm.ErrNotFound) {
logger.Info().Msg("Sending confirmation email")
if err := s.SMTPService.SendConfirmationEmail(email, token); err != nil {
return err
}

logger.Info().Msgf("Saving new subscriber %+v into the database", newSubscriber)
if err := s.SubscribeService.Insert(newSubscriber); err != nil {
return err
}

logger.Info().Msg("Rendering the response message")
if err := s.Renderer.RenderResponseMessage(w, fmt.Sprintf(confirmationMessage, newSubscriber.Email)); err != nil {
return err
}
} else {
return NewError(err, http.StatusNotFound, fmt.Sprintf("Cannot found email: %s", email))
}
} else {
logger.Info().Msgf("Found subscriber %+v in the database", subscribe)
switch subscribe.Status {
case blog.StatusPending:
if err := s.Renderer.RenderResponseMessage(w, pendingMessage); err != nil {
Expand All @@ -54,7 +60,13 @@ func (s *Server) subscribeHandler(w http.ResponseWriter, r *http.Request) error
return err
}

if err := s.SubscribeService.UpdateStatus(email); err != nil {
logger.Info().Msgf("Updating status to %s", blog.StatusPending)
if err := s.SubscribeService.Update(email, token); err != nil {
return err
}

logger.Info().Msg("Rendering the response message")
if err := s.Renderer.RenderResponseMessage(w, fmt.Sprintf(confirmationMessage, email)); err != nil {
return err
}
}
Expand Down
41 changes: 32 additions & 9 deletions mock/SubscribeService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mongo/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (ss *subscribeService) Insert(s *blog.Subscribe) error {
return nil
}

func (ss *subscribeService) UpdateStatus(email string) error {
func (ss *subscribeService) Update(email, token string) error {
fmt.Println("Not implemented")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package blog
type SubscribeService interface {
FindByEmail(email string) (*Subscribe, error)
Insert(s *Subscribe) error
UpdateStatus(email string) error
Update(email, token string) error
FindByToken(token string) (*Subscribe, error)
FindByStatus(status string) ([]Subscribe, error)
Subscribe(token string) error
Expand Down

0 comments on commit 18fe362

Please sign in to comment.