-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b63a5dc
commit 3eb706b
Showing
7 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package admin | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// ApplyRoutes applies router to gin Router | ||
func ApplyRoutes(r *gin.RouterGroup) { | ||
g := r.Group("/admin") | ||
{ | ||
g.GET("", getAdmin) | ||
g.POST("", createAdmin) | ||
g.DELETE("", deleteAdmin) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package admin | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/NetSepio/gateway/api/middleware/auth/paseto" | ||
"github.com/NetSepio/gateway/config/dbconfig" | ||
"github.com/NetSepio/gateway/models" | ||
"github.com/NetSepio/gateway/util/pkg/logwrapper" | ||
"github.com/TheLazarusNetwork/go-helpers/httpo" | ||
"github.com/gin-gonic/gin" | ||
"github.com/jackc/pgconn" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func createAdmin(c *gin.Context) { | ||
db := dbconfig.GetDb() | ||
var request CreateAdminRequest | ||
err := c.BindJSON(&request) | ||
if err != nil { | ||
//TODO not override status or not set status again | ||
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c) | ||
return | ||
} | ||
|
||
walletAddress := c.GetString(paseto.CTX_WALLET_ADDRES) | ||
|
||
err = db.Model(&models.DomainAdmin{}). | ||
Where(&models.DomainAdmin{DomainId: request.DomainId, AdminWalletAddress: walletAddress}). | ||
First(&models.DomainAdmin{}).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
httpo.NewErrorResponse(http.StatusNotFound, "domain not exist or user is not admin of the domain").SendD(c) | ||
return | ||
} | ||
|
||
logwrapper.Errorf("failed to get domain admin: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to update admin").SendD(c) | ||
} | ||
|
||
adminDetails := make([]models.DomainAdmin, len(request.Admins)) | ||
|
||
i := 0 | ||
for _, v := range request.Admins { | ||
adminDetails[i].DomainId = request.DomainId | ||
adminDetails[i].AdminWalletAddress = v.AdminWalletAddress | ||
adminDetails[i].UpdatedByAddress = walletAddress | ||
adminDetails[i].Name = v.AdminName | ||
adminDetails[i].Role = v.AdminRole | ||
} | ||
err = db.Create(&adminDetails).Error | ||
if err != nil { | ||
var pgError *pgconn.PgError | ||
if errors.As(err, &pgError) { | ||
if pgError.Code == "23503" && pgError.ConstraintName == "fk_domain_admins_admin" { | ||
httpo.NewErrorResponse(http.StatusBadRequest, "admin address not found").SendD(c) | ||
return | ||
} | ||
|
||
if pgError.Code == "23505" && pgError.ConstraintName == "domain_admins_pkey" { | ||
httpo.NewErrorResponse(http.StatusBadRequest, "admin already exist").SendD(c) | ||
return | ||
} | ||
} | ||
logwrapper.Errorf("failed to get domain admin: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to update admin").SendD(c) | ||
return | ||
} | ||
|
||
httpo.NewSuccessResponse(200, "updated admins").SendD(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package admin | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/NetSepio/gateway/api/middleware/auth/paseto" | ||
"github.com/NetSepio/gateway/config/dbconfig" | ||
"github.com/NetSepio/gateway/models" | ||
"github.com/NetSepio/gateway/util/pkg/logwrapper" | ||
"github.com/TheLazarusNetwork/go-helpers/httpo" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func deleteAdmin(c *gin.Context) { | ||
db := dbconfig.GetDb() | ||
var request DeleteAdminRequest | ||
err := c.BindJSON(&request) | ||
if err != nil { | ||
//TODO not override status or not set status again | ||
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c) | ||
return | ||
} | ||
|
||
walletAddress := c.GetString(paseto.CTX_WALLET_ADDRES) | ||
|
||
err = db.Model(&models.DomainAdmin{}). | ||
Where(&models.DomainAdmin{DomainId: request.DomainId, AdminWalletAddress: walletAddress}). | ||
First(&models.DomainAdmin{}).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
httpo.NewErrorResponse(http.StatusNotFound, "domain not exist or user is not admin of the domain").SendD(c) | ||
return | ||
} | ||
|
||
logwrapper.Errorf("failed to get domain admin: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to update admin").SendD(c) | ||
} | ||
|
||
res := db.Delete(&models.DomainAdmin{DomainId: request.DomainId, AdminWalletAddress: strings.ToLower(request.AdminWalletAddres)}) | ||
if res.Error != nil { | ||
logwrapper.Errorf("failed to delete domain admin: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to remove admin").SendD(c) | ||
return | ||
} | ||
|
||
if res.RowsAffected == 0 { | ||
httpo.NewErrorResponse(http.StatusNotFound, "admin not exist for that domain").SendD(c) | ||
return | ||
} | ||
|
||
httpo.NewSuccessResponse(200, "admin removed").SendD(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package admin | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/NetSepio/gateway/api/middleware/auth/paseto" | ||
"github.com/NetSepio/gateway/config/dbconfig" | ||
"github.com/NetSepio/gateway/models" | ||
"github.com/NetSepio/gateway/util/pkg/logwrapper" | ||
"github.com/TheLazarusNetwork/go-helpers/httpo" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func getAdmin(c *gin.Context) { | ||
db := dbconfig.GetDb() | ||
var request GetAdminQuery | ||
err := c.BindJSON(&request) | ||
if err != nil { | ||
//TODO not override status or not set status again | ||
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c) | ||
return | ||
} | ||
|
||
walletAddress := c.GetString(paseto.CTX_WALLET_ADDRES) | ||
|
||
err = db.Model(&models.DomainAdmin{}). | ||
Where(&models.DomainAdmin{DomainId: request.DomainId, AdminWalletAddress: walletAddress}). | ||
First(&models.DomainAdmin{}).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
httpo.NewErrorResponse(http.StatusNotFound, "domain not exist or user is not admin of the domain").SendD(c) | ||
return | ||
} | ||
|
||
logwrapper.Errorf("failed to get domain admin: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to get admins").SendD(c) | ||
} | ||
|
||
var domainAdmins []models.DomainAdmin | ||
err = db.Where(&models.DomainAdmin{DomainId: request.DomainId}).Find(&domainAdmins).Error | ||
if err != nil { | ||
logwrapper.Errorf("failed to get domain admins: %s", err) | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to get admins").SendD(c) | ||
} | ||
|
||
httpo.NewSuccessResponseP(200, "fetch admins", domainAdmins).SendD(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package admin | ||
|
||
type GetAdminQuery struct { | ||
DomainId string `json:"domainId" binding:"required"` | ||
} | ||
|
||
type DeleteAdminRequest struct { | ||
AdminWalletAddres string `json:"adminWalletAddress" binding:"required,min=5"` | ||
DomainId string `json:"domainId" binding:"required,min=10"` | ||
} | ||
|
||
type AdminDetails struct { | ||
AdminName string `json:"adminName" binding:"required,min=1"` | ||
AdminRole string `json:"adminRole" binding:"required,min=1"` | ||
AdminWalletAddress string `json:"adminWalletAddress" binding:"required,min=1"` | ||
} | ||
|
||
type CreateAdminRequest struct { | ||
Admins []AdminDetails `json:"admins" binding:"min=1,dive"` | ||
DomainId string `json:"domainId" binding:"required,min=10"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters