Skip to content

Commit

Permalink
Introduce api to update domain (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisommore authored Nov 29, 2023
1 parent 8d87da2 commit b63a5dc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/v1/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
g.POST("", postDomain)
g.GET("", queryDomain)
g.DELETE("", deleteDomain)
g.PATCH("", patchDomain)
g.PATCH("/verify", verifyDomain)
}
}
62 changes: 62 additions & 0 deletions api/v1/domain/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package domain

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 patchDomain(c *gin.Context) {
db := dbconfig.GetDb()
var requestBody PatchDomainRequest
err := c.BindJSON(&requestBody)
if err != nil {
httpo.NewErrorResponse(http.StatusForbidden, 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: requestBody.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)
}
domainUpdate := models.Domain{
Title: requestBody.Title,
Headline: requestBody.Headline,
Description: requestBody.Description,
LogoHash: requestBody.LogoHash,
Category: requestBody.Category,
CoverImageHash: requestBody.CoverImageHash,
Blockchain: requestBody.Blockchain,
UpdatedByAddress: strings.ToLower(walletAddress),
}
result := db.Model(&models.Domain{}).
Where("id = ?", requestBody.DomainId).
Updates(&domainUpdate)
if result.Error != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "unexpected error occured").SendD(c)
return
}
if result.RowsAffected == 0 {
httpo.NewErrorResponse(http.StatusNotFound, "domain not found").SendD(c)
return
}
httpo.NewSuccessResponse(200, "domain successfully updated").SendD(c)

}
11 changes: 11 additions & 0 deletions api/v1/domain/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ type GetDomainsQuery struct {
Verified *bool `form:"verified"`
Page *int `form:"page" binding:"required,min=1"`
}

type PatchDomainRequest struct {
Title string `json:"title"`
Headline string `json:"headline"`
Description string `json:"description"`
CoverImageHash string `json:"coverImageHash"`
LogoHash string `json:"logoHash"`
Category string `json:"category"`
Blockchain string `json:"blockchain"`
DomainId string `json:"domainId" binding:"required"`
}

0 comments on commit b63a5dc

Please sign in to comment.