-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c2f021
commit 5180984
Showing
36 changed files
with
937 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.application.name=api |
11 changes: 11 additions & 0 deletions
11
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/ApiApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.awesome.api | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
|
||
@SpringBootApplication | ||
class ApiApplication | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<ApiApplication>(*args) | ||
} |
18 changes: 18 additions & 0 deletions
18
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/CorsConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.awesome.api | ||
|
||
// CorsConfig.kt | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
class GlobalCorsConfig : WebMvcConfigurer { | ||
override fun addCorsMappings(registry: CorsRegistry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins("http://localhost:5173") | ||
.allowedMethods("GET", "POST", "PUT", "DELETE") | ||
.allowedHeaders("*") | ||
.allowCredentials(true) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/controller/BookController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.awesome.api.controller | ||
|
||
import com.awesome.api.model.Book | ||
import com.awesome.api.model.Order | ||
import com.awesome.api.service.BookService | ||
import com.awesome.api.service.OrderService | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
|
||
|
||
@RestController | ||
@RequestMapping("/api") | ||
class BookController(private val bookService: BookService, private val orderService: OrderService) { | ||
|
||
@GetMapping("/books") | ||
fun getAllBooks(): List<Book> { | ||
return bookService.getAllBooks() | ||
} | ||
|
||
@GetMapping("/books/{id}") | ||
fun getSingleBook(@PathVariable id: Int): Book { | ||
return bookService.getBookById(id) | ||
} | ||
|
||
@GetMapping("/orders") | ||
fun getOrders(): List<Order> { | ||
return orderService.getOrders() | ||
} | ||
|
||
|
||
|
||
@PostMapping("/order") | ||
fun createOrder(@RequestBody order: Order): ResponseEntity<Order> { | ||
val createdOrder = orderService.createOrder(order) | ||
return ResponseEntity(createdOrder, HttpStatus.CREATED) | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/datasource/BookDatasource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.awesome.api.datasource | ||
|
||
import com.awesome.api.model.Book | ||
|
||
interface BookDatasource { | ||
|
||
fun retrieveAll(): List<Book> | ||
|
||
fun retrieveById(id: Int): Book | ||
} |
9 changes: 9 additions & 0 deletions
9
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/datasource/BookImageResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.awesome.api.datasource | ||
|
||
import com.awesome.api.model.Book | ||
|
||
data class BookImageResponse( | ||
val book: Book, | ||
|
||
val image: ByteArray | ||
) |
90 changes: 90 additions & 0 deletions
90
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/datasource/mock/MockBookDatasource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.awesome.api.datasource.mock | ||
|
||
import com.awesome.api.datasource.BookDatasource | ||
import com.awesome.api.model.Book | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class MockBookDatasource : BookDatasource { | ||
|
||
val books = listOf( | ||
Book(0,"Drakula", "Bram Stoker", 10.99), | ||
Book(1,"Behawiorysta", "Remigiusz Mróz", 8.99), | ||
Book(2,"Dwie wieże", "J.R.R. Tolkien", 11.99), | ||
|
||
Book(3,"Jobs", "Walter Isaacson", 11.99), | ||
Book(4,"Out", "Natsu Kirimo", 11.99), | ||
Book(5,"Solaris", "Stanisław Lem", 11.99), | ||
|
||
Book(6,"Szklany Klosz", "Sylvia Plath", 11.99), | ||
Book(7,"Ziemiomorze", "Ursula K. Le Guin", 11.99), | ||
Book(8,"Miasteczko Salem", "Stephen King", 11.99), | ||
) | ||
|
||
override fun retrieveAll(): List<Book> = books | ||
|
||
|
||
override fun retrieveById(id: Int): Book = books[id] | ||
|
||
|
||
} | ||
|
||
//[ | ||
//{ | ||
// "id": 1, | ||
// "name": "Drakula", | ||
// "author": "Bram Stoker", | ||
// "price": 10.99, | ||
// "imgUrl": "/imgs/drakula.jpg" | ||
//}, | ||
//{ | ||
// "id": 2, | ||
// "name": "Dwie wieże", | ||
// "author": "J. R. Tolkien", | ||
// "price": 11.99, | ||
// "imgUrl": "/imgs/dwie-wieze.jpg" | ||
//}, | ||
//{ | ||
// "id": 3, | ||
// "name": "Jobs", | ||
// "author": "Walter Isaacson", | ||
// "price": 1.05, | ||
// "imgUrl": "/imgs/jobs.jpg" | ||
//}, | ||
//{ | ||
// "id": 4, | ||
// "name": "Out", | ||
// "author": "Natsuo Kirimo", | ||
// "price": 140, | ||
// "imgUrl": "/imgs/out.jpg" | ||
//}, | ||
//{ | ||
// "id": 5, | ||
// "name": "Solaris", | ||
// "author": "Stanisław Lem", | ||
// "price": 11.99, | ||
// "imgUrl": "/imgs/solaris.jpg" | ||
//}, | ||
//{ | ||
// "id": 6, | ||
// "name": "Szklany klosz", | ||
// "author": "Sylvia Plath", | ||
// "price": 1.05, | ||
// "imgUrl": "/imgs/szklany-klosz.jpg" | ||
//}, | ||
//{ | ||
// "id": 7, | ||
// "name": "Ziemiomorze", | ||
// "author": "Ursula K. Le Guin", | ||
// "price": 350, | ||
// "imgUrl": "/imgs/ziemiomorze.jpg" | ||
//}, | ||
//{ | ||
// "id": 8, | ||
// "name": "Miasteczko Salem", | ||
// "author": "Stephen King", | ||
// "price": 32.00, | ||
// "imgUrl": "/imgs/miasteczko-salem.jpg" | ||
//} | ||
//] | ||
|
9 changes: 9 additions & 0 deletions
9
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/model/Book.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.awesome.api.model | ||
|
||
|
||
data class Book( | ||
var id: Int, | ||
val name: String, | ||
val author: String, | ||
val price: Double, | ||
) |
12 changes: 12 additions & 0 deletions
12
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/model/Order.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.awesome.api.model | ||
|
||
class OrderBook ( | ||
val id: Int, | ||
val quantity: Int | ||
) | ||
|
||
data class Order ( | ||
var id: Int, | ||
var name: String, | ||
var books: List<OrderBook> // Map<bookId, quantity> | ||
) |
22 changes: 22 additions & 0 deletions
22
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/repository/MockOrderRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.awesome.api.repository | ||
|
||
import com.awesome.api.model.Order | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class MockOrderRepo : OrderRepo { | ||
|
||
private val orders = mutableMapOf<Int, Order>() | ||
private var nextId: Int = 1 | ||
|
||
override fun getAllOrders(): List<Order> { | ||
return orders.values.toList() | ||
} | ||
|
||
override fun createOrder(order: Order): Order { | ||
order.id = nextId++ | ||
orders[order.id] = order | ||
return order | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/repository/OrderRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.awesome.api.repository | ||
|
||
import com.awesome.api.model.Order | ||
|
||
interface OrderRepo { | ||
fun getAllOrders(): List<Order> | ||
fun createOrder(order: Order): Order | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/service/BookService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.awesome.api.service | ||
|
||
import com.awesome.api.datasource.mock.MockBookDatasource | ||
import com.awesome.api.model.Book | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class BookService(private val bookDatasource: MockBookDatasource) { | ||
|
||
fun getAllBooks(): List<Book> = bookDatasource.retrieveAll() | ||
|
||
fun getBookById(id: Int): Book = bookDatasource.retrieveById(id) | ||
} |
17 changes: 17 additions & 0 deletions
17
Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/service/OrderService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.awesome.api.service | ||
|
||
import com.awesome.api.model.Order | ||
import com.awesome.api.repository.MockOrderRepo | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class OrderService(private val orderRepo: MockOrderRepo) { | ||
|
||
fun createOrder(order: Order): Order { | ||
return orderRepo.createOrder(order) | ||
} | ||
|
||
fun getOrders(): List<Order> { | ||
return orderRepo.getAllOrders() | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Zad7 - Sonar/booksBackend/bin/test/com/awesome/api/ApiApplicationTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.awesome.api | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@SpringBootTest | ||
class ApiApplicationTests { | ||
|
||
@Test | ||
fun contextLoads() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id("org.springframework.boot") version "3.2.5" | ||
id("io.spring.dependency-management") version "1.1.4" | ||
kotlin("jvm") version "1.9.23" | ||
kotlin("plugin.spring") version "1.9.23" | ||
} | ||
|
||
group = "com.awesome" | ||
version = "0.0.1-SNAPSHOT" | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin") | ||
implementation("org.jetbrains.kotlin:kotlin-reflect") | ||
testImplementation("org.springframework.boot:spring-boot-starter-test") | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs += "-Xjsr305=strict" | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
Zad7 - Sonar/booksBackend/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.