Skip to content

Commit

Permalink
feat: field validation for weblink.
Browse files Browse the repository at this point in the history
  • Loading branch information
puni9869 committed Nov 8, 2024
1 parent edd32c2 commit a269d37
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
5 changes: 5 additions & 0 deletions frontend/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ async function AddNewLink(webLink, selectedTag) {
RefreshPage();
}
} catch (error) {
const errEl = document.getElementById('weblink-err');
if (errEl) {
errEl.classList.toggle("hidden");
errEl.innerText = error.message;
}
console.error(error.message);
}
}
20 changes: 11 additions & 9 deletions server/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ func AddWeblink(c *gin.Context) {
log := logger.NewLogger()

var requestBody struct {
Url string `json:"url"`
Tag string `json:"tag"`
Url string `json:"url" binding:"required"`
Tag string `json:"tag" binding:"required"`
}
session := sessions.Default(c)
currentlyLoggedIn := session.Get(middlewares.Userkey)

// Handle the error for url validation
_ = c.ShouldBind(&requestBody)
err := c.ShouldBind(&requestBody)
if err != nil {
log.WithError(err).Error("Bad request body")
c.JSON(http.StatusBadRequest, gin.H{"Status": "NOT_OK", "Message": "Field is required."})
return
}
db := database.Db()
url := models.Url{
WebLink: requestBody.Url,
IsActive: true,
IsDeleted: false,
CreatedBy: currentlyLoggedIn.(string),
Tag: requestBody.Tag,
url := models.Url{WebLink: requestBody.Url,
IsActive: true, IsDeleted: false,
CreatedBy: currentlyLoggedIn.(string), Tag: requestBody.Tag,
}
db.Save(&url)
log.Info("Requested to add %s in tag: %s ", requestBody.Url, requestBody.Tag)
Expand Down
1 change: 1 addition & 0 deletions templates/widget/add_link.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<!-- Input Box -->
<input autofocus type="text" id="add-weblink-input-box" placeholder="Enter link" class="w-full p-1 border rounded mb-3 focus:outline-none focus:ring-2 focus:ring-indigo-500">
<p class="text-base text-red-600 text-left hidden" id="weblink-err"></p>
<div class="flex select-none">
<div class="colors flex items-center" id="tags">
<ul>
Expand Down

0 comments on commit a269d37

Please sign in to comment.