95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package mapper
|
|
|
|
import (
|
|
"narasi-ahli-be/app/database/entity"
|
|
ebookRatingsResponse "narasi-ahli-be/app/module/ebooks/response"
|
|
)
|
|
|
|
func ToEbookRatingsResponse(rating *entity.EbookRatings) *ebookRatingsResponse.EbookRatingsResponse {
|
|
if rating == nil {
|
|
return nil
|
|
}
|
|
|
|
var userName, userEmail *string
|
|
if rating.User != nil {
|
|
if rating.IsAnonymous != nil && *rating.IsAnonymous {
|
|
userName = stringPtr("Anonymous")
|
|
userEmail = nil
|
|
} else {
|
|
userName = &rating.User.Fullname
|
|
userEmail = &rating.User.Email
|
|
}
|
|
}
|
|
|
|
var ebookTitle *string
|
|
if rating.Ebook != nil {
|
|
ebookTitle = &rating.Ebook.Title
|
|
}
|
|
|
|
return &ebookRatingsResponse.EbookRatingsResponse{
|
|
ID: rating.ID,
|
|
UserId: rating.UserId,
|
|
UserName: userName,
|
|
UserEmail: userEmail,
|
|
EbookId: rating.EbookId,
|
|
EbookTitle: ebookTitle,
|
|
PurchaseId: rating.PurchaseId,
|
|
Rating: rating.Rating,
|
|
Review: rating.Review,
|
|
IsAnonymous: rating.IsAnonymous,
|
|
IsVerified: rating.IsVerified,
|
|
StatusId: rating.StatusId,
|
|
IsActive: rating.IsActive,
|
|
CreatedAt: rating.CreatedAt,
|
|
UpdatedAt: rating.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
func ToEbookRatingsResponseList(ratings []*entity.EbookRatings) []*ebookRatingsResponse.EbookRatingsResponse {
|
|
if ratings == nil {
|
|
return nil
|
|
}
|
|
|
|
var responses []*ebookRatingsResponse.EbookRatingsResponse
|
|
for _, rating := range ratings {
|
|
responses = append(responses, ToEbookRatingsResponse(rating))
|
|
}
|
|
|
|
return responses
|
|
}
|
|
|
|
func ToEbookRatingSummaryResponse(ebookId uint, ebookTitle string, averageRating float64, totalRatings int, ratingCounts map[int]int, recentReviews []*entity.EbookRatings) *ebookRatingsResponse.EbookRatingSummaryResponse {
|
|
reviews := ToEbookRatingsResponseList(recentReviews)
|
|
var reviewsList []ebookRatingsResponse.EbookRatingsResponse
|
|
for _, review := range reviews {
|
|
reviewsList = append(reviewsList, *review)
|
|
}
|
|
|
|
return &ebookRatingsResponse.EbookRatingSummaryResponse{
|
|
EbookId: ebookId,
|
|
EbookTitle: ebookTitle,
|
|
AverageRating: averageRating,
|
|
TotalRatings: totalRatings,
|
|
RatingCounts: ratingCounts,
|
|
RecentReviews: reviewsList,
|
|
}
|
|
}
|
|
|
|
func ToEbookRatingStatsResponse(totalRatings int, averageRating float64, ratingCounts map[int]int) *ebookRatingsResponse.EbookRatingStatsResponse {
|
|
return &ebookRatingsResponse.EbookRatingStatsResponse{
|
|
TotalRatings: totalRatings,
|
|
AverageRating: averageRating,
|
|
RatingCounts: ratingCounts,
|
|
FiveStarCount: ratingCounts[5],
|
|
FourStarCount: ratingCounts[4],
|
|
ThreeStarCount: ratingCounts[3],
|
|
TwoStarCount: ratingCounts[2],
|
|
OneStarCount: ratingCounts[1],
|
|
}
|
|
}
|
|
|
|
// Helper function
|
|
func stringPtr(s string) *string {
|
|
return &s
|
|
}
|