Skip to content

Commit

Permalink
Merge pull request #7 from datasektionen/6-implement-resultatrapport
Browse files Browse the repository at this point in the history
6 implement resultatrapport
  • Loading branch information
DouglasFischer authored Sep 29, 2024
2 parents 59626bd + 39489f8 commit 602fd91
Show file tree
Hide file tree
Showing 17 changed files with 1,160 additions and 597 deletions.
14 changes: 11 additions & 3 deletions cmd/web/web.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/datasektionen/GOrdian/internal/config"
"github.com/datasektionen/GOrdian/internal/database"
"log"
"net/http"
Expand All @@ -9,11 +10,18 @@ import (
)

func main() {
db, err := database.Connect()
envVar := config.GetEnv()
dbGO, err := database.Connect(envVar.PsqlconnStringGOrdian)
if err != nil {
log.Printf("error accessing database: %v", err)
log.Printf("error accessing GOrdian database: %v", err)
}
if err := web.Mount(http.DefaultServeMux, db); err != nil {

dbCF, err := database.Connect(envVar.PsqlconnStringCashflow)
if err != nil {
log.Printf("error accessing Cashflow database: %v", err)
}

if err := web.Mount(http.DefaultServeMux, web.Databases{DBCF: dbCF, DBGO: dbGO}); err != nil {
panic(err)
}
panic(http.ListenAndServe("0.0.0.0:3000", nil))
Expand Down
62 changes: 27 additions & 35 deletions internal/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,39 @@ package config
import "os"

type EnvVar struct {
DBHost string
DBPort string
DBUser string
DBPass string
DBName string
LoginURL string
LoginToken string
PlsURL string
PlsSystem string
ServerPort string
ServerURL string
PsqlconnStringGOrdian string
PsqlconnStringCashflow string
LoginURL string
LoginToken string
PlsURL string
PlsSystem string
ServerPort string
ServerURL string
}

func GetEnv() EnvVar {

envConfig := EnvVar{
DBHost: os.Getenv("DB_HOST"),
DBPort: os.Getenv("DB_PORT"),
DBUser: os.Getenv("DB_USER"),
DBPass: os.Getenv("DB_PASS"),
DBName: os.Getenv("DB_NAME"),
LoginURL: os.Getenv("LOGIN_URL"),
LoginToken: os.Getenv("LOGIN_TOKEN"),
PlsURL: os.Getenv("PLS_URL"),
PlsSystem: os.Getenv("PLS_SYSTEM"),
ServerPort: os.Getenv("SERVER_PORT"),
ServerURL: os.Getenv("SERVER_URL"),
//prod
PsqlconnStringGOrdian: os.Getenv("GO_CONN"),
PsqlconnStringCashflow: os.Getenv("CF_CONN"),
LoginURL: os.Getenv("LOGIN_URL"),
LoginToken: os.Getenv("LOGIN_TOKEN"),
PlsURL: os.Getenv("PLS_URL"),
PlsSystem: os.Getenv("PLS_SYSTEM"),
ServerPort: os.Getenv("SERVER_PORT"),
ServerURL: os.Getenv("SERVER_URL"),

// local
// PsqlconnStringGOrdian: "host=localhost port=5432 user=alexander password=kopis dbname=budget_local sslmode=disable",
// PsqlconnStringCashflow: "host=localhost port=54321 user=cashflow password=cashflow dbname=cashflow sslmode=disable",
// LoginURL: "https://login.datasektionen.se",
// PlsURL: "https://pls.datasektionen.se",
// PlsSystem: "gordian",
// ServerPort: "3000",
// ServerURL: "http://localhost:3000",
// LoginToken: "this is secret fuck you",
}

return envConfig
}

//example
// DBHost: localhost,
// DBPort: "5432",
// DBUser: "alexander",
// DBPass: "kopis",
// DBName: "budget_local",
// LoginURL: "https://login.datasektionen.se",
// LoginToken: "this is secret fuck you",
// PlsURL: "https://pls.datasektionen.se",
// PlsSystem: "gordian",
// ServerPort: "3000",
// ServerURL: "http://localhost:3000",
9 changes: 3 additions & 6 deletions internal/database/loader.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package database

import (
"database/sql"
"fmt"
"github.com/datasektionen/GOrdian/internal/excel"
"io"
)

func SaveBudget(fileReader io.Reader) error {
func SaveBudget(fileReader io.Reader, db *sql.DB) error {
fmt.Println("You have very many money")
//testBudget := "test/Budget_2024.xlsx"
costCentres, secondaryCostCentres, budgetLines, err := excel.ReadExcel(fileReader)
if err != nil {
return fmt.Errorf("error parsing Excel file: %v", err)
}

db, err := Connect()
if err != nil {
return fmt.Errorf("error accessing database: %v", err)
return fmt.Errorf("error parsing Excel file: %v", err)
}

err = WipeDatabase(db)
Expand Down
8 changes: 1 addition & 7 deletions internal/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ package database
import (
"database/sql"
"fmt"
"github.com/datasektionen/GOrdian/internal/config"
_ "github.com/lib/pq"
)

func Connect() (*sql.DB, error) {

envVar := config.GetEnv()

psqlconnString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
envVar.DBHost, envVar.DBPort, envVar.DBUser, envVar.DBPass, envVar.DBName)
func Connect(psqlconnString string) (*sql.DB, error) {

db, err := sql.Open("postgres", psqlconnString)
if err != nil {
Expand Down
149 changes: 149 additions & 0 deletions internal/web/DBgetters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package web

import (
"database/sql"
"fmt"
"github.com/datasektionen/GOrdian/internal/excel"
)

func getBudgetLinesByCostCentreID(db *sql.DB, costCentreID int) ([]excel.BudgetLine, error) {
var budgetLinesGetStatementStatic = `
SELECT
budget_lines.id,
budget_lines.name,
income,
expense,
comment,
account,
secondary_cost_centres.id,
secondary_cost_centres.name
FROM budget_lines
JOIN secondary_cost_centres ON secondary_cost_centres.id = secondary_cost_centre_id
WHERE cost_centre_id = $1
ORDER BY secondary_cost_centre_id
`
result, err := db.Query(budgetLinesGetStatementStatic, costCentreID)
if err != nil {
return nil, fmt.Errorf("failed to get budget lines from database: %v", err)
}
var budgetLines []excel.BudgetLine
for result.Next() {
var budgetLine excel.BudgetLine

err := result.Scan(
&budgetLine.BudgetLineID,
&budgetLine.BudgetLineName,
&budgetLine.BudgetLineIncome,
&budgetLine.BudgetLineExpense,
&budgetLine.BudgetLineComment,
&budgetLine.BudgetLineAccount,
&budgetLine.SecondaryCostCentreID,
&budgetLine.SecondaryCostCentreName,
)
if err != nil {
return nil, fmt.Errorf("failed to scan budget line from query result: %v", err)
}
budgetLines = append(budgetLines, budgetLine)
}
return budgetLines, nil
}

func getCostCentres(db *sql.DB) ([]excel.CostCentre, error) {
var costCentresGetStatementStatic = `SELECT id, name, type FROM cost_centres ORDER BY name`
result, err := db.Query(costCentresGetStatementStatic)
if err != nil {
return nil, fmt.Errorf("failed to get cost centres from database: %v", err)
}
var costCentres []excel.CostCentre
for result.Next() {
var costCentre excel.CostCentre

err := result.Scan(&costCentre.CostCentreID, &costCentre.CostCentreName, &costCentre.CostCentreType)
if err != nil {
return nil, fmt.Errorf("failed to scan cost centre from query result: %v", err)
}
costCentres = append(costCentres, costCentre)
}
return costCentres, nil
}

func getCostCentreByID(db *sql.DB, costCentreID int) (excel.CostCentre, error) {
var costCentreGetStatementStatic = `SELECT id, name, type FROM cost_centres WHERE id = $1`
result := db.QueryRow(costCentreGetStatementStatic, costCentreID)
var costCentre excel.CostCentre
err := result.Scan(&costCentre.CostCentreID, &costCentre.CostCentreName, &costCentre.CostCentreType)
if err != nil {
return excel.CostCentre{}, fmt.Errorf("failed to scan cost centre from query result: %v", err)
}
return costCentre, nil
}

func getSecondaryCostCentresByCostCentreID(db *sql.DB, costCentreID int) ([]excel.SecondaryCostCentre, error) {
var SecondaryCostCentresGetStatementStatic = `
SELECT
id,
name,
cost_centre_id
FROM secondary_cost_centres
WHERE cost_centre_id = $1
ORDER BY id
`
result, err := db.Query(SecondaryCostCentresGetStatementStatic, costCentreID)
if err != nil {
return nil, fmt.Errorf("failed to get secondary cost centres from database: %v", err)
}
var secondaryCostCentres []excel.SecondaryCostCentre
for result.Next() {
var secondaryCostCentre excel.SecondaryCostCentre

err := result.Scan(
&secondaryCostCentre.SecondaryCostCentreID,
&secondaryCostCentre.SecondaryCostCentreName,
&secondaryCostCentre.CostCentreID,
)
if err != nil {
return nil, fmt.Errorf("failed to scan secondary cost centre from query result: %v", err)
}
secondaryCostCentres = append(secondaryCostCentres, secondaryCostCentre)
}
return secondaryCostCentres, nil
}

func getBudgetLinesBySecondaryCostCentreID(db *sql.DB, secondaryCostCentreID int) ([]excel.BudgetLine, error) {
var budgetLinesGetStatementStatic = `
SELECT
id,
name,
income,
expense,
comment,
account,
secondary_cost_centre_id
FROM budget_lines
WHERE secondary_cost_centre_id = $1
ORDER BY id
`
result, err := db.Query(budgetLinesGetStatementStatic, secondaryCostCentreID)
if err != nil {
return nil, fmt.Errorf("failed to get budgetlines from database: %v", err)
}
var budgetLines []excel.BudgetLine
for result.Next() {
var budgetLine excel.BudgetLine

err := result.Scan(
&budgetLine.BudgetLineID,
&budgetLine.BudgetLineName,
&budgetLine.BudgetLineIncome,
&budgetLine.BudgetLineExpense,
&budgetLine.BudgetLineComment,
&budgetLine.BudgetLineAccount,
&budgetLine.SecondaryCostCentreID,
)
if err != nil {
return nil, fmt.Errorf("failed to scan budget line from query result: %v", err)
}
budgetLines = append(budgetLines, budgetLine)
}
return budgetLines, nil
}
53 changes: 53 additions & 0 deletions internal/web/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package web

import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"strconv"
)

func apiCostCentres(w http.ResponseWriter, r *http.Request, db *sql.DB) error {
costCentres, err := getCostCentres(db)
if err != nil {
return fmt.Errorf("failed get scan cost centres information from database: %v", err)
}
err = json.NewEncoder(w).Encode(costCentres)
if err != nil {
return fmt.Errorf("failed to encode cost centres to json: %v", err)
}
return nil
}

func apiSecondaryCostCentre(w http.ResponseWriter, r *http.Request, db *sql.DB) error {
idCC, err := strconv.Atoi(r.FormValue("id"))
if err != nil {
return fmt.Errorf("failed to convert secondary cost centre id to int: %v", err)
}
secondaryCostCentres, err := getSecondaryCostCentresByCostCentreID(db, idCC)
if err != nil {
return fmt.Errorf("failed get scan sendondary cost centres information from database: %v", err)
}
err = json.NewEncoder(w).Encode(secondaryCostCentres)
if err != nil {
return fmt.Errorf("failed to encode secondary cost centres to json: %v", err)
}
return nil
}

func apiBudgetLine(w http.ResponseWriter, r *http.Request, db *sql.DB) error {
idSCC, err := strconv.Atoi(r.FormValue("id"))
if err != nil {
return fmt.Errorf("failed to convert SCC id fromstring to int: %v", err)
}
budgetLines, err := getBudgetLinesBySecondaryCostCentreID(db, idSCC)
if err != nil {
return fmt.Errorf("failed get scan budget lines information from database: %v", err)
}
err = json.NewEncoder(w).Encode(budgetLines)
if err != nil {
return fmt.Errorf("failed to encode budget lines to json: %v", err)
}
return nil
}
Loading

0 comments on commit 602fd91

Please sign in to comment.