Bump root version to v4
Remove all files from v3 Move all files from v4/ to project root
This commit is contained in:
parent
3c0d45fa04
commit
9a0bf87e7b
40 changed files with 423 additions and 2130 deletions
106
handlers/insert.go
Normal file
106
handlers/insert.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/bottin/v4/data"
|
||||
"git.agecem.com/agecem/bottin/v4/models"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func PostMembres(c echo.Context) error {
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
var membres []models.Membre
|
||||
|
||||
if err := c.Bind(&membres); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "Could not bind membres",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
if len(membres) == 0 {
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Nothing to do",
|
||||
})
|
||||
}
|
||||
|
||||
newMembres, err := client.InsertMembres(membres)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not insert membres",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
||||
"message": "Insert successful",
|
||||
"data": map[string]interface{}{
|
||||
"membres": newMembres,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func PostProgrammes(c echo.Context) error {
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
var programmes []models.Programme
|
||||
|
||||
if err := c.Bind(&programmes); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "Could not bind programmes",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
if len(programmes) == 0 {
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Nothing to do",
|
||||
})
|
||||
}
|
||||
|
||||
newProgrammes, err := client.InsertProgrammes(programmes)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not insert programmes",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, map[string]interface{}{
|
||||
"message": "Insert successful",
|
||||
"data": map[string]interface{}{
|
||||
"programmes": newProgrammes,
|
||||
},
|
||||
})
|
||||
}
|
117
handlers/read.go
Normal file
117
handlers/read.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/bottin/v4/data"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func ReadMembre(c echo.Context) error {
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
membreID := c.Param("membre_id")
|
||||
|
||||
membre, err := client.GetMembre(membreID)
|
||||
if err != nil {
|
||||
if err.Error() == "No membre by that id was found" {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{
|
||||
"message": "Not Found",
|
||||
})
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Unknown error during GetMembre",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"message": "Read successful",
|
||||
"data": map[string]interface{}{
|
||||
"membre": &membre,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/*
|
||||
func PostMembres(c echo.Context) error {
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
programmes := []models.Programme{
|
||||
{
|
||||
ID: "foo",
|
||||
Titre: "Foo",
|
||||
},
|
||||
{
|
||||
ID: "bar",
|
||||
Titre: "Bar",
|
||||
},
|
||||
}
|
||||
|
||||
newProgrammes, err := client.InsertProgrammes(programmes)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not insert programmes",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
membres := []models.Membre{
|
||||
{
|
||||
ID: "1327163",
|
||||
PreferedName: "victor",
|
||||
ProgrammeID: "foo",
|
||||
},
|
||||
{
|
||||
ID: "0000000",
|
||||
PreferedName: "test user",
|
||||
ProgrammeID: "bar",
|
||||
},
|
||||
}
|
||||
|
||||
newMembres, err := client.InsertMembres(membres)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not insert membres",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"message": "Insert successful",
|
||||
"data": map[string]interface{}{
|
||||
"membres": newMembres,
|
||||
"programmes": newProgrammes,
|
||||
},
|
||||
})
|
||||
}
|
||||
*/
|
42
handlers/seed.go
Normal file
42
handlers/seed.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/bottin/v4/data"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func PostSeed(c echo.Context) error {
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
rows, err := client.Seed()
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Seed failed",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"message": "Seed successful",
|
||||
"data": map[string]interface{}{
|
||||
"rows": rows,
|
||||
},
|
||||
})
|
||||
}
|
60
handlers/update.go
Normal file
60
handlers/update.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/bottin/v4/data"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func PutMembrePreferedName(c echo.Context) error {
|
||||
connection := data.PostgresConnection{
|
||||
User: viper.GetString("db.user"),
|
||||
Password: viper.GetString("db.password"),
|
||||
Host: viper.GetString("db.host"),
|
||||
Database: viper.GetString("db.database"),
|
||||
Port: viper.GetInt("db.port"),
|
||||
}
|
||||
|
||||
client, err := data.NewDataClient(connection)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not establish database connection",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
membreID := c.Param("membre_id")
|
||||
|
||||
var newName string
|
||||
|
||||
err = c.Bind(&newName)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "Could not bind newName",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
rows, err := client.UpdateMembreName(membreID, newName)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||
"message": "Could not update membre name",
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
if rows == 0 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"message": "No update was done, probably no membre by that id",
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"message": "Update successful",
|
||||
"data": map[string]interface{}{
|
||||
"rows": rows,
|
||||
},
|
||||
})
|
||||
}
|
13
handlers/v4.go
Normal file
13
handlers/v4.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func GetV4(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, map[string]string{
|
||||
"message": "Bottin API v4 is ready",
|
||||
})
|
||||
}
|
Reference in a new issue