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

feat: get user testimonials #393

Open
wants to merge 2 commits into
base: dev
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
26 changes: 26 additions & 0 deletions pkg/controller/testimonial/testimonial.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,29 @@ func (base *Controller) Create(c *gin.Context) {
c.JSON(http.StatusCreated, rd)

}


func (base *Controller) GetUserTestimonials(c *gin.Context) {
userId := c.Param("user_id")
if userId == "" {
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "user_id parameter is required", "failed to fetch testimonials", nil)
c.JSON(http.StatusBadRequest, rd)
return
}

if base.TestimonialService == nil {
rd := utility.BuildErrorResponse(http.StatusInternalServerError, "error", "Testimonial service is not initialized", "failed to fetch testimonials", nil)
c.JSON(http.StatusInternalServerError, rd)
return
}

testimonials, err := base.TestimonialService.GetUserTestimonials(userId)
if err != nil {
rd := utility.BuildErrorResponse(http.StatusInternalServerError, "error", err.Error(), "failed to fetch testimonials", nil)
c.JSON(http.StatusInternalServerError, rd)
return
}

rd := utility.BuildSuccessResponse(http.StatusOK, "User testimonials retrieved successfully", testimonials)
c.JSON(http.StatusOK, rd)
}
6 changes: 6 additions & 0 deletions pkg/router/testimonial.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ func Testimonial(r *gin.Engine, ApiVersion string, validator *validator.Validate
{
squeezeURL.POST("/testimonials", controller.Create)
}

publicGroup := r.Group(fmt.Sprintf("%v", ApiVersion))
{
publicGroup.GET("/testimonials/user/:user_id", controller.GetUserTestimonials)
}

return r
}
15 changes: 15 additions & 0 deletions services/testimonial/testimonial.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type TestimonialService interface {
CreateTestimonial(req models.TestimonialReq, userId string) (*models.Testimonial, error)
GetUserTestimonials(userID string) ([]models.Testimonial, error)
}

type testimonialService struct {
Expand Down Expand Up @@ -37,3 +38,17 @@ func (s *testimonialService) CreateTestimonial(req models.TestimonialReq, userId
return testimonial, nil

}

func (s *testimonialService) GetUserTestimonials(userID string) ([]models.Testimonial, error) {
var testimonials []models.Testimonial

pdb := s.db

if err := pdb.Where("user_id = ?", userID).Find(&testimonials).Error; err != nil {
return nil, err
}

return testimonials, nil
}


1 change: 1 addition & 0 deletions tests/test_testimonial/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ func SetupTestimonialRoutes(r *gin.Engine, testimonialController *testimonial.Co
middleware.Authorize(testimonialController.Db.Postgresql.DB(), models.RoleIdentity.User),
testimonialController.Create,
)
r.GET("/api/v1/testimonials/user/:user_id", testimonialController.GetUserTestimonials)
}
69 changes: 69 additions & 0 deletions tests/test_testimonial/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package test_testimonial

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/controller/auth"
"github.com/hngprojects/hng_boilerplate_golang_web/tests"
"github.com/hngprojects/hng_boilerplate_golang_web/utility"
)

func TestGetUserTestimonials(t *testing.T) {
setup := func() (*gin.Engine, *auth.Controller) {
router, testimonialController := SetupTestimonialTestRouter()
authController := auth.Controller{
Db: testimonialController.Db,
Validator: testimonialController.Validator,
Logger: testimonialController.Logger,
}
return router, &authController
}

_, testimonialController := SetupTestimonialTestRouter()
db := testimonialController.Db.Postgresql

user := models.User{
ID: utility.GenerateUUID(),
Name: "Test User",
Email: fmt.Sprintf("user_%[email protected]", utility.RandomString(6)),
Password: "hashedpassword",
Role: int(models.RoleIdentity.User),
}

result := db.DB().Create(&user)
if result.Error != nil {
t.Fatalf(" Failed to create test user: %v", result.Error)
}

testimonials := []models.Testimonial{
{ID: utility.GenerateUUID(), UserID: user.ID, Content: "Testimonial 1", CreatedAt: time.Now(), UpdatedAt: time.Now()},
{ID: utility.GenerateUUID(), UserID: user.ID, Content: "Testimonial 2", CreatedAt: time.Now(), UpdatedAt: time.Now()},
}
for _, testimonial := range testimonials {
result := db.DB().Create(&testimonial)
if result.Error != nil {
t.Fatalf(" Failed to create testimonial: %v", result.Error)
}
}

t.Run("Successful Get User Testimonials", func(t *testing.T) {
router, _ := setup()

url := fmt.Sprintf("/api/v1/testimonials/user/%s", user.ID)
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Content-Type", "application/json")

resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)

tests.AssertStatusCode(t, resp.Code, http.StatusOK)
response := tests.ParseResponse(resp)
tests.AssertResponseMessage(t, response["message"].(string), "User testimonials retrieved successfully")
})
}
Loading