all repos

onasty @ a705387

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
        33
         		auth.POST("/reset-password", a.requestResetPasswordHandler)

      
        34
        34
         		auth.POST("/reset-password/:token", a.resetPasswordHandler)

      
        35
        35
         

      
        36
        
        -		authorized := auth.Group("/", a.authorizedMiddleware)

      
        37
        
        -		{

      
        38
        
        -			authorized.POST("/logout", a.logOutHandler)

      
        39
        
        -			authorized.POST("/change-password", a.changePasswordHandler)

      
        40
        
        -		}

      
        41
        
        -

      
        42
        36
         		oauth := r.Group("/oauth")

      
        43
        37
         		{

      
        44
        38
         			oauth.GET("/:provider", a.oauthLoginHandler)

      
        45
        39
         			oauth.GET("/:provider/callback", a.oauthCallbackHandler)

      
        46
        40
         		}

      
        
        41
        +

      
        
        42
        +		authorized := auth.Group("/", a.authorizedMiddleware)

      
        
        43
        +		{

      
        
        44
        +			authorized.POST("/logout", a.logOutHandler)

      
        
        45
        +			authorized.POST("/change-password", a.changePasswordHandler)

      
        
        46
        +		}

      
        47
        47
         	}

      
        48
        48
         

      
        49
        
        -	note := r.Group("/note", a.couldBeAuthorizedMiddleware)

      
        
        49
        +	note := r.Group("/note")

      
        50
        50
         	{

      
        51
        51
         		note.GET("/:slug", a.getNoteBySlugHandler)

      
        52
        
        -		note.POST("", a.createNoteHandler)

      
        
        52
        +

      
        
        53
        +		possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware)

      
        
        54
        +		{

      
        
        55
        +			possiblyAuthorized.POST("", a.createNoteHandler)

      
        
        56
        +		}

      
        53
        57
         	}

      
        54
        58
         }

      
M internal/transport/http/apiv1/middleware.go
···
        70
        70
         	}

      
        71
        71
         }

      
        72
        72
         

      
        73
        
        -//nolint:unused

      
        74
        
        -func (a *APIV1) isUserAuthorized(c *gin.Context) bool {

      
        75
        
        -	return !a.getUserID(c).IsNil()

      
        76
        
        -}

      
        77
        
        -

      
        78
        73
         func getTokenFromAuthHeaders(c *gin.Context) (token string, ok bool) { //nolint:nonamedreturns

      
        79
        74
         	header := c.GetHeader("Authorization")

      
        80
        75
         	if header == "" {

      
M internal/transport/http/apiv1/note.go
···
        63
        63
         }

      
        64
        64
         

      
        65
        65
         type getNoteBySlugRequest struct {

      
        66
        
        -	Password string `json:"password,omitempty"`

      
        
        66
        +	Password string `json:"password"`

      
        67
        67
         }

      
        68
        68
         

      
        69
        69
         type getNoteBySlugResponse struct {

      
        70
        
        -	Content   string    `json:"content,omitempty"`

      
        71
        
        -	ReadAt    time.Time `json:"read_at"`

      
        72
        
        -	CratedAt  time.Time `json:"crated_at"`

      
        73
        
        -	ExpiresAt time.Time `json:"expires_at"`

      
        
        70
        +	Content   string    `json:"content"`

      
        
        71
        +	ReadAt    time.Time `json:"read_at,omitzero"`

      
        
        72
        +	CreatedAt time.Time `json:"created_at"`

      
        
        73
        +	ExpiresAt time.Time `json:"expires_at,omitzero"`

      
        74
        74
         }

      
        75
        75
         

      
        76
        76
         func (a *APIV1) getNoteBySlugHandler(c *gin.Context) {

      ···
        100
        100
         	c.JSON(status, getNoteBySlugResponse{

      
        101
        101
         		Content:   note.Content,

      
        102
        102
         		ReadAt:    note.ReadAt,

      
        103
        
        -		CratedAt:  note.CreatedAt,

      
        
        103
        +		CreatedAt: note.CreatedAt,

      
        104
        104
         		ExpiresAt: note.ExpiresAt,

      
        105
        105
         	})

      
        106
        106
         }

      
M internal/transport/http/apiv1/response.go
···
        25
        25
         		errors.Is(err, models.ErrUserIsNotActivated) ||

      
        26
        26
         		errors.Is(err, models.ErrUserInvalidEmail) ||

      
        27
        27
         		errors.Is(err, models.ErrUserInvalidPassword) ||

      
        
        28
        +		errors.Is(err, models.ErrUserNotFound) ||

      
        28
        29
         		// notes

      
        29
        30
         		errors.Is(err, models.ErrNoteContentIsEmpty) ||

      
        30
        31
         		errors.Is(err, models.ErrNoteSlugIsAlreadyInUse) {

      ···
        40
        41
         	if errors.Is(err, models.ErrNoteNotFound) ||

      
        41
        42
         		errors.Is(err, models.ErrVerificationTokenNotFound) {

      
        42
        43
         		newErrorStatus(c, http.StatusNotFound, err.Error())

      
        43
        
        -		return

      
        44
        
        -	}

      
        45
        
        -

      
        46
        
        -	if errors.Is(err, models.ErrUserNotFound) {

      
        47
        
        -		newErrorStatus(c, http.StatusBadRequest, err.Error())

      
        48
        44
         		return

      
        49
        45
         	}

      
        50
        46