better end points and better tests
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"Engine/internal/database"
|
||||
"Engine/internal/model"
|
||||
@@ -23,6 +24,12 @@ func (h *ActualsHandler) Ingest(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
errs := req.Valid()
|
||||
if len(errs) > 0 {
|
||||
writeError(w, http.StatusBadRequest, strings.Join(errs, "; "))
|
||||
return
|
||||
}
|
||||
|
||||
actual, err := h.repo.Ingest(r.Context(), req)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
@@ -31,3 +38,28 @@ func (h *ActualsHandler) Ingest(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
writeJSON(w, http.StatusCreated, actual)
|
||||
}
|
||||
|
||||
func (h *ActualsHandler) IngestBatch(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
|
||||
}
|
||||
var errs []string
|
||||
for _, atual := range req {
|
||||
errs = append(errs, atual.Valid()...)
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
writeError(w, http.StatusBadRequest, strings.Join(errs, "; "))
|
||||
return
|
||||
}
|
||||
|
||||
actual, err := h.repo.IngestBatch(r.Context(), req)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, actual)
|
||||
}
|
||||
|
||||
40
internal/handler/report.go
Normal file
40
internal/handler/report.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"Engine/internal/model"
|
||||
"Engine/internal/service"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ReportHandler struct {
|
||||
svc *service.ReportService
|
||||
}
|
||||
|
||||
func NewReportHandler(svc *service.ReportService) *ReportHandler {
|
||||
return &ReportHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (report *ReportHandler) PnL(w http.ResponseWriter, r *http.Request) {
|
||||
var req model.PnLRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
for _, year := range req.FiscalYears {
|
||||
for _, period := range req.FiscalPeriods {
|
||||
for _, Department := range req.DeptCodes {
|
||||
report.svc.CreatePnL(context.TODO(), service.PnLFilter{
|
||||
FiscalYear: year,
|
||||
FiscalPeriod: period,
|
||||
DeptCode: Department,
|
||||
Version: req.Version,
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user