feat : User init MVC
This commit is contained in:
@@ -18,6 +18,7 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-jdbc:2.7.3'
|
||||
implementation 'org.projectlombok:lombok:1.18.24'
|
||||
|
||||
// https://mvnrepository.com/artifact/org.postgresql/postgresql
|
||||
//implementation group: 'org.postgresql', name: 'postgresql', version: '42.5.0'
|
||||
|
||||
12
src/main/java/com/io/realworld/DTO/UserResponse.java
Normal file
12
src/main/java/com/io/realworld/DTO/UserResponse.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.io.realworld.DTO;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
|
||||
public class UserResponse {
|
||||
|
||||
private String email;
|
||||
private String token;
|
||||
private String username;
|
||||
private String bio;
|
||||
private String image;
|
||||
}
|
||||
19
src/main/java/com/io/realworld/DTO/UserSignupRequest.java
Normal file
19
src/main/java/com/io/realworld/DTO/UserSignupRequest.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.io.realworld.DTO;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class UserSignupRequest {
|
||||
|
||||
@NotBlank
|
||||
private String username;
|
||||
@Email
|
||||
private String email;
|
||||
@NotBlank
|
||||
private String password;
|
||||
}
|
||||
@@ -1,13 +1,25 @@
|
||||
package com.io.realworld.api.users;
|
||||
|
||||
import com.io.realworld.DTO.UserSignupRequest;
|
||||
import com.io.realworld.DTO.UserResponse;
|
||||
import com.io.realworld.repository.User;
|
||||
import com.io.realworld.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(path = "/api")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping("/users")
|
||||
public String createUser(){
|
||||
return "hello";
|
||||
public User signup(@Valid @RequestBody UserSignupRequest userSignupRequest){
|
||||
return userService.signup(userSignupRequest);
|
||||
}
|
||||
}
|
||||
|
||||
33
src/main/java/com/io/realworld/repository/User.java
Normal file
33
src/main/java/com/io/realworld/repository/User.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.io.realworld.repository;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Table(name = "users")
|
||||
@Entity
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
@Builder
|
||||
public User(String username, String email, String password){
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public static User of(String username, String email, String password){
|
||||
return new User(username,email,password);
|
||||
}
|
||||
|
||||
protected User(){
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.io.realworld.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User,Long> {
|
||||
User save(User user);
|
||||
|
||||
}
|
||||
21
src/main/java/com/io/realworld/service/UserService.java
Normal file
21
src/main/java/com/io/realworld/service/UserService.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.io.realworld.service;
|
||||
|
||||
import com.io.realworld.DTO.UserSignupRequest;
|
||||
import com.io.realworld.DTO.UserResponse;
|
||||
import com.io.realworld.repository.User;
|
||||
import com.io.realworld.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
public User signup(UserSignupRequest userSignupRequest){
|
||||
System.out.println(userSignupRequest.getUsername());
|
||||
return userRepository.save(User.of(userSignupRequest.getUsername(),
|
||||
userSignupRequest.getEmail(),
|
||||
userSignupRequest.getPassword()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,10 +2,13 @@
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:~/testdb
|
||||
spring.datasource.url=jdbc:h2:~/realworldDB
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
spring.jpq.show-sql=true
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user