Skip to content
Open
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
92 changes: 92 additions & 0 deletions sampleApplication/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.codingChallenge</groupId>
<artifactId>sampleApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sampleApplication</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codingChallenge.sampleApplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;

@SpringBootApplication(exclude={SecurityAutoConfiguration.class})
public class SampleApplication {

public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.codingChallenge.sampleApplication.common;

import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;

@Data
public class CommonPage<T> {
private Integer page;
private Integer perPage;
private Integer totalPages;
private Long total;
private List<T> list;

public static <T> CommonPage<T> restPage(List<T> list) {
CommonPage<T> result = new CommonPage<T>();
PageInfo<T> pageInfo = new PageInfo<T>(list);
result.setTotalPages(pageInfo.getPages());
result.setPage(pageInfo.getPageNum());
result.setPerPage(pageInfo.getPageSize());
result.setTotal(pageInfo.getTotal());
result.setList(pageInfo.getList());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.codingChallenge.sampleApplication.common;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.List;

@NoArgsConstructor
@Data
public class CommonResponse<T> {
private long code;
private String[] messages;
private List errItems;
private T result;

protected CommonResponse(long code, String message, T data) {
this.code = code;
if (messages == null) {
messages = new String[]{message};
}
result = data;
}

protected CommonResponse(long code, String[] messages, List errItems) {
this.code = code;
this.messages = messages;
this.errItems = errItems;
}

/**
* @param data
* @param message
* @param <T>
* @return
*/
public static <T> CommonResponse<T> success(T data, String message) {
return new CommonResponse<T>(HttpStatus.OK.value(), message, data);
}

/**
* @param message
* @param <T>
* @return
*/
public static <T> CommonResponse<T> success(String message) {
return new CommonResponse<T>(HttpStatus.OK.value(), message, null);
}

public static <T> CommonResponse<T> success(T data) {
return new CommonResponse<T>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
}

/**
* @param errorCode
* @param <T>
* @return
*/
public static <T> CommonResponse<T> failed(HttpStatus errorCode) {
return new CommonResponse<T>(errorCode.value(), errorCode.getReasonPhrase(), null);
}

/**
* @param errorCode
* @param message
* @param <T>
* @return
*/
public static <T> CommonResponse<T> failed(HttpStatus errorCode, String message) {
return new CommonResponse<T>(errorCode.value(), message, null);
}

/**
* @param errorCode
* @param messages
* @param errItems
* @param <T>
* @return
*/
public static <T> CommonResponse<T> failed(HttpStatus errorCode, String[] messages, List errItems) {
return new CommonResponse<T>(errorCode.value(), messages, errItems);
}

/**
* @param messages
* @param <T>
* @return
*/
public static <T> CommonResponse<T> failed(String messages) {
return new CommonResponse<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), messages, null);
}

/**
* @param <T>
* @return
*/
public static <T> CommonResponse<T> failed() {
return failed(HttpStatus.INTERNAL_SERVER_ERROR);
}

/**
* @param data
* @param <T>
* @return
*/
public static <T> CommonResponse<T> unauthorized(T data) {
return new CommonResponse<T>(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase(), data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.codingChallenge.sampleApplication.controller;

import com.codingChallenge.sampleApplication.common.CommonPage;
import com.codingChallenge.sampleApplication.common.CommonResponse;
import com.codingChallenge.sampleApplication.dto.UserInfoDto;
import com.codingChallenge.sampleApplication.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class UserController {

@Autowired
UserService userService;

@GetMapping("/users")
public CommonResponse<CommonPage<UserInfoDto>> userList(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "12") int limit) {
List<UserInfoDto> userList = userService.getUserList(page, limit);
return CommonResponse.success(CommonPage.restPage(userList));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.codingChallenge.sampleApplication.dto;

import lombok.Data;

@Data
public class ProductDto {
private Long id;
private String name;
private Integer year;
private String color;
private String pantone_value;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.codingChallenge.sampleApplication.dto;

import lombok.Data;

@Data
public class UserInfoDto {
private Long id;
private String email;
private String first_name;
private String last_name;
private String avatar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.codingChallenge.sampleApplication.entity;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class LoginEntity {
private Long id;
private String name;
private String email;
private String password;
private Boolean active;
private Integer previlige;
private LocalDateTime created_at;
private LocalDateTime updated_at;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.codingChallenge.sampleApplication.entity;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class ProductEntity {
private Long id;
private String name;
private String color;
private String pantone_value;
private String code;
private Integer year;
private LocalDateTime created_at;
private LocalDateTime updated_at;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.codingChallenge.sampleApplication.entity;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class UserEntity {
private Long id;
private String first_name;
private String last_name;
private String email;
private Integer age;
private String address;
private String city;
private String postCode;
private LocalDateTime dob;
private String avatar;
private LocalDateTime created_at;
private LocalDateTime updated_at;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.codingChallenge.sampleApplication.enums;

public enum ErrorStatus {
DATA_NOT_FOUND("Data not found");
private final String errMsg;

ErrorStatus(String errMsg) {
this.errMsg = errMsg;
}

public String getErrorMessage() {
return errMsg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.codingChallenge.sampleApplication.exception;

import com.codingChallenge.sampleApplication.enums.ErrorStatus;
import lombok.Data;

import java.text.MessageFormat;

@Data
public class ServiceException extends RuntimeException{
private static final long serialVersionUID = 123456789098L;
public ServiceException(ErrorStatus errorStatus){
super(errorStatus.getErrorMessage());
}

public ServiceException(ErrorStatus errorStatus, Object... params){
super(MessageFormat.format(errorStatus.getErrorMessage(), params));
}
}
Loading