all repos

onasty @ fa8001ef7b978b20df8dba33a04af109a2b5b18e

a one-time notes service

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

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: keep metadate on note removal (#96)..., 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,omitempty"`
71
	ReadAt    *time.Time `json:"read_at,omitempty"`
72
	CratedAt  time.Time  `json:"crated_at"`
73
	ExpiresAt time.Time  `json:"expires_at"`
74
}
75
76
func (a *APIV1) getNoteBySlugHandler(c *gin.Context) {
77
	var req getNoteBySlugRequest
78
	if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
79
		newError(c, http.StatusBadRequest, "invalid request")
80
		return
81
	}
82
83
	slug := c.Param("slug")
84
	note, err := a.notesrv.GetBySlugAndRemoveIfNeeded(
85
		c.Request.Context(),
86
		notesrv.GetNoteBySlugInput{
87
			Slug:     slug,
88
			Password: req.Password,
89
		},
90
	)
91
	if err != nil {
92
		errorResponse(c, err)
93
		return
94
	}
95
96
	status := http.StatusOK
97
	if note.ReadAt != nil && !note.ReadAt.IsZero() {
98
		status = http.StatusNotFound
99
	}
100
101
	c.JSON(status, getNoteBySlugResponse{
102
		Content:   note.Content,
103
		ReadAt:    note.ReadAt,
104
		CratedAt:  note.CreatedAt,
105
		ExpiresAt: note.ExpiresAt,
106
	})
107
}