Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

employee-system-dev007-rx-java - Rewrite project with RX Java #171

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion springEmployeeSystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ docker-compose up
| API | Type | Purpose | Example cmd | Comment|
| ----- | -------- | ---- | ----- | ---- |
| http://localhost:8080/ | FE App | | |
| http://localhost:9998/swagger-ui.html | BE API | | |
| http://localhost:9998/webjars/swagger-ui/index.html | BE swagger API | | |


## Important Concepts
Expand Down
118 changes: 89 additions & 29 deletions springEmployeeSystem/backend/EmployeeSystem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<!-- <version>2.4.5</version>-->
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -17,14 +18,20 @@
<description>EmployeeSystem demo</description>

<properties>
<java.version>8</java.version>
<java.version>17</java.version>
</properties>

<dependencies>

<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </dependency>-->

<!-- Webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
Expand All @@ -33,10 +40,10 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -50,16 +57,28 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger2</artifactId>-->
<!-- <version>2.7.0</version>-->
<!-- </dependency>-->

<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger-ui</artifactId>-->
<!-- <version>2.7.0</version>-->
<!-- </dependency>-->

<!-- <dependency>-->
<!-- <groupId>org.springdoc</groupId>-->
<!-- <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>-->
<!-- <version>2.0.2</version>-->
<!-- </dependency>-->

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
<version>2.2.0</version>
</dependency>

<dependency>
Expand All @@ -68,10 +87,51 @@
<version>2.6</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->

<!--
R2DBC for mysql

https://mvnrepository.com/artifact/io.asyncer/r2dbc-mysql

https://youtu.be/_1HwzpWx5UM?si=LZsO06P998GAoVzp&t=81
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<groupId>io.asyncer</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>1.1.3</version>
</dependency>

<!--
https://youtu.be/_1HwzpWx5UM?si=LZsO06P998GAoVzp&t=81
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-r2dbc
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>3.2.5</version>
</dependency>


<!--
fix R2DBC network issue (suggested by GPT)

The error you encountered is related to the Netty library,
which is used by R2DBC under the hood for network operations.
Specifically, the error suggests that a native library required by Netty for DNS resolution on macOS is missing.
This doesn’t directly affect the execution of your SQL query but can cause network-related issues.
Fixing the Issue To resolve this issue, you can add the necessary Netty dependency for macOS.
Update your pom.xml to include the following dependency:
-->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver-dns-native-macos</artifactId>
<version>${netty.version}</version>
<classifier>osx-aarch_64</classifier>
</dependency>

<!-- log -->
Expand All @@ -85,17 +145,17 @@
<artifactId>lombok</artifactId>
</dependency>

<!--
spring monitor

endpoints:
/actuator/health
/actuator/info
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- &lt;!&ndash;-->
<!-- spring monitor-->

<!-- endpoints:-->
<!-- /actuator/health-->
<!-- /actuator/info-->
<!-- &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-actuator</artifactId>-->
<!-- </dependency>-->

<!-- mail service -->
<dependency>
Expand Down
19 changes: 18 additions & 1 deletion springEmployeeSystem/backend/EmployeeSystem/sql/dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ VALUES

-- truncate tickets;

CREATE TABLE tickets (
id INT AUTO_INCREMENT PRIMARY KEY,
subject VARCHAR(255),
description TEXT,
user_id INT,
assigned_to INT,
status VARCHAR(50),
tag VARCHAR(50)
);

INSERT INTO `tickets` (id, description, status, subject, tag, user_id, assigned_to)
VALUES
(1001, "this is a ticket ...", "PENDING", "subject 1", "tag1", 1001, 1001),
Expand All @@ -61,7 +71,14 @@ VALUES

-- truncate check_in;

INSERT INTO `check_in` (id, user_id, create_time)
CREATE TABLE checkin (
id INT NOT NULL PRIMARY KEY,
create_time DATETIME DEFAULT NULL,
user_id INT DEFAULT NULL
);


INSERT INTO `checkin` (id, user_id, create_time)
VALUES
(1001, 1, now()),
(1002, 2, now()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2 // Fix using Swagger 2.x : https://blog.51cto.com/u_15740726/5540690
// no need to add annotation for swagger 3, swagger UI can be accessed via below
// http://localhost:9998/webjars/swagger-ui/index.html
@SpringBootApplication
public class EmployeeSystemApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package EmployeeSystem.config;

// https://youtu.be/42MTtF44XAs?si=VnXx5TwPHOVeSZv5&t=53

import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

// TODO : check how it works / if is necessary
@EnableR2dbcRepositories // enable r2dbc repo : similar as JPA
@Configuration
public class R2dbcConfig {}
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package EmployeeSystem.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// TODO : check if can merge below
// 1) enable CORS 2) show swagger 2.x UI properly
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOriginPatterns("*") // SpringBoot2.4.0 [allowedOriginPatterns]代替[allowedOrigins]
.allowedMethods("*")
.maxAge(3600)
.allowCredentials(true);
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS");
}
};
}
}
//package EmployeeSystem.config;
//
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.CorsRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//
//// TODO : check if can merge below
//// 1) enable CORS 2) show swagger 2.x UI properly
//@Configuration
//public class WebConfig implements WebMvcConfigurer {
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// registry
// .addMapping("/**")
// .allowedOriginPatterns("*") // SpringBoot2.4.0 [allowedOriginPatterns]代替[allowedOrigins]
// .allowedMethods("*")
// .maxAge(3600)
// .allowCredentials(true);
// }
//
// @Bean
// public WebMvcConfigurer corsConfigurer() {
// return new WebMvcConfigurer() {
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// registry
// .addMapping("/**")
// .allowedOrigins("*")
// .allowedMethods("GET", "PUT", "POST", "PATCH", "DELETE", "OPTIONS");
// }
// };
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package EmployeeSystem.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
@EnableWebFlux
public class WebFluxConfig implements WebFluxConfigurer {
// Additional configuration if necessary
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,50 @@

import EmployeeSystem.common.ApiResponse;
import EmployeeSystem.model.Checkin;
import EmployeeSystem.model.dto.AddCheckinDto;
import EmployeeSystem.service.CheckinService;
import java.util.List;
import java.util.stream.Stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequestMapping("/checkin")
public class CheckinController {

@Autowired CheckinService checkinService;

@GetMapping("/")
public ResponseEntity<List<Checkin>> getCheckin() {
public ResponseEntity<Flux<Checkin>> getCheckin() {

List<Checkin> checkinList = checkinService.getCheckIns();
Flux<Checkin> checkinList = checkinService.getCheckIns();
return new ResponseEntity<>(checkinList, HttpStatus.OK);
}

@GetMapping("/{userId}")
public ResponseEntity<List<Checkin>> getCheckinByUserId(@PathVariable("userId") Integer userId) {
public ResponseEntity<Mono<Stream<Checkin>>> getCheckinByUserId(@PathVariable("userId") Integer userId) {

List<Checkin> checkinList = checkinService.getCheckinByUserId(userId);
Mono<Stream<Checkin>> checkinList = checkinService.getCheckinByUserId(userId);
return new ResponseEntity<>(checkinList, HttpStatus.OK);
}

@PostMapping("/add")
public ResponseEntity<ApiResponse> addCheckin(@RequestBody Checkin checkin) {
public ResponseEntity<ApiResponse> addCheckin(@RequestBody AddCheckinDto addCheckinDto) {

// if (addCheckinDto.getUserId() < 0){
// return new ResponseEntity<>(
// new ApiResponse(false, "checkin Id < 0"), HttpStatus.BAD_REQUEST);
// }

if (checkin.getUserId() < 0){
return new ResponseEntity<>(
new ApiResponse(false, "checkin Id < 0"), HttpStatus.BAD_REQUEST);
}
checkinService.addCheckin(checkin.getUserId());
log.info("(CheckinController) add checkIn, addCheckinDto = " + addCheckinDto);
Mono<Checkin> res = checkinService.addCheckin(addCheckinDto);
return new ResponseEntity<>(
new ApiResponse(true, "Checkin has been added"), HttpStatus.CREATED);
}
Expand Down
Loading
Loading