diff --git a/blog-service/handlers.go b/blog-service/handlers.go index 1116096..a4d7e26 100644 --- a/blog-service/handlers.go +++ b/blog-service/handlers.go @@ -4,6 +4,7 @@ import ( "blog/contracttesting/db" "blog/contracttesting/models" "net/http" + "strconv" "github.com/gin-gonic/gin" ) @@ -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) +} \ No newline at end of file diff --git a/blog-service/main.go b/blog-service/main.go index a5a0fba..553b21d 100644 --- a/blog-service/main.go +++ b/blog-service/main.go @@ -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) diff --git a/db/get.go b/db/get.go index 392302e..a949559 100644 --- a/db/get.go +++ b/db/get.go @@ -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 } \ No newline at end of file diff --git a/models/models.go b/models/models.go index 4bdb9e2..53dff9d 100644 --- a/models/models.go +++ b/models/models.go @@ -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"`