all repos

onasty @ e4abd99

a one-time notes service

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package apiv1

import (
	"errors"
	"io"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/olexsmir/onasty/internal/dtos"
	"github.com/olexsmir/onasty/internal/service/notesrv"
)

type createNoteRequest struct {
	Content              string    `json:"content"`
	Slug                 string    `json:"slug"`
	Password             string    `json:"password"`
	BurnBeforeExpiration bool      `json:"burn_before_expiration"`
	ExpiresAt            time.Time `json:"expires_at"`
}

type createNoteResponse struct {
	Slug string `json:"slug"`
}

func (a *APIV1) createNoteHandler(c *gin.Context) {
	var req createNoteRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		newError(c, http.StatusBadRequest, "invalid request")
		return
	}

	// TODO: burn_before_expiration shouldn't be set if user has not set or specified expires_at

	slug, err := a.notesrv.Create(c.Request.Context(), dtos.CreateNote{
		Content:              req.Content,
		UserID:               a.getUserID(c),
		Slug:                 req.Slug,
		Password:             req.Password,
		BurnBeforeExpiration: req.BurnBeforeExpiration,
		CreatedAt:            time.Now(),
		ExpiresAt:            req.ExpiresAt,
	}, a.getUserID(c))
	if err != nil {
		errorResponse(c, err)
		return
	}

	c.JSON(http.StatusCreated, createNoteResponse{slug})
}

type getNoteBySlugRequest struct {
	Password string `json:"password"`
}

type getNoteBySlugResponse struct {
	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) {
	var req getNoteBySlugRequest
	if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
		newError(c, http.StatusBadRequest, "invalid request")
		return
	}

	note, err := a.notesrv.GetBySlugAndRemoveIfNeeded(
		c.Request.Context(),
		notesrv.GetNoteBySlugInput{
			Slug:     c.Param("slug"),
			Password: req.Password,
		},
	)
	if err != nil {
		errorResponse(c, err)
		return
	}

	status := http.StatusOK
	if !note.ReadAt.IsZero() {
		status = http.StatusNotFound
	}

	c.JSON(status, getNoteBySlugResponse{
		Content:   note.Content,
		ReadAt:    note.ReadAt,
		CreatedAt: note.CreatedAt,
		ExpiresAt: note.ExpiresAt,
	})
}

type getNotesResponse struct {
	Content              string    `json:"content"`
	Slug                 string    `json:"slug"`
	BurnBeforeExpiration bool      `json:"burn_before_expiration"`
	HasPassword          bool      `json:"has_password"`
	CreatedAt            time.Time `json:"created_at"`
	ExpiresAt            time.Time `json:"expires_at,omitzero"`
	ReadAt               time.Time `json:"read_at,omitzero"`
}

func (a *APIV1) getNotesHandler(c *gin.Context) {
	notes, err := a.notesrv.GetAllByAuthorID(c.Request.Context(), a.getUserID(c))
	if err != nil {
		errorResponse(c, err)
		return
	}

	var response []getNotesResponse
	for _, note := range notes {
		response = append(response, getNotesResponse{
			Content:              note.Content,
			Slug:                 note.Slug,
			BurnBeforeExpiration: note.BurnBeforeExpiration,
			HasPassword:          note.HasPassword,
			CreatedAt:            note.CreatedAt,
			ExpiresAt:            note.ExpiresAt,
			ReadAt:               note.ReadAt,
		})
	}

	c.JSON(http.StatusOK, response)
}

type updateNoteRequest struct {
	ExpiresAt            *time.Time `json:"expires_at,omitempty"`
	BurnBeforeExpiration *bool      `json:"burn_before_expiration,omitempty"`
}

func (a *APIV1) updateNoteHandler(c *gin.Context) {
	var req updateNoteRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		newError(c, http.StatusBadRequest, "invalid request")
		return
	}

	// TODO: burn_before_expiration shouldn't be set if user has not set or specified expires_at

	if err := a.notesrv.UpdateExpirationTimeSettings(
		c.Request.Context(),
		dtos.PatchNote{
			BurnBeforeExpiration: req.BurnBeforeExpiration,
			ExpiresAt:            req.ExpiresAt,
		},
		c.Param("slug"),
		a.getUserID(c),
	); err != nil {
		errorResponse(c, err)
		return
	}

	c.Status(http.StatusOK)
}

func (a *APIV1) deleteNoteHandler(c *gin.Context) {
	if err := a.notesrv.DeleteBySlug(
		c.Request.Context(),
		c.Param("slug"),
		a.getUserID(c),
	); err != nil {
		errorResponse(c, err)
		return
	}

	c.Status(http.StatusNoContent)
}

type setNotePasswordRequest struct {
	Password string `json:"password"`
}

func (a *APIV1) setNotePasswordHandler(c *gin.Context) {
	var req setNotePasswordRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		newError(c, http.StatusBadRequest, "invalid request")
		return
	}

	if err := a.notesrv.UpdatePassword(
		c.Request.Context(),
		c.Param("slug"),
		req.Password,
		a.getUserID(c),
	); err != nil {
		errorResponse(c, err)
		return
	}

	c.Status(http.StatusOK)
}