Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

File renamed without changes.
File renamed without changes.
0 mvnw → thisgottawork/mvnw
100755 → 100644
File renamed without changes.
File renamed without changes.
45 changes: 39 additions & 6 deletions pom.xml → thisgottawork/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.coded.spring</groupId>
<artifactId>Ordering</artifactId>
<groupId>com.hooba</groupId>
<artifactId>thisgottawork</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Kotlin.SpringbootV2</name>
<description>Kotlin.SpringbootV2</description>
<name>thisgottawork</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
Expand All @@ -31,6 +31,10 @@
<kotlin.version>1.9.25</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -48,6 +52,10 @@
<artifactId>kotlin-stdlib</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -58,8 +66,33 @@
<artifactId>kotlin-test-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>10.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
<version>6.4.4</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>6.4.4</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hooba.thisgottawork

import com.hooba.thisgottawork.users.UserEntity
import com.hooba.thisgottawork.users.UserRepository
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 HelloWorldController(
val usersRepository: UserRepository,
){

@GetMapping("/home")
fun helloWorld() = "Welcome to online ordering!"


@PostMapping("/add-name")
fun sayMyName( @RequestBody request: SayMyNameRequest)
= usersRepository.save(
UserEntity(
id =request.id,
name = request.name,
age = request.age,
username = request.username,
password = request.password
)
)

}

data class SayMyNameRequest(
val id: Long?,
val name: String,
val age: Int,
val username: String,
val password: String
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.coded.spring.ordering
package com.hooba.thisgottawork

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Application
class ThisgottaworkApplication

fun main(args: Array<String>) {
runApplication<Application>(*args)
runApplication<ThisgottaworkApplication>(*args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hooba.thisgottawork.authentication


import com.hooba.thisgottawork.users.UserRepository
import org.springframework.security.core.userdetails.*
import org.springframework.stereotype.Service

@Service
class CustomUserDetailsService(
private val usersRepository: UserRepository
) : 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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//package com.hooba.thisgottawork.authentication
//
//import org.springframework.context.annotation.Bean
//import org.springframework.context.annotation.Configuration
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
//import org.springframework.security.crypto.password.PasswordEncoder
//
//import org.springframework.security.config.annotation.web.builders.HttpSecurity
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
//import org.springframework.security.core.userdetails.UserDetailsService
//import org.springframework.security.web.SecurityFilterChain
//
//@Configuration
//@EnableWebSecurity
//class SecurityConfig(
// private val userDetailsService: UserDetailsService
//) {
//
// @Bean
// fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder()
//
// @Bean
// fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
// http.csrf { it.disable() }
// .authorizeHttpRequests {
// it.requestMatchers("/public/**").permitAll()
// .anyRequest().authenticated()
// }
// .formLogin { it.defaultSuccessUrl("/hello", true) }
// .userDetailsService(userDetailsService)
// return http.build();
// }
//}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hooba.thisgottawork.items

import com.fasterxml.jackson.annotation.JsonBackReference
import com.hooba.thisgottawork.orders.OrderEntity
import jakarta.inject.Named
import jakarta.persistence.*
import org.springframework.data.jpa.repository.JpaRepository

@Named
interface ItemsRepository: JpaRepository<ItemsEntity, Long>

@Entity
@Table(name = "items")
data class ItemsEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,

// maps each item to its parent order using the foreign key items.order_id → orders.id
@ManyToOne
@JoinColumn(name = "order_id")
@JsonBackReference
val order: OrderEntity,
val name: String,
val price: Double
){
constructor() : this(null, OrderEntity(), "", 0.0)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.hooba.thisgottawork.orders

import com.fasterxml.jackson.annotation.JsonManagedReference
import com.hooba.thisgottawork.items.ItemsEntity
import com.hooba.thisgottawork.users.UserEntity
import jakarta.persistence.*
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDateTime

interface OrderRepository: JpaRepository<OrderEntity, Long>

@Entity
@Table(name = "orders")
data class OrderEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,

@ManyToOne
@JoinColumn(name = "user_id")
var user: UserEntity,

var restaurant: String,

@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
@JsonManagedReference
val items: List<ItemsEntity>? = null,
var timeOrdered: LocalDateTime? = null
){
constructor() : this(null, UserEntity(), "",null,null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.hooba.thisgottawork.orders

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 OrdersController(
private val ordersService: OrdersService
) {

@GetMapping("/orders")
fun getOrders() = ordersService.getOrders()

@PostMapping("/orders")
fun addOrder(@RequestBody request: OrderRequest) =
ordersService.addOrder(request)
}



// the DTO (Data Transfer Object) for our orders and items list
data class RequestItem(
val name: String,
val price: Double
)

data class OrderRequest(
val userId: Long,
val restaurant: String,
val items: List<RequestItem>
)

data class OrderResponseDTO(
val id: Long,
val username: String,
val restaurant: String,
val items: List<RequestItem>,
val timeOrdered: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.hooba.thisgottawork.orders

import com.hooba.thisgottawork.items.ItemsEntity
import com.hooba.thisgottawork.users.UserRepository
import org.springframework.stereotype.Service


@Service
class OrdersService(
private val orderRepository: OrderRepository,
private var userRepository: UserRepository
) {
fun getOrders(): List<OrderEntity> = orderRepository.findAll().filter { it.user != null }.sortedBy { it.timeOrdered }

fun addOrder(request: OrderRequest): OrderResponseDTO {
val user = userRepository.findById(request.userId).orElseThrow {
IllegalArgumentException("User with ID ${request.userId} not found")
}

val order = orderRepository.save(
OrderEntity(
user = user,
restaurant = request.restaurant
)
)

val items = request.items.map { item ->
ItemsEntity(
order = order,
name = item.name,
price = item.price
)
}

return OrderResponseDTO(
id = order.id!!,
username = user.username,
restaurant = order.restaurant,
timeOrdered = order.timeOrdered.toString(),
items = items.map {
RequestItem(name = it.name, price = it.price)
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hooba.thisgottawork.users

import jakarta.persistence.*
import org.springframework.data.jpa.repository.JpaRepository
import jakarta.inject.Named

@Named
interface UserRepository : JpaRepository<UserEntity, Long>{
fun findByUsername(userName: String): UserEntity?
fun existsByUsername(username: String): Boolean
}

@Entity
@Table(name = "users")
data class UserEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,
val name: String,
val age: Int,
val username: String,
val password: String
){
constructor() : this(null, "",0,"", "")
}
Loading