-
Notifications
You must be signed in to change notification settings - Fork 0
Add GET /v1/currencies endpoint #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ca1df0
Initial plan
Copilot b23bf0a
Add /v1/rates/named-symbols endpoint
Copilot 9e70e9c
Rename named-symbols endpoint to currencies, rename symbols field to …
Copilot 4772203
Move currencies endpoint to /v1/currencies in its own route group
Copilot 9aac4db
Define route path constants in handlers package; use them in routers …
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/kamaal111/forex-api/database" | ||
| "github.com/kamaal111/forex-api/utils" | ||
| ) | ||
|
|
||
| func GetCurrencies(writer http.ResponseWriter, request *http.Request) { | ||
| ctx := context.Background() | ||
| client, err := database.CreateClient(ctx) | ||
| if err != nil { | ||
| utils.ErrorHandler(writer, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
| defer client.Close() | ||
|
|
||
| repo := NewFirestoreRatesRepository(ctx, client) | ||
| service := NewRatesService(repo) | ||
|
|
||
| record, err := service.GetAllNamedSymbols() | ||
| if err != nil { | ||
| utils.ErrorHandler(writer, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
| if record == nil { | ||
| utils.ErrorHandler(writer, "symbols not found", http.StatusNotFound) | ||
| return | ||
| } | ||
|
|
||
| output, err := json.Marshal(record) | ||
| if err != nil { | ||
| utils.ErrorHandler(writer, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| writer.Header().Set("content-type", "application/json") | ||
| writer.Write(output) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/kamaal111/forex-api/utils" | ||
| ) | ||
|
|
||
| func TestableCurrenciesHandler(repo RatesRepository) http.HandlerFunc { | ||
| return func(writer http.ResponseWriter, request *http.Request) { | ||
| service := NewRatesService(repo) | ||
|
|
||
| record, err := service.GetAllNamedSymbols() | ||
| if err != nil { | ||
| utils.ErrorHandler(writer, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
| if record == nil { | ||
| utils.ErrorHandler(writer, "symbols not found", http.StatusNotFound) | ||
| return | ||
| } | ||
|
|
||
| output, err := json.Marshal(record) | ||
| if err != nil { | ||
| utils.ErrorHandler(writer, err.Error(), http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| writer.Header().Set("content-type", "application/json") | ||
| writer.Write(output) | ||
| } | ||
| } | ||
|
|
||
| func TestGetCurrenciesHandler(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| mockRecord *SymbolsRecord | ||
| mockErr error | ||
| wantStatusCode int | ||
| wantSymbols []NamedSymbol | ||
| }{ | ||
| { | ||
| name: "returns named symbols for symbols that have rates in the database", | ||
| mockRecord: &SymbolsRecord{Date: "2025-11-21", Symbols: []string{"EUR", "USD", "GBP"}}, | ||
| mockErr: nil, | ||
| wantStatusCode: http.StatusOK, | ||
| wantSymbols: []NamedSymbol{ | ||
| {Symbol: "EUR", Name: "Euro"}, | ||
| {Symbol: "USD", Name: "US Dollar"}, | ||
| {Symbol: "GBP", Name: "British Pound Sterling"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "returns 404 when no data exists in the database", | ||
| mockRecord: nil, | ||
| mockErr: nil, | ||
| wantStatusCode: http.StatusNotFound, | ||
| wantSymbols: nil, | ||
| }, | ||
| { | ||
| name: "returns 500 on database error", | ||
| mockRecord: nil, | ||
| mockErr: errors.New("database error"), | ||
| wantStatusCode: http.StatusInternalServerError, | ||
| wantSymbols: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| mockRepo := &MockRatesRepository{ | ||
| GetAllSymbolsFunc: func() (*SymbolsRecord, error) { | ||
| return tt.mockRecord, tt.mockErr | ||
| }, | ||
| } | ||
|
|
||
| handler := TestableCurrenciesHandler(mockRepo) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, CurrenciesPath, nil) | ||
| recorder := httptest.NewRecorder() | ||
|
|
||
| handler(recorder, req) | ||
|
|
||
| if recorder.Code != tt.wantStatusCode { | ||
| t.Errorf("GetCurrencies() status = %d, want %d", recorder.Code, tt.wantStatusCode) | ||
| } | ||
|
|
||
| if tt.wantSymbols != nil { | ||
| contentType := recorder.Header().Get("content-type") | ||
| if contentType != "application/json" { | ||
| t.Errorf("GetCurrencies() content-type = %q, want %q", contentType, "application/json") | ||
| } | ||
|
|
||
| var record CurrenciesRecord | ||
| if err := json.NewDecoder(recorder.Body).Decode(&record); err != nil { | ||
| t.Fatalf("failed to decode response: %v", err) | ||
| } | ||
|
|
||
| if len(record.Data) != len(tt.wantSymbols) { | ||
| t.Errorf("GetCurrencies() returned %d symbols, want %d", len(record.Data), len(tt.wantSymbols)) | ||
| } | ||
|
|
||
| for i, expected := range tt.wantSymbols { | ||
| if i >= len(record.Data) { | ||
| break | ||
| } | ||
| if record.Data[i].Symbol != expected.Symbol { | ||
| t.Errorf("GetCurrencies() data[%d].symbol = %q, want %q", i, record.Data[i].Symbol, expected.Symbol) | ||
| } | ||
| if record.Data[i].Name != expected.Name { | ||
| t.Errorf("GetCurrencies() data[%d].name = %q, want %q", i, record.Data[i].Name, expected.Name) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package handlers | ||
|
|
||
| const ( | ||
| LatestPath = "/v1/rates/latest" | ||
| SymbolsPath = "/v1/rates/symbols" | ||
| CurrenciesPath = "/v1/currencies" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package routers | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/kamaal111/forex-api/handlers" | ||
| ) | ||
|
|
||
| func currenciesGroup(mux *http.ServeMux) { | ||
| mux.Handle(handlers.CurrenciesPath, loggerMiddleware(http.HandlerFunc(handlers.GetCurrencies))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot you must ensure that this map contains all currency symbols above, or you can do it the other way around which is derive the currencies above from this maps keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 9e70e9c.
Currenciesis now derived from the keys ofCurrencyNames, so they're always in sync — adding a currency toCurrencyNamesautomatically makes it available inCurrencies.