From a149c3e48692fc84b03b9127dd04c74a942bf465 Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Tue, 8 Apr 2025 20:38:06 +0300 Subject: [PATCH 1/7] Post Done to postgres without bonus --- pom.xml | 18 ++++++++++++- .../spring/ordering/OnlineOrderController.kt | 25 ++++++++++++++++++ .../coded/spring/ordering/OrdersRepository.kt | 22 ++++++++++++++++ .../coded/spring/ordering/OrdersService.kt | 26 +++++++++++++++++++ src/main/resources/application.properties | 4 +++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/OrdersService.kt diff --git a/pom.xml b/pom.xml index 163ad53..e18821a 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,11 @@ org.springframework.boot spring-boot-starter-web + + org.postgresql + postgresql + + com.fasterxml.jackson.module jackson-module-kotlin @@ -47,7 +52,18 @@ org.jetbrains.kotlin kotlin-stdlib - + + jakarta.inject + jakarta.inject-api + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + org.springframework.boot spring-boot-starter-test 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..a849e14 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt @@ -0,0 +1,25 @@ +package com.coded.spring.ordering + + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RestController + +@RestController +class OnlineOrderController( + private val ordersRepository: OrdersRepository +){ + @GetMapping("/home") + fun onlineOrder() = "Start Ordering Food!" + + @PostMapping("/orders") + fun orderFood(@RequestBody request: OrderFoodRequest) = ordersRepository.save(OrderEntity(name = request.name, restaurant=request.restaurant, items=request.items)) + +} + +data class OrderFoodRequest( + val name: String, + val restaurant: String, + val items: Array +) diff --git a/src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt new file mode 100644 index 0000000..f19bc8b --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt @@ -0,0 +1,22 @@ +package com.coded.spring.ordering + + +import jakarta.inject.Named +import jakarta.persistence.* +import org.springframework.data.jpa.repository.JpaRepository + +@Named +interface OrdersRepository : JpaRepository + +@Entity +@Table(name = "orders") +data class OrderEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var name: String, + var restaurant:String, + var items:Array +){ + constructor() : this(null, "","", emptyArray()) +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/OrdersService.kt b/src/main/kotlin/com/coded/spring/ordering/OrdersService.kt new file mode 100644 index 0000000..b02d14c --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/OrdersService.kt @@ -0,0 +1,26 @@ +package com.coded.spring.ordering + + + +import jakarta.inject.Named + +@Named +class UsersService( + private val ordersRepository: OrdersRepository, +) { + + fun listUsers(): List = ordersRepository.findAll().map { + Order( + name = it.name, + restaurant = it.restaurant, + items=it.items + + ) + } +} + +data class Order( + val name: String, + val restaurant: String, + val items: Array +) \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3704dc6..06c8834 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,5 @@ spring.application.name=Kotlin.SpringbootV2 +spring.datasource.url=jdbc:postgresql://localhost:5433/onlineOrderingDatabase +spring.datasource.username=postgres +spring.datasource.password=ilovedjango# +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect From 38f1c17198f6c83ed02b91a2d5901bce29e257d0 Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Fri, 11 Apr 2025 12:47:29 +0300 Subject: [PATCH 2/7] Done relationship between users and orders --- .../{ => Orders}/OnlineOrderController.kt | 17 ++++++---- .../ordering/{ => Orders}/OrdersRepository.kt | 19 +++++++---- .../spring/ordering/Orders/OrdersService.kt | 33 +++++++++++++++++++ .../coded/spring/ordering/OrdersService.kt | 26 --------------- .../spring/ordering/items/ItemsController.kt | 2 ++ .../spring/ordering/items/ItemsRepository.kt | 19 +++++++++++ .../spring/ordering/users/UserService.kt | 22 +++++++++++++ .../spring/ordering/users/UsersController.kt | 14 ++++++++ .../spring/ordering/users/UsersRepository.kt | 20 +++++++++++ 9 files changed, 133 insertions(+), 39 deletions(-) rename src/main/kotlin/com/coded/spring/ordering/{ => Orders}/OnlineOrderController.kt (52%) rename src/main/kotlin/com/coded/spring/ordering/{ => Orders}/OrdersRepository.kt (51%) create mode 100644 src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt delete mode 100644 src/main/kotlin/com/coded/spring/ordering/OrdersService.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/ItemsRepository.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/users/UserService.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/UsersRepository.kt diff --git a/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt similarity index 52% rename from src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt rename to src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt index a849e14..28323f6 100644 --- a/src/main/kotlin/com/coded/spring/ordering/OnlineOrderController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt @@ -1,4 +1,4 @@ -package com.coded.spring.ordering +package com.coded.spring.ordering.Orders import org.springframework.web.bind.annotation.GetMapping @@ -8,18 +8,21 @@ import org.springframework.web.bind.annotation.RestController @RestController class OnlineOrderController( - private val ordersRepository: OrdersRepository + private val ordersService: OrdersService, + ){ @GetMapping("/home") fun onlineOrder() = "Start Ordering Food!" + @GetMapping("/orders/v1/orders") + fun getOrders(): List = ordersService.listOrders() + @PostMapping("/orders") - fun orderFood(@RequestBody request: OrderFoodRequest) = ordersRepository.save(OrderEntity(name = request.name, restaurant=request.restaurant, items=request.items)) + fun orderFood(@RequestBody request: CreateOrderRequest) = ordersService.createOrder(request.userId) } -data class OrderFoodRequest( - val name: String, - val restaurant: String, - val items: Array +data class CreateOrderRequest( + val userId: Long + ) diff --git a/src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt similarity index 51% rename from src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt rename to src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt index f19bc8b..9e9cd33 100644 --- a/src/main/kotlin/com/coded/spring/ordering/OrdersRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt @@ -1,6 +1,8 @@ -package com.coded.spring.ordering +package com.coded.spring.ordering.Orders +import com.coded.spring.ordering.items.ItemEntity +import com.coded.spring.ordering.users.UserEntity import jakarta.inject.Named import jakarta.persistence.* import org.springframework.data.jpa.repository.JpaRepository @@ -14,9 +16,14 @@ data class OrderEntity( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, - var name: String, - var restaurant:String, - var items:Array -){ - constructor() : this(null, "","", emptyArray()) + + @ManyToOne + val user: UserEntity, + +// @OneToMany(mappedBy = "order_id") +// val items: List? = null + + + ){ + constructor() : this(null, UserEntity()) } \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt new file mode 100644 index 0000000..0f6adb0 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt @@ -0,0 +1,33 @@ +package com.coded.spring.ordering.Orders + + + +import com.coded.spring.ordering.users.UserEntity +import jakarta.inject.Named +import com.coded.spring.ordering.users.UsersRepository + +@Named +class OrdersService( + private val ordersRepository: OrdersRepository, + private val usersRepository: UsersRepository, +) { + + fun listOrders(): List = ordersRepository.findAll().map { + Order( + user = it.user, + ) + } + + + fun createOrder(userId: Long){ + val user = usersRepository.findById(userId).get() + val newOrder = OrderEntity(user=user) + ordersRepository.save(newOrder) + } + } + + +data class Order( + val user: UserEntity, + +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/OrdersService.kt b/src/main/kotlin/com/coded/spring/ordering/OrdersService.kt deleted file mode 100644 index b02d14c..0000000 --- a/src/main/kotlin/com/coded/spring/ordering/OrdersService.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.coded.spring.ordering - - - -import jakarta.inject.Named - -@Named -class UsersService( - private val ordersRepository: OrdersRepository, -) { - - fun listUsers(): List = ordersRepository.findAll().map { - Order( - name = it.name, - restaurant = it.restaurant, - items=it.items - - ) - } -} - -data class Order( - val name: String, - val restaurant: String, - val items: Array -) \ 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..c04f3a5 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -0,0 +1,2 @@ +package com.coded.spring.ordering.items + diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt new file mode 100644 index 0000000..dfbc8c5 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt @@ -0,0 +1,19 @@ +package com.coded.spring.ordering.items + +import jakarta.persistence.* +import org.springframework.data.jpa.repository.JpaRepository + +interface ItemsRepository: JpaRepository + +@Entity +@Table(name="items") +data class ItemEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + val name: String, + val quantity: Int, + val order_id: Long +){ + constructor() : this(null, "", 0, 0) +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt new file mode 100644 index 0000000..760db17 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt @@ -0,0 +1,22 @@ +package com.coded.spring.ordering.users + +import jakarta.inject.Named + +@Named +class UserService( + private val userRepository: UsersRepository +) { + + fun listOrders(): List = userRepository.findAll().map { + User( + name = it.name, + age=it.age + + ) + } +} + +data class User( + val name: String, + val age: Int, +) \ No newline at end of file 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..7c89a39 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -0,0 +1,14 @@ +package com.coded.spring.ordering.users +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class UsersController( + val usersRepository: UsersRepository + +){ + @GetMapping("/users/v1/users") + fun getUsers() = usersRepository.findAll() +} + + diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt new file mode 100644 index 0000000..026e626 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.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 UsersRepository : JpaRepository + +@Entity +@Table(name = "users") +data class UserEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var name: String, + var age: Int +){ + constructor() : this(null, "",0) +} \ No newline at end of file From df495bffb414596e23de4243f564c42405c990de Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Fri, 11 Apr 2025 13:07:23 +0300 Subject: [PATCH 3/7] Done relationship between users and orders --- .../coded/spring/ordering/users/UserService.kt | 10 +++++++++- .../spring/ordering/users/UsersController.kt | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt index 760db17..83ee860 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt @@ -1,5 +1,6 @@ package com.coded.spring.ordering.users +import com.coded.spring.ordering.Orders.OrderEntity import jakarta.inject.Named @Named @@ -7,13 +8,20 @@ class UserService( private val userRepository: UsersRepository ) { - fun listOrders(): List = userRepository.findAll().map { + fun listUsers(): List = userRepository.findAll().map { User( name = it.name, age=it.age ) } + + fun createUser(name: String,age:Int){ + + val newUser = UserEntity( name=name, age=age) + userRepository.save(newUser) + } + } data class User( 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 7c89a39..0a5ebf3 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -1,14 +1,25 @@ package com.coded.spring.ordering.users import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController @RestController class UsersController( - val usersRepository: UsersRepository + val usersRepository: UsersRepository, + val usersService: UserService ){ @GetMapping("/users/v1/users") - fun getUsers() = usersRepository.findAll() + fun getUsers() = usersService.listUsers() + + @PostMapping("/users") + fun createUser(@RequestBody request: CreateUserRequest) = usersService.createUser(request.name,request.age) } +data class CreateUserRequest( + var name: String, + var age: Int + +) From 83ad827b9693fccfca91a923a597eb151ae77865 Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Fri, 11 Apr 2025 19:08:19 +0300 Subject: [PATCH 4/7] Bug Fixes --- src/main/kotlin/com/coded/spring/ordering/users/UserService.kt | 1 + .../kotlin/com/coded/spring/ordering/users/UsersController.kt | 1 + .../kotlin/com/coded/spring/ordering/users/UsersRepository.kt | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt index 83ee860..d7a20e9 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt @@ -25,6 +25,7 @@ class UserService( } data class User( + val name: String, val age: Int, ) \ No newline at end of file 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 0a5ebf3..a8a35a5 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -18,6 +18,7 @@ class UsersController( } data class CreateUserRequest( + var name: String, var age: Int diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt index 026e626..6857a0e 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt @@ -13,6 +13,8 @@ data class UserEntity( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, + + var name: String, var age: Int ){ From efcd2cb6722aad53c50cfc015987227f2e1a2158 Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Thu, 17 Apr 2025 18:14:42 +0300 Subject: [PATCH 5/7] Added Menu and items --- .../coded/spring/ordering/InitUserRunner.kt | 17 +++++++++ .../ordering/Orders/OnlineOrderController.kt | 7 ++-- .../ordering/Orders/OrdersRepository.kt | 5 +-- .../spring/ordering/Orders/OrdersService.kt | 10 +++--- .../CustomUserDetailsService.kt | 2 ++ .../ordering/authentication/SecurityConfig.kt | 2 ++ .../spring/ordering/items/ItemsController.kt | 29 +++++++++++++++ .../spring/ordering/items/ItemsRepository.kt | 9 +++-- .../spring/ordering/items/ItemsService.kt | 36 +++++++++++++++++++ .../spring/ordering/menu/MenuController.kt | 27 ++++++++++++++ .../spring/ordering/menu/MenuRepository.kt | 21 +++++++++++ .../coded/spring/ordering/menu/MenuService.kt | 32 +++++++++++++++++ .../spring/ordering/users/UserService.kt | 4 +-- .../spring/ordering/users/UsersController.kt | 6 ++-- .../spring/ordering/users/UsersRepository.kt | 10 ++++-- 15 files changed, 198 insertions(+), 19 deletions(-) create mode 100644 src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/menu/MenuController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/menu/MenuRepository.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/menu/MenuService.kt diff --git a/src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt b/src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt new file mode 100644 index 0000000..fa58e8b --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt @@ -0,0 +1,17 @@ +package com.coded.spring.ordering + +import org.springframework.boot.runApplication +import org.springframework.context.annotation.Bean + +class InitUserRunner{ + @Bean + fun initUser(){ + TODO() + } + + +} + +fun main(args: Array){ + runApplication(*args).close() +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt index 28323f6..d25a6c4 100644 --- a/src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OnlineOrderController.kt @@ -17,12 +17,13 @@ class OnlineOrderController( @GetMapping("/orders/v1/orders") fun getOrders(): List = ordersService.listOrders() - @PostMapping("/orders") - fun orderFood(@RequestBody request: CreateOrderRequest) = ordersService.createOrder(request.userId) + @PostMapping("/orders/v1/orders") + fun orderFood(@RequestBody request: CreateOrderRequest) = ordersService.createOrder(request.userId,request.restaurant) } data class CreateOrderRequest( - val userId: Long + val userId: Long, + val restaurant:String, ) diff --git a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt index 9e9cd33..d62d8ed 100644 --- a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt @@ -19,11 +19,12 @@ data class OrderEntity( @ManyToOne val user: UserEntity, + val restaurant:String, -// @OneToMany(mappedBy = "order_id") +// @OneToMany(mappedBy = "orderId") // val items: List? = null ){ - constructor() : this(null, UserEntity()) + constructor() : this(null, UserEntity(),"") } \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt index 0f6adb0..e68aee7 100644 --- a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt @@ -14,20 +14,22 @@ class OrdersService( fun listOrders(): List = ordersRepository.findAll().map { Order( - user = it.user, + userId = it.user.id, + restaurant = it.restaurant ) } - fun createOrder(userId: Long){ + fun createOrder(userId: Long,restaurant:String){ val user = usersRepository.findById(userId).get() - val newOrder = OrderEntity(user=user) + val newOrder = OrderEntity(user=user, restaurant = restaurant) ordersRepository.save(newOrder) } } data class Order( - val user: UserEntity, + val userId: Long?, + val restaurant: String ) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt new file mode 100644 index 0000000..b38fc56 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt @@ -0,0 +1,2 @@ +package com.coded.spring.ordering.authentication + diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt new file mode 100644 index 0000000..b38fc56 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt @@ -0,0 +1,2 @@ +package com.coded.spring.ordering.authentication + 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 c04f3a5..c3a086c 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -1,2 +1,31 @@ package com.coded.spring.ordering.items +import com.coded.spring.ordering.Orders.Order +import com.coded.spring.ordering.Orders.OrdersService +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RestController + +@RestController +class ItemsController( + private val itemsService: ItemsService, + + ){ + + +// @GetMapping("/items/v1/items/{orderId}") +// fun getItems(@PathVariable orderId: Long): List = itemsService.listItems(orderId) + + @PostMapping("/items/v1/items") + fun addItem(@RequestBody request: CreateItemRequest) = itemsService.addItem(request.orderId,request.name,request.quantity) + +} + +data class CreateItemRequest( + val name: String, + val quantity: Int, + val orderId: Long + +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt index dfbc8c5..d2ee4ec 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsRepository.kt @@ -1,9 +1,11 @@ package com.coded.spring.ordering.items +import com.coded.spring.ordering.Orders.OrderEntity import jakarta.persistence.* import org.springframework.data.jpa.repository.JpaRepository -interface ItemsRepository: JpaRepository +interface ItemsRepository: JpaRepository{ +} @Entity @Table(name="items") @@ -13,7 +15,8 @@ data class ItemEntity( val id: Long? = null, val name: String, val quantity: Int, - val order_id: Long + @ManyToOne + val order: OrderEntity ){ - constructor() : this(null, "", 0, 0) + constructor() : this(null, "", 0, OrderEntity()) } \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt new file mode 100644 index 0000000..1453ec1 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt @@ -0,0 +1,36 @@ +package com.coded.spring.ordering.items + +import com.coded.spring.ordering.Orders.OrderEntity +import com.coded.spring.ordering.Orders.OrdersRepository +import com.coded.spring.ordering.users.UserEntity +import jakarta.inject.Named +import com.coded.spring.ordering.users.UsersRepository + +@Named +class ItemsService( + private val itemsRepository: ItemsRepository, + private val ordersRepository: OrdersRepository +) { + +// fun listItems(orderId: Long): List { +// val order = ordersRepository.findById(orderId).get() +// return itemsRepository.findByOrder(order) +// } + + fun addItem( orderId: Long,name: String,quantity: Int){ + val order = ordersRepository.findById(orderId).get() + val newItem = ItemEntity(name = name, quantity=quantity,order=order) + itemsRepository.save(newItem) + + + } +} + + +data class Item( + val userId: Long?, + val name: String, + val quantity: Int, + val orderId: Long + + ) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/menu/MenuController.kt b/src/main/kotlin/com/coded/spring/ordering/menu/MenuController.kt new file mode 100644 index 0000000..15369de --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/menu/MenuController.kt @@ -0,0 +1,27 @@ +package com.coded.spring.ordering.menu + + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RestController + +@RestController +class MenuController( + private val menuService: MenuService, + + ){ + + @GetMapping("/menu/v1/menu") + fun getMenu(): List = menuService.listMenu() + + @PostMapping("/menu/v1/menu") + fun addToMenu(@RequestBody request: CreateOrderRequest) = menuService.addToMenu(request.name,request.price) + +} + +data class CreateOrderRequest( + val name: String, + val price: Double, + +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/menu/MenuRepository.kt b/src/main/kotlin/com/coded/spring/ordering/menu/MenuRepository.kt new file mode 100644 index 0000000..3d654fb --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/menu/MenuRepository.kt @@ -0,0 +1,21 @@ +package com.coded.spring.ordering.menu + + +import jakarta.persistence.* +import org.springframework.data.jpa.repository.JpaRepository + +interface MenuRepository: JpaRepository{ +} + +@Entity +@Table(name="menu") +data class MenuEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + val name: String, + val price: Double, + +){ + constructor() : this(null, "", 0.0) +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/menu/MenuService.kt b/src/main/kotlin/com/coded/spring/ordering/menu/MenuService.kt new file mode 100644 index 0000000..0242333 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/menu/MenuService.kt @@ -0,0 +1,32 @@ +package com.coded.spring.ordering.menu + +import jakarta.inject.Named + + +@Named +class MenuService( +private val menuRepository: MenuRepository +) { + + fun listMenu(): List = menuRepository.findAll().map { + Menu( + id=it.id, + name = it.name, + price = it.price + ) + } + + + fun addToMenu(name: String,price: Double){ + val newItem = MenuEntity(name=name, price = price) + menuRepository.save(newItem) + } +} + + +data class Menu( + val id:Long?, + val name: String, + val price: Double, + +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt index d7a20e9..cc86f21 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt @@ -16,9 +16,9 @@ class UserService( ) } - fun createUser(name: String,age:Int){ + fun createUser(name: String,age:Int, username:String,password:String){ - val newUser = UserEntity( name=name, age=age) + val newUser = UserEntity( name=name, age=age, username = username,password=password) userRepository.save(newUser) } 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 a8a35a5..a5070ec 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -14,13 +14,15 @@ class UsersController( fun getUsers() = usersService.listUsers() @PostMapping("/users") - fun createUser(@RequestBody request: CreateUserRequest) = usersService.createUser(request.name,request.age) + fun createUser(@RequestBody request: CreateUserRequest) = usersService.createUser(request.name,request.age,request.username,request.password) } data class CreateUserRequest( var name: String, - var age: Int + var age: Int, + var username:String, + var password:String ) diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt index 6857a0e..b60e8c7 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt @@ -5,7 +5,9 @@ import jakarta.persistence.* import org.springframework.data.jpa.repository.JpaRepository @Named -interface UsersRepository : JpaRepository +interface UsersRepository : JpaRepository{ + fun findByUsername(username: String):UserEntity? +} @Entity @Table(name = "users") @@ -16,7 +18,9 @@ data class UserEntity( var name: String, - var age: Int + var age: Int, + var username:String, + var password:String ){ - constructor() : this(null, "",0) + constructor() : this(null, "",0,"","") } \ No newline at end of file From 1e6d483a38d6a047e0c04255c6429f4cb0339aed Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Thu, 24 Apr 2025 18:18:11 +0300 Subject: [PATCH 6/7] Fixed auth and added some tests --- pom.xml | 23 +++++++ .../coded/spring/ordering/InitUserRunner.kt | 17 ----- .../AuthenticationController.kt | 47 ++++++++++++++ .../CustomUserDetailsService.kt | 17 +++++ .../ordering/authentication/SecurityConfig.kt | 56 +++++++++++++++++ .../jwt/JwtAuthenticationFilter.kt | 45 ++++++++++++++ .../ordering/authentication/jwt/JwtService.kt | 42 +++++++++++++ .../spring/ordering/users/UserService.kt | 12 ++-- .../spring/ordering/users/UsersController.kt | 16 ++++- .../spring/ordering/users/UsersRepository.kt | 12 +++- src/main/resources/application.properties | 1 + .../coded/spring/ordering/ApplicationTests.kt | 62 ++++++++++++++++++- 12 files changed, 324 insertions(+), 26 deletions(-) delete mode 100644 src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/authentication/AuthenticationController.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtAuthenticationFilter.kt create mode 100644 src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtService.kt diff --git a/pom.xml b/pom.xml index e18821a..98888f6 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ org.postgresql postgresql + compile @@ -60,9 +61,31 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-security + com.h2database h2 + test + + + io.jsonwebtoken + jjwt-api + 0.11.5 + + + io.jsonwebtoken + jjwt-impl + runtime + 0.11.5 + + + io.jsonwebtoken + jjwt-jackson + runtime + 0.11.5 org.springframework.boot diff --git a/src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt b/src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt deleted file mode 100644 index fa58e8b..0000000 --- a/src/main/kotlin/com/coded/spring/ordering/InitUserRunner.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.coded.spring.ordering - -import org.springframework.boot.runApplication -import org.springframework.context.annotation.Bean - -class InitUserRunner{ - @Bean - fun initUser(){ - TODO() - } - - -} - -fun main(args: Array){ - runApplication(*args).close() -} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/AuthenticationController.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/AuthenticationController.kt new file mode 100644 index 0000000..12bd9be --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/AuthenticationController.kt @@ -0,0 +1,47 @@ +package com.coded.spring.ordering.authentication + + +import com.coded.spring.ordering.authentication.jwt.JwtService +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.security.authentication.* +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.core.userdetails.UsernameNotFoundException +import org.springframework.web.bind.annotation.* + + +@RestController +@RequestMapping("/auth") +class AuthenticationController( + private val authenticationManager: AuthenticationManager, + private val userDetailsService: UserDetailsService, + private val jwtService: JwtService +) { + + @PostMapping("/login") + fun login(@RequestBody request: AuthenticationRequest): ResponseEntity<*> { + val authToken = UsernamePasswordAuthenticationToken(request.username, request.password) + val authentication = authenticationManager.authenticate(authToken) + try { + authenticationManager.authenticate(authToken) + } catch (e: BadCredentialsException) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password") + } catch (e: DisabledException) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User is disabled") + } catch (e: LockedException) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User is locked") + } + + val token = jwtService.generateToken(request.username) + return ResponseEntity.ok(AuthenticationResponse(token)) + } +} + +data class AuthenticationRequest( + val username: String, + val password: String +) + +data class AuthenticationResponse( + val token: String +) \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt index b38fc56..84c4a1e 100644 --- a/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt @@ -1,2 +1,19 @@ package com.coded.spring.ordering.authentication +import com.coded.spring.ordering.users.UsersRepository +import org.springframework.security.core.userdetails.* +import org.springframework.stereotype.Service + +@Service +class CustomUserDetailsService( + private val usersRepository: UsersRepository +) : UserDetailsService { + override fun loadUserByUsername(username: String): UserDetails { + val user = usersRepository.findByUsername(username) + ?: throw UsernameNotFoundException("User not found") + + return User.builder() + .username(user.username) + .password(user.password).build() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt index b38fc56..b092583 100644 --- a/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt @@ -1,2 +1,58 @@ package com.coded.spring.ordering.authentication + +import com.coded.spring.ordering.authentication.jwt.JwtAuthenticationFilter +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.authentication.AuthenticationProvider +import org.springframework.security.authentication.dao.DaoAuthenticationProvider +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity +import org.springframework.security.config.http.SessionCreationPolicy +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.security.web.SecurityFilterChain +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter + + +@Configuration +@EnableWebSecurity +class SecurityConfig( + private val jwtAuthFilter: JwtAuthenticationFilter, + private val userDetailsService: UserDetailsService +) { + + @Bean + fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { + http.csrf { it.disable() } + .authorizeHttpRequests { + it.requestMatchers("/auth/**", "/users/v1/users").permitAll() + .anyRequest().authenticated() + } + .sessionManagement { + it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) + } + .authenticationProvider(authenticationProvider()) + .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter::class.java) + + return http.build() + } + + @Bean + fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder() + + @Bean + fun authenticationManager(config: AuthenticationConfiguration): AuthenticationManager = + config.authenticationManager + + @Bean + fun authenticationProvider(): AuthenticationProvider { + val provider = DaoAuthenticationProvider() + provider.setUserDetailsService(userDetailsService) + provider.setPasswordEncoder(passwordEncoder()) + return provider + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtAuthenticationFilter.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtAuthenticationFilter.kt new file mode 100644 index 0000000..f180b7c --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtAuthenticationFilter.kt @@ -0,0 +1,45 @@ +package com.coded.spring.ordering.authentication.jwt + +import jakarta.servlet.FilterChain +import jakarta.servlet.http.* +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.web.authentication.WebAuthenticationDetailsSource +import org.springframework.stereotype.Component +import org.springframework.web.filter.OncePerRequestFilter + +@Component +class JwtAuthenticationFilter( + private val jwtService: JwtService, + private val userDetailsService: UserDetailsService +) : OncePerRequestFilter() { + + override fun doFilterInternal( + request: HttpServletRequest, + response: HttpServletResponse, + filterChain: FilterChain + ) { + val authHeader = request.getHeader("Authorization") + if (authHeader == null || !authHeader.startsWith("Bearer ")) { + filterChain.doFilter(request, response) + return + } + + val token = authHeader.substring(7) + val username = jwtService.extractUsername(token) + + if (SecurityContextHolder.getContext().authentication == null) { + if (jwtService.isTokenValid(token, username)) { + val userDetails = userDetailsService.loadUserByUsername(username) + val authToken = UsernamePasswordAuthenticationToken( + userDetails, null, userDetails.authorities + ) + authToken.details = WebAuthenticationDetailsSource().buildDetails(request) + SecurityContextHolder.getContext().authentication = authToken + } + } + + filterChain.doFilter(request, response) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtService.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtService.kt new file mode 100644 index 0000000..c877c7c --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/JwtService.kt @@ -0,0 +1,42 @@ +package com.coded.spring.ordering.authentication.jwt + +import io.jsonwebtoken.* +import io.jsonwebtoken.security.Keys +import org.springframework.stereotype.Component +import java.util.* +import javax.crypto.SecretKey + +@Component +class JwtService { + + private val secretKey: SecretKey = Keys.secretKeyFor(SignatureAlgorithm.HS256) + private val expirationMs: Long = 1000 * 60 * 60 + + fun generateToken(username: String): String { + val now = Date() + val expiry = Date(now.time + expirationMs) + + return Jwts.builder() + .setSubject(username) + .setIssuedAt(now) + .setExpiration(expiry) + .signWith(secretKey) + .compact() + } + + fun extractUsername(token: String): String = + Jwts.parserBuilder() + .setSigningKey(secretKey) + .build() + .parseClaimsJws(token) + .body + .subject + + fun isTokenValid(token: String, username: String): Boolean { + return try { + extractUsername(token) == username + } catch (e: Exception) { + false + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt index cc86f21..c838e41 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt @@ -2,10 +2,12 @@ package com.coded.spring.ordering.users import com.coded.spring.ordering.Orders.OrderEntity import jakarta.inject.Named +import org.springframework.security.crypto.password.PasswordEncoder @Named class UserService( - private val userRepository: UsersRepository + private val userRepository: UsersRepository, + private val passwordEncoder: PasswordEncoder ) { fun listUsers(): List = userRepository.findAll().map { @@ -17,8 +19,8 @@ class UserService( } fun createUser(name: String,age:Int, username:String,password:String){ - - val newUser = UserEntity( name=name, age=age, username = username,password=password) + if (password.length < 6) throw InvalidPasswordException() + val newUser = UserEntity( name=name, age=age, username = username,password=passwordEncoder.encode(password)) userRepository.save(newUser) } @@ -28,4 +30,6 @@ data class User( val name: String, val age: Int, -) \ No newline at end of file +) + +class InvalidPasswordException(message: String = "Password must be at least 6 characters") : RuntimeException(message) 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 a5070ec..d479700 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersController.kt @@ -1,10 +1,12 @@ package com.coded.spring.ordering.users +import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController @RestController + class UsersController( val usersRepository: UsersRepository, val usersService: UserService @@ -13,8 +15,18 @@ class UsersController( @GetMapping("/users/v1/users") fun getUsers() = usersService.listUsers() - @PostMapping("/users") - fun createUser(@RequestBody request: CreateUserRequest) = usersService.createUser(request.name,request.age,request.username,request.password) + @PostMapping("/users/v1/users") + fun createUser(@RequestBody request: CreateUserRequest): ResponseEntity<*> { + return try{ + usersService.createUser(request.name,request.age,request.username,request.password) + ResponseEntity.ok("User created successfully") + + } catch (e: InvalidPasswordException) { + ResponseEntity.badRequest().body(e.message) + } + + + } } data class CreateUserRequest( diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt index b60e8c7..679c496 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UsersRepository.kt @@ -20,7 +20,15 @@ data class UserEntity( var name: String, var age: Int, var username:String, - var password:String + var password:String, + + @Enumerated(EnumType.STRING) + val role: Roles = Roles.USER + ){ - constructor() : this(null, "",0,"","") + constructor() : this(null, "",0,"","",) +} + +enum class Roles { + USER, ADMIN } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 06c8834..8eb35db 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,3 +3,4 @@ spring.datasource.url=jdbc:postgresql://localhost:5433/onlineOrderingDatabase spring.datasource.username=postgres spring.datasource.password=ilovedjango# spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect +server.port=8081 diff --git a/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt b/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt index b2e2320..c26455f 100644 --- a/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt +++ b/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt @@ -1,13 +1,73 @@ package com.coded.spring.ordering +import com.coded.spring.ordering.authentication.jwt.JwtService +import com.coded.spring.ordering.menu.Menu +import com.coded.spring.ordering.users.UserEntity +import com.coded.spring.ordering.users.UsersRepository +import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.http.HttpStatus +import org.springframework.http.HttpEntity +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpMethod +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.util.MultiValueMap +import kotlin.test.junit5.JUnit5Asserter.assertEquals -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class ApplicationTests { + companion object { + @JvmStatic + @BeforeAll + fun setUp( + @Autowired usersRepository: UsersRepository, + @Autowired passwordEncoder: PasswordEncoder, + ){ + usersRepository.deleteAll() + val testUser = UserEntity( + name = "coded", + age = 18, + username = "coded", + password = passwordEncoder.encode("joincoded") + ) + usersRepository.save(testUser) + } + } + + + @Autowired + lateinit var restTemplate: TestRestTemplate + @Test fun contextLoads() { } + @Test + fun getMenu(@Autowired jwtService: JwtService){ + val token = jwtService.generateToken("coded") + val headers = HttpHeaders( + MultiValueMap.fromSingleValue(mapOf("Authorization" to "Bearer $token")) + ) + val requestEntity = HttpEntity(headers) + + val result = restTemplate.exchange( + "/menu/v1/menu", + HttpMethod.GET, + requestEntity, + emptyList()::class.java + ) + assertEquals(expected = HttpStatus.OK, actual = result.statusCode,message = "status code is OK" ) + assertEquals(expected = emptyList(), actual = result.body, message = "output equals menu list") + + } + + + + + + } From 5a044c872c118aba08cec5b052ca524ad4524f42 Mon Sep 17 00:00:00 2001 From: fatemahAlMomen Date: Thu, 24 Apr 2025 18:34:39 +0300 Subject: [PATCH 7/7] Removed un-necessary comments --- .../com/coded/spring/ordering/Orders/OrdersRepository.kt | 1 - .../com/coded/spring/ordering/Orders/OrdersService.kt | 1 - .../com/coded/spring/ordering/items/ItemsController.kt | 8 +------- .../com/coded/spring/ordering/items/ItemsService.kt | 5 +---- .../kotlin/com/coded/spring/ordering/users/UserService.kt | 1 - .../kotlin/com/coded/spring/ordering/ApplicationTests.kt | 4 ++++ 6 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt index d62d8ed..b802c06 100644 --- a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersRepository.kt @@ -1,7 +1,6 @@ package com.coded.spring.ordering.Orders -import com.coded.spring.ordering.items.ItemEntity import com.coded.spring.ordering.users.UserEntity import jakarta.inject.Named import jakarta.persistence.* diff --git a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt index e68aee7..1fa1762 100644 --- a/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Orders/OrdersService.kt @@ -2,7 +2,6 @@ package com.coded.spring.ordering.Orders -import com.coded.spring.ordering.users.UserEntity import jakarta.inject.Named import com.coded.spring.ordering.users.UsersRepository 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 c3a086c..93c9242 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsController.kt @@ -1,9 +1,6 @@ package com.coded.spring.ordering.items -import com.coded.spring.ordering.Orders.Order -import com.coded.spring.ordering.Orders.OrdersService -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable + import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController @@ -15,9 +12,6 @@ class ItemsController( ){ -// @GetMapping("/items/v1/items/{orderId}") -// fun getItems(@PathVariable orderId: Long): List = itemsService.listItems(orderId) - @PostMapping("/items/v1/items") fun addItem(@RequestBody request: CreateItemRequest) = itemsService.addItem(request.orderId,request.name,request.quantity) diff --git a/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt b/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt index 1453ec1..c9b11f4 100644 --- a/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/items/ItemsService.kt @@ -12,10 +12,7 @@ class ItemsService( private val ordersRepository: OrdersRepository ) { -// fun listItems(orderId: Long): List { -// val order = ordersRepository.findById(orderId).get() -// return itemsRepository.findByOrder(order) -// } + fun addItem( orderId: Long,name: String,quantity: Int){ val order = ordersRepository.findById(orderId).get() diff --git a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt index c838e41..d6e0835 100644 --- a/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt +++ b/src/main/kotlin/com/coded/spring/ordering/users/UserService.kt @@ -1,6 +1,5 @@ package com.coded.spring.ordering.users -import com.coded.spring.ordering.Orders.OrderEntity import jakarta.inject.Named import org.springframework.security.crypto.password.PasswordEncoder diff --git a/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt b/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt index c26455f..df5bf41 100644 --- a/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt +++ b/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt @@ -1,5 +1,6 @@ package com.coded.spring.ordering + import com.coded.spring.ordering.authentication.jwt.JwtService import com.coded.spring.ordering.menu.Menu import com.coded.spring.ordering.users.UserEntity @@ -70,4 +71,7 @@ class ApplicationTests { + + + }