feat: add endpoints

This commit is contained in:
log101 2024-06-18 21:36:30 +03:00
parent 2b89b90fc5
commit 14e3bed6bb
6 changed files with 70 additions and 19 deletions

View File

@ -34,8 +34,7 @@ func PostEmojiForm(c *gin.Context) {
// Check if parameters are missing
if reactedPostId == "" || reaction == "" {
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"})
return
c.HTML(http.StatusBadRequest, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"})
}
// Add the new emoji reaction to the database
@ -44,14 +43,12 @@ func PostEmojiForm(c *gin.Context) {
}).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
if result.Error != nil {
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error writing to database"})
return
}
// get emoji counts for each emoji
emojiCounter, err := CountEmojis(reactedPostId)
if err != nil {
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error getting the emoji counts"})
return
}
// Return the html with the updated emoji counter

41
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"log"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
@ -11,6 +12,7 @@ import (
DB "log101-blog-services/db"
"log101-blog-services/handlers"
"log101-blog-services/middleware"
"log101-blog-services/models"
)
func main() {
@ -42,15 +44,48 @@ func main() {
r.Use(cors.New(corsConfig))
r.Use(middleware.AnonymizeIPMiddleware())
blogForm := r.Group("/blog/api/")
blog := r.Group("/api/blog/")
{
// Get the emoji form, you must provide postId query parameter
blogForm.GET("/forms/emoji", handlers.GetEmojiForm)
blog.GET("/forms/emoji", handlers.GetEmojiForm)
// Update the user's reaction to post, this handler will
// add a new entry to the database with anonymized ip
// updates if user reacted before
blogForm.POST("/forms/emoji/post", handlers.PostEmojiForm)
blog.POST("/forms/emoji", handlers.PostEmojiForm)
blog.GET("/comments", func(c *gin.Context) {
db := DB.GetDB()
postId := c.Query("postId")
comments := []models.Comment{}
rows := db.Where("post_id = ?", postId).Find(&comments)
if rows.Error != nil {
c.HTML(http.StatusInternalServerError, "comment_form.tmpl", gin.H{"errorMessage": "error getting comments", "comments": comments})
}
c.HTML(http.StatusOK, "comment_form.tmpl", gin.H{"errorMessage": "error getting comments", "comments": comments})
})
blog.POST("/comments", func(c *gin.Context) {
db := DB.GetDB()
postId := c.PostForm("postId")
commentBody := c.PostForm("commentBody")
username := c.PostForm("username")
if postId == "" || commentBody == "" {
c.HTML(http.StatusBadRequest, "comment_form.tmpl", gin.H{})
}
result := db.Create(&models.Comment{Body: commentBody, PostId: postId, Username: username})
if result.Error != nil {
c.HTML(http.StatusInternalServerError, "comment_form.tmpl", gin.H{})
}
c.HTML(http.StatusOK, "comment_form.tmpl", gin.H{})
})
}
r.Run(":8000")

8
models/Comment.go Normal file
View File

@ -0,0 +1,8 @@
package models
// Gorm model
type Comment struct {
Body string
PostId string
Username string
}

View File

@ -0,0 +1,11 @@
<div class="comment border-gray-400 border border-solid pt-1 pb-6 ml-4">
<p
class="font-semibold pl-3 pr-2 pb-1 border-b border-x-0 border-t-0 border-gray-400 border-solid"
>
Arafat Candan
</p>
<p class="pl-3 py-2 pr-4">
Daha önce hiç bu açıdan bakmamıştım, harika bir yazı olmuş.
</p>
</div>

View File

@ -1,9 +1,9 @@
<div class="emoji-buttons-container">
<button name="emojiInput" value="👍" type="submit" class="emoji-button">👍&nbsp; {{ if gt (index .results "👍") 0 }} {{ index .results "👍"}} {{ end }}</button>
<button name="emojiInput" value="👎" type="submit" class="emoji-button">👎&nbsp; {{ if gt (index .results "👎") 0 }} {{ index .results "👎"}} {{ end }}</button>
<button name="emojiInput" value="😀" type="submit" class="emoji-button">😀&nbsp; {{ if gt (index .results "😀") 0 }} {{ index .results "😀"}} {{ end }}</button>
<button name="emojiInput" value="😑" type="submit" class="emoji-button">😑&nbsp; {{ if gt (index .results "😑") 0 }} {{ index .results "😑"}} {{ end }}</button>
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">🤢&nbsp; {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">👀&nbsp; {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
<button name="emojiInput" value="👍" type="submit" class="emoji-button">&nbsp;👍&nbsp; {{ if gt (index .results "👍") 0 }} {{ index .results "👍"}} {{ end }}</button>
<button name="emojiInput" value="👎" type="submit" class="emoji-button">&nbsp;👎&nbsp; {{ if gt (index .results "👎") 0 }} {{ index .results "👎"}} {{ end }}</button>
<button name="emojiInput" value="😀" type="submit" class="emoji-button">&nbsp;😀&nbsp; {{ if gt (index .results "😀") 0 }} {{ index .results "😀"}} {{ end }}</button>
<button name="emojiInput" value="😑" type="submit" class="emoji-button">&nbsp;😑&nbsp; {{ if gt (index .results "😑") 0 }} {{ index .results "😑"}} {{ end }}</button>
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">&nbsp;🤢&nbsp; {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">&nbsp;👀&nbsp; {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
</div>

View File

@ -1,9 +1,9 @@
<div class="emoji-buttons-container">
<button name="emojiInput" value="👍" type="submit" class="emoji-button">👍&nbsp;</button>
<button name="emojiInput" value="👎" type="submit" class="emoji-button">👎&nbsp;</button>
<button name="emojiInput" value="😀" type="submit" class="emoji-button">😀&nbsp;</button>
<button name="emojiInput" value="😑" type="submit" class="emoji-button">😑&nbsp;</button>
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">🤢&nbsp;</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">👀&nbsp;</button>
<button name="emojiInput" value="👍" type="submit" class="emoji-button">&nbsp;👍&nbsp;</button>
<button name="emojiInput" value="👎" type="submit" class="emoji-button">&nbsp;👎&nbsp;</button>
<button name="emojiInput" value="😀" type="submit" class="emoji-button">&nbsp;😀&nbsp;</button>
<button name="emojiInput" value="😑" type="submit" class="emoji-button">&nbsp;😑&nbsp;</button>
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">&nbsp;🤢&nbsp;</button>
<button name="emojiInput" value="👀" type="submit" class="emoji-button">&nbsp;👀&nbsp;</button>
</div>
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>