Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aait.backend.g7.mohammed bug fixes and refactoring #2313

Open
wants to merge 3 commits into
base: aait.backend.g7.main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,238 +7,165 @@ import (
"github.com/gin-gonic/gin"
)

type blogController struct {
type BlogController struct {
BlogUsecase domain.IBlogUsecase
}

func NewBlogController(blogUsecase domain.IBlogUsecase) domain.IBlogController {
return &blogController{BlogUsecase: blogUsecase}
return &BlogController{BlogUsecase: blogUsecase}
}

func (bc *blogController) GetAllBlogs(c *gin.Context) {
func (bc *BlogController) GetAllBlogs(c *gin.Context) {
sortOrder := c.DefaultQuery("sort", "DESC") // Default to "DESC" if not specified
page := c.DefaultQuery("page", "1")
limit := c.DefaultQuery("limit", "10")

// Convert page and limit to integers
pageInt, err := strconv.Atoi(page)
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
c.JSON(400, gin.H{"error": "Invalid page number"})
return
}

limitInt, err := strconv.Atoi(limit)
limit, err := strconv.Atoi(c.DefaultQuery("limit", "10"))
if err != nil {
c.JSON(400, gin.H{"error": "Invalid limit number"})
return
}

blogs, err := bc.BlogUsecase.GetAllBlogs(c, sortOrder, pageInt, limitInt)
blogs, err := bc.BlogUsecase.GetAllBlogs(c, sortOrder, page, limit)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, blogs)
}

func (bc *blogController) CreateBlog(c *gin.Context) {
func (bc *BlogController) CreateBlog(c *gin.Context) {
var blog domain.Blog
err := c.BindJSON(&blog)

if err != nil {
if err := c.BindJSON(&blog); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

newBlog, err := bc.BlogUsecase.CreateBlog(c, blog)

if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, newBlog)
}

func (bc *blogController) UpdateBlog(c *gin.Context) {
id := c.Param("id")

idInt, err := strconv.Atoi(id)

func (bc *BlogController) UpdateBlog(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
c.JSON(400, gin.H{"error": "Invalid blog ID"})
return
}

var blog domain.Blog
err = c.BindJSON(&blog)

if err != nil {
if err := c.BindJSON(&blog); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

updatedBlog, err := bc.BlogUsecase.UpdateBlog(c, idInt, blog)

updatedBlog, err := bc.BlogUsecase.UpdateBlog(c, id, blog)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, updatedBlog)

}

func (bc *blogController) DeleteBlog(c *gin.Context) {

id := c.Param("id")

idInt, err := strconv.Atoi(id)

func (bc *BlogController) DeleteBlog(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
c.JSON(400, gin.H{"error": "Invalid blog ID"})
return
}

err = bc.BlogUsecase.DeleteBlog(c, idInt)

if err != nil {
if err := bc.BlogUsecase.DeleteBlog(c, id); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "Blog deleted successfully"})

}

func (bc *blogController) LikeBlog(c *gin.Context) {
blogID := c.Param("blog_id")
authorID := c.Param("author_id")

blogIDInt, err := strconv.Atoi(blogID)

func (bc *BlogController) LikeBlog(c *gin.Context) {
blogID, err := strconv.Atoi(c.Query("id"))
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
c.JSON(400, gin.H{"error": "Invalid blog ID"})
return
}

authorIDInt, err := strconv.Atoi(authorID)

if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

likedBlog, err := bc.BlogUsecase.LikeBlog(c, blogIDInt, authorIDInt)

likedBlog, err := bc.BlogUsecase.LikeBlog(c, blogID)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, likedBlog)

}

func (bc *blogController) DislikeBlog(c *gin.Context) {
blogID := c.Param("blog_id")
authorID := c.Param("author_id")

blogIDInt, err := strconv.Atoi(blogID)

func (bc *BlogController) DislikeBlog(c *gin.Context) {
blogID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

authorIDInt, err := strconv.Atoi(authorID)

if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
c.JSON(400, gin.H{"error": "Invalid blog ID"})
return
}

dislikedBlog, err := bc.BlogUsecase.DislikeBlog(c, blogIDInt, authorIDInt)

dislikedBlog, err := bc.BlogUsecase.DislikeBlog(c, blogID)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, dislikedBlog)

}

func (bc *blogController) AddComment(c *gin.Context) {

blogID := c.Param("blog_id")
authorID := c.Param("author_id")

blogIDInt, err := strconv.Atoi(blogID)

if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

authorIDInt, err := strconv.Atoi(authorID)

func (bc *BlogController) AddComment(c *gin.Context) {
blogID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
c.JSON(400, gin.H{"error": "Invalid blog ID"})
return
}

var comment domain.Comment
err = c.BindJSON(&comment)

comment.UserID = authorIDInt

if err != nil {
if err := c.BindJSON(&comment); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

commentedBlog, err := bc.BlogUsecase.AddComent(c, blogIDInt, authorIDInt, comment.Content)

commentedBlog, err := bc.BlogUsecase.AddComment(c, blogID, comment.Content)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, commentedBlog)

}

func (bc *blogController) Search(c *gin.Context) {
func (bc *BlogController) Search(c *gin.Context) {
author := c.Query("author")
tags := c.QueryArray("tags")
title := c.Query("title")

blogs, err := bc.BlogUsecase.Search(c, author, tags, title)

if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, blogs)

}

type BlogContent struct {
Content string `json:"content"`
}

func (bc *blogController) AiRecommendation(c *gin.Context) {
func (bc *BlogController) AiRecommendation(c *gin.Context) {
var content BlogContent

err := c.ShouldBindJSON(&content)
// print("content is", content)
if err != nil {
print("error is here")
if err := c.ShouldBindJSON(&content); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}

recommendation, err := bc.BlogUsecase.AiRecommendation(c, content.Content)

if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, recommendation)

}
Loading