Skip to content
Snippets Groups Projects
Commit aeaeddc2 authored by Tamas Bunth's avatar Tamas Bunth
Browse files

REST: Fix upcoming issues of new fields

parent a2ce9dc2
Branches
No related tags found
No related merge requests found
...@@ -8,6 +8,8 @@ import javax.persistence.* ...@@ -8,6 +8,8 @@ import javax.persistence.*
// user should be quoted, because this keyword might be resolved in // user should be quoted, because this keyword might be resolved in
// various DBMS's (e.g. postgresql) // various DBMS's (e.g. postgresql)
//@ManyToMany: use for list fields
@Entity @Entity
//@JsonRootName("user") //@JsonRootName("user")
@Table(name = "\"user\"") @Table(name = "\"user\"")
...@@ -15,11 +17,10 @@ data class User(var email: String = "", ...@@ -15,11 +17,10 @@ data class User(var email: String = "",
@JsonIgnore @JsonIgnore
var password: String = "", var password: String = "",
var token: String = "", var token: String = "",
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/mm/yyyy") var birthDate: Date = Date(), @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/mm/yyyy")
var birthDate: Date = Date(),
var deviceType: String = "", // TODO type var deviceType: String = "", // TODO type
var androidVersion: String = "", var androidVersion: Int? = null,
//@ManyToMany
//@JsonIgnore
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Id @GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0) { var id: Long = 0) {
override fun toString(): String = "User($email)" override fun toString(): String = "User($email)"
......
package mobildata.model.inout package mobildata.model.inout
import com.fasterxml.jackson.annotation.JsonRootName
import io.swagger.annotations.ApiModelProperty import io.swagger.annotations.ApiModelProperty
import io.swagger.annotations.Example
import javax.validation.constraints.NotNull import javax.validation.constraints.NotNull
import javax.validation.constraints.Pattern import javax.validation.constraints.Pattern
import javax.validation.constraints.Size import javax.validation.constraints.Size
...@@ -27,8 +25,20 @@ class Register { ...@@ -27,8 +25,20 @@ class Register {
@ApiModelProperty(example = "16/01/1996", required = false) @ApiModelProperty(example = "16/01/1996", required = false)
var birthDate: String? = "" var birthDate: String? = ""
constructor(email: String?, password: String?) { // TODO integer validation
@ApiModelProperty(example = "28", required = false)
var androidVersion: Int? = null
@Size(min = 1, max = 500, message = "invalid field size")
@ApiModelProperty(example = "Samsung Galaxy blabla bla", required = false)
var deviceType: String? = ""
constructor(email: String?, password: String?, birthDate: String? = null, androidVersion: Int? = null,
deviceType: String? = null) {
this.email = email this.email = email
this.password = password this.password = password
this.birthDate = birthDate
this.androidVersion = androidVersion
this.deviceType = deviceType
} }
} }
\ No newline at end of file
...@@ -17,5 +17,24 @@ class UpdateUser { ...@@ -17,5 +17,24 @@ class UpdateUser {
@ApiModelProperty(example = "16/01/1996", required = false) @ApiModelProperty(example = "16/01/1996", required = false)
var birthDate: String? = "" var birthDate: String? = ""
@Size(min = 1, message = "can't be empty")
@Pattern(regexp = "([1-9]\\d)", message = "must be a valid andoid API level")
@ApiModelProperty(example = "28", required = false)
var androidVersion: Int? = null
@Size(min = 1, max = 500, message = "invalid field size")
@ApiModelProperty(example = "Samsung Galaxy blabla bla", required = false)
var deviceType: String? = ""
var password: String? = null var password: String? = null
constructor(email: String?, password: String?, birthDate: String? = null, androidVersion: Int? = null,
deviceType: String? = null) {
this.email = email
this.password = password
this.birthDate = birthDate
this.androidVersion = androidVersion
this.deviceType = deviceType
}
} }
\ No newline at end of file
...@@ -52,7 +52,7 @@ class UserHandler(val repository: UserRepository, ...@@ -52,7 +52,7 @@ class UserHandler(val repository: UserRepository,
val date = service.createDate(register.birthDate) ?: Date(); val date = service.createDate(register.birthDate) ?: Date();
val user = User(email = register.email!!, password = BCrypt.hashpw(register.password, BCrypt.gensalt()), val user = User(email = register.email!!, password = BCrypt.hashpw(register.password, BCrypt.gensalt()),
birthDate = date) birthDate = date, deviceType = register.deviceType ?: "", androidVersion = register.androidVersion)
user.token = service.newToken(user) user.token = service.newToken(user)
val saved = repository.save(user) val saved = repository.save(user)
...@@ -62,7 +62,7 @@ class UserHandler(val repository: UserRepository, ...@@ -62,7 +62,7 @@ class UserHandler(val repository: UserRepository,
@ApiKeySecured @ApiKeySecured
@GetMapping("/api/user") @GetMapping("/api/user")
fun currentUser() = view(service.currentUser()) fun currentUser() = service.currentUser()
@ApiKeySecured @ApiKeySecured
@PutMapping("/api/user") @PutMapping("/api/user")
...@@ -87,7 +87,8 @@ class UserHandler(val repository: UserRepository, ...@@ -87,7 +87,8 @@ class UserHandler(val repository: UserRepository,
// update the user // update the user
val u = currentUser.copy(email = user.email ?: currentUser.email, val u = currentUser.copy(email = user.email ?: currentUser.email,
password = BCrypt.hashpw(user.password, BCrypt.gensalt()), birthDate = date) password = BCrypt.hashpw(user.password, BCrypt.gensalt()), birthDate = date,
deviceType = user.deviceType ?: "", androidVersion = user.androidVersion)
// update token only if email changed // update token only if email changed
if (currentUser.email != u.email) { if (currentUser.email != u.email) {
u.token = service.newToken(u) u.token = service.newToken(u)
...@@ -104,5 +105,4 @@ class UserHandler(val repository: UserRepository, ...@@ -104,5 +105,4 @@ class UserHandler(val repository: UserRepository,
} }
} }
fun view(user: User) = mapOf("user" to user)
} }
\ No newline at end of file
...@@ -13,12 +13,12 @@ create sequence hibernate_sequence start 1 increment 1; ...@@ -13,12 +13,12 @@ create sequence hibernate_sequence start 1 increment 1;
create table "user" ( create table "user" (
id int8 not null, id int8 not null,
bio varchar(255),
email varchar(255), email varchar(255),
image varchar(255),
password varchar(255), password varchar(255),
token varchar(255), token varchar(255),
username varchar(255), android_version INTEGER,
birth_date TIMESTAMP,
device_type VARCHAR(255),
primary key (id) primary key (id)
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment