Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored dto structure to reside in a single package #68

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -10,7 +10,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 @@ -102,10 +102,10 @@ open class Dto<T : Any> {
*/
@Suppress("UNCHECKED_CAST")
fun <T : Any> getTypeConversionClassWithCache(
clazz: KClass<out Dto<T>>,
clazz: KClass<out EntityDto<T>>,
conversionClass: KClass<T>? = null
): KClass<T> {
if (clazz == Dto::class) throw IllegalCallerException("DTO is not extended by any class")
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