-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote_handler.go
More file actions
128 lines (112 loc) · 3.13 KB
/
Copy pathnote_handler.go
File metadata and controls
128 lines (112 loc) · 3.13 KB
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
package handlers
import (
"errors"
"net/http"
"notes-api/internal/models/note"
"notes-api/internal/repository"
"time"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
type NoteHandler struct {
repo *repository.NoteRepository
}
func NewNoteHandler(repo *repository.NoteRepository) *NoteHandler {
return &NoteHandler{
repo: repo,
}
}
func (h *NoteHandler) CreateNote(c *gin.Context) {
var req note.CreateNoteRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request payload"})
return
}
now := time.Now().UTC()
note := note.Note{
ID: bson.NewObjectID(),
Title: req.Title,
Content: req.Content,
Pinned: req.Pinned,
CreatedAt: now,
UpdatedAt: now,
}
createdNote, err := h.repo.Create(c.Request.Context(), note)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create note"})
return
}
c.JSON(http.StatusCreated, createdNote)
}
func (h *NoteHandler) GetAllNotes(c *gin.Context) {
notes, err := h.repo.GetAll(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve notes"})
return
}
c.JSON(http.StatusOK, notes)
}
func (h *NoteHandler) GetNoteByID(c *gin.Context) {
id := c.Param("id")
objID, err := bson.ObjectIDFromHex(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid note ID"})
return
}
note, err := h.repo.GetByID(c.Request.Context(), objID)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
c.JSON(http.StatusNotFound, gin.H{"error": "Note not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve note"})
return
}
c.JSON(http.StatusOK, note)
}
func (h *NoteHandler) UpdateNote(c *gin.Context) {
id := c.Param("id")
objID, err := bson.ObjectIDFromHex(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid note ID"})
return
}
var req note.UpdateNoteRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request payload"})
return
}
updatedNote, err := h.repo.Update(c.Request.Context(), objID, req)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
c.JSON(http.StatusNotFound, gin.H{"error": "Note not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update note"})
return
}
c.JSON(http.StatusOK, updatedNote)
}
func (h *NoteHandler) DeleteNote(c *gin.Context) {
id := c.Param("id")
objID, err := bson.ObjectIDFromHex(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid note ID"})
return
}
deleted, err := h.repo.Delete(c.Request.Context(), objID)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
c.JSON(http.StatusNotFound, gin.H{"error": "Note not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete note"})
return
}
if !deleted {
c.JSON(http.StatusNotFound, gin.H{"error": "Note not found"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Note deleted successfully"})
}