spring cloud : e-commerce - create user
This commit is contained in:
@@ -24,8 +24,11 @@ ext {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
|
||||
|
||||
implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.4.4'
|
||||
|
||||
runtimeOnly group: 'com.h2database', name: 'h2', version: '1.3.176'
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package com.example.userservice.controller;
|
||||
|
||||
import com.example.userservice.dto.UserDto;
|
||||
import com.example.userservice.service.UserService;
|
||||
import com.example.userservice.vo.Greeting;
|
||||
import com.example.userservice.vo.RequestUser;
|
||||
import com.example.userservice.vo.ResponseUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -13,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class UserController {
|
||||
|
||||
private final Greeting greeting;
|
||||
private final UserService userService;
|
||||
|
||||
@GetMapping("/health_check")
|
||||
public String status() {
|
||||
@@ -23,4 +32,18 @@ public class UserController {
|
||||
public String welcome() {
|
||||
return greeting.getMessage() ;
|
||||
}
|
||||
|
||||
@PostMapping("/users")
|
||||
public ResponseEntity<ResponseUser> createUser(@Valid @RequestBody RequestUser requestUser) {
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
|
||||
UserDto userDto = mapper.map(requestUser, UserDto.class);
|
||||
userService.createUser(userDto);
|
||||
|
||||
ResponseUser responseUser = mapper.map(userDto, ResponseUser.class);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(responseUser);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.userservice.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class UserDto {
|
||||
|
||||
private String email;
|
||||
private String name;
|
||||
private String pwd;
|
||||
private String userId;
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private String encryptedPwd;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.userservice.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 50, unique = true)
|
||||
private String email;
|
||||
@Column(nullable = false, length = 50)
|
||||
private String name;
|
||||
@Column(nullable = false, unique = true)
|
||||
private String userId;
|
||||
@Column(nullable = false)
|
||||
private String encryptedPwd;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.userservice.repository;
|
||||
|
||||
import com.example.userservice.entity.UserEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.userservice.service;
|
||||
|
||||
import com.example.userservice.dto.UserDto;
|
||||
|
||||
public interface UserService {
|
||||
|
||||
void createUser(UserDto userDto);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.userservice.service;
|
||||
|
||||
import com.example.userservice.dto.UserDto;
|
||||
import com.example.userservice.entity.UserEntity;
|
||||
import com.example.userservice.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.convention.MatchingStrategies;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public void createUser(UserDto userDto) {
|
||||
userDto.setUserId(UUID.randomUUID().toString());
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
UserEntity userEntity = mapper.map(userDto, UserEntity.class);
|
||||
userEntity.setEncryptedPwd("encrypted_password");
|
||||
|
||||
userRepository.save(userEntity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.userservice.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Data
|
||||
public class RequestUser {
|
||||
|
||||
@NotBlank(message = "Email can not be null")
|
||||
@Size(min = 2, message = "Email not less than 2 characters")
|
||||
private String email;
|
||||
|
||||
@NotBlank(message = "Name can not be null")
|
||||
@Size(min = 2, message = "Name not less than 2 characters")
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "Password can not be null")
|
||||
@Size(min = 8, message = "Password must be equal or grater than 8 characters")
|
||||
private String pwd;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.userservice.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ResponseUser {
|
||||
|
||||
private String email;
|
||||
private String name;
|
||||
private String userId;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 0
|
||||
port: 8081
|
||||
|
||||
spring:
|
||||
application:
|
||||
@@ -10,6 +10,11 @@ spring:
|
||||
settings:
|
||||
web-allow-others: true
|
||||
path: /h2-console
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
url: jdbc:h2:mem:testdb
|
||||
username: sa
|
||||
password:
|
||||
|
||||
eureka:
|
||||
instance:
|
||||
|
||||
Reference in New Issue
Block a user