all repos

onasty @ 7e5389df8b77bcb3308f4586bad32bbdae57c0d2

a one-time notes service

onasty/internal/transport/http/apiv1/apiv1.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: notes manipulations for the note authors (#117)..., 1 year ago
1
package apiv1
2
3
import (
4
	"github.com/gin-gonic/gin"
5
	"github.com/olexsmir/onasty/internal/service/notesrv"
6
	"github.com/olexsmir/onasty/internal/service/usersrv"
7
)
8
9
type APIV1 struct {
10
	usersrv usersrv.UserServicer
11
	notesrv notesrv.NoteServicer
12
}
13
14
func NewAPIV1(
15
	us usersrv.UserServicer,
16
	ns notesrv.NoteServicer,
17
) *APIV1 {
18
	return &APIV1{
19
		usersrv: us,
20
		notesrv: ns,
21
	}
22
}
23
24
func (a *APIV1) Routes(r *gin.RouterGroup) {
25
	r.Use(a.metricsMiddleware)
26
	auth := r.Group("/auth")
27
	{
28
		auth.POST("/signup", a.signUpHandler)
29
		auth.POST("/signin", a.signInHandler)
30
		auth.POST("/refresh-tokens", a.refreshTokensHandler)
31
		auth.GET("/verify/:token", a.verifyHandler)
32
		auth.POST("/resend-verification-email", a.resendVerificationEmailHandler)
33
		auth.POST("/reset-password", a.requestResetPasswordHandler)
34
		auth.POST("/reset-password/:token", a.resetPasswordHandler)
35
36
		oauth := r.Group("/oauth")
37
		{
38
			oauth.GET("/:provider", a.oauthLoginHandler)
39
			oauth.GET("/:provider/callback", a.oauthCallbackHandler)
40
		}
41
42
		authorized := auth.Group("/", a.authorizedMiddleware)
43
		{
44
			authorized.POST("/logout", a.logOutHandler)
45
			authorized.POST("/change-password", a.changePasswordHandler)
46
		}
47
	}
48
49
	note := r.Group("/note")
50
	{
51
		note.GET("/:slug", a.getNoteBySlugHandler)
52
53
		possiblyAuthorized := note.Group("", a.couldBeAuthorizedMiddleware)
54
		{
55
			possiblyAuthorized.POST("", a.createNoteHandler)
56
		}
57
58
		authorized := note.Group("", a.authorizedMiddleware)
59
		{
60
			authorized.GET("", a.getNotesHandler)
61
			authorized.PATCH(":slug/expires", a.updateNoteHandler)
62
			authorized.PATCH(":slug/password", a.setNotePasswordHandler)
63
			authorized.DELETE(":slug", a.deleteNoteHandler)
64
		}
65
	}
66
}