devlog


How to use Slinky

Using slinky’s generic classes #

The following project is available at paulo-e/slinky-kotlin-starter.

Having in mind our simple model “Todo” below, this is how you would go about creating a simple CRUD for this entity.

@Entity
@Table(name = "todos")
data class Todo(
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	override var id: Long?, // 2
	var name: String?,
	var description: String?,
	var completed: Boolean?
) : IGenericEntity<Long> // 1
  1. Here we extend IGenericEntity. Inside slinky, this class is used as your entity. We can easily access your ID (primary key) inside slinky this way. We also provide Long, the class we are going to use as our ID.
  2. Here we need to override our ID so slinky can access our actual ID. We use @Id and @GeneratedValue with AUTO to automatically generate our ID for us. But this is not required.

Repository #

@Repository
class TodoRepository(entityManager: EntityManager)
	: GenericRepository<Todo, Long>(Todo::class.java, entityManager)

Here we are creating a repository class that receives EntityManager through dependency injection and passes this value to slinky’s GenericRepository. Passing Todo::class.java or Class<T> is also required.

Business #

@Service
class TodoBusiness(repository: TodoRepository)
   : GenericBusiness<Todo, Long>(repository)

Inside our business (or service) class, we receive our repository (again, through dependency injection) and pass it to slinky’s GenericBusiness class.

Controller #

@RestController
@RequestMapping("/api/v1/todos") // 1
class TodoController(business: TodoBusiness)
   : GenericController<Todo, Long>(business) // 2
  1. Here we provide the base mapping we want our controller to have. Supposing we are running our server at localhost with the port 8080, we are going to access our controller at localhost:8080/api/v1/todos.
  2. One more time, we are extending slinky’s generic class to receive basic functionality.

Done #

Provided you configured your application.properties, all you need to do is to access your server URL and use the required HTTP verbs to access your resource. Our controller will accept: