Skip to content

Commit

Permalink
API renaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Jun 29, 2024
1 parent 2306e74 commit 92ca2f3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
14 changes: 14 additions & 0 deletions backend/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

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

type FilesContentResponse struct {
Content string `json:"content"`
}

type FilesSaveRequest struct {
Password string `json:"password"`
Content string `json:"content"`
}
10 changes: 5 additions & 5 deletions backend/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestFilesContentHandler(t *testing.T) {
os.WriteFile(testFile, []byte(encryptedContent), 0644)

// Create a request to pass to the handler
reqBody := Password{Password: password} // TODO: Should be protected.
reqBody := FilesContentRequest{Password: password} // TODO: Should be protected.
reqBodyBytes, _ := json.Marshal(reqBody)
req, err := http.NewRequest("POST", "/files/content", bytes.NewBuffer(reqBodyBytes))
if err != nil {
Expand All @@ -61,8 +61,8 @@ func TestFilesContentHandler(t *testing.T) {
}

// Check the response body is what we expect
expected := File{Content: content}
var resp File
expected := FilesContentResponse{Content: content}
var resp FilesContentResponse
if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil {
t.Fatalf("Could not decode response: %v", err)
}
Expand All @@ -79,11 +79,11 @@ func TestFilesSaveHandler(t *testing.T) {
// Create a request to pass to the handler
password := "testpassword"
content := "This is a test file content."
reqBody := File{Password: password, Content: content}
reqBody := FilesSaveRequest{Password: password, Content: content}
reqBodyBytes, _ := json.Marshal(reqBody)
req, err := http.NewRequest("POST", "/files/save", bytes.NewBuffer(reqBodyBytes))
if err != nil {
t.Fatalf("Could not create request: %v", err)
t.Fatalf("Could not save request: %v", err)
}

// Create a ResponseRecorder to record the response
Expand Down
6 changes: 3 additions & 3 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func filesContentHandler(w http.ResponseWriter, r *http.Request) {
mu.RLock()
defer mu.RUnlock()

var req Password
var req FilesContentRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized) // TODO: check errors.
Expand All @@ -36,13 +36,13 @@ func filesContentHandler(w http.ResponseWriter, r *http.Request) {
log.Fatalf("failed to decrypt message: %v", err)
}

resp := File{Content: string(decrypted)}
resp := FilesContentResponse{Content: string(decrypted)}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}

func filesSaveHandler(w http.ResponseWriter, r *http.Request) {
var file File
var file FilesSaveRequest
if err := json.NewDecoder(r.Body).Decode(&file); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
10 changes: 0 additions & 10 deletions backend/models.go

This file was deleted.

0 comments on commit 92ca2f3

Please sign in to comment.