45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"Portifolio/internal/model"
|
|
"Portifolio/internal/service"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func AddCurrencyHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var input model.CurrencyInput
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
id, err := service.InsertCurrency(db, input)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
json.NewEncoder(w).Encode(map[string]any{"status": "created", "id": id})
|
|
}
|
|
}
|
|
|
|
func GetCurrenciesHandler(db *sql.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
currencies, err := service.GetAllCurrencies(db)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(currencies)
|
|
}
|
|
}
|