Skip to content

Commit

Permalink
Merge pull request #80 from MTthoas/create-users
Browse files Browse the repository at this point in the history
Create users
  • Loading branch information
MTthoas authored Apr 24, 2024
2 parents d602e61 + 75611aa commit 8b12018
Show file tree
Hide file tree
Showing 39 changed files with 829 additions and 714 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
[submodule "contracts/lib/openzeppelin-contracts"]
path = contracts/lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
1 change: 1 addition & 0 deletions address/address.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UserRegistry=0x45B762Ae626e40b4A0739146361d9c6e7c2ff0b9
3 changes: 1 addition & 2 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ docker build -t api-build .

go install github.com/swaggo/swag/cmd/swag@latest
swag init --generalInfo ./routes/swagger_routes.go --output ./docs

> > go run main.go
>> go run main.go
87 changes: 42 additions & 45 deletions api/controllers/transaction.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetTransactions(c *fiber.Ctx) error {
// @Success 200 {object} models.Transaction
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/transaction/{id} [get]
// @Router /api/v1/transactions/{id} [get]
func GetTransaction(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
Expand All @@ -72,14 +72,15 @@ func GetTransaction(c *fiber.Ctx) error {

// CreateTransaction function to create a new transaction
// @Summary Create a new transaction
// @Description Creates a new transaction in the database.
// @Description Creates a new transaction in the database
// @Examples { "amount": 0, "from": "IDDIZAIDAD", "to": "IDDIZAIDADddd", "transaction": "IDDIZAIDADdddddddd", "created_at": "2021-09-01T00:00:00Z", "updated_at": "2021-09-01T00:00:00Z"}
// @Tags Transactions
// @Accept json
// @Produce json
// @Param transaction body models.Transaction true "Transaction object"
// @Success 200 {object} models.Transaction
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/transaction [post]
// @Router /api/v1/transactions [post]
func CreateTransaction(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
Expand Down Expand Up @@ -117,51 +118,50 @@ func CreateTransaction(c *fiber.Ctx) error {
// @Success 200 {object} models.Transaction
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/transaction/{id} [put]
// @Router /api/v1/transactions/{id} [put]
func UpdateTransaction(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
db, err := database.OpenDBConnection()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}

id := c.Params("id")
// Vérifie si la transaction existe avant la mise à jour
_, err = db.TransactionQueries.GetTransactionByID(utils.StringToInt(id))
if err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(200).JSON(fiber.Map{
"error": "Transaction not found",
})
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}

id := c.Params("id")
// Vérifie si la transaction existe avant la mise à jour
_, err = db.TransactionQueries.GetTransactionByID(utils.StringToInt(id))
if err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": "Transaction not found",
})
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
transaction := new(models.Transaction)
if err := c.BodyParser(transaction); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
}

transaction := new(models.Transaction)
if err := c.BodyParser(transaction); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": err.Error(),
})
}

transaction.ID = uint(utils.StringToInt(id))
transaction.ID = uint(utils.StringToInt(id))
// Assurez-vous que l'ID est correctement défini
updatedTransaction, err := db.TransactionQueries.UpdateTransaction(*transaction)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
updatedTransaction, err := db.TransactionQueries.UpdateTransaction(*transaction)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}

return c.Status(fiber.StatusOK).JSON(fiber.Map{
"data": updatedTransaction,
})
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"data": updatedTransaction,
})
}


// DeleteTransaction function to delete a transaction
// @Summary Delete a transaction
// @Description Deletes a transaction from the database.
Expand All @@ -172,7 +172,7 @@ func UpdateTransaction(c *fiber.Ctx) error {
// @Success 200 {object} map[string]interface{}
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/transaction/{id} [delete]
// @Router /api/v1/transactions/{id} [delete]
func DeleteTransaction(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
Expand All @@ -191,6 +191,3 @@ func DeleteTransaction(c *fiber.Ctx) error {
"msg": "Transaction deleted successfully",
})
}



52 changes: 44 additions & 8 deletions api/controllers/user.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// GetUsers function to get all users
// @Summary Get all users
// @Description Retrieves a list of all users in the database.
// @Tags Users
// @Tags User
// @Accept json
// @Produce json
// @Success 200 {object} models.User
Expand All @@ -38,14 +38,14 @@ func GetUsers(c *fiber.Ctx) error {
// GetUser function to get user by ID
// @Summary Get user by ID
// @Description Retrieves a user by ID from the database.
// @Tags Users
// @Tags User
// @Accept json
// @Produce json
// @Param id path int true "User ID"
// @Success 200 {object} models.User
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/user/{id} [get]
// @Router /api/v1/users/{id} [get]
func GetUser(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
Expand All @@ -71,10 +71,46 @@ func GetUser(c *fiber.Ctx) error {
})
}

// GetUserByAddress function to get user by address
// @Summary Get user by address
// @Description Retrieves a user by address from the database.
// @Tags User
// @Accept json
// @Produce json
// @Param address path string true "User address"
// @Success 200 {object} models.User
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/users/address/{address} [get]
func GetUserByAddress(c *fiber.Ctx) error {
db, err := database.OpenDBConnection()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
address := c.Params("address")
user, err := db.UserQueries.GetUserByAddress(address)
if err != nil {
if err == gorm.ErrRecordNotFound {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": true,
"msg": "user not found",
})
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"data": user,
})
}

// CreateUser function to create a new user
// @Summary Create a new user
// @Description Creates a new user in the database.
// @Tags Users
// @Tags User
// @Accept json
// @Produce json
// @Param user body models.User true "User object"
Expand Down Expand Up @@ -109,7 +145,7 @@ func CreateUser(c *fiber.Ctx) error {
// UpdateUser function to update a user
// @Summary Update a user
// @Description Updates a user in the database.
// @Tags Users
// @Tags User
// @Accept json
// @Produce json
// @Param id path int true "User ID"
Expand All @@ -118,7 +154,7 @@ func CreateUser(c *fiber.Ctx) error {
// @Failure 400 {object} map[string]interface{}
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/user/{id} [put]
// @Router /api/v1/users/{id} [put]
func UpdateUser(c *fiber.Ctx) error {

db, err := database.OpenDBConnection()
Expand Down Expand Up @@ -159,14 +195,14 @@ func UpdateUser(c *fiber.Ctx) error {
// DeleteUser function to delete a user
// @Summary Delete a user
// @Description Deletes a user from the database.
// @Tags Users
// @Tags User
// @Accept json
// @Produce json
// @Param id path int true "User ID"
// @Success 204
// @Failure 404 {object} map[string]interface{}
// @Failure 500 {object} map[string]interface{}
// @Router /api/v1/user/{id} [delete]
// @Router /api/v1/users/{id} [delete]
func DeleteUser(c *fiber.Ctx) error {

db, err := database.OpenDBConnection()
Expand Down
1 change: 0 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"log"

"github.com/MTthoas/dex/api/configs"
_ "github.com/MTthoas/dex/api/docs"
"github.com/MTthoas/dex/api/middleware"
Expand Down
7 changes: 3 additions & 4 deletions api/models/base.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
)

type Base struct {
ID uint `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt time.Time `json:"deleted_at"`
ID uint `json:"id" gorm:"primaryKey" example:"1"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime" example:"2021-08-01T00:00:00Z"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime" example:"2021-08-01T00:00:00Z"`
}
12 changes: 8 additions & 4 deletions api/models/transaction.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package models

type Transaction struct {
Base
From string `json:"from"`
To string `json:"to"`
Amount float64 `json:"amount"`
Transaction string `json:"transaction"`
From string `json:"from" example:"0x123abc"`
To string `json:"to" example:"0x456def"`
Amount float64 `json:"amount" example:"99.99"`
Transaction string `json:"transaction" example:"tx_789ghi"`
}

func (transaction *Transaction) TableName() string {
return "transaction"
}
8 changes: 6 additions & 2 deletions api/models/user.model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package models

type User struct {
Base
Name string `json:"name"`
Address string `json:"address" gorm:"unique"`
Name string `json:"name" gorm:"not null" example:"John Doe"`
Address string `json:"address" gorm:"unique" example:"0x123abc" validate:"required"`
}

func (user *User) TableName() string {
return "user"
}
9 changes: 8 additions & 1 deletion api/platform/database/open_db_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package database
import (
"os"

"github.com/MTthoas/dex/api/models"
"github.com/MTthoas/dex/api/queries"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// Queries struct for collect all app queries.
type Queries struct {
*queries.UserQueries
*queries.UserQueries
*queries.TransactionQueries
*queries.PoolQueries
*queries.TokenQueries
Expand All @@ -24,6 +25,12 @@ func OpenDBConnection() (*Queries, error) {
return nil, err
}

// Perform auto-migration to keep the schema updated.
err = db.AutoMigrate(&models.User{}, &models.Transaction{})
if err != nil {
return nil, err
}

return &Queries{
UserQueries: &queries.UserQueries{
DB: db,
Expand Down
10 changes: 8 additions & 2 deletions api/queries/user.query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"gorm.io/gorm"
)


type UserQueries struct {
DB *gorm.DB
}
Expand All @@ -26,6 +25,14 @@ func (uq *UserQueries) GetUserByID(id int) (models.User, error) {
return user, nil
}

func (uq *UserQueries) GetUserByAddress(address string) (models.User, error) {
var user models.User
if err := uq.DB.Where("address = ?", address).First(&user).Error; err != nil {
return models.User{}, err
}
return user, nil
}

func (uq *UserQueries) CreateUser(user models.User) (models.User, error) {
if err := uq.DB.Create(&user).Error; err != nil {
return models.User{}, err
Expand All @@ -40,7 +47,6 @@ func (uq *UserQueries) UpdateUser(user models.User) (models.User, error) {
return user, nil
}


func (uq *UserQueries) DeleteUser(id int) error {
if err := uq.DB.Delete(&models.User{}, id).Error; err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions api/routes/not_found_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ func NotFoundRoute(a *fiber.App) {
// Anonymous function.
func(c *fiber.Ctx) error {
// Return HTTP 404 status and JSON response.
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
return c.Status(200).JSON(fiber.Map{
"error": true,
"msg": "sorry, endpoint is not found",
})
},
)
}
}
Loading

0 comments on commit 8b12018

Please sign in to comment.