feat: add endpoints
This commit is contained in:
parent
2b89b90fc5
commit
14e3bed6bb
|
@ -34,8 +34,7 @@ func PostEmojiForm(c *gin.Context) {
|
||||||
|
|
||||||
// Check if parameters are missing
|
// Check if parameters are missing
|
||||||
if reactedPostId == "" || reaction == "" {
|
if reactedPostId == "" || reaction == "" {
|
||||||
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"})
|
c.HTML(http.StatusBadRequest, "emoji_form_error.tmpl", gin.H{"errorMessage": "missing parameters"})
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the new emoji reaction to the database
|
// 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})
|
}).Create(&models.EmojiReaction{UserAnonIp: c.Request.RemoteAddr, Emoji: reaction, PostId: reactedPostId})
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error writing to database"})
|
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error writing to database"})
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get emoji counts for each emoji
|
// get emoji counts for each emoji
|
||||||
emojiCounter, err := CountEmojis(reactedPostId)
|
emojiCounter, err := CountEmojis(reactedPostId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusOK, "emoji_form_error.tmpl", gin.H{"errorMessage": "error getting the emoji counts"})
|
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
|
// Return the html with the updated emoji counter
|
||||||
|
|
41
main.go
41
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -11,6 +12,7 @@ import (
|
||||||
DB "log101-blog-services/db"
|
DB "log101-blog-services/db"
|
||||||
"log101-blog-services/handlers"
|
"log101-blog-services/handlers"
|
||||||
"log101-blog-services/middleware"
|
"log101-blog-services/middleware"
|
||||||
|
"log101-blog-services/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -42,15 +44,48 @@ func main() {
|
||||||
r.Use(cors.New(corsConfig))
|
r.Use(cors.New(corsConfig))
|
||||||
r.Use(middleware.AnonymizeIPMiddleware())
|
r.Use(middleware.AnonymizeIPMiddleware())
|
||||||
|
|
||||||
blogForm := r.Group("/blog/api/")
|
blog := r.Group("/api/blog/")
|
||||||
{
|
{
|
||||||
// Get the emoji form, you must provide postId query parameter
|
// 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
|
// Update the user's reaction to post, this handler will
|
||||||
// add a new entry to the database with anonymized ip
|
// add a new entry to the database with anonymized ip
|
||||||
// updates if user reacted before
|
// 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")
|
r.Run(":8000")
|
||||||
|
|
8
models/Comment.go
Normal file
8
models/Comment.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
// Gorm model
|
||||||
|
type Comment struct {
|
||||||
|
Body string
|
||||||
|
PostId string
|
||||||
|
Username string
|
||||||
|
}
|
11
templates/comment_form.tmpl
Normal file
11
templates/comment_form.tmpl
Normal 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>
|
|
@ -1,9 +1,9 @@
|
||||||
<div class="emoji-buttons-container">
|
<div class="emoji-buttons-container">
|
||||||
<button name="emojiInput" value="👍" type="submit" class="emoji-button">👍 {{ if gt (index .results "👍") 0 }} {{ index .results "👍"}} {{ end }}</button>
|
<button name="emojiInput" value="👍" type="submit" class="emoji-button"> 👍 {{ if gt (index .results "👍") 0 }} {{ index .results "👍"}} {{ end }}</button>
|
||||||
<button name="emojiInput" value="👎" type="submit" class="emoji-button">👎 {{ if gt (index .results "👎") 0 }} {{ index .results "👎"}} {{ end }}</button>
|
<button name="emojiInput" value="👎" type="submit" class="emoji-button"> 👎 {{ if gt (index .results "👎") 0 }} {{ index .results "👎"}} {{ end }}</button>
|
||||||
<button name="emojiInput" value="😀" type="submit" class="emoji-button">😀 {{ if gt (index .results "😀") 0 }} {{ index .results "😀"}} {{ end }}</button>
|
<button name="emojiInput" value="😀" type="submit" class="emoji-button"> 😀 {{ if gt (index .results "😀") 0 }} {{ index .results "😀"}} {{ end }}</button>
|
||||||
<button name="emojiInput" value="😑" type="submit" class="emoji-button">😑 {{ if gt (index .results "😑") 0 }} {{ index .results "😑"}} {{ end }}</button>
|
<button name="emojiInput" value="😑" type="submit" class="emoji-button"> 😑 {{ if gt (index .results "😑") 0 }} {{ index .results "😑"}} {{ end }}</button>
|
||||||
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">🤢 {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
|
<button name="emojiInput" value="🤢" type="submit" class="emoji-button"> 🤢 {{ if gt (index .results "🤢") 0 }} {{ index .results "🤢"}} {{ end }}</button>
|
||||||
<button name="emojiInput" value="👀" type="submit" class="emoji-button">👀 {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
|
<button name="emojiInput" value="👀" type="submit" class="emoji-button"> 👀 {{ if gt (index .results "👀") 0 }} {{ index .results "👀"}} {{ end }}</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
<div class="emoji-buttons-container">
|
<div class="emoji-buttons-container">
|
||||||
<button name="emojiInput" value="👍" type="submit" class="emoji-button">👍 </button>
|
<button name="emojiInput" value="👍" type="submit" class="emoji-button"> 👍 </button>
|
||||||
<button name="emojiInput" value="👎" type="submit" class="emoji-button">👎 </button>
|
<button name="emojiInput" value="👎" type="submit" class="emoji-button"> 👎 </button>
|
||||||
<button name="emojiInput" value="😀" type="submit" class="emoji-button">😀 </button>
|
<button name="emojiInput" value="😀" type="submit" class="emoji-button"> 😀 </button>
|
||||||
<button name="emojiInput" value="😑" type="submit" class="emoji-button">😑 </button>
|
<button name="emojiInput" value="😑" type="submit" class="emoji-button"> 😑 </button>
|
||||||
<button name="emojiInput" value="🤢" type="submit" class="emoji-button">🤢 </button>
|
<button name="emojiInput" value="🤢" type="submit" class="emoji-button"> 🤢 </button>
|
||||||
<button name="emojiInput" value="👀" type="submit" class="emoji-button">👀 </button>
|
<button name="emojiInput" value="👀" type="submit" class="emoji-button"> 👀 </button>
|
||||||
</div>
|
</div>
|
||||||
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>
|
<div id="emoji-form-error"><p>{{ .errorMessage }}</p></div>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user