Implement Sign-up, Add id generation type identity to Entities

This commit is contained in:
Marcel Schwarz 2020-05-09 19:12:43 +02:00
parent 46262af668
commit 8e7f9c06d3
6 changed files with 18 additions and 14 deletions

View File

@ -18,6 +18,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.mariadb.jdbc:mariadb-java-client'

View File

@ -5,9 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.*;
import java.time.Duration;
import java.util.Date;
@ -18,6 +16,7 @@ import java.util.Date;
public class TimeRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToOne
private TimetrackAccount account; // TimetrackAccount ID (Lazy)

View File

@ -16,7 +16,7 @@ import javax.persistence.Id;
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
// TODO: Permission List

View File

@ -14,7 +14,7 @@ import javax.persistence.*;
public class TimetrackAccount {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToOne
private TimetrackUser timetrackUser; // TimetrackUser Id (Lazy) [REMOVE]

View File

@ -17,9 +17,9 @@ import java.util.List;
public class TimetrackUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@UniqueElements
@Column(unique = true)
private String username;
private String password; // strip
private String firstname;

View File

@ -1,11 +1,11 @@
package de.hft.geotime.user;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@RestController
@RequestMapping("/user")
public class UserController {
@ -24,12 +24,16 @@ public class UserController {
return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname();
}
// TODO: implement register, maybe move to another class
@PostMapping("/sign-up")
public HashMap<String, Object> signUp(@RequestBody HashMap<String, Object> payload) {
return payload;
// user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
// userRepository.save(user);
public ResponseEntity<String> signUp(@RequestBody TimetrackUser user) {
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
TimetrackUser byUsername = userRepository.findFirstByUsername(user.getUsername());
if (byUsername == null) {
userRepository.save(user);
return new ResponseEntity<>("Created", HttpStatus.CREATED);
} else {
return new ResponseEntity<>("Username already exists!", HttpStatus.CONFLICT);
}
}
}