Skip to content

Commit

Permalink
feat: add api to find urls by user id
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Park committed Jan 6, 2025
1 parent 31671df commit 07cd949
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<UrlResponse> {
return urlRepository.findAllByUserId(userId).map(::UrlResponse)
}
}
Original file line number Diff line number Diff line change
@@ -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!!,
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import org.springframework.data.jpa.repository.JpaRepository
interface UrlRepository : JpaRepository<Url, Long> {

fun findByHash(hash: String): Url?

fun findAllByUserId(userId: Long): List<Url>
}

0 comments on commit 07cd949

Please sign in to comment.