all repos

onasty @ e9b6f8af632cc700104bc1784dd0f4e28893825a

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: add password support to notes (#41)..., 1 year ago
1
package apiv1
2
3
import (
4
	"errors"
5
	"io"
6
	"net/http"
7
	"time"
8
9
	"github.com/gin-gonic/gin"
10
	"github.com/olexsmir/onasty/internal/dtos"
11
	"github.com/olexsmir/onasty/internal/models"
12
	"github.com/olexsmir/onasty/internal/service/notesrv"
13
)
14
15
type createNoteRequest struct {
16
	Content              string    `json:"content"`
17
	Slug                 string    `json:"slug"`
18
	Password             string    `json:"password"`
19
	BurnBeforeExpiration bool      `json:"burn_before_expiration"`
20
	ExpiresAt            time.Time `json:"expires_at"`
21
}
22
23
type createNoteResponse struct {
24
	Slug string `json:"slug"`
25
}
26
27
func (a *APIV1) createNoteHandler(c *gin.Context) {
28
	var req createNoteRequest
29
	if err := c.ShouldBindJSON(&req); err != nil {
30
		newError(c, http.StatusBadRequest, "invalid request")
31
		return
32
	}
33
34
	note := models.Note{ //nolint:exhaustruct
35
		Content:              req.Content,
36
		Slug:                 req.Slug,
37
		BurnBeforeExpiration: req.BurnBeforeExpiration,
38
		CreatedAt:            time.Now(),
39
		Password:             req.Password,
40
		ExpiresAt:            req.ExpiresAt,
41
	}
42
43
	if err := note.Validate(); err != nil {
44
		newErrorStatus(c, http.StatusBadRequest, err.Error())
45
		return
46
	}
47
48
	slug, err := a.notesrv.Create(c.Request.Context(), dtos.CreateNoteDTO{
49
		Content:              note.Content,
50
		UserID:               a.getUserID(c),
51
		Slug:                 note.Slug,
52
		Password:             note.Password,
53
		BurnBeforeExpiration: note.BurnBeforeExpiration,
54
		CreatedAt:            note.CreatedAt,
55
		ExpiresAt:            note.ExpiresAt,
56
	}, a.getUserID(c))
57
	if err != nil {
58
		errorResponse(c, err)
59
		return
60
	}
61
62
	c.JSON(http.StatusCreated, createNoteResponse{slug})
63
}
64
65
type getNoteBySlugRequest struct {
66
	Password string `json:"password,omitempty"`
67
}
68
69
type getNoteBySlugResponse struct {
70
	Content   string    `json:"content"`
71
	CratedAt  time.Time `json:"crated_at"`
72
	ExpiresAt time.Time `json:"expires_at"`
73
}
74
75
func (a *APIV1) getNoteBySlugHandler(c *gin.Context) {
76
	var req getNoteBySlugRequest
77
	if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
78
		newError(c, http.StatusBadRequest, "invalid request")
79
		return
80
	}
81
82
	slug := c.Param("slug")
83
	note, err := a.notesrv.GetBySlugAndRemoveIfNeeded(
84
		c.Request.Context(),
85
		notesrv.GetNoteBySlugInput{
86
			Slug:     slug,
87
			Password: req.Password,
88
		},
89
	)
90
	if err != nil {
91
		errorResponse(c, err)
92
		return
93
	}
94
95
	c.JSON(http.StatusOK, getNoteBySlugResponse{
96
		Content:   note.Content,
97
		CratedAt:  note.CreatedAt,
98
		ExpiresAt: note.ExpiresAt,
99
	})
100
}