-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into feature/auth
- Loading branch information
Showing
20 changed files
with
721 additions
and
48 deletions.
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
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
46 changes: 43 additions & 3 deletions
46
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
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
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 | ||
import javax.validation.constraints.NotEmpty | ||
|
||
@Entity | ||
class CustomWebsite( | ||
@JsonProperty(required = true) | ||
@field:NotEmpty | ||
@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
33 changes: 33 additions & 0 deletions
33
src/test/kotlin/pt/up/fe/ni/website/backend/annotations/validation/NullOrNotBlankTest.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,33 @@ | ||
package pt.up.fe.ni.website.backend.annotations.validation | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
internal class NullOrNotBlankTest { | ||
@Test | ||
fun `should succeed when null`() { | ||
val validator = NullOrNotBlankValidator() | ||
validator.initialize(NullOrNotBlank()) | ||
assert(validator.isValid(null, null)) | ||
} | ||
|
||
@Test | ||
fun `should succeed when not blank`() { | ||
val validator = NullOrNotBlankValidator() | ||
validator.initialize(NullOrNotBlank()) | ||
assert(validator.isValid("not blank", null)) | ||
} | ||
|
||
@Test | ||
fun `should fail when empty`() { | ||
val validator = NullOrNotBlankValidator() | ||
validator.initialize(NullOrNotBlank()) | ||
assert(!validator.isValid("", null)) | ||
} | ||
|
||
@Test | ||
fun `should fail when blank`() { | ||
val validator = NullOrNotBlankValidator() | ||
validator.initialize(NullOrNotBlank()) | ||
assert(!validator.isValid(" ", null)) | ||
} | ||
} |
Oops, something went wrong.