From 07cd9490d537d9fd1cefc8594ac5dfcb09a82bb8 Mon Sep 17 00:00:00 2001 From: Peter Park Date: Mon, 6 Jan 2025 01:24:46 +0100 Subject: [PATCH] feat: add api to find urls by user id --- .../urlshortener/controller/UserController.kt | 21 ++++++++++++++++ .../controller/dto/UrlResponse.kt | 25 +++++++++++++++++++ .../repository/db/UrlRepository.kt | 2 ++ 3 files changed, 48 insertions(+) create mode 100644 src/main/kotlin/com/bgpark/urlshortener/controller/UserController.kt create mode 100644 src/main/kotlin/com/bgpark/urlshortener/controller/dto/UrlResponse.kt diff --git a/src/main/kotlin/com/bgpark/urlshortener/controller/UserController.kt b/src/main/kotlin/com/bgpark/urlshortener/controller/UserController.kt new file mode 100644 index 0000000..a6d59df --- /dev/null +++ b/src/main/kotlin/com/bgpark/urlshortener/controller/UserController.kt @@ -0,0 +1,21 @@ +package com.bgpark.urlshortener.controller + +import com.bgpark.urlshortener.controller.dto.UrlResponse +import com.bgpark.urlshortener.repository.db.UrlRepository +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestController + +@RestController +class UserController( + private val urlRepository: UrlRepository +) { + + @GetMapping("/api/v1/users/{userId}/urls") + @ResponseStatus(HttpStatus.OK) + fun getUrlByUserId(@PathVariable("userId") userId: Long): List { + return urlRepository.findAllByUserId(userId).map(::UrlResponse) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/bgpark/urlshortener/controller/dto/UrlResponse.kt b/src/main/kotlin/com/bgpark/urlshortener/controller/dto/UrlResponse.kt new file mode 100644 index 0000000..7130f54 --- /dev/null +++ b/src/main/kotlin/com/bgpark/urlshortener/controller/dto/UrlResponse.kt @@ -0,0 +1,25 @@ +package com.bgpark.urlshortener.controller.dto + +import com.bgpark.urlshortener.domain.Url +import java.time.LocalDateTime + +data class UrlResponse( + val id: Long, + val longUrl: String, + val shortUrl: String, + val hash: String, + val userId: Long?, + val createdAt: LocalDateTime, + val updatedAt: LocalDateTime +) { + constructor(url: Url): this( + id = url.id, + longUrl = url.longUrl, + shortUrl = url.shortUrl, + hash = url.hash, + userId = url.userId, + createdAt = url.createdAt!!, + updatedAt = url.updatedAt!!, + ) + +} \ No newline at end of file diff --git a/src/main/kotlin/com/bgpark/urlshortener/repository/db/UrlRepository.kt b/src/main/kotlin/com/bgpark/urlshortener/repository/db/UrlRepository.kt index fa91eaf..6f89c37 100644 --- a/src/main/kotlin/com/bgpark/urlshortener/repository/db/UrlRepository.kt +++ b/src/main/kotlin/com/bgpark/urlshortener/repository/db/UrlRepository.kt @@ -6,4 +6,6 @@ import org.springframework.data.jpa.repository.JpaRepository interface UrlRepository : JpaRepository { fun findByHash(hash: String): Url? + + fun findAllByUserId(userId: Long): List } \ No newline at end of file