all repos

onasty @ a705387a9a0d0f2a3be3bed3d320144939519ab0

a one-time notes service
4 files changed, 19 insertions(+), 24 deletions(-)
feat(api): dont return empty fields (#114)

* feat(api): dont return empty fields

* fixup! feat(api): dont return empty fields

* refactor(api): remove unnecessary if-statement

* fix(api): typo

* refactor(api): remove unused helper function

* refactor(api): use couldBeAuthorizedMiddleware only when it's actually
needed, and not on the whole group
Author: Smirnov Oleksandr ss2316544@gmail.com
Committed by: GitHub noreply@github.com
Committed at: 2025-05-22 17:20:33 +0300
Parent: 423cbf0
M internal/transport/http/apiv1/apiv1.go

@@ -33,22 +33,26 @@ auth.POST("/resend-verification-email", a.resendVerificationEmailHandler)

auth.POST("/reset-password", a.requestResetPasswordHandler) auth.POST("/reset-password/:token", a.resetPasswordHandler) - authorized := auth.Group("/", a.authorizedMiddleware) - { - authorized.POST("/logout", a.logOutHandler) - authorized.POST("/change-password", a.changePasswordHandler) - } - oauth := r.Group("/oauth") { oauth.GET("/:provider", a.oauthLoginHandler) oauth.GET("/:provider/callback", a.oauthCallbackHandler) } + + authorized := auth.Group("/", a.authorizedMiddleware) + { + authorized.POST("/logout", a.logOutHandler) + authorized.POST("/change-password", a.changePasswordHandler) + } } - note := r.Group("/note", a.couldBeAuthorizedMiddleware) + note := r.Group("/note") { note.GET("/:slug", a.getNoteBySlugHandler) - note.POST("", a.createNoteHandler) + + possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware) + { + possiblyAuthorized.POST("", a.createNoteHandler) + } } }
M internal/transport/http/apiv1/middleware.go

@@ -70,11 +70,6 @@ metrics.RecordFailedRequestMetric(c.Request.Method, c.Request.RequestURI)

} } -//nolint:unused -func (a *APIV1) isUserAuthorized(c *gin.Context) bool { - return !a.getUserID(c).IsNil() -} - func getTokenFromAuthHeaders(c *gin.Context) (token string, ok bool) { //nolint:nonamedreturns header := c.GetHeader("Authorization") if header == "" {
M internal/transport/http/apiv1/note.go

@@ -63,14 +63,14 @@ c.JSON(http.StatusCreated, createNoteResponse{slug})

} type getNoteBySlugRequest struct { - Password string `json:"password,omitempty"` + Password string `json:"password"` } type getNoteBySlugResponse struct { - Content string `json:"content,omitempty"` - ReadAt time.Time `json:"read_at"` - CratedAt time.Time `json:"crated_at"` - ExpiresAt time.Time `json:"expires_at"` + Content string `json:"content"` + ReadAt time.Time `json:"read_at,omitzero"` + CreatedAt time.Time `json:"created_at"` + ExpiresAt time.Time `json:"expires_at,omitzero"` } func (a *APIV1) getNoteBySlugHandler(c *gin.Context) {

@@ -100,7 +100,7 @@

c.JSON(status, getNoteBySlugResponse{ Content: note.Content, ReadAt: note.ReadAt, - CratedAt: note.CreatedAt, + CreatedAt: note.CreatedAt, ExpiresAt: note.ExpiresAt, }) }
M internal/transport/http/apiv1/response.go

@@ -25,6 +25,7 @@ errors.Is(err, models.ErrUserIsAlreadyVerified) ||

errors.Is(err, models.ErrUserIsNotActivated) || errors.Is(err, models.ErrUserInvalidEmail) || errors.Is(err, models.ErrUserInvalidPassword) || + errors.Is(err, models.ErrUserNotFound) || // notes errors.Is(err, models.ErrNoteContentIsEmpty) || errors.Is(err, models.ErrNoteSlugIsAlreadyInUse) {

@@ -40,11 +41,6 @@

if errors.Is(err, models.ErrNoteNotFound) || errors.Is(err, models.ErrVerificationTokenNotFound) { newErrorStatus(c, http.StatusNotFound, err.Error()) - return - } - - if errors.Is(err, models.ErrUserNotFound) { - newErrorStatus(c, http.StatusBadRequest, err.Error()) return }