Skip to content

Commit 2dbd6d1

Browse files
committed
feat: commit add import files route validations and handler scafolding and mux
1 parent 2659072 commit 2dbd6d1

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

backend/cmd/server/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ func main() {
8181
}
8282
})))
8383

84+
mux.Handle("/notes/imports", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
85+
switch r.Method {
86+
case http.MethodPost:
87+
handler.ImportNotesHandler(w, r)
88+
default:
89+
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
90+
}
91+
})))
92+
8493
mux.Handle("/notes/", authMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8594
switch r.Method {
8695
case http.MethodGet:

backend/internal/api/handler/handler.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ func (h *Handler) CreateNodeHandler(w http.ResponseWriter, r *http.Request) {
8383
response.WriteResponse(w, http.StatusAccepted, fmt.Sprintf("%d", noteID))
8484
}
8585

86+
func (h *Handler) ImportNotesHandler(w http.ResponseWriter, r *http.Request) {
87+
h.Logger.Debug("ImportNotesHandler called")
88+
89+
httperr := validator.ImportNotesValidator(r)
90+
if httperr != nil {
91+
validator.WriteError(w, httperr)
92+
return
93+
}
94+
95+
clerkID, ok := auth.UserIDFromContext(r.Context())
96+
if !ok {
97+
http.Error(w, "unauthorized", http.StatusUnauthorized)
98+
return
99+
}
100+
fmt.Println(clerkID)
101+
//response.WriteResponse(w, http.StatusAccepted, fmt.Sprintf("%d", noteID))
102+
}
103+
86104
func (db *Handler) UpdateNoteHandler(w http.ResponseWriter, r *http.Request) {
87105
db.Logger.Debug("UpdateNoteHandler called")
88106

backend/internal/api/validator/validator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,23 @@ func UpdateNoteValidator(r *http.Request) (int, *httpError) {
135135

136136
return id, nil
137137
}
138+
139+
func ImportNotesValidator(r *http.Request) *httpError {
140+
if err := validatehttpmethod(r, http.MethodPost); err != nil {
141+
return &httpError{Status: http.StatusMethodNotAllowed, Msg: "method not supported"}
142+
}
143+
144+
contentType := r.Header.Get("Content-Type")
145+
if !strings.HasPrefix(contentType, "multipart/form-data") {
146+
log.Printf("invalid content-type: %s", contentType)
147+
return &httpError{Status: http.StatusUnsupportedMediaType, Msg: "media not supported"}
148+
}
149+
150+
parts := formatpath(r.URL.Path)
151+
if len(parts) != 2 || parts[0] != "notes" || parts[1] != "imports" {
152+
log.Printf("invalid path: %s", r.URL.Path)
153+
return &httpError{Status: http.StatusBadRequest, Msg: "path wrong"}
154+
}
155+
156+
return nil
157+
}

0 commit comments

Comments
 (0)