Skip to content

Commit

Permalink
Add ktlint to backend server
Browse files Browse the repository at this point in the history
  • Loading branch information
rabarbar15 authored Jun 8, 2024
1 parent 6c2f021 commit 5180984
Show file tree
Hide file tree
Showing 36 changed files with 937 additions and 0 deletions.
Empty file added Zad7 - Sonar/README.md
Empty file.
1 change: 1 addition & 0 deletions Zad7 - Sonar/booksBackend/bin/main/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=api
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 Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/CorsConfig.kt
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)
}
}
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)
}

}
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
}
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
)
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"
//}
//]

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 Zad7 - Sonar/booksBackend/bin/main/com/awesome/api/model/Order.kt
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>
)
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
}

}
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

}
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)
}
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()
}
}
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() {
}

}
37 changes: 37 additions & 0 deletions Zad7 - Sonar/booksBackend/build.gradle.kts
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.
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
Loading

0 comments on commit 5180984

Please sign in to comment.