package handlers import ( "fmt" "net/http" "git.agecem.com/agecem/bottin-agenda/data" "git.agecem.com/agecem/bottin-agenda/models" "git.agecem.com/agecem/bottin-agenda/responses" bottindata "git.agecem.com/agecem/bottin/v6/data" "github.com/labstack/echo/v4" "github.com/spf13/viper" ) // GetTransactions handles the listing of transactions func GetTransactions(c echo.Context) error { var statusCode int = http.StatusInternalServerError var response responses.GetTransactionsResponse client, err := data.NewDataClientFromViper() if err != nil { response.Message = fmt.Sprintf("Erreur pendant data.NewDataClientFromViper(): %s", err) return c.JSON(statusCode, response) } defer client.DB.Close() transactions, err := client.ListTransactions() response.Data.Transactions = transactions if err != nil { response.Message = fmt.Sprintf("Erreur pendant client.ListTransactions(): %s", err) return c.JSON(statusCode, response) } if len(transactions) == 0 { response.Message = "Aucune transaction trouvée" statusCode = http.StatusNotFound return c.JSON(statusCode, response) } response.Message = "Liste complétée" statusCode = http.StatusOK return c.JSON(statusCode, response) } // PostTransactions handles the creation of transactions func PostTransactions(c echo.Context) error { var statusCode int = http.StatusInternalServerError var response responses.PostTransactionsResponse var transactions []models.Transaction if err := c.Bind(&transactions); err != nil { statusCode = http.StatusBadRequest response.Message = fmt.Sprintf("Erreur pendant c.Bind(): %s", err) return c.JSON(statusCode, response) } client, err := data.NewDataClientFromViper() if err != nil { response.Message = fmt.Sprintf("Erreur pendant data.NewDataClientFromViper(): %s", err) return c.JSON(statusCode, response) } defer client.DB.Close() if len(transactions) == 0 { response.Message = fmt.Sprintf("Rien à faire") statusCode = http.StatusOK return c.JSON(statusCode, response) } bottinApiKey := viper.GetString("bottin.api.key") bottinApiHost := viper.GetString("bottin.api.host") bottinApiProtocol := viper.GetString("bottin.api.protocol") bottinApiPort := viper.GetInt("bottin.api.port") bottinClient := http.DefaultClient defer bottinClient.CloseIdleConnections() // Using bottin's API client bottinApiClient := bottindata.NewApiClient( bottinClient, bottinApiKey, bottinApiHost, bottinApiProtocol, bottinApiPort, ) // Check if membre_id exists according to bottin for _, transaction := range transactions { if transaction.MembreID == "" { response.Message = fmt.Sprintf("Impossible d'insérer une transaction sans membre_id (numéro étudiant)") statusCode = http.StatusBadRequest return c.JSON(statusCode, response) } membre, err := bottinApiClient.GetMembre(transaction.MembreID) if err != nil { response.Message = fmt.Sprintf("Erreur pendant bottinApiClient.GetMembre(): %s", err) return c.JSON(statusCode, response) } if membre.ID == "" { response.Message = fmt.Sprintf("Aucun membre avec numéro étudiant '%s' dans le bottin de l'AGECEM", membre.ID) statusCode = http.StatusNotFound return c.JSON(statusCode, response) } // membre exists, can keep going } // Check for already-existing transactions for _, transaction := range transactions { transaction, err := client.GetTransaction(transaction.MembreID, transaction.IsPerpetual) if err != nil { if err.Error() != "sql: no rows in result set" { response.Message = fmt.Sprintf("Erreur pendant client.GetTransaction(): %s", err) } } if transaction.ID != "" { agendaType := "non-perpétuel" if transaction.IsPerpetual { agendaType = "perpétuel" } response.Message = fmt.Sprintf("Membre %s a déjà son agenda %s", transaction.MembreID, agendaType) statusCode = http.StatusBadRequest return c.JSON(statusCode, response) } } insertedTransactions, err := client.InsertTransactions(transactions) response.Data.Transactions = insertedTransactions if err != nil { response.Message = fmt.Sprintf("Erreur pendant client.InsertTransactions(): %s", err) return c.JSON(statusCode, response) } statusCode = http.StatusCreated response.Message = "Insertion complétée" return c.JSON(statusCode, response) }