83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"Portifolio/internal/database"
|
|
"Portifolio/internal/model"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func (s *Service) AddRevenueEntryHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
CompanyID int `json:"company_id"`
|
|
CurrencyID int `json:"currency_id"`
|
|
PeriodType string `json:"period_type"`
|
|
Year int `json:"year"`
|
|
Index int `json:"index"`
|
|
Category string `json:"category"`
|
|
Value float64 `json:"value"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var period model.Period
|
|
switch input.PeriodType {
|
|
case "Q":
|
|
period = model.QuarterPeriod(input.Year, input.Index)
|
|
case "H":
|
|
period = model.HalfYearPeriod(input.Year, input.Index)
|
|
case "Y":
|
|
period = model.FullYearPeriod(input.Year)
|
|
default:
|
|
http.Error(w, "invalid period_type", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
rev := model.RevenueInsert{
|
|
CompanyID: input.CompanyID,
|
|
CurrencyID: input.CurrencyID,
|
|
CategoryName: input.Category,
|
|
Period: period,
|
|
Value: input.Value,
|
|
}
|
|
|
|
err := database.InsertRevenue(s.db, rev)
|
|
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]string{"status": "created"})
|
|
}
|
|
}
|
|
|
|
func (s *Service) GetCompanyRevenueCategories() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
CompanyID int `json:"company_id"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
catlist, err := database.GetCategoriesByCompanyID(s.db, input.CompanyID)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Could not find categories by that id:%s", err), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
json.NewEncoder(w).Encode(map[string][]string{"list": catlist})
|
|
}
|
|
}
|