Skip to content

Commit 7120936

Browse files
tanakaryotanakaryo
tanakaryo
authored and
tanakaryo
committed
#1 add new resource.
1 parent 5b60e18 commit 7120936

File tree

12 files changed

+245
-5
lines changed

12 files changed

+245
-5
lines changed

java/microservice/app1/department-service/src/main/java/jp/co/app/mcrsrvc/dptmnt/service/impl/DepartmentServiceImpl.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package jp.co.app.mcrsrvc.dptmnt.service.impl;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.stereotype.Service;
54

65
import jp.co.app.mcrsrvc.dptmnt.entity.Department;
76
import jp.co.app.mcrsrvc.dptmnt.repository.DepartmentRepository;
87
import jp.co.app.mcrsrvc.dptmnt.service.DepartmentService;
98
import lombok.AllArgsConstructor;
10-
import lombok.extern.slf4j.Slf4j;
11-
129

1310
@Service
1411
@AllArgsConstructor
15-
@Slf4j
1612
public class DepartmentServiceImpl implements DepartmentService {
1713

1814
private DepartmentRepository departmentRepository;
@@ -26,5 +22,5 @@ public Department saveDepartment(Department department) {
2622
public Department getDepartmentById(Long departmentId) {
2723
return departmentRepository.findById(departmentId).get();
2824
}
29-
25+
3026
}

java/microservice/app1/user-service/pom.xml

+35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@
2121
<groupId>org.springframework.boot</groupId>
2222
<artifactId>spring-boot-starter</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
<version>3.2.2</version>
33+
</dependency>
34+
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
35+
<dependency>
36+
<groupId>com.mysql</groupId>
37+
<artifactId>mysql-connector-j</artifactId>
38+
<version>8.3.0</version>
39+
</dependency>
40+
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
41+
<dependency>
42+
<groupId>org.projectlombok</groupId>
43+
<artifactId>lombok</artifactId>
44+
<version>1.18.30</version>
45+
<scope>provided</scope>
46+
</dependency>
47+
<!-- https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api -->
48+
<dependency>
49+
<groupId>jakarta.persistence</groupId>
50+
<artifactId>jakarta.persistence-api</artifactId>
51+
<version>3.1.0</version>
52+
</dependency>
53+
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
54+
<dependency>
55+
<groupId>org.hibernate.orm</groupId>
56+
<artifactId>hibernate-core</artifactId>
57+
<version>6.4.3.Final</version>
58+
</dependency>
2459

2560
<dependency>
2661
<groupId>org.springframework.boot</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package jp.co.app.mcrsrvc.usr.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.client.RestTemplate;
6+
7+
@Configuration
8+
public class UserAppConfiguration {
9+
10+
@Bean
11+
public RestTemplate restTemplate() {
12+
return new RestTemplate();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package jp.co.app.mcrsrvc.usr.controller;
2+
3+
import org.springframework.web.bind.annotation.RestController;
4+
5+
import jp.co.app.mcrsrvc.usr.dto.ResponseDto;
6+
import jp.co.app.mcrsrvc.usr.entity.User;
7+
import jp.co.app.mcrsrvc.usr.service.UserService;
8+
import lombok.AllArgsConstructor;
9+
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
18+
19+
@RestController
20+
@RequestMapping("api/users")
21+
@AllArgsConstructor
22+
public class UserController {
23+
24+
private UserService userService;
25+
26+
@PostMapping
27+
public ResponseEntity<User> saveUser(@RequestBody User user) {
28+
User savedUser = this.userService.saveUser(user);
29+
return new ResponseEntity<>(savedUser, HttpStatus.CREATED);
30+
}
31+
32+
@GetMapping("{id}")
33+
public ResponseEntity<ResponseDto> getUser(@PathVariable("id") Long userId) {
34+
ResponseDto responseDto = this.userService.getUser(userId);
35+
return ResponseEntity.ok(responseDto);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package jp.co.app.mcrsrvc.usr.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class DepartmentDto {
11+
private Long id;
12+
private String departmentName;
13+
private String departmentAddress;
14+
private String departmentCode;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package jp.co.app.mcrsrvc.usr.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class ResponseDto {
11+
private DepartmentDto departmentDto;
12+
private UserDto userDto;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package jp.co.app.mcrsrvc.usr.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class UserDto {
11+
private Long id;
12+
private String firstName;
13+
private String lastName;
14+
private String email;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package jp.co.app.mcrsrvc.usr.entity;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.GeneratedValue;
6+
import jakarta.persistence.GenerationType;
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.Table;
9+
import lombok.AllArgsConstructor;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
13+
@Entity
14+
@Table(name = "users")
15+
@Data
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
public class User {
19+
20+
@Id
21+
@GeneratedValue(strategy = GenerationType.IDENTITY)
22+
private Long id;
23+
private String firstName;
24+
private String lastName;
25+
26+
@Column(nullable = false, unique = true)
27+
private String email;
28+
private String departmentId;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package jp.co.app.mcrsrvc.usr.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import jp.co.app.mcrsrvc.usr.entity.User;
6+
7+
public interface UserRepository extends JpaRepository<User, Long> {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package jp.co.app.mcrsrvc.usr.service;
2+
3+
import jp.co.app.mcrsrvc.usr.dto.ResponseDto;
4+
import jp.co.app.mcrsrvc.usr.entity.User;
5+
6+
public interface UserService {
7+
User saveUser(User user);
8+
9+
ResponseDto getUser(Long userId);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package jp.co.app.mcrsrvc.usr.service.impl;
2+
3+
import org.apache.logging.slf4j.SLF4JLogger;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
import jp.co.app.mcrsrvc.usr.dto.DepartmentDto;
9+
import jp.co.app.mcrsrvc.usr.dto.ResponseDto;
10+
import jp.co.app.mcrsrvc.usr.dto.UserDto;
11+
import jp.co.app.mcrsrvc.usr.entity.User;
12+
import jp.co.app.mcrsrvc.usr.repository.UserRepository;
13+
import jp.co.app.mcrsrvc.usr.service.UserService;
14+
import lombok.AllArgsConstructor;
15+
import lombok.extern.slf4j.Slf4j;
16+
17+
@Service
18+
@AllArgsConstructor
19+
@Slf4j
20+
public class UserServiceImpl implements UserService {
21+
22+
private UserRepository userRepository;
23+
24+
private RestTemplate restTemplate;
25+
26+
27+
@Override
28+
public User saveUser(User user) {
29+
return userRepository.save(user);
30+
}
31+
32+
@Override
33+
public ResponseDto getUser(Long userId) {
34+
ResponseDto responseDto = new ResponseDto();
35+
User user = userRepository.findById(userId).get();
36+
UserDto userDto = this.mapToUser(user);
37+
38+
ResponseEntity<DepartmentDto> responseEntity = restTemplate.getForEntity(
39+
"http://localhost:8081/api/departments/" + user.getDepartmentId()
40+
, DepartmentDto.class);
41+
42+
DepartmentDto departmentDto = responseEntity.getBody();
43+
44+
System.out.println(responseEntity.getStatusCode());
45+
46+
responseDto.setUserDto(userDto);
47+
responseDto.setDepartmentDto(departmentDto);
48+
49+
return responseDto;
50+
}
51+
52+
private UserDto mapToUser(User user) {
53+
UserDto userDto = new UserDto();
54+
userDto.setId(user.getId());
55+
userDto.setFirstName(user.getFirstName());
56+
userDto.setLastName(user.getLastName());
57+
userDto.setEmail(user.getEmail());
58+
return userDto;
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/employee_db
2+
spring.datasource.username=root
3+
spring.datasource.password=
14

5+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
6+
spring.jpa.hibernate.ddl-auto=update
7+
8+
server.port=8082

0 commit comments

Comments
 (0)