diff --git a/pom.xml b/pom.xml index 163ad53..2177048 100644 --- a/pom.xml +++ b/pom.xml @@ -1,68 +1,109 @@ - + + 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.springframework.boot + spring-boot-starter-data-jpa + + + + org.postgresql + postgresql + + 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 +112,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/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 new file mode 100644 index 0000000..4c897c0 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -0,0 +1,20 @@ +package com.coded.spring.ordering.items + +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/items") +class ItemsController(private val itemsRepo: ItemsRepo) { + + @PostMapping + fun addItem(@RequestBody request: AddItemRequest) = + itemsRepo.save(Item(name = request.name, quantity = request.quantity)) + + @GetMapping + fun getAllItems() = itemsRepo.findAll() +} + +data class AddItemRequest( + val name: String, + 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 new file mode 100644 index 0000000..435e7d2 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepo.kt @@ -0,0 +1,7 @@ +package com.coded.spring.ordering.items + +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@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 new file mode 100644 index 0000000..f0f9070 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersController.kt @@ -0,0 +1,30 @@ +package com.coded.spring.ordering.orders +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/orders") +class OrdersController( + private val ordersRepo: OrdersRepo +) { + + @PostMapping + fun orderItems(@RequestBody request: OrderItemsRequest): Order { + return ordersRepo.save( + Order( + userId = request.user, + restaurant = request.restaurant, + items = request.items + ) + ) + } + + @GetMapping + fun getAllOrders(): List = + ordersRepo.findAllByOrderByCreatedAtAsc() +} + +data class OrderItemsRequest( + val user: Long, + val restaurant: String, + 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 new file mode 100644 index 0000000..220cfd5 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/orders/OrdersRepo.kt @@ -0,0 +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 + +@Repository +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 new file mode 100644 index 0000000..26d9d67 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -0,0 +1,19 @@ +package com.coded.spring.ordering.users + +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/users") +class UsersController(private val usersRepo: UsersRepo) { + + @PostMapping + fun createUser(@RequestBody request: CreateUserRequest) = + usersRepo.save(User(name = request.name)) + + @GetMapping + fun getAllUsers() = usersRepo.findAll() +} + +data class CreateUserRequest( + val name: 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 new file mode 100644 index 0000000..a811360 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepo.kt @@ -0,0 +1,7 @@ +package com.coded.spring.ordering.users + +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@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 3704dc6..b2548db 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,7 @@ -spring.application.name=Kotlin.SpringbootV2 +spring.datasource.url=jdbc:postgresql://localhost:5432/food_ordering +spring.datasource.username=postgres +spring.datasource.password=55866653 +spring.datasource.driver-class-name=org.postgresql.Driver + +spring.jpa.hibernate.ddl-auto=create +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect