diff --git a/Humoud-online-ordering-api-swagger-01.json b/Humoud-online-ordering-api-swagger-01.json new file mode 100644 index 0000000..48f9136 --- /dev/null +++ b/Humoud-online-ordering-api-swagger-01.json @@ -0,0 +1,490 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "OpenAPI definition", + "version": "v0" + }, + "servers": [ + { + "url": "http://localhost:8080", + "description": "Generated server url" + } + ], + "tags": [ + { + "name": "Users API", + "description": "Operations related to Users and Profiles" + }, + { + "name": "Authentication API", + "description": "User Authentication and JWT generation" + }, + { + "name": "Hello API", + "description": "Operations related to Hello" + }, + { + "name": "Orders API", + "description": "Operations related to Orders" + } + ], + "paths": { + "/users/v1/{userId}/profile": { + "post": { + "tags": [ + "Users API" + ], + "summary": "Create a user profile", + "description": "Create a profile for an existing user", + "operationId": "createProfile", + "parameters": [ + { + "name": "userId", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ProfileRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Profile successfully created", + "content": { + "application/json": {} + } + }, + "400": { + "description": "Invalid profile data" + } + } + } + }, + "/users/v1/register": { + "post": { + "tags": [ + "Users API" + ], + "summary": "Register a new user", + "description": "Create a new user account", + "operationId": "createUser", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UserRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "User successfully created", + "content": { + "application/json": {} + } + }, + "400": { + "description": "Invalid user data" + } + } + } + }, + "/orders/v1/orders": { + "post": { + "tags": [ + "Orders API" + ], + "summary": "Create a new order", + "description": "Creates a new order for a specific user", + "operationId": "createNewOrder", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/OrderRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Order successfully created", + "content": { + "application/json": {} + } + }, + "400": { + "description": "Invalid order data" + } + } + } + }, + "/hello": { + "get": { + "tags": [ + "Hello API" + ], + "summary": "Say Hello World", + "operationId": "helloWorld", + "responses": { + "200": { + "description": "Successful Hello World response", + "content": { + "*/*": { + "schema": { + "type": "string" + } + } + } + } + } + }, + "post": { + "tags": [ + "Hello API" + ], + "summary": "Say Hello with Name", + "operationId": "helloName", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Name" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful personalized Hello response", + "content": { + "*/*": { + "schema": { + "type": "string" + } + } + } + } + } + } + }, + "/auth/login": { + "post": { + "tags": [ + "Authentication API" + ], + "summary": "User Login", + "description": "Authenticate user and return a JWT token", + "operationId": "login", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/AuthenticationRequest" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Login successful, JWT token returned", + "content": { + "application/json": {} + } + }, + "401": { + "description": "Invalid username or password", + "content": { + "*/*": { + "schema": { + "type": "string" + } + } + } + } + } + } + }, + "/users/v1/{userId}": { + "get": { + "tags": [ + "Users API" + ], + "summary": "Get user by ID", + "description": "Retrieve a specific user by their ID", + "operationId": "user", + "parameters": [ + { + "name": "userId", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "User found", + "content": { + "application/json": {} + } + }, + "404": { + "description": "User not found", + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + } + } + } + }, + "/users/v1/list": { + "get": { + "tags": [ + "Users API" + ], + "summary": "List all users", + "description": "Retrieve a list of all users", + "operationId": "users", + "responses": { + "200": { + "description": "List of all users", + "content": { + "application/json": {} + } + } + } + } + }, + "/users/profiles/v1/list": { + "get": { + "tags": [ + "Users API" + ], + "summary": "List all profiles", + "description": "Retrieve a list of all user profiles", + "operationId": "profiles", + "responses": { + "200": { + "description": "List of all profiles", + "content": { + "application/json": {} + } + } + } + } + }, + "/orders": { + "get": { + "tags": [ + "Orders API" + ], + "summary": "Retrieve all orders", + "description": "Get a list of all existing orders", + "operationId": "getOrders", + "responses": { + "200": { + "description": "List of all orders", + "content": { + "application/json": {} + } + } + } + } + }, + "/boo": { + "get": { + "tags": [ + "Hello API" + ], + "summary": "Testing endpoint", + "operationId": "testing", + "responses": { + "200": { + "description": "Successful test response", + "content": { + "*/*": { + "schema": { + "type": "string" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "ProfileRequest": { + "required": [ + "firstName", + "lastName" + ], + "type": "object", + "properties": { + "firstName": { + "pattern": "^[a-zA-Z]+$", + "type": "string" + }, + "lastName": { + "pattern": "^[a-zA-Z]+$", + "type": "string" + }, + "phoneNumber": { + "pattern": "^\\d{8}$", + "type": "string" + } + } + }, + "UserRequest": { + "required": [ + "age", + "name", + "password", + "role", + "username" + ], + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "age": { + "type": "integer", + "format": "int32" + }, + "username": { + "type": "string" + }, + "password": { + "type": "string" + }, + "role": { + "type": "string", + "enum": [ + "USER", + "ADMIN" + ] + } + } + }, + "ItemEntity": { + "required": [ + "name", + "order_id", + "quantity" + ], + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "quantity": { + "type": "integer", + "format": "int32" + }, + "order_id": { + "type": "integer", + "format": "int64" + } + } + }, + "OrderRequest": { + "required": [ + "items", + "restaurant", + "user" + ], + "type": "object", + "properties": { + "user": { + "type": "integer", + "format": "int64" + }, + "restaurant": { + "type": "string" + }, + "items": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ItemEntity" + } + } + } + }, + "Name": { + "required": [ + "name" + ], + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, + "AuthenticationRequest": { + "required": [ + "password", + "username" + ], + "type": "object", + "properties": { + "username": { + "type": "string" + }, + "password": { + "type": "string" + } + } + }, + "User": { + "required": [ + "id", + "name", + "username" + ], + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "username": { + "type": "string" + } + } + } + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 163ad53..8ac2167 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot @@ -8,11 +8,11 @@ 3.4.4 - com.coded.spring - Ordering + com.example + ordering 0.0.1-SNAPSHOT - Kotlin.SpringbootV2 - Kotlin.SpringbootV2 + ordering + Demo project for Spring Boot @@ -31,6 +31,37 @@ 1.9.25 + + org.springframework.boot + spring-boot-starter-security + + + io.jsonwebtoken + jjwt-api + 0.11.5 + + + io.jsonwebtoken + jjwt-impl + runtime + 0.11.5 + + + io.jsonwebtoken + jjwt-jackson + runtime + 0.11.5 + + + jakarta.validation + jakarta.validation-api + 3.0.2 + + + org.hibernate.validator + hibernate-validator + 8.0.1.Final + org.springframework.boot spring-boot-starter-web @@ -47,7 +78,19 @@ org.jetbrains.kotlin kotlin-stdlib - + + org.springframework.boot + spring-boot-starter-data-jpa + + + jakarta.inject + jakarta.inject-api + + + org.postgresql + postgresql + runtime + org.springframework.boot spring-boot-starter-test @@ -58,6 +101,49 @@ kotlin-test-junit5 test + + org.springframework.security + spring-security-test + test + + + com.h2database + h2 + test + + + io.cucumber + cucumber-java + 7.20.1 + test + + + io.cucumber + cucumber-junit-platform-engine + 7.20.1 + test + + + org.springdoc + springdoc-openapi-starter-webmvc-api + 2.6.0 + + + io.cucumber + cucumber-spring + 7.14.0 + test + + + org.apache.commons + commons-lang3 + 3.14.0 + + + com.hazelcast + hazelcast + 5.5.0 + diff --git a/src/main/kotlin/com/coded/spring/ordering/Application.kt b/src/main/kotlin/com/coded/spring/ordering/Application.kt index 8554e49..9108d56 100644 --- a/src/main/kotlin/com/coded/spring/ordering/Application.kt +++ b/src/main/kotlin/com/coded/spring/ordering/Application.kt @@ -1,11 +1,20 @@ package com.coded.spring.ordering +import com.hazelcast.config.Config; +import com.hazelcast.core.Hazelcast +import com.hazelcast.core.HazelcastInstance import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableScheduling +@EnableScheduling @SpringBootApplication class Application fun main(args: Array) { runApplication(*args) + usersConfig.getMapConfig("users-cache").timeToLiveSeconds = 5 } + +val usersConfig = Config("users-cache") +val serverCache: HazelcastInstance = Hazelcast.newHazelcastInstance(usersConfig) diff --git a/src/main/kotlin/com/coded/spring/ordering/LoggingFilter.kt b/src/main/kotlin/com/coded/spring/ordering/LoggingFilter.kt new file mode 100644 index 0000000..0681429 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/LoggingFilter.kt @@ -0,0 +1,56 @@ +package com.coded.spring.ordering + +import jakarta.servlet.FilterChain +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.springframework.beans.factory.annotation.Value +import org.springframework.core.annotation.Order +import org.springframework.stereotype.Component +import org.springframework.web.filter.OncePerRequestFilter +import org.springframework.web.util.ContentCachingRequestWrapper +import org.springframework.web.util.ContentCachingResponseWrapper +import org.slf4j.LoggerFactory + +@Component +@Order(1) +class LoggingFilter( + @Value("\${logging-level}") + private val logLevel: String +) : OncePerRequestFilter() { + + override fun doFilterInternal( + request: HttpServletRequest, + response: HttpServletResponse, + filterChain: FilterChain + ) { + val cachedRequest = ContentCachingRequestWrapper(request) + val cachedResponse = ContentCachingResponseWrapper(response) + + filterChain.doFilter(cachedRequest, cachedResponse) + + logRequest(cachedRequest) + logResponse(cachedResponse) + + cachedResponse.copyBodyToResponse() + } + + private fun logRequest(request: ContentCachingRequestWrapper) { + val requestBody = String(request.contentAsByteArray) + + if (logLevel == "DEBUG") { + logger.info("Request: method=${request.method}, uri=${request.requestURI}, body=$requestBody") + } else { + logger.info("Request: method=${request.method}, uri=${request.requestURI}") + } + } + + private fun logResponse(response: ContentCachingResponseWrapper) { + val responseBody = String(response.contentAsByteArray) + + if (logLevel == "DEBUG") { + logger.info("Response: status=${response.status}, body=$responseBody") + } else { + logger.info("Response: status=${response.status}") + } + } +} 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..7f652db --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/CustomUserDetailsService.kt @@ -0,0 +1,21 @@ +package com.coded.spring.ordering.authentication + +import com.coded.spring.ordering.repositories.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) + .roles(user.role.toString()) + .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 new file mode 100644 index 0000000..599f96f --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/SecurityConfig.kt @@ -0,0 +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/**", "/api-docs", "/hello").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/AuthenticationController.kt b/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/AuthenticationController.kt new file mode 100644 index 0000000..36b4c95 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/authentication/jwt/AuthenticationController.kt @@ -0,0 +1,57 @@ +package com.coded.spring.ordering.authentication.jwt + +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.responses.ApiResponses +import io.swagger.v3.oas.annotations.tags.Tag +import io.swagger.v3.oas.annotations.media.Content +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") +@Tag(name = "Authentication API", description = "User Authentication and JWT generation") +class AuthenticationController( + private val authenticationManager: AuthenticationManager, + private val userDetailsService: UserDetailsService, + private val jwtService: JwtService +) { + + @Operation( + summary = "User Login", + description = "Authenticate user and return a JWT token", + tags = ["Authentication API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "Login successful, JWT token returned", + content = [Content(mediaType = "application/json")] + ), + ApiResponse( + responseCode = "401", + description = "Invalid username or password") + ] + ) + @PostMapping("/login") + fun login(@RequestBody authRequest: AuthenticationRequest): String { + val authToken = UsernamePasswordAuthenticationToken(authRequest.username, authRequest.password) + val authentication = authenticationManager.authenticate(authToken) + + if (authentication.isAuthenticated) { + val userDetails = userDetailsService.loadUserByUsername(authRequest.username) + val token = jwtService.generateToken(userDetails.username) + return token + } else { + throw UsernameNotFoundException("Invalid user request!") + } + } +} + +data class AuthenticationRequest( + val username: String, + val password: String +) 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/controllers/HelloWorldController.kt b/src/main/kotlin/com/coded/spring/ordering/controllers/HelloWorldController.kt new file mode 100644 index 0000000..43e3bc1 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/controllers/HelloWorldController.kt @@ -0,0 +1,72 @@ +package com.coded.spring.ordering.controllers + +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.responses.ApiResponses +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.beans.factory.annotation.Value +import org.springframework.web.bind.annotation.* +import java.math.BigDecimal + + +@RestController +@Tag(name = "Hello API", description = "Operations related to Hello") +class HelloWorldController( + @Value("\${server-welcome-message}") + val welcomeMessage: String, + @Value("\${eid-message}") + val eidMessage: String, + @Value("\${feature.eid.enabled}") + private val eidEnabled: Boolean, + @Value("\${discount}") + val discount: Boolean +) { + + @Operation(summary = "Say Hello World") + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "Successful Hello World response") + ] + ) + @GetMapping("/hello") + fun helloWorld(): String { + return if(eidEnabled){ + eidMessage + } + else { + welcomeMessage + } + } + + @Operation(summary = "Testing endpoint") + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "Successful test response") + ] + ) + @GetMapping("/boo") + fun testing(): String { + return "AAAA" + } + + @Operation(summary = "Say Hello with Name") + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "Successful personalized Hello response") + ] + ) + @PostMapping("/hello") + fun helloName(@RequestBody req: Name): String { + return if(discount){ + "Hello ${req.name}, The discounted price is: ${req.price * BigDecimal("0.8")}!" + } + else { + "Hello ${req.name}, The Price is: ${req.price}!" + } + } +} + +data class Name( + val name: String, + val price: BigDecimal = BigDecimal("100.0") +) diff --git a/src/main/kotlin/com/coded/spring/ordering/controllers/OrdersController.kt b/src/main/kotlin/com/coded/spring/ordering/controllers/OrdersController.kt new file mode 100644 index 0000000..62bd42a --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/controllers/OrdersController.kt @@ -0,0 +1,72 @@ +package com.coded.spring.ordering.controllers + +import com.coded.spring.ordering.entities.OrderEntity +import com.coded.spring.ordering.repositories.OrdersRepository +import com.coded.spring.ordering.services.OrdersService +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.responses.ApiResponses +import io.swagger.v3.oas.annotations.media.Content +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.web.bind.annotation.* + +@RestController +@Tag(name = "Orders API", description = "Operations related to Orders") +class OrdersController( + val ordersRepository: OrdersRepository, + val ordersService: OrdersService, +) { + + @Operation( + summary = "Create a new order", + description = "Creates a new order for a specific user", + tags = ["Orders API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "Order successfully created", + content = [Content(mediaType = "application/json")] + ), + ApiResponse( + responseCode = "400", + description = "Invalid order data" + ) + ] + ) + @PostMapping("/orders/v1/orders") + fun createNewOrder(@RequestBody req: OrderRequest) { + ordersService.createOrder(req) + } + + @Operation( + summary = "Retrieve all orders", + description = "Get a list of all existing orders", + tags = ["Orders API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "List of all orders", + content = [Content(mediaType = "application/json")] + ) + ] + ) + @GetMapping("/orders") + fun getOrders(): MutableList { + return ordersRepository.findAll() + } + + data class OrderRequest( + val user: Long, + val restaurant: String, + val items: List + ) + + data class ItemRequest( + val name: String, + val quantity: Int + ) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/controllers/UsersController.kt b/src/main/kotlin/com/coded/spring/ordering/controllers/UsersController.kt new file mode 100644 index 0000000..d10efa2 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/controllers/UsersController.kt @@ -0,0 +1,159 @@ +package com.coded.spring.ordering.controllers + +import com.coded.spring.ordering.entities.Roles +import com.coded.spring.ordering.entities.UserEntity +import com.coded.spring.ordering.services.ProfilesService +import com.coded.spring.ordering.services.User +import com.coded.spring.ordering.services.UsersService +import com.coded.spring.ordering.services.UsersProvider +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.responses.ApiResponses +import io.swagger.v3.oas.annotations.media.Content +import io.swagger.v3.oas.annotations.tags.Tag +import jakarta.validation.Valid +import jakarta.validation.constraints.Pattern +import org.springframework.web.bind.annotation.* + +@RestController +@Tag(name = "Users API", description = "Operations related to Users and Profiles") +class UsersControllers( + private val usersService: UsersService, + private val profileService: ProfilesService, +) { + + @Operation( + summary = "List all users", + description = "Retrieve a list of all users", + tags = ["Users API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "List of all users", + content = [Content(mediaType = "application/json")] + ) + ] + ) + @GetMapping("/users/v1/list") + fun fetchUsers(): List = usersService.listUsers() + + @Operation( + summary = "List all profiles", + description = "Retrieve a list of all user profiles", + tags = ["Users API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "List of all profiles", + content = [Content(mediaType = "application/json")] + ) + ] + ) + @GetMapping("/users/profiles/v1/list") + fun profiles() = profileService.listProfiles() + + @Operation( + summary = "Get user by ID", + description = "Retrieve a specific user by their ID", + tags = ["Users API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "User found", + content = [Content(mediaType = "application/json")] + ), + ApiResponse( + responseCode = "404", + description = "User not found" + ) + ] + ) + @GetMapping("/users/v1/{userId}") + fun user(@PathVariable userId: Long) = usersService.getUserDtoById(userId) + + @Operation( + summary = "Register a new user", + description = "Create a new user account", + tags = ["Users API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "User successfully created", + content = [Content(mediaType = "application/json")] + ), + ApiResponse( + responseCode = "400", + description = "Invalid user data" + ) + ] + ) + @PostMapping("/users/v1/register") + fun createUser(@RequestBody request: UserRequest) { + val user = UserEntity( + name = request.name, + age = request.age, + username = request.username, + password = request.password, + role = request.role + ) + usersService.createUser(user) + } + + @Operation( + summary = "Create a user profile", + description = "Create a profile for an existing user", + tags = ["Users API"] + ) + @ApiResponses( + value = [ + ApiResponse( + responseCode = "200", + description = "Profile successfully created", + content = [Content(mediaType = "application/json")] + ), + ApiResponse( + responseCode = "400", + description = "Invalid profile data" + ) + ] + ) + @PostMapping("/users/v1/{userId}/profile") + fun createProfile( + @RequestBody @Valid request: ProfileRequest, + @PathVariable userId: Long + ) { + profileService.createProfileForUser( + userId = userId, + firstName = request.firstName, + lastName = request.lastName, + phoneNumber = request.phoneNumber + ) + } + + data class UserRequest( + val name: String, + val age: Int, + val username: String, + val password: String, + val role: Roles + ) + + data class ProfileRequest( + @field:Pattern(regexp = "^[a-zA-Z]+$", message = "First name must contain only letters") + val firstName: String, + + @field:Pattern(regexp = "^[a-zA-Z]+$", message = "Last name must contain only letters") + val lastName: String, + + @field:Pattern(regexp = "^\\d{8}$", message = "Phone number must be exactly 8 digits with no letters") + val phoneNumber: String? + ) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/entities/ItemEntity.kt b/src/main/kotlin/com/coded/spring/ordering/entities/ItemEntity.kt new file mode 100644 index 0000000..eb78a4b --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/entities/ItemEntity.kt @@ -0,0 +1,23 @@ +package com.coded.spring.ordering.entities + +import com.fasterxml.jackson.annotation.JsonBackReference +import jakarta.persistence.* + +@Entity +@Table(name = "items") +data class ItemEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + + val name: String, + + val quantity: Int, + + @ManyToOne + @JoinColumn(name = "order_id", nullable = false) + @JsonBackReference // ✅ Add this + var order: OrderEntity? = null +) { + constructor() : this(null, "", 0, null) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/entities/OrderEntity.kt b/src/main/kotlin/com/coded/spring/ordering/entities/OrderEntity.kt new file mode 100644 index 0000000..95c85d0 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/entities/OrderEntity.kt @@ -0,0 +1,23 @@ +package com.coded.spring.ordering.entities + +import com.fasterxml.jackson.annotation.JsonManagedReference +import jakarta.persistence.* + +@Entity +@Table(name = "orders") +data class OrderEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + + val restaurant: String? = null, + + @ManyToOne + val user: UserEntity, + + @OneToMany(mappedBy = "order", cascade = [CascadeType.ALL], orphanRemoval = true) + @JsonManagedReference + var items: List = listOf() +) { + constructor() : this(null, null, UserEntity(), listOf()) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/entities/ProfileEntity.kt b/src/main/kotlin/com/coded/spring/ordering/entities/ProfileEntity.kt new file mode 100644 index 0000000..dffd215 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/entities/ProfileEntity.kt @@ -0,0 +1,22 @@ +package com.coded.spring.ordering.entities + +import com.coded.spring.ordering.entities.UserEntity +import jakarta.persistence.* + +@Entity +@Table(name = "profiles") +data class ProfileEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long? = null, + + @OneToOne + @JoinColumn(name = "user_id", nullable = false, unique = true) + val user: UserEntity, + + val firstName: String, + val lastName: String, + val phoneNumber: String? +) { + constructor() : this(null, UserEntity(),"","",null) +} diff --git a/src/main/kotlin/com/coded/spring/ordering/entities/UserEntity.kt b/src/main/kotlin/com/coded/spring/ordering/entities/UserEntity.kt new file mode 100644 index 0000000..353cd61 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/entities/UserEntity.kt @@ -0,0 +1,31 @@ +package com.coded.spring.ordering.entities + +import jakarta.persistence.* + +@Entity +@Table(name = "users") +data class UserEntity( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + var name: String, + var age: Int, + + @Column(unique = true) + var username: String, + + var password: String, + + @Enumerated(EnumType.STRING) + val role: Roles = Roles.USER, + + @OneToOne(mappedBy = "user", cascade = [CascadeType.ALL], fetch = FetchType.LAZY) + var profile: ProfileEntity? = null + +) { + constructor() : this(0, "name", 0, "username", "password", Roles.USER) +} + +enum class Roles { + USER, ADMIN +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/repositories/ItemsRepository.kt b/src/main/kotlin/com/coded/spring/ordering/repositories/ItemsRepository.kt new file mode 100644 index 0000000..ccafe63 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/repositories/ItemsRepository.kt @@ -0,0 +1,7 @@ +package com.coded.spring.ordering.repositories + +import com.coded.spring.ordering.entities.ItemEntity +import org.springframework.data.jpa.repository.JpaRepository + + +interface ItemsRepository: JpaRepository diff --git a/src/main/kotlin/com/coded/spring/ordering/repositories/OrdersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/repositories/OrdersRepository.kt new file mode 100644 index 0000000..f2a4d81 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/repositories/OrdersRepository.kt @@ -0,0 +1,9 @@ +package com.coded.spring.ordering.repositories + +import com.coded.spring.ordering.entities.OrderEntity +import jakarta.inject.Named +import org.springframework.data.jpa.repository.JpaRepository + +@Named +interface OrdersRepository : JpaRepository + diff --git a/src/main/kotlin/com/coded/spring/ordering/repositories/ProfileRepository.kt b/src/main/kotlin/com/coded/spring/ordering/repositories/ProfileRepository.kt new file mode 100644 index 0000000..e330be8 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/repositories/ProfileRepository.kt @@ -0,0 +1,9 @@ +package com.coded.spring.ordering.repositories + +import com.coded.spring.ordering.entities.ProfileEntity +import com.coded.spring.ordering.entities.UserEntity +import org.springframework.data.jpa.repository.JpaRepository + +interface ProfilesRepository : JpaRepository { + fun existsByUser(user: UserEntity): Boolean +} diff --git a/src/main/kotlin/com/coded/spring/ordering/repositories/UsersRepository.kt b/src/main/kotlin/com/coded/spring/ordering/repositories/UsersRepository.kt new file mode 100644 index 0000000..8c21439 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/repositories/UsersRepository.kt @@ -0,0 +1,13 @@ +package com.coded.spring.ordering.repositories + +import com.coded.spring.ordering.entities.UserEntity +import jakarta.inject.Named +import org.springframework.data.jpa.repository.JpaRepository + +@Named +interface UsersRepository : JpaRepository { + fun findByUsername(username: String): UserEntity? +} + + + diff --git a/src/main/kotlin/com/coded/spring/ordering/script/InitUserRunner.kt b/src/main/kotlin/com/coded/spring/ordering/script/InitUserRunner.kt new file mode 100644 index 0000000..f771fd0 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/script/InitUserRunner.kt @@ -0,0 +1,36 @@ +package com.coded.spring.ordering.script + +import com.coded.spring.ordering.Application +import com.coded.spring.ordering.entities.Roles +import com.coded.spring.ordering.entities.UserEntity +import com.coded.spring.ordering.repositories.UsersRepository + +import org.springframework.boot.CommandLineRunner +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication +import org.springframework.context.annotation.Bean +import org.springframework.security.crypto.password.PasswordEncoder + +@SpringBootApplication +class InitUserRunner { + @Bean + fun initUsers(userRepository: UsersRepository, passwordEncoder: PasswordEncoder) = CommandLineRunner { + val user = UserEntity( + name = "HelloUser", + username = "testuser", + password = passwordEncoder.encode("password123"), + age = 18, + role = Roles.USER + ) + if (userRepository.findByUsername(user.username) == null) { + println("Creating user ${user.username}") + userRepository.save(user) + } else { + println("User ${user.username} already exists") + } + } +} + +fun main(args: Array) { + runApplication(*args).close() +} \ No newline at end of file diff --git a/src/main/kotlin/com/coded/spring/ordering/services/OrdersService.kt b/src/main/kotlin/com/coded/spring/ordering/services/OrdersService.kt new file mode 100644 index 0000000..a72e5db --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/services/OrdersService.kt @@ -0,0 +1,49 @@ +package com.coded.spring.ordering.services + +import com.coded.spring.ordering.controllers.OrdersController +import com.coded.spring.ordering.entities.ItemEntity +import com.coded.spring.ordering.entities.OrderEntity +import com.coded.spring.ordering.entities.UserEntity +import com.coded.spring.ordering.repositories.OrdersRepository +import com.coded.spring.ordering.repositories.UsersRepository +import jakarta.inject.Named + +@Named +class OrdersService( + private val usersRepository: UsersRepository, + private val orderRepository: OrdersRepository +) { + + fun createOrder(req: OrdersController.OrderRequest): OrderResponse { + val user = usersRepository.findById(req.user).orElseThrow { + IllegalArgumentException("User with id ${req.user} not found") + } + + val order = OrderEntity( + restaurant = req.restaurant, + user = user + ) + + val items = req.items.map { itemRequest -> + ItemEntity( + name = itemRequest.name, + quantity = itemRequest.quantity, + order = order // point to the parent order + ) + } + + order.items = items + + val savedOrder = orderRepository.save(order) + + return OrderResponse( + user = savedOrder.user, + items = savedOrder.items + ) + } +} + +data class OrderResponse( + val user: UserEntity, + val items: List? +) diff --git a/src/main/kotlin/com/coded/spring/ordering/services/ProfileService.kt b/src/main/kotlin/com/coded/spring/ordering/services/ProfileService.kt new file mode 100644 index 0000000..042fd47 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/services/ProfileService.kt @@ -0,0 +1,40 @@ +package com.coded.spring.ordering.services + +import com.coded.spring.ordering.entities.ProfileEntity +import com.coded.spring.ordering.repositories.ProfilesRepository +import com.coded.spring.ordering.repositories.UsersRepository +import org.springframework.stereotype.Service + +@Service +class ProfilesService( + private val profilesRepository: ProfilesRepository, + private val usersRepository: UsersRepository +) { + fun createProfileForUser(userId: Long, firstName: String, lastName: String, phoneNumber: String?) { + val user = usersRepository.findById(userId).orElseThrow() + val profile = ProfileEntity( + user = user, + firstName = firstName, + lastName = lastName, + phoneNumber = phoneNumber + ) + profilesRepository.save(profile) + + } + + fun listProfiles(): List = profilesRepository.findAll().map { + Profile( + id = it.id!!, + firstName = it.firstName, + lastName = it.lastName, + phoneNumber = it.phoneNumber + ) + } +} + +data class Profile( + val id: Long, + val firstName: String, + val lastName: String, + val phoneNumber: String?, +) diff --git a/src/main/kotlin/com/coded/spring/ordering/services/UsersClient.kt b/src/main/kotlin/com/coded/spring/ordering/services/UsersClient.kt new file mode 100644 index 0000000..423a557 --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/services/UsersClient.kt @@ -0,0 +1,36 @@ +package com.coded.spring.ordering.services + +import com.coded.spring.ordering.authentication.jwt.JwtService +import jakarta.inject.Named +import org.springframework.core.ParameterizedTypeReference +import org.springframework.http.HttpEntity +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpMethod +import org.springframework.web.client.RestTemplate +import org.springframework.web.client.exchange + + +@Named +class UsersClient( + private val jwtService: JwtService +) { + fun getUsers(): List { + val restTemplate = RestTemplate() + val url = "http://localhost:8080/users/v1/list" + + val headers = HttpHeaders() + headers.setBearerAuth(jwtService.generateToken("testuser") + ) + + val entity = HttpEntity(headers) + + val response = restTemplate.exchange>( + url = url, + method = HttpMethod.GET, + requestEntity = entity, + object : ParameterizedTypeReference>() {} + ) + + return response.body ?: listOf() + } +} diff --git a/src/main/kotlin/com/coded/spring/ordering/services/UsersProvider.kt b/src/main/kotlin/com/coded/spring/ordering/services/UsersProvider.kt new file mode 100644 index 0000000..63d444a --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/services/UsersProvider.kt @@ -0,0 +1,31 @@ +package com.coded.spring.ordering.services + +import com.coded.spring.ordering.repositories.UsersRepository +import com.coded.spring.ordering.serverCache +import jakarta.inject.Named + +val usersCache = serverCache.getMap>("users-cache") + +@Named +class UsersProvider( + private val usersRepository: UsersRepository +) { + + fun getUsers(): List { + val cachedUsers = usersCache["users"] + if (!cachedUsers.isNullOrEmpty()) { + println("Returning ${cachedUsers.size} users from cache.") + return cachedUsers + } + + println("No cached users found, calling API...") + val users = usersRepository.findAll().map { User( + id = it.id!!, + name = it.name, + username = it.username + ) } + usersCache.put("users", users) + println("Cached ${users.size} users.") + return users + } +} diff --git a/src/main/kotlin/com/coded/spring/ordering/services/UsersService.kt b/src/main/kotlin/com/coded/spring/ordering/services/UsersService.kt new file mode 100644 index 0000000..74c14fb --- /dev/null +++ b/src/main/kotlin/com/coded/spring/ordering/services/UsersService.kt @@ -0,0 +1,54 @@ +package com.coded.spring.ordering.services + +import com.coded.spring.ordering.entities.UserEntity +import com.coded.spring.ordering.repositories.UsersRepository +import jakarta.inject.Named +import org.springframework.cache.annotation.Cacheable + +@Named +class UsersService( + private val usersRepository: UsersRepository, private val usersProvider: UsersProvider +) { + + fun getUserById(userId: Long): UserEntity { + return usersRepository.findById(userId).orElseThrow() + } + + fun getUserDtoById(userId: Long): User { + val user = usersRepository.findById(userId).orElseThrow() + return User( + id = user.id!!, + name = user.name, + username = user.username + ) + } + + +// fun listUsers(): List = usersRepository.findAll().map { +// User( +// id = it.id!!, +// name = it.name, +// username = it.username, +// ) +// } + fun listUsers(): List = usersProvider.getUsers() + + fun listUsersDirect(): List = usersRepository.findAll().map { + User( + id = it.id!!, + name = it.name, + username = it.username, + ) + } + + + fun createUser(user: UserEntity): UserEntity { + return usersRepository.save(user) + } +} + +data class User( + val id: Long, + val name: String, + val username: String, +) \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3704dc6..ff1cb19 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/orderingDB +spring.datasource.username=postgres +spring.datasource.password= Lockz1998! +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect +springdoc.api-docs.path=/api-docs \ No newline at end of file diff --git a/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt b/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt index b2e2320..82ed488 100644 --- a/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt +++ b/src/test/kotlin/com/coded/spring/ordering/ApplicationTests.kt @@ -1,13 +1,114 @@ package com.coded.spring.ordering +import com.coded.spring.ordering.authentication.jwt.JwtService +import com.coded.spring.ordering.entities.UserEntity +import com.coded.spring.ordering.repositories.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.HttpEntity +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpMethod +import org.springframework.http.HttpStatus +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.test.context.ActiveProfiles +import kotlin.test.assertEquals -@SpringBootTest +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") class ApplicationTests { + companion object { + lateinit var savedUser: UserEntity + + @JvmStatic + @BeforeAll + fun setUp( + @Autowired usersRepository: UsersRepository, + @Autowired passwordEncoder: PasswordEncoder, + ) { + usersRepository.deleteAll() + savedUser = usersRepository.save( + UserEntity( + name = "coded", + age = 10, + username = "coded", + password = passwordEncoder.encode("joincoded") + ) + ) + } + } + + @Autowired + lateinit var restTemplate: TestRestTemplate + @Test - fun contextLoads() { + fun testHelloWorld(@Autowired jwtService: JwtService) { + val token = jwtService.generateToken("coded") + val headers = HttpHeaders() + headers.setBearerAuth(token) + + val requestEntity = HttpEntity(headers) + + val result = restTemplate.exchange( + "/hello", + HttpMethod.GET, + requestEntity, + String::class.java + ) + assertEquals(HttpStatus.OK, result.statusCode) + assertEquals("Hello World!", result.body) } + @Test + fun testOrderSubmit(@Autowired jwtService: JwtService) { + val token = jwtService.generateToken("coded") + val headers = HttpHeaders() + headers.setBearerAuth(token) + headers.set("Content-Type", "application/json") + + val requestBody = mapOf( + "user" to savedUser.id!!, + "restaurant" to "Testaurant", + "items" to listOf( + mapOf("name" to "Chicken Burger", "quantity" to 3) + ) + ) + + val requestEntity = HttpEntity(requestBody, headers) + val response = restTemplate.exchange( + "/orders/v1/orders", + HttpMethod.POST, + requestEntity, + String::class.java + ) + + assertEquals(HttpStatus.OK, response.statusCode) + } + + @Test + fun testProfileSubmit(@Autowired jwtService: JwtService) { + val token = jwtService.generateToken("coded") + val headers = HttpHeaders() + headers.setBearerAuth(token) + headers.set("Content-Type", "application/json") + + val requestBody = mapOf( + "firstName" to "Humoud", + "lastName" to "AlGhanim", + "phoneNumber" to "99996703" + ) + + val requestEntity = HttpEntity(requestBody, headers) + val response = restTemplate.exchange( + "/users/v1/${savedUser.id}/profile", + HttpMethod.POST, + requestEntity, + String::class.java + ) + + assertEquals(HttpStatus.OK, response.statusCode) + } } diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties new file mode 100644 index 0000000..c63d0df --- /dev/null +++ b/src/test/resources/application-test.properties @@ -0,0 +1,11 @@ +spring.application.name=Kotlin.SpringbootV2 + +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password= +spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect + +# Optional: enable H2 web console for debugging +spring.h2.console.enabled=true