Skip to content

Commit

Permalink
Refactored dto structure to reside in a single package
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoRosendo committed Dec 18, 2022
1 parent abba3d1 commit fed15e5
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import pt.up.fe.ni.website.backend.model.dto.AccountDto
import pt.up.fe.ni.website.backend.dto.entity.AccountDto
import pt.up.fe.ni.website.backend.service.AccountService

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import pt.up.fe.ni.website.backend.dto.auth.LoginDto
import pt.up.fe.ni.website.backend.dto.auth.TokenDto
import pt.up.fe.ni.website.backend.service.AuthService

data class LoginDto(
val email: String,
val password: String
)

data class TokenDto(
val token: String
)

@RestController
@RequestMapping("/auth")
class AuthController(val authService: AuthService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import pt.up.fe.ni.website.backend.model.dto.EventDto
import pt.up.fe.ni.website.backend.dto.entity.EventDto
import pt.up.fe.ni.website.backend.service.EventService

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import pt.up.fe.ni.website.backend.model.dto.PostDto
import pt.up.fe.ni.website.backend.dto.entity.PostDto
import pt.up.fe.ni.website.backend.service.PostService

@RestController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import pt.up.fe.ni.website.backend.model.dto.ProjectDto
import pt.up.fe.ni.website.backend.dto.entity.ProjectDto
import pt.up.fe.ni.website.backend.service.ProjectService

@RestController
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pt.up.fe.ni.website.backend.dto.auth

data class LoginDto(
val email: String,
val password: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pt.up.fe.ni.website.backend.dto.auth

data class TokenDto(
val token: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pt.up.fe.ni.website.backend.model.dto
package pt.up.fe.ni.website.backend.dto.entity

import pt.up.fe.ni.website.backend.model.Account
import java.util.Date
Expand All @@ -13,4 +13,4 @@ class AccountDto(
val linkedin: String?,
val github: String?,
val websites: List<CustomWebsiteDto>?
) : Dto<Account>()
) : EntityDto<Account>()
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pt.up.fe.ni.website.backend.model.dto
package pt.up.fe.ni.website.backend.dto.entity

import pt.up.fe.ni.website.backend.model.CustomWebsite

class CustomWebsiteDto(
val url: String,
val iconPath: String?
) : Dto<CustomWebsite>()
) : EntityDto<CustomWebsite>()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pt.up.fe.ni.website.backend.model.dto
package pt.up.fe.ni.website.backend.dto.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.databind.ObjectMapper
Expand All @@ -9,7 +9,7 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.jvm.jvmErasure

open class Dto<T : Any> {
abstract class EntityDto<T : Any> {

@JsonIgnore
private val entityClass: KClass<T>
Expand Down Expand Up @@ -47,7 +47,7 @@ open class Dto<T : Any> {
}

object DtoReflectionUtils {
private val typeArgumentCache = HashMap<KClass<out Dto<*>>, KClass<*>>()
private val typeArgumentCache = HashMap<KClass<out EntityDto<*>>, KClass<*>>()

/*
* @Suppress("UNCHECKED_CAST") is a hint to the compiler to suppress all warnings which are related to unchecked
Expand Down Expand Up @@ -77,8 +77,8 @@ open class Dto<T : Any> {
* ```
*/
@Suppress("UNCHECKED_CAST")
private fun <T : Any> getTypeConversionClass(clazz: KClass<out Dto<T>>): KClass<T>? {
val thisType = clazz.supertypes.first { it.classifier == Dto::class }
private fun <T : Any> getTypeConversionClass(clazz: KClass<out EntityDto<T>>): KClass<T>? {
val thisType = clazz.supertypes.first { it.classifier == EntityDto::class }

/*
* I don't really know *how* jvmErasure works...
Expand All @@ -101,8 +101,11 @@ open class Dto<T : Any> {
* determination but we still ensure it's an entity before putting it on the cache.
*/
@Suppress("UNCHECKED_CAST")
fun <T : Any> getTypeConversionClassWithCache(clazz: KClass<out Dto<T>>, conversionClass: KClass<T>? = null): KClass<T> {
if (clazz == Dto::class) throw IllegalCallerException("DTO is not extended by any class")
fun <T : Any> getTypeConversionClassWithCache(
clazz: KClass<out EntityDto<T>>,
conversionClass: KClass<T>? = null
): KClass<T> {
if (clazz == EntityDto::class) throw IllegalCallerException("DTO is not extended by any class")
if (!typeArgumentCache.containsKey(clazz)) {
val typeArgumentErasure = conversionClass ?: getTypeConversionClass(clazz)
typeArgumentCache[clazz] = ensureEntity(typeArgumentErasure)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pt.up.fe.ni.website.backend.model.dto
package pt.up.fe.ni.website.backend.dto.entity

import pt.up.fe.ni.website.backend.model.Event
import java.util.Date
Expand All @@ -7,4 +7,4 @@ class EventDto(
val title: String,
val description: String,
val date: Date
) : Dto<Event>()
) : EntityDto<Event>()
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pt.up.fe.ni.website.backend.model.dto
package pt.up.fe.ni.website.backend.dto.entity

import pt.up.fe.ni.website.backend.model.Post

class PostDto(
val title: String,
val body: String,
val thumbnailPath: String
) : Dto<Post>()
) : EntityDto<Post>()
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pt.up.fe.ni.website.backend.model.dto
package pt.up.fe.ni.website.backend.dto.entity

import pt.up.fe.ni.website.backend.model.Project

class ProjectDto(
val title: String,
val description: String,
val isArchived: Boolean = false
) : Dto<Project>()
) : EntityDto<Project>()
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package pt.up.fe.ni.website.backend.service
import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.dto.entity.AccountDto
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.model.dto.AccountDto
import pt.up.fe.ni.website.backend.repository.AccountRepository

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pt.up.fe.ni.website.backend.service

import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.dto.entity.EventDto
import pt.up.fe.ni.website.backend.model.Event
import pt.up.fe.ni.website.backend.model.dto.EventDto
import pt.up.fe.ni.website.backend.repository.EventRepository

@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package pt.up.fe.ni.website.backend.service

import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.dto.entity.PostDto
import pt.up.fe.ni.website.backend.model.Post
import pt.up.fe.ni.website.backend.model.dto.PostDto
import pt.up.fe.ni.website.backend.repository.PostRepository

@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package pt.up.fe.ni.website.backend.service

import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.dto.entity.ProjectDto
import pt.up.fe.ni.website.backend.model.Project
import pt.up.fe.ni.website.backend.model.dto.ProjectDto
import pt.up.fe.ni.website.backend.repository.ProjectRepository

@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import org.springframework.test.web.servlet.post
import pt.up.fe.ni.website.backend.dto.auth.LoginDto
import pt.up.fe.ni.website.backend.dto.auth.TokenDto
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.model.CustomWebsite
import pt.up.fe.ni.website.backend.repository.AccountRepository
Expand Down

0 comments on commit fed15e5

Please sign in to comment.