From b63a5dca4188335ef0e59927806ac22fdb45ee2f Mon Sep 17 00:00:00 2001 From: Om More <51229945+thisisommore@users.noreply.github.com> Date: Wed, 29 Nov 2023 11:10:58 +0530 Subject: [PATCH] Introduce api to update domain (#122) --- api/v1/domain/domain.go | 1 + api/v1/domain/patch.go | 62 +++++++++++++++++++++++++++++++++++++++++ api/v1/domain/type.go | 11 ++++++++ 3 files changed, 74 insertions(+) create mode 100644 api/v1/domain/patch.go diff --git a/api/v1/domain/domain.go b/api/v1/domain/domain.go index 7643975..2252897 100644 --- a/api/v1/domain/domain.go +++ b/api/v1/domain/domain.go @@ -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) } } diff --git a/api/v1/domain/patch.go b/api/v1/domain/patch.go new file mode 100644 index 0000000..60a146b --- /dev/null +++ b/api/v1/domain/patch.go @@ -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) + +} diff --git a/api/v1/domain/type.go b/api/v1/domain/type.go index d4fd295..124a842 100644 --- a/api/v1/domain/type.go +++ b/api/v1/domain/type.go @@ -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"` +}