Skip to content

Commit

Permalink
Added routes and handlers for Categories and CategoriesAssignmentsTable
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Singh <[email protected]>
  • Loading branch information
SkySingh04 committed Jul 12, 2024
1 parent bca72b2 commit e7414dd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
29 changes: 29 additions & 0 deletions blog-service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"blog/contracttesting/db"
"blog/contracttesting/models"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -78,3 +79,31 @@ func deleteblogHandler(c *gin.Context) {

c.JSON(http.StatusOK, gin.H{"message": "Blog deleted successfully"})
}

func getAllCategoriesHandler(c *gin.Context) {
// Implement logic to fetch all categories
categories, err := db.GetAllCategories()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve categories"})
return
}

c.JSON(http.StatusOK, categories)
}

func getCategoriesByBlogIDHandler(c *gin.Context) {
// Implement logic to fetch categories by blog ID
blogID := c.Param("blogID")
intBlogID, err := strconv.Atoi(blogID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid blog ID"})
return
}
categories, err := db.GetCategoriesByBlogID(intBlogID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve categories for blog"})
return
}

c.JSON(http.StatusOK, categories)
}
2 changes: 2 additions & 0 deletions blog-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func main() {
// Define routes
r.GET("/", healthCheckHandler)
r.GET("/blogs/:blogID", getblogHandler)
r.GET("/categories", getAllCategoriesHandler)
r.GET("/blogs/:blogID/categories", getCategoriesByBlogIDHandler)
r.DELETE("/blogs/:blogID", deleteblogHandler)
r.POST("/blogs", createblogHandler)
r.PUT("/blogs", updateblogHandler)
Expand Down
49 changes: 49 additions & 0 deletions db/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,53 @@ func GetCommentForCommentID(commentID string) (*models.Comment, error) {
return nil, err
}
return comment, nil
}

func GetAllCategories() ([]*models.Category, error) {
query := "SELECT id, name FROM categories"
rows, err := db.Query(query)
if err != nil {
log.Printf("Error executing query: %v\n", err)
return nil, err
}
defer rows.Close()

categories := []*models.Category{}
for rows.Next() {
category := &models.Category{}
err := rows.Scan(&category.ID, &category.Name)
if err != nil {
log.Printf("Error scanning category row: %v\n", err)
return nil, err
}
categories = append(categories, category)
}
return categories, nil
}

func GetCategoriesByBlogID(blogID int) ([]*models.Category, error) {
query := `
SELECT c.id, c.name
FROM categories c
JOIN category_assignments ca ON c.id = ca.category_id
WHERE ca.blog_id = $1
`
rows, err := db.Query(query, blogID)
if err != nil {
log.Printf("Error executing query: %v\n", err)
return nil, err
}
defer rows.Close()

categories := []*models.Category{}
for rows.Next() {
category := &models.Category{}
err := rows.Scan(&category.ID, &category.Name)
if err != nil {
log.Printf("Error scanning category row: %v\n", err)
return nil, err
}
categories = append(categories, category)
}
return categories, nil
}
11 changes: 11 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ type User struct {
Password string
}

type Category struct {
ID int `json:"id"`
Name string `json:"name"`
}

type CategoryAssignment struct {
ID int `json:"id"`
CategoryID int `json:"category_id"`
BlogID int `json:"blog_id"`
}

type BlogPost struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Expand Down

0 comments on commit e7414dd

Please sign in to comment.