From 34cdb41e437ac6220fa5cf21fc27b81a8cb3f7f4 Mon Sep 17 00:00:00 2001 From: Sarah Date: Thu, 10 Apr 2025 19:56:15 +0300 Subject: [PATCH 1/4] Get an Order --- .../coded/spring/ordering/OnlineOrderController.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt diff --git a/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt b/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt new file mode 100644 index 0000000..4ac7a11 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt @@ -0,0 +1,11 @@ +package com.coded.spring.ordering + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class OnlineOrderController(){ + + @GetMapping("/order") + fun helloWorld() = "Happy Shopping!" +} \ No newline at end of file From 6f583a0a585111cbe1c7a0a38d4ea5e3f3e19f93 Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 14 Apr 2025 17:36:30 +0300 Subject: [PATCH 2/4] Post order with bonus of arranging orders --- pom.xml | 113 ++++++++++++------ .../spring/ordering/OnlineOrderController.kt | 11 -- .../spring/ordering/items/ItemsController.kt | 18 +++ .../coded/spring/ordering/items/ItemsRepo.kt | 20 ++++ .../ordering/orders/OrdersController.kt | 21 ++++ .../spring/ordering/orders/OrdersRepo.kt | 22 ++++ .../spring/ordering/users/UsersController.kt | 18 +++ .../coded/spring/ordering/users/UsersRepo.kt | 20 ++++ src/main/resources/application.properties | 2 +- 9 files changed, 194 insertions(+), 51 deletions(-) delete mode 100644 src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt diff --git a/pom.xml b/pom.xml index 163ad53..00292ad 100644 --- a/pom.xml +++ b/pom.xml @@ -1,68 +1,101 @@ - + + 4.0.0 + org.springframework.boot spring-boot-starter-parent 3.4.4 - com.coded.spring - Ordering + + com.coded.spring.ordering + food-ordering 0.0.1-SNAPSHOT - Kotlin.SpringbootV2 - Kotlin.SpringbootV2 - - - - - - - - - - - - - + Online Food Ordering System + My first project + - 21 - 1.9.25 + 23 + 2.1.20 + + org.springframework.boot spring-boot-starter-web + + com.fasterxml.jackson.module jackson-module-kotlin + + org.jetbrains.kotlin kotlin-reflect + + org.jetbrains.kotlin - kotlin-stdlib + kotlin-stdlib-jdk8 + ${kotlin.version} + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + 2.1.214 + + + + + jakarta.inject + jakarta.inject-api + + + org.springframework.boot spring-boot-starter-test test + + org.jetbrains.kotlin kotlin-test-junit5 test + + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + - ${project.basedir}/src/main/kotlin - ${project.basedir}/src/test/kotlin + src/main/kotlin + src/test/kotlin org.springframework.boot @@ -71,21 +104,23 @@ org.jetbrains.kotlin kotlin-maven-plugin - - - -Xjsr305=strict - - - spring - - - - - org.jetbrains.kotlin - kotlin-maven-allopen - ${kotlin.version} - - + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + diff --git a/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt b/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt deleted file mode 100644 index 4ac7a11..0000000 --- a/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.coded.spring.ordering - -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RestController - -@RestController -class OnlineOrderController(){ - - @GetMapping("/order") - fun helloWorld() = "Happy Shopping!" -} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt new file mode 100644 index 0000000..d7de7bf --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -0,0 +1,18 @@ +package com.coded.spring.ordering.items + +import org.springframework.web.bind.annotation.* + +@RestController +class ItemsController(private val itemsRepository: ItemsRepo) { + + @PostMapping("/com/coded/spring/ordering/items") + fun addItem(@RequestBody request: AddItemRequest) = itemsRepository.save(Item(name = request.name, price = request.price)) + + @GetMapping("/com/coded/spring/ordering/items") + fun getAllItems() = itemsRepository.findAll() +} + +data class AddItemRequest( + val name: String, + val price: Double +) diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt new file mode 100644 index 0000000..5ae6f91 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt @@ -0,0 +1,20 @@ +package com.coded.spring.ordering.items + +import jakarta.inject.Named +import jakarta.persistence.* +import org.springframework.data.jpa.repository.JpaRepository + +@Named +interface ItemsRepo : JpaRepository + +@Entity +@Table(name = "items") +data class Item( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var name: String, + var price: Double +) { + constructor() : this(null, "", 0.0) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt new file mode 100644 index 0000000..126c9e7 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt @@ -0,0 +1,21 @@ +package com.coded.spring.ordering.orders + +import org.springframework.web.bind.annotation.* + +@RestController + +class OrdersController(private val ordersRepository: OrdersRepo) { + + @GetMapping("/orders") + fun getAllOrders() = ordersRepository.findAll() + + @PostMapping("/orders") + fun orderItems(@RequestBody request: OrderItemsRequest) = + ordersRepository.save(Order(username = request.user, restaurant = request.restaurant, items = request.items.toString())) +} + +data class OrderItemsRequest( + val user: String, + var restaurant: String, + var items: List +) diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt new file mode 100644 index 0000000..12ce8c5 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt @@ -0,0 +1,22 @@ +package com.coded.spring.ordering.orders + +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository +import jakarta.persistence.* + +@Repository +interface OrdersRepo : JpaRepository + +@Entity +@Table(name = "orders") +data class Order( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var username: String, + var restaurant: String, + + var items: String +) { + constructor() : this(null, "", "", "") +} diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt new file mode 100644 index 0000000..6c72e48 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -0,0 +1,18 @@ +package com.coded.spring.ordering.users + +import org.springframework.web.bind.annotation.* + +@RestController +class UsersController(private val usersRepository: UsersRepo) { + + @PostMapping("/com/coded/spring/ordering/users") + fun createUser(@RequestBody request: CreateUserRequest) = usersRepository.save(User(name = request.name, email = request.email)) + + @GetMapping("/com/coded/spring/ordering/users") + fun getAllUsers() = usersRepository.findAll() +} + +data class CreateUserRequest( + val name: String, + val email: String +) diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt new file mode 100644 index 0000000..a90a06a --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt @@ -0,0 +1,20 @@ +package com.coded.spring.ordering.users + +import jakarta.inject.Named +import jakarta.persistence.* +import org.springframework.data.jpa.repository.JpaRepository + +@Named +interface UsersRepo : JpaRepository + +@Entity +@Table(name = "users") +data class User( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var name: String, + var email: String +) { + constructor() : this(null, "", "") +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3704dc6..4c00e40 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ -spring.application.name=Kotlin.SpringbootV2 +server.port=8080 From 5056524087d09dbb0372cb23ed2a09df2f04944e Mon Sep 17 00:00:00 2001 From: Sarah Date: Mon, 14 Apr 2025 23:56:09 +0300 Subject: [PATCH 3/4] fixed some errors --- pom.xml | 8 ++++++++ .../spring/ordering/items/ItemsController.kt | 2 +- .../coded/spring/ordering/items/ItemsRepo.kt | 2 +- .../spring/ordering/orders/OrdersController.kt | 17 ++++++++++++----- .../coded/spring/ordering/orders/OrdersRepo.kt | 8 ++++++-- .../spring/ordering/users/UsersController.kt | 1 + .../coded/spring/ordering/users/UsersRepo.kt | 2 +- src/main/resources/application.properties | 5 +++++ 8 files changed, 35 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 00292ad..2177048 100644 --- a/pom.xml +++ b/pom.xml @@ -42,7 +42,15 @@ org.jetbrains.kotlin kotlin-reflect + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.postgresql + postgresql + org.jetbrains.kotlin diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt index d7de7bf..38b68b5 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -15,4 +15,4 @@ class ItemsController(private val itemsRepository: ItemsRepo) { data class AddItemRequest( val name: String, val price: Double -) +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt index 5ae6f91..e03c431 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt @@ -17,4 +17,4 @@ data class Item( var price: Double ) { constructor() : this(null, "", 0.0) -} +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt index 126c9e7..ce93075 100644 --- a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt @@ -1,21 +1,28 @@ package com.coded.spring.ordering.orders import org.springframework.web.bind.annotation.* +import java.time.LocalDateTime +import org.springframework.data.domain.Sort @RestController - class OrdersController(private val ordersRepository: OrdersRepo) { @GetMapping("/orders") - fun getAllOrders() = ordersRepository.findAll() + fun getAllOrders() = ordersRepository.findAll(Sort.by(Sort.Direction.ASC, "createdAt")) @PostMapping("/orders") fun orderItems(@RequestBody request: OrderItemsRequest) = - ordersRepository.save(Order(username = request.user, restaurant = request.restaurant, items = request.items.toString())) + ordersRepository.save( + Order( + username = request.user, + restaurant = request.restaurant, + items = request.items.joinToString(", ") + ) + ) } data class OrderItemsRequest( val user: String, - var restaurant: String, - var items: List + val restaurant: String, + val items: List ) diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt index 12ce8c5..564dd09 100644 --- a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt @@ -3,6 +3,7 @@ package com.coded.spring.ordering.orders import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository import jakarta.persistence.* +import java.time.LocalDateTime @Repository interface OrdersRepo : JpaRepository @@ -13,10 +14,13 @@ data class Order( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, + var username: String, var restaurant: String, + var items: String, - var items: String + @Column(name = "created_at", nullable = false) + var createdAt: LocalDateTime = LocalDateTime.now() ) { - constructor() : this(null, "", "", "") + constructor() : this(null, "", "", "", LocalDateTime.now()) } diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt index 6c72e48..b106ed0 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -1,3 +1,4 @@ + package com.coded.spring.ordering.users import org.springframework.web.bind.annotation.* diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt index a90a06a..b1df5fc 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt @@ -17,4 +17,4 @@ data class User( var email: String ) { constructor() : this(null, "", "") -} +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4c00e40..7761f2f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ server.port=8080 +spring.datasource.url=jdbc:postgresql://localhost:5432/food_ordering +spring.datasource.username=postgres +spring.datasource.password=55866653 +spring.jpa.hibernate.ddl-auto=update +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect From 8eeab2dedfbf9d9bac2cedd99278aa18f454e65c Mon Sep 17 00:00:00 2001 From: Sarah Date: Thu, 17 Apr 2025 18:29:15 +0300 Subject: [PATCH 4/4] combined all 3 tasks with the welcome page and fixed some minor errors --- .../coded/spring/ordering/HomeController.kt | 12 +++++++ .../com/coded/spring/ordering/items/Item.kt | 12 +++++++ .../spring/ordering/items/ItemsController.kt | 14 ++++---- .../coded/spring/ordering/items/ItemsRepo.kt | 19 ++--------- .../com/coded/spring/ordering/orders/Order.kt | 19 +++++++++++ .../ordering/orders/OrdersController.kt | 30 +++++++++-------- .../spring/ordering/orders/OrdersRepo.kt | 32 +++++++------------ .../com/coded/spring/ordering/users/User.kt | 15 +++++++++ .../spring/ordering/users/UsersController.kt | 16 +++++----- .../coded/spring/ordering/users/UsersRepo.kt | 19 ++--------- src/main/resources/application.properties | 5 +-- 11 files changed, 111 insertions(+), 82 deletions(-) create mode 100644 src/main/kotlin/com/coded/spring/ordering/HomeController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/items/Item.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/orders/Order.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/users/User.kt diff --git a/src/main/kotlin/com/coded/spring/ordering/HomeController.kt b/src/main/kotlin/com/coded/spring/ordering/HomeController.kt new file mode 100644 index 0000000..7221170 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/HomeController.kt @@ -0,0 +1,12 @@ +package com.coded.spring.ordering +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class HomeController { + + @GetMapping("/") + fun welcome(): String { + return "We don't want to keep you hungry, order now!" + } +} diff --git a/src/main/kotlin/com/coded/spring/ordering/items/Item.kt b/src/main/kotlin/com/coded/spring/ordering/items/Item.kt new file mode 100644 index 0000000..cdf82f2 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/Item.kt @@ -0,0 +1,12 @@ +package com.coded.spring.ordering.items + +import jakarta.persistence.* + +@Entity +@Table(name = "items") +data class Item( + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var name: String, + var quantity: Int +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt index 38b68b5..4c897c0 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -3,16 +3,18 @@ package com.coded.spring.ordering.items import org.springframework.web.bind.annotation.* @RestController -class ItemsController(private val itemsRepository: ItemsRepo) { +@RequestMapping("/items") +class ItemsController(private val itemsRepo: ItemsRepo) { - @PostMapping("/com/coded/spring/ordering/items") - fun addItem(@RequestBody request: AddItemRequest) = itemsRepository.save(Item(name = request.name, price = request.price)) + @PostMapping + fun addItem(@RequestBody request: AddItemRequest) = + itemsRepo.save(Item(name = request.name, quantity = request.quantity)) - @GetMapping("/com/coded/spring/ordering/items") - fun getAllItems() = itemsRepository.findAll() + @GetMapping + fun getAllItems() = itemsRepo.findAll() } data class AddItemRequest( val name: String, - val price: Double + val quantity: Int ) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt index e03c431..435e7d2 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt @@ -1,20 +1,7 @@ package com.coded.spring.ordering.items -import jakarta.inject.Named -import jakarta.persistence.* import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository -@Named -interface ItemsRepo : JpaRepository - -@Entity -@Table(name = "items") -data class Item( - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - var id: Long? = null, - var name: String, - var price: Double -) { - constructor() : this(null, "", 0.0) -} \ No newline at end of file +@Repository +interface ItemsRepo : JpaRepository \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/Order.kt b/src/main/kotlin/com/coded/spring/ordering/orders/Order.kt new file mode 100644 index 0000000..065394f --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/orders/Order.kt @@ -0,0 +1,19 @@ +package com.coded.spring.ordering.orders +import jakarta.persistence.* + +@Entity +@Table(name = "orders") +data class Order( + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + + var userId: Long, + var restaurant: String, + var items: String, + var createdAt: Long = System.currentTimeMillis() + +) { + constructor() : this(id = null, userId = 0L, restaurant = "", items = "", createdAt = 0L) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt index ce93075..f0f9070 100644 --- a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt @@ -1,28 +1,30 @@ package com.coded.spring.ordering.orders - import org.springframework.web.bind.annotation.* -import java.time.LocalDateTime -import org.springframework.data.domain.Sort @RestController -class OrdersController(private val ordersRepository: OrdersRepo) { - - @GetMapping("/orders") - fun getAllOrders() = ordersRepository.findAll(Sort.by(Sort.Direction.ASC, "createdAt")) +@RequestMapping("/orders") +class OrdersController( + private val ordersRepo: OrdersRepo +) { - @PostMapping("/orders") - fun orderItems(@RequestBody request: OrderItemsRequest) = - ordersRepository.save( + @PostMapping + fun orderItems(@RequestBody request: OrderItemsRequest): Order { + return ordersRepo.save( Order( - username = request.user, + userId = request.user, restaurant = request.restaurant, - items = request.items.joinToString(", ") + items = request.items ) ) + } + + @GetMapping + fun getAllOrders(): List = + ordersRepo.findAllByOrderByCreatedAtAsc() } data class OrderItemsRequest( - val user: String, + val user: Long, val restaurant: String, - val items: List + val items: String ) diff --git a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt index 564dd09..220cfd5 100644 --- a/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt @@ -1,26 +1,18 @@ +//package com.coded.spring.ordering.orders +// +//import org.springframework.data.jpa.repository.JpaRepository +//import org.springframework.stereotype.Repository +// +//@Repository +// interface OrdersRepo : JpaRepository { +// fun findAllByOrderByCreatedAtAsc(): List +// } package com.coded.spring.ordering.orders import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository -import jakarta.persistence.* -import java.time.LocalDateTime @Repository -interface OrdersRepo : JpaRepository - -@Entity -@Table(name = "orders") -data class Order( - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - var id: Long? = null, - - var username: String, - var restaurant: String, - var items: String, - - @Column(name = "created_at", nullable = false) - var createdAt: LocalDateTime = LocalDateTime.now() -) { - constructor() : this(null, "", "", "", LocalDateTime.now()) -} +interface OrdersRepo : JpaRepository { + fun findAllByOrderByCreatedAtAsc(): List +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/users/User.kt b/src/main/kotlin/com/coded/spring/ordering/users/User.kt new file mode 100644 index 0000000..8d519e6 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/User.kt @@ -0,0 +1,15 @@ +package com.coded.spring.ordering.users + +import jakarta.persistence.* + +@Entity +@Table(name = "users") +data class User( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long = 0, + + val name: String = "" +) { + constructor() : this(0, "") +} diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt index b106ed0..26d9d67 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -1,19 +1,19 @@ - package com.coded.spring.ordering.users import org.springframework.web.bind.annotation.* @RestController -class UsersController(private val usersRepository: UsersRepo) { +@RequestMapping("/users") +class UsersController(private val usersRepo: UsersRepo) { - @PostMapping("/com/coded/spring/ordering/users") - fun createUser(@RequestBody request: CreateUserRequest) = usersRepository.save(User(name = request.name, email = request.email)) + @PostMapping + fun createUser(@RequestBody request: CreateUserRequest) = + usersRepo.save(User(name = request.name)) - @GetMapping("/com/coded/spring/ordering/users") - fun getAllUsers() = usersRepository.findAll() + @GetMapping + fun getAllUsers() = usersRepo.findAll() } data class CreateUserRequest( val name: String, - val email: String -) +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt index b1df5fc..a811360 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt @@ -1,20 +1,7 @@ package com.coded.spring.ordering.users -import jakarta.inject.Named -import jakarta.persistence.* import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository -@Named -interface UsersRepo : JpaRepository - -@Entity -@Table(name = "users") -data class User( - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - var id: Long? = null, - var name: String, - var email: String -) { - constructor() : this(null, "", "") -} \ No newline at end of file +@Repository +interface UsersRepo : JpaRepository \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7761f2f..b2548db 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,7 @@ -server.port=8080 spring.datasource.url=jdbc:postgresql://localhost:5432/food_ordering spring.datasource.username=postgres spring.datasource.password=55866653 -spring.jpa.hibernate.ddl-auto=update +spring.datasource.driver-class-name=org.postgresql.Driver + +spring.jpa.hibernate.ddl-auto=create spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect