Bookholder-API/server/transaction.go

219 lines
4.3 KiB
Go
Raw Normal View History

2024-12-20 00:52:27 +00:00
package server
import (
"encoding/json"
2024-12-20 00:52:27 +00:00
"net/http"
"strconv"
2024-12-20 00:52:27 +00:00
"github.com/LeRoid-hub/Bookholder-API/database"
2024-12-20 00:52:27 +00:00
"github.com/gin-gonic/gin"
)
func getTransaction(c *gin.Context) {
id := c.Param("TransactionID")
idInt, err := strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid id; must be an integer",
})
return
}
if idInt < 1 {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid id; must be greater than 0",
})
return
}
transaction, err := database.GetTransaction(Database, idInt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error",
})
return
}
if transaction.ID == 0 {
c.JSON(http.StatusNotFound, gin.H{
"message": "transaction not found",
})
return
}
jasondata, err := json.Marshal(transaction)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error",
})
return
}
2024-12-20 00:52:27 +00:00
c.JSON(http.StatusOK, gin.H{
"transaction": jasondata,
2024-12-20 00:52:27 +00:00
})
}
func getTransactions(c *gin.Context) {
year := c.Param("year")
month := c.Param("month")
yearInt, err := strconv.Atoi(year)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid year; must be an integer",
})
return
}
if yearInt < 1 {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid year; must be greater than 0",
})
return
}
monthInt, err := strconv.Atoi(month)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid month; must be an integer",
})
return
}
if monthInt < 1 || monthInt > 12 {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid month; must be between 1 and 12",
})
return
}
account := c.Param("AccountID")
accountInt, err := strconv.Atoi(account)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid account; must be an integer",
})
return
}
if accountInt < 1 {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid account; must be greater than 0",
})
return
}
transactions, err := database.GetTransactions(Database, accountInt, yearInt, monthInt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error",
})
return
}
jasondata, err := json.Marshal(transactions)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error",
})
return
}
2024-12-20 00:52:27 +00:00
c.JSON(http.StatusOK, gin.H{
"transactions": jasondata,
2024-12-20 00:52:27 +00:00
})
}
func newTransaction(c *gin.Context) {
var transaction database.Transaction
err := c.BindJSON(&transaction)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid json",
})
return
}
if transaction.OffsetAccount == transaction.Account {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid offset account; must be different from account",
})
return
}
err = database.NewTransaction(Database, transaction)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error: " + err.Error(),
})
return
}
c.JSON(http.StatusCreated, gin.H{
"message": "transaction created",
2024-12-20 00:52:27 +00:00
})
}
func updateTransaction(c *gin.Context) {
var transaction database.Transaction
err := c.BindJSON(&transaction)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid json",
})
return
}
if transaction.OffsetAccount == transaction.Account {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid offset account; must be different from account",
})
return
}
err = database.UpdateTransaction(Database, transaction)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error",
})
return
}
2024-12-20 00:52:27 +00:00
c.JSON(http.StatusOK, gin.H{
"message": "transaction updated",
2024-12-20 00:52:27 +00:00
})
}
func deleteTransaction(c *gin.Context) {
id := c.Param("TransactionID")
idInt, err := strconv.Atoi(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid id; must be an integer",
})
return
}
if idInt < 1 {
c.JSON(http.StatusBadRequest, gin.H{
"message": "invalid id; must be greater than 0",
})
return
}
err = database.DeleteTransaction(Database, idInt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "internal server error",
})
return
}
2024-12-20 00:52:27 +00:00
c.JSON(http.StatusOK, gin.H{
"message": "transaction deleted",
2024-12-20 00:52:27 +00:00
})
}