Skip to content

Commit 8154cb5

Browse files
committed
fixes made by @MihajloBOS suggestions
1 parent 15e5c50 commit 8154cb5

18 files changed

+168
-150
lines changed

src/main/java/org/example/java_kotlin/FirstJavaKotlinGradleApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.example.java_kotlin;
22

3-
import org.example.java_kotlin.config.TodoConfiguration;
3+
import org.example.java_kotlin.config.ToDoConfiguration;
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.boot.context.properties.EnableConfigurationProperties;
77

88
@SpringBootApplication
9-
@EnableConfigurationProperties(TodoConfiguration.class)
9+
@EnableConfigurationProperties(ToDoConfiguration.class)
1010
public class FirstJavaKotlinGradleApp {
1111

1212
public static void main(String[] args) {

src/main/java/org/example/java_kotlin/config/TodoConfiguration.java renamed to src/main/java/org/example/java_kotlin/config/ToDoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@Configuration
1414
@ConfigurationProperties("spring.flyway")
15-
public class TodoConfiguration {
15+
public class ToDoConfiguration {
1616

1717
@Bean
1818
public DataSource datasource(
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package org.example.java_kotlin.controller
22

33
import org.example.java_kotlin.repositories.ArticleRepository
4-
import org.example.java_kotlin.repositories.UserRepository
54
import org.springframework.http.HttpStatus
65
import org.springframework.web.bind.annotation.GetMapping
76
import org.springframework.web.bind.annotation.PathVariable
87
import org.springframework.web.bind.annotation.RequestMapping
98
import org.springframework.web.bind.annotation.RestController
109
import org.springframework.web.server.ResponseStatusException
1110

12-
/* These classes are just part of SpringBoot Kotlin tutorial */
1311
@RestController
1412
@RequestMapping("/api/article")
1513
class ArticleController(private val articleRepository: ArticleRepository) {
@@ -21,17 +19,4 @@ class ArticleController(private val articleRepository: ArticleRepository) {
2119
fun findOne(@PathVariable slug: String) = articleRepository
2220
.findBySlug(slug) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "This article does not exist!")
2321

24-
}
25-
26-
@RestController
27-
@RequestMapping("/api/user")
28-
class UserController(private val userRepository: UserRepository) {
29-
30-
@GetMapping("/")
31-
fun findAll() = userRepository.findAll()
32-
33-
@GetMapping("/{login}")
34-
fun findOne(@PathVariable login: String) = userRepository
35-
.findByLogin(login) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "This user does not exist!")
36-
3722
}

src/main/java/org/example/java_kotlin/controller/TodoController.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.example.java_kotlin.controller
22

3-
import org.example.java_kotlin.model.Todo
3+
import org.example.java_kotlin.model.ToDoEntity
44
import org.example.java_kotlin.repositories.TodoRepository
5-
import org.example.java_kotlin.service.TodoService
5+
import org.example.java_kotlin.service.ToDoService
66
import org.springframework.http.HttpStatus
77
import org.springframework.http.ResponseEntity
88
import org.springframework.util.ObjectUtils
@@ -11,33 +11,33 @@ import org.springframework.web.bind.annotation.*
1111

1212
@RestController
1313
@RequestMapping("/api/todos")
14-
class TodoController(private val todoRepository: TodoRepository, private val todoService: TodoService) {
14+
class TodoController(private val todoRepository: TodoRepository, private val toDoService: ToDoService) {
1515

1616
@GetMapping("/{todoId}")
17-
fun getTodo(@PathVariable todoId: Long): ResponseEntity<Todo> {
18-
val todo = todoService.getTodoById(todoId)
17+
fun getTodo(@PathVariable todoId: Long): ResponseEntity<ToDoEntity> {
18+
val todo = toDoService.getTodoById(todoId)
1919
if (todo.isPresent) return ResponseEntity(todo.get(), HttpStatus.OK)
2020
return ResponseEntity(HttpStatus.NOT_FOUND)
2121
}
2222

2323
@GetMapping
24-
fun getAllTodos(): ResponseEntity<List<Todo>> {
25-
val todos = todoService.getAllTodos().toList()
24+
fun getAllTodos(): ResponseEntity<List<ToDoEntity>> {
25+
val todos = toDoService.getAllTodos().toList()
2626
if (todos.isNotEmpty()) return ResponseEntity(todos, HttpStatus.OK)
2727
return ResponseEntity(HttpStatus.NOT_FOUND)
2828
}
2929

3030
@PostMapping
31-
fun createTodo(@Validated @RequestBody todo: Todo): ResponseEntity<Void> {
32-
if (todoService.createTodo(todo)) {
31+
fun createTodo(@Validated @RequestBody toDoEntity: ToDoEntity): ResponseEntity<Void> {
32+
if (toDoService.createTodo(toDoEntity)) {
3333
return ResponseEntity(HttpStatus.CREATED)
3434
}
3535
return ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR)
3636
}
3737

3838
@PutMapping("/{todoId}")
39-
fun updateTodo(@Validated @RequestBody todo: Todo, @PathVariable("todoId") todoId: Long): ResponseEntity<Void> {
40-
val todoUpdated = todoService.updateTodo(todo, todoId)
39+
fun updateTodo(@Validated @RequestBody toDoEntity: ToDoEntity, @PathVariable("todoId") todoId: Long): ResponseEntity<Void> {
40+
val todoUpdated = toDoService.updateTodo(toDoEntity, todoId)
4141
if (ObjectUtils.isEmpty(todoUpdated)) {
4242
return ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR)
4343
}
@@ -46,8 +46,8 @@ class TodoController(private val todoRepository: TodoRepository, private val tod
4646
}
4747

4848
@PatchMapping("/{todoId}")
49-
fun partialUpdateTodo(@RequestBody todo: Todo, @PathVariable todoId: Long): ResponseEntity<Void> {
50-
val patchedTodo = todoService.patchTodo(todo, todoId)
49+
fun partialUpdateTodo(@RequestBody toDoEntity: ToDoEntity, @PathVariable todoId: Long): ResponseEntity<Void> {
50+
val patchedTodo = toDoService.patchTodo(toDoEntity, todoId)
5151
if (ObjectUtils.isEmpty(patchedTodo)) {
5252
return ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR)
5353
}
@@ -56,7 +56,7 @@ class TodoController(private val todoRepository: TodoRepository, private val tod
5656

5757
@DeleteMapping("/{todoId}")
5858
fun deleteTodo(@PathVariable todoId: Long): ResponseEntity<Void> {
59-
if (todoService.deleteTodo(todoId)) {
59+
if (toDoService.deleteTodo(todoId)) {
6060
return ResponseEntity(HttpStatus.NO_CONTENT)
6161
}
6262
return ResponseEntity(HttpStatus.NOT_FOUND)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.example.java_kotlin.controller
2+
3+
import org.example.java_kotlin.repositories.UserRepository
4+
import org.springframework.http.HttpStatus
5+
import org.springframework.web.bind.annotation.GetMapping
6+
import org.springframework.web.bind.annotation.PathVariable
7+
import org.springframework.web.bind.annotation.RequestMapping
8+
import org.springframework.web.bind.annotation.RestController
9+
import org.springframework.web.server.ResponseStatusException
10+
11+
@RestController
12+
@RequestMapping("/api/user")
13+
class UserController(private val userRepository: UserRepository) {
14+
15+
@GetMapping("/")
16+
fun findAll() = userRepository.findAll()
17+
18+
@GetMapping("/{login}")
19+
fun findOne(@PathVariable login: String) = userRepository
20+
.findByLogin(login) ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "This user does not exist!")
21+
22+
}

src/main/java/org/example/java_kotlin/model/Entities.kt renamed to src/main/java/org/example/java_kotlin/model/ArticleEntity.kt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,16 @@ import org.example.java_kotlin.util.toSlug
44
import java.time.LocalDateTime
55
import javax.persistence.*
66

7-
@Entity(name="users")
8-
class User(
9-
var login: String,
10-
var firstName: String,
11-
var lastName: String,
12-
var description: String? = null,
13-
@Id
14-
@SequenceGenerator(name="user_generator", sequenceName = "user_sequence", allocationSize = 1)
15-
@GeneratedValue(generator = "user_generator")
16-
var userId: Long? = null)
17-
187
@Entity(name="article")
19-
class Article(
8+
@Table(name="article")
9+
class ArticleEntity(
2010
var title: String,
2111
var headline: String,
2212
var content: String,
23-
@ManyToOne var author: User,
13+
@ManyToOne var author: UserEntity,
2414
var slug: String = title.toSlug(),
2515
var addedAt: LocalDateTime = LocalDateTime.now(),
2616
@Id
2717
@SequenceGenerator(name="article_generator", sequenceName = "article_sequence", allocationSize = 1)
2818
@GeneratedValue(generator = "article_generator")
29-
var articleId: Long? = null)
30-
19+
var articleId: Long? = null)

src/main/java/org/example/java_kotlin/model/Todo.kt renamed to src/main/java/org/example/java_kotlin/model/ToDoEntity.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.example.java_kotlin.model
22

3-
import javax.persistence.Entity
4-
import javax.persistence.GeneratedValue
5-
import javax.persistence.Id
6-
import javax.persistence.SequenceGenerator
3+
import javax.persistence.*
74

85
@Entity
9-
data class Todo(
6+
@Table(name="todo")
7+
data class ToDoEntity(
108
@Id
119
@SequenceGenerator(name="todo_generator", sequenceName = "todo_sequence", allocationSize = 1)
1210
@GeneratedValue(generator = "todo_generator")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.java_kotlin.model
2+
3+
import javax.persistence.*
4+
5+
@Entity(name="users")
6+
@Table(name="users")
7+
class UserEntity(
8+
var login: String,
9+
var firstName: String,
10+
var lastName: String,
11+
var description: String? = null,
12+
@Id
13+
@SequenceGenerator(name="user_generator", sequenceName = "user_sequence", allocationSize = 1)
14+
@GeneratedValue(generator = "user_generator")
15+
var userId: Long? = null)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example.java_kotlin.repositories
2+
3+
import org.example.java_kotlin.model.ArticleEntity
4+
import org.springframework.data.repository.CrudRepository
5+
import org.springframework.stereotype.Repository
6+
7+
@Repository
8+
interface ArticleRepository: CrudRepository<ArticleEntity, Long> {
9+
fun findBySlug(slug: String): ArticleEntity?
10+
fun findAllByOrderByAddedAtDesc(): Iterable<ArticleEntity>
11+
}

src/main/java/org/example/java_kotlin/repositories/Repositories.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)