Skip to content
Merged
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
@@ -0,0 +1,56 @@
package org.shangahi.sellio_backend.api.controller

import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
import org.shangahi.sellio_backend.api.dto.request.ProductRatingRequest
import org.shangahi.sellio_backend.api.dto.response.MessageResponse
import org.shangahi.sellio_backend.api.dto.response.PageResponse
import org.shangahi.sellio_backend.api.dto.response.ProductRatingResponse
import org.shangahi.sellio_backend.api.dto.response.ProductRatingSummaryResponse
import org.shangahi.sellio_backend.api.swagger.doc.ProductRatingDoc
import org.shangahi.sellio_backend.service.ProductRatingService
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
@RequestMapping("/v1/product-rating")
@Tag(name = "Product rating", description = "Endpoints for managing product ratings")
class ProductRatingController(
private val productRatingService: ProductRatingService
) {

@ProductRatingDoc.AddRating
@PostMapping("/{productId}")
fun addRating(
@PathVariable productId: UUID,
@Valid @RequestBody request: ProductRatingRequest
): MessageResponse {
return productRatingService.addRating(productId, request)
}

@ProductRatingDoc.GetProductRatings
@GetMapping("/{productId}")
fun getProductRatings(
@PathVariable productId: UUID,
pageable: Pageable
): PageResponse<ProductRatingResponse> {
return productRatingService.getProductRatings(productId, pageable)
}

@ProductRatingDoc.GetProductRatingSummary
@GetMapping("/{productId}/summary")
fun getProductRatingSummary(
@PathVariable productId: UUID
): ProductRatingSummaryResponse {
return productRatingService.getProductRatingSummary(productId)
}

@ProductRatingDoc.DeleteRating
@DeleteMapping("/{ratingId}")
fun deleteRating(
@PathVariable ratingId: UUID
) {
productRatingService.deleteRating(ratingId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.shangahi.sellio_backend.api.dto.request

import jakarta.validation.constraints.Max
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size

data class ProductRatingRequest(
@field:Min(1)
@field:Max(5)
val ratingValue: Int,

@field:NotBlank
@field:Size(max = 500)
val comment: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.shangahi.sellio_backend.api.dto.response

import java.time.Instant
import java.util.*

data class ProductRatingResponse(
val id: UUID,
val userName: String,
val userAvatarUrl: String?,
val ratingValue: Int,
val comment: String,
val createdAt: Instant,
val deletable: Boolean,
val editable: Boolean
)

data class ProductRatingSummaryResponse(
val productId: UUID,
val averageRating: Double,
val totalRatings: Long,
val ratingCategorize: Map<Int, Long>,
val canReview: Boolean,
val userReview: ProductRatingResponse?,
val recentReviews: List<ProductRatingResponse>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.shangahi.sellio_backend.api.mapper

import org.shangahi.sellio_backend.api.dto.response.ProductRatingResponse
import org.shangahi.sellio_backend.entity.ProductRating
import java.time.Instant

fun ProductRating.toResponse(isOwnRating: Boolean = false): ProductRatingResponse {
return ProductRatingResponse(
id = id!!,
userName = user.fullName,
userAvatarUrl = user.avatarUrl,
ratingValue = ratingValue,
comment = comment,
createdAt = createdAt ?: Instant.now(),
deletable = isOwnRating,
editable = isOwnRating
)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.shangahi.sellio_backend.api.swagger

import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.info.License
import io.swagger.v3.oas.models.security.SecurityRequirement
import io.swagger.v3.oas.models.security.SecurityScheme
import io.swagger.v3.oas.models.servers.Server
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -12,7 +15,20 @@ class OpenApiConfig {

@Bean
fun sellioOpenAPI(): OpenAPI {
val securitySchemeName = "bearerAuth"
return OpenAPI()
.addSecurityItem(SecurityRequirement().addList(securitySchemeName))
.components(
Components()
.addSecuritySchemes(
securitySchemeName,
SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
)
.info(
Info()
.title("Sellio Backend API")
Expand Down
Loading
Loading