basic online
This commit is contained in:
33
internal/handler/actuals.go
Normal file
33
internal/handler/actuals.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"Engine/internal/database"
|
||||
"Engine/internal/model"
|
||||
)
|
||||
|
||||
type ActualsHandler struct {
|
||||
repo *database.ActualsRepo
|
||||
}
|
||||
|
||||
func NewActualsHandler(repo *database.ActualsRepo) *ActualsHandler {
|
||||
return &ActualsHandler{repo: repo}
|
||||
}
|
||||
|
||||
func (h *ActualsHandler) Ingest(w http.ResponseWriter, r *http.Request) {
|
||||
var req model.IngestActualsRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
actual, err := h.repo.Ingest(r.Context(), req)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, actual)
|
||||
}
|
||||
68
internal/handler/budget.go
Normal file
68
internal/handler/budget.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"Engine/internal/model"
|
||||
"Engine/internal/service"
|
||||
)
|
||||
|
||||
type BudgetHandler struct {
|
||||
svc *service.BudgetService
|
||||
}
|
||||
|
||||
func NewBudgetHandler(svc *service.BudgetService) *BudgetHandler {
|
||||
return &BudgetHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *BudgetHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
var req model.CreateBudgetRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
budget, err := h.svc.Create(r.Context(), req)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, budget)
|
||||
}
|
||||
|
||||
func (h *BudgetHandler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Amount float64 `json:"amount"`
|
||||
Notes string `json:"notes"`
|
||||
ChangedBy string `json:"changed_by"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
budget, err := h.svc.Update(r.Context(), id, body.Amount, body.Notes, body.ChangedBy)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, budget)
|
||||
}
|
||||
|
||||
func (h *BudgetHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
if err := h.svc.Delete(r.Context(), id); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
16
internal/handler/helpers.go
Normal file
16
internal/handler/helpers.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, msg string) {
|
||||
writeJSON(w, status, map[string]string{"error": msg})
|
||||
}
|
||||
64
internal/handler/variance.go
Normal file
64
internal/handler/variance.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"Engine/internal/model"
|
||||
"Engine/internal/service"
|
||||
)
|
||||
|
||||
type VarianceHandler struct {
|
||||
svc *service.VarianceService
|
||||
}
|
||||
|
||||
func NewVarianceHandler(svc *service.VarianceService) *VarianceHandler {
|
||||
return &VarianceHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *VarianceHandler) Report(w http.ResponseWriter, r *http.Request) {
|
||||
f := filterFromQuery(r)
|
||||
report, err := h.svc.Report(r.Context(), f)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, report)
|
||||
}
|
||||
|
||||
func (h *VarianceHandler) Alerts(w http.ResponseWriter, r *http.Request) {
|
||||
f := filterFromQuery(r)
|
||||
|
||||
threshold := 10.0
|
||||
if t := r.URL.Query().Get("threshold"); t != "" {
|
||||
if v, err := strconv.ParseFloat(t, 64); err == nil {
|
||||
threshold = v
|
||||
}
|
||||
}
|
||||
|
||||
alerts, err := h.svc.Alerts(r.Context(), f, threshold)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, alerts)
|
||||
}
|
||||
|
||||
func filterFromQuery(r *http.Request) service.VarianceFilter {
|
||||
q := r.URL.Query()
|
||||
|
||||
year, _ := strconv.Atoi(q.Get("year"))
|
||||
period, _ := strconv.Atoi(q.Get("period"))
|
||||
|
||||
version := model.BudgetVersion(q.Get("version"))
|
||||
if version == "" {
|
||||
version = model.VersionOriginal
|
||||
}
|
||||
|
||||
return service.VarianceFilter{
|
||||
FiscalYear: year,
|
||||
FiscalPeriod: period,
|
||||
DeptCode: q.Get("dept"),
|
||||
Version: version,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user