Skip to content

Commit 774bcd6

Browse files
author
Peter Vestlin
committed
Lade till repo.
1 parent b0c3904 commit 774bcd6

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/main/kotlin/nu/westlin/kartrepo/Application.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package nu.westlin.kartrepo
22

3+
import org.springframework.boot.CommandLineRunner
34
import org.springframework.boot.SpringApplication
45
import org.springframework.boot.autoconfigure.SpringBootApplication
6+
import org.springframework.context.annotation.Bean
57

68
@SpringBootApplication
7-
open class Application
9+
open class Application {
10+
11+
@Bean
12+
open fun init(repository: KartRepository) = CommandLineRunner {
13+
repository.store(User("pwestlin", "Peter", "Westlin"))
14+
repository.store(User("awestlin", "Adam", "Westlin"))
15+
}
16+
17+
}
818

919
fun main(args: Array<String>) {
1020
SpringApplication.run(Application::class.java, *args)

src/main/kotlin/nu/westlin/kartrepo/KartController.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package nu.westlin.kartrepo
22

3+
import org.springframework.beans.factory.annotation.Autowired
34
import org.springframework.http.HttpStatus
45
import org.springframework.web.bind.annotation.*
56
import javax.servlet.http.HttpServletResponse
67

78
@RestController
8-
class KartController {
9+
class KartController @Autowired constructor(val kartRepository: KartRepository) {
10+
911

1012
@RequestMapping("/user")
1113
fun greeting(@RequestParam(value = "username", defaultValue = "pwestlin") username: String, response: HttpServletResponse): User {
12-
if (username == "pwestlin")
13-
return User(username, "Peter", "Westlin")
14-
else
14+
val user: User? = kartRepository.load(username)
15+
16+
if (user == null) {
1517
throw NotFoundException("User $username not found")
18+
}
19+
20+
return user
1621

1722
}
1823

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package nu.westlin.kartrepo
2+
3+
import org.springframework.stereotype.Repository
4+
5+
@Repository
6+
class KartRepository {
7+
8+
var users: MutableMap<String, User> = mutableMapOf()
9+
10+
fun store(user: User) {
11+
users.put(user.username, user)
12+
}
13+
14+
fun load(username: String) = users.get(username)
15+
}

0 commit comments

Comments
 (0)