133 lines
2.9 KiB
Go
133 lines
2.9 KiB
Go
package transaction
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"strconv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"gitnet.fr/deblan/budget/backend/controller/crud"
|
|
"gitnet.fr/deblan/budget/database/manager"
|
|
"gitnet.fr/deblan/budget/database/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Controller struct {
|
|
crud *crud.Controller
|
|
}
|
|
|
|
func (ctrl *Controller) Config() crud.Configuration {
|
|
return crud.Configuration{
|
|
Table: "transactions",
|
|
Model: model.Transaction{},
|
|
Models: []model.Transaction{},
|
|
ValidOrders: []string{"accounted_at", "short_label", "label", "reference", "information", "operation_type", "debit", "credit", "date", "category_id", "bank_account_id"},
|
|
DefaultLimit: 20,
|
|
DefaultOrder: "date",
|
|
DefaultSort: "desc",
|
|
CreateModel: func() interface{} {
|
|
return new(model.Transaction)
|
|
},
|
|
ListQuery: func(db *gorm.DB) {
|
|
db.Joins("BankAccount")
|
|
db.Joins("Category")
|
|
},
|
|
}
|
|
}
|
|
|
|
func New(e *echo.Echo) *Controller {
|
|
c := Controller{
|
|
crud: crud.New(),
|
|
}
|
|
|
|
e.GET("/api/transaction", c.List)
|
|
e.POST("/api/transactions/update_categories", c.UpdateCategories)
|
|
e.POST("/api/transactions", c.Create)
|
|
e.GET("/api/transaction/:id", c.Show)
|
|
|
|
return &c
|
|
}
|
|
|
|
func (ctrl *Controller) List(c echo.Context) error {
|
|
if nil == model.LoadApiUser(c) {
|
|
return c.JSON(403, crud.Error{Code: 403, Message: "Login required"})
|
|
}
|
|
|
|
return ctrl.crud.With(ctrl.Config()).List(c)
|
|
}
|
|
|
|
func (ctrl *Controller) Show(c echo.Context) error {
|
|
if nil == model.LoadApiUser(c) {
|
|
return c.JSON(403, crud.Error{Code: 403, Message: "Login required"})
|
|
}
|
|
|
|
return ctrl.crud.With(ctrl.Config()).Show(c)
|
|
}
|
|
|
|
func (ctrl *Controller) UpdateCategories(c echo.Context) error {
|
|
if nil == model.LoadApiUser(c) {
|
|
return c.JSON(403, crud.Error{Code: 403, Message: "Login required"})
|
|
}
|
|
|
|
datas := model.UpdateTransactionsCategories()
|
|
|
|
return c.JSON(200, datas)
|
|
}
|
|
|
|
func (ctrl *Controller) Create(c echo.Context) error {
|
|
if nil == model.LoadApiUser(c) {
|
|
return c.JSON(403, crud.Error{Code: 403, Message: "Login required"})
|
|
}
|
|
|
|
db := manager.Get().Db
|
|
bankAccountID, err := strconv.Atoi(c.FormValue("bank_account_id"))
|
|
|
|
if err != nil {
|
|
return c.JSON(400, crud.Error{
|
|
Code: 400,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
|
|
var count int64
|
|
db.Model(model.BankAccount{}).Where("id = ?", bankAccountID).Count(&count)
|
|
|
|
if count == 0 {
|
|
return c.JSON(400, crud.Error{
|
|
Code: 400,
|
|
Message: "Invalid bank account",
|
|
})
|
|
}
|
|
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
return c.JSON(400, crud.Error{
|
|
Code: 400,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
return c.JSON(400, crud.Error{
|
|
Code: 400,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
var buf bytes.Buffer
|
|
io.Copy(&buf, src)
|
|
|
|
datas, err := model.ImportTransactions(string(buf.Bytes()), bankAccountID, c.FormValue("format"))
|
|
|
|
if err != nil {
|
|
return c.JSON(400, crud.Error{
|
|
Code: 400,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.JSON(200, datas)
|
|
}
|