-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add api to find urls by user id
- Loading branch information
Peter Park
committed
Jan 6, 2025
1 parent
31671df
commit 07cd949
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/bgpark/urlshortener/controller/UserController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/bgpark/urlshortener/controller/dto/UrlResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!, | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters