-
Notifications
You must be signed in to change notification settings - Fork 0
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
Models and endpoints for user accounts #46
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a83578f
Changed jackson's default date format
BrunoRosendo d92dac6
Added handling for illegal argument and mismatched input exceptions
BrunoRosendo 6cd8514
Add account model and endpoints
BrunoRosendo f375df2
Fixed user websites relationship
BrunoRosendo f94810b
Fixed tests to reflect changes on dates and URLs
BrunoRosendo 9fa3312
Unit tests for NullOrNotBlankValidator
BrunoRosendo befa5fd
Fixed birthdate with wrong format
BrunoRosendo 8e18ce1
Removed unnecessary prints in tests
BrunoRosendo 503626f
Improving test DB consistency by setting dirty state after test class
BrunoRosendo 826f83c
Created tests for account endpoints
BrunoRosendo b90a373
Added validation for account's custom websites
BrunoRosendo 823edf9
Fixed typo in isEmail() validator
BrunoRosendo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/pt/up/fe/ni/website/backend/annotations/validation/NullOrNotBlank.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pt.up.fe.ni.website.backend.annotations.validation | ||
|
||
import javax.validation.Constraint | ||
import javax.validation.ConstraintValidator | ||
import javax.validation.ConstraintValidatorContext | ||
import javax.validation.Payload | ||
import kotlin.reflect.KClass | ||
|
||
@Target(AnnotationTarget.FIELD) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Constraint(validatedBy = [NullOrNotBlankValidator::class]) | ||
@MustBeDocumented | ||
annotation class NullOrNotBlank( | ||
val message: String = "must be null or not blank", | ||
val groups: Array<KClass<*>> = [], | ||
val payload: Array<KClass<Payload>> = [] | ||
) | ||
|
||
class NullOrNotBlankValidator : ConstraintValidator<NullOrNotBlank, String?> { | ||
override fun isValid(value: String?, context: ConstraintValidatorContext): Boolean { | ||
return value == null || value.isNotBlank() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/pt/up/fe/ni/website/backend/controller/AccountController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pt.up.fe.ni.website.backend.controller | ||
|
||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
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.service.AccountService | ||
|
||
@RestController | ||
@RequestMapping("/accounts") | ||
class AccountController(private val service: AccountService) { | ||
@GetMapping | ||
fun getAllAccounts() = service.getAllAccounts() | ||
|
||
@GetMapping("/{id}") | ||
fun getAccountById(@PathVariable id: Long) = service.getAccountById(id) | ||
|
||
@PostMapping("/new") | ||
fun createAccount(@RequestBody dto: AccountDto) = service.createAccount(dto) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/pt/up/fe/ni/website/backend/model/Account.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pt.up.fe.ni.website.backend.model | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import org.hibernate.validator.constraints.URL | ||
import pt.up.fe.ni.website.backend.annotations.validation.NullOrNotBlank | ||
import java.util.Date | ||
import javax.persistence.CascadeType | ||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.FetchType | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.Id | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.OneToMany | ||
import javax.persistence.Temporal | ||
import javax.persistence.TemporalType | ||
import javax.validation.constraints.Email | ||
import javax.validation.constraints.NotEmpty | ||
import javax.validation.constraints.Past | ||
import javax.validation.constraints.Size | ||
import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants | ||
|
||
@Entity | ||
class Account( | ||
@JsonProperty(required = true) | ||
@field:Size(min = Constants.Name.minSize, max = Constants.Name.maxSize) | ||
var name: String, | ||
|
||
@JsonProperty(required = true) | ||
@Column(unique = true) | ||
@field:NotEmpty | ||
@field:Email | ||
var email: String, | ||
|
||
@field:Size(min = Constants.Name.minSize, max = Constants.Name.maxSize) | ||
var bio: String?, | ||
|
||
@Temporal(TemporalType.DATE) | ||
@field:Past | ||
var birthDate: Date?, | ||
|
||
@field:NullOrNotBlank | ||
@field:URL | ||
var photoPath: String?, | ||
|
||
@field:NullOrNotBlank | ||
@field:URL | ||
var linkedin: String?, | ||
|
||
@field:NullOrNotBlank | ||
@field:URL | ||
var github: String?, | ||
|
||
@JoinColumn | ||
@OneToMany(cascade = [CascadeType.ALL], fetch = FetchType.EAGER) | ||
val websites: List<CustomWebsite>, | ||
|
||
@Id @GeneratedValue | ||
val id: Long? = null | ||
) |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/pt/up/fe/ni/website/backend/model/CustomWebsite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pt.up.fe.ni.website.backend.model | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import org.hibernate.validator.constraints.URL | ||
import pt.up.fe.ni.website.backend.annotations.validation.NullOrNotBlank | ||
import javax.persistence.Entity | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.Id | ||
|
||
@Entity | ||
class CustomWebsite( | ||
@JsonProperty(required = true) | ||
@field:URL | ||
val url: String, | ||
|
||
@field:NullOrNotBlank | ||
@field:URL | ||
val iconPath: String?, | ||
|
||
@Id @GeneratedValue | ||
val id: Long? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/pt/up/fe/ni/website/backend/model/constants/AccountConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pt.up.fe.ni.website.backend.model.constants | ||
|
||
object AccountConstants { | ||
object Name { | ||
const val minSize = 2 | ||
const val maxSize = 100 | ||
} | ||
|
||
object Bio { | ||
const val minSize = 5 | ||
const val maxSize = 500 | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/pt/up/fe/ni/website/backend/model/dto/AccountDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package pt.up.fe.ni.website.backend.model.dto | ||
|
||
import pt.up.fe.ni.website.backend.model.Account | ||
import java.util.Date | ||
|
||
class AccountDto( | ||
val name: String, | ||
val email: String, | ||
val bio: String?, | ||
val birthDate: Date?, | ||
val photoPath: String?, | ||
val linkedin: String?, | ||
val github: String?, | ||
val websites: List<CustomWebsiteDto> | ||
) : Dto<Account>() |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/pt/up/fe/ni/website/backend/model/dto/CustomWebsiteDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pt.up.fe.ni.website.backend.model.dto | ||
|
||
import pt.up.fe.ni.website.backend.model.CustomWebsite | ||
|
||
class CustomWebsiteDto( | ||
val url: String, | ||
val iconPath: String? | ||
) : Dto<CustomWebsite>() |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/pt/up/fe/ni/website/backend/repository/AccountRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pt.up.fe.ni.website.backend.repository | ||
|
||
import org.springframework.data.repository.CrudRepository | ||
import pt.up.fe.ni.website.backend.model.Account | ||
|
||
interface AccountRepository : CrudRepository<Account, Long> { | ||
fun findByEmail(email: String): Account? | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/pt/up/fe/ni/website/backend/service/AccountService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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.model.Account | ||
import pt.up.fe.ni.website.backend.model.dto.AccountDto | ||
import pt.up.fe.ni.website.backend.repository.AccountRepository | ||
|
||
@Service | ||
class AccountService(private val repository: AccountRepository) { | ||
fun getAllAccounts(): List<Account> = repository.findAll().toList() | ||
|
||
fun createAccount(dto: AccountDto): Account { | ||
repository.findByEmail(dto.email)?.let { | ||
throw IllegalArgumentException("email already exists") | ||
} | ||
|
||
val account = dto.create() | ||
return repository.save(account) | ||
} | ||
|
||
fun getAccountById(id: Long): Account = repository.findByIdOrNull(id) | ||
?: throw NoSuchElementException("account not found with id $id") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Details about these annotations' behaviour can be found at:
https://stackoverflow.com/questions/52887653/onetomany-relationship-does-not-save-the-primary-key-of-the-parent-in-the-child
https://stackoverflow.com/questions/2990799/difference-between-fetchtype-lazy-and-eager-in-java-persistence-api