Skip to content

Commit

Permalink
fix: use correct dto for skillab api
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwwinter committed Nov 18, 2024
1 parent 25a1835 commit 863d2c4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.aamdigital.aamexternalmockservice.skillab.controller
import com.aamdigital.aamexternalmockservice.skillab.error.SkillLabError
import com.aamdigital.aamexternalmockservice.skillab.error.SkillLabErrorResponseDto
import com.aamdigital.aamexternalmockservice.skillab.repository.ProfileCrudRepository
import com.aamdigital.aamexternalmockservice.skillab.repository.ProfileEntity
import com.aamdigital.aamexternalmockservice.skillab.repository.ProfilePagingRepository
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.data.domain.Pageable
Expand All @@ -16,59 +15,78 @@ import org.springframework.web.bind.annotation.RestController
import java.util.*

// SkillLab API
data class UserProfileResponse(
val pagination: PaginationDto,
val results: List<ProfileEntity>,
data class UserProfilesResponse(
val pagination: PaginationDto,
val results: List<ProfileIdDto>,
)

// SkillLab API
data class ProfileIdDto(
val id: String,
)

// SkillLab API
data class InvalidPage(
val error: String,
)

// SkillLab API
data class PaginationDto(
val currentPage: Int,
val perPage: Int,
val totalEntries: Int,
val currentPage: Int,
val perPage: Int,
val totalEntries: Int,
)

@RestController
@RequestMapping("/skilllab")
class SkillLabController(
val objectMapper: ObjectMapper,
val profilePagingRepository: ProfilePagingRepository,
val profileCrudRepository: ProfileCrudRepository,
val objectMapper: ObjectMapper,
val profilePagingRepository: ProfilePagingRepository,
val profileCrudRepository: ProfileCrudRepository,
) {

// SkillLab API
@GetMapping("/profiles")
fun getProfiles(
currentPage: Int = 0,
perPage: Int = 100,
): ResponseEntity<UserProfileResponse> {
val pageable = Pageable.ofSize(perPage).withPage(currentPage)
return UserProfileResponse(
pagination = PaginationDto(
currentPage = pageable.pageNumber,
perPage = pageable.pageSize,
totalEntries = profileCrudRepository.count().toInt(),
),
results = profilePagingRepository.findAll(pageable).toList(),
).let { ResponseEntity.ok(it) }
// SkillLab API
@GetMapping("/profiles")
fun getProfiles(
page: Int = 1,
perPage: Int = 100,
): ResponseEntity<Any> {
if (page < 1) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(InvalidPage(error = "The page you were looking for doesn't exist (404)"))
}

// SkillLab API
@GetMapping("/profiles/{profileId}")
fun getProfile(
@PathVariable profileId: UUID,
): ResponseEntity<Any> {
return profileCrudRepository.findById(profileId).let { ResponseEntity.ok(it) }
}
val pageable = Pageable.ofSize(perPage).withPage(page - 1)
return UserProfilesResponse(
pagination = PaginationDto(
currentPage = pageable.pageNumber + 1,
perPage = pageable.pageSize,
totalEntries = profileCrudRepository.count().toInt(),
),
results = profilePagingRepository.findAll(pageable).map {
ProfileIdDto(
id = it.id.toString()
)
}.toList(),
).let { ResponseEntity.ok(it) }
}

@RequestMapping("/**")
fun handleInvalidPaths(): ResponseEntity<Any> {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
objectMapper.writeValueAsString(
SkillLabErrorResponseDto(
error = SkillLabError.NotFound()
)
)
// SkillLab API
@GetMapping("/profiles/{profileId}")
fun getProfile(
@PathVariable profileId: UUID,
): ResponseEntity<Any> {
return profileCrudRepository.findById(profileId).let { ResponseEntity.ok(it) }
}

@RequestMapping("/**")
fun handleInvalidPaths(): ResponseEntity<Any> {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
objectMapper.writeValueAsString(
SkillLabErrorResponseDto(
error = SkillLabError.NotFound()
)
}
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import java.util.*
import kotlin.random.Random

@RestController
@RequestMapping("/skilllab/faker")
class FakerController(
@RequestMapping("/skilllab/test-data")
class TestDataController(
val profileCrudRepository: ProfileCrudRepository,
val skillCrudRepository: SkillCrudRepository,
) {
Expand All @@ -30,7 +30,6 @@ class FakerController(
numberOfEntities: Int = 1000,
): ResponseEntity<Any> {
val entities = mutableListOf<SkillEntity>()
val faker = Faker()

for (i in 1..numberOfEntities) {
val entity = getSkill()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ server:
error:
include-message: always
include-binding-errors: always
port: 9100
port: 9005

logging:
level:
Expand Down

0 comments on commit 863d2c4

Please sign in to comment.