Skip to content

Commit

Permalink
finished all
Browse files Browse the repository at this point in the history
  • Loading branch information
upangka committed May 23, 2023
1 parent 5845ad0 commit 41d0266
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 47 deletions.
5 changes: 5 additions & 0 deletions ddd/multi-layers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.alibaba.fastjson2</groupId>
Expand Down
25 changes: 25 additions & 0 deletions ddd/multi-layers/src/main/java/org/hzz/controller/TestApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.hzz.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hzz.domain.common.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "测试")
@RequestMapping("/test")
public interface TestApi {

@Operation(summary = "测试异常接口")
@GetMapping("/exception")
default Result<String> exception() {
throw new RuntimeException("测试异常");
}

@Operation(summary = "测试hello接口")
@GetMapping("/hello")
default Result<String> hello() {
return Result.success("hello Q10Viking");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.hzz.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.hzz.domain.common.Result;
import org.hzz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
public class TestController implements TestApi{

@Autowired
HttpServletRequest request;

@Operation(summary = "测试request属性注入与方法参数注入")
@ApiResponse(responseCode = "200", description = "测试request属性注入与方法参数注入")
@GetMapping("/request")
public Result<List<String>> request(HttpServletRequest methodParamRequest){
HttpServletRequest contextRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
System.out.println(this.request == methodParamRequest);
System.out.println(this.request == contextRequest);
System.out.println(methodParamRequest == contextRequest);

return Result.success(
Arrays.asList(
String.format("request: %s, methodParamRequest: %s, contextRequest: %s",
this.request, methodParamRequest, contextRequest),
String.format("request == methodParamRequest: %s, " +
"request == contextRequest: %s, " +
"methodParamRequest == contextRequest: %s",
this.request == methodParamRequest, this.request == contextRequest,methodParamRequest == contextRequest)
)
);

}
}
48 changes: 39 additions & 9 deletions ddd/multi-layers/src/main/java/org/hzz/controller/UserApi.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package org.hzz.controller;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.NotImplementedException;
import org.hzz.domain.common.PageResult;
import org.hzz.domain.common.Result;
import org.hzz.domain.dto.UserDTO;
import org.hzz.domain.dto.UserQueryDTO;
import org.hzz.domain.vo.UserVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/user")
@Tag(name = "用户管理",description = "用户管理(增删改查)",
Expand All @@ -29,9 +26,7 @@
public interface UserApi {

@Operation(summary = "查询用户信息", description = "根据用户id查询用户信息")
@ApiResponse(responseCode = "200", description = "查询成功"
/**, content = {@Content(mediaType = "application/json", schema = @Schema(implementation = Result.class))
}*/)
@ApiResponse(responseCode = "200", description = "查询成功")
@GetMapping("/queryById")
default Result<UserVO> queryById(
@Parameter(description = "用户id",required = true,example = "1220708537638920191")
Expand All @@ -40,6 +35,21 @@ default Result<UserVO> queryById(
throw new NotImplementedException("接口未实现");
}

@Operation(summary = "分页查询", description = "分页查询用户信息")
@ApiResponse(responseCode = "200", description = "分页查询成功")
@GetMapping("/query")
default Result<PageResult<List<UserVO>>> query(
@Parameter(description = "当前页(默认为1)",example = "1")
@RequestParam(name = "pageNum",defaultValue = "1",required = false)
Integer pageNum,
@Parameter(description = "每页显示条数(默认为20)",example = "20")
@RequestParam(name = "pageSize",defaultValue = "20",required = false)
Integer pageSize,
@Parameter(description = "用户查询条件")
@Valid UserQueryDTO userQueryDTO) {
throw new NotImplementedException("接口未实现");
}


@Operation(summary = "保存用户信息", description = "保存用户信息到数据库")
@ApiResponse(responseCode = "200", description = "保存成功"
Expand All @@ -51,4 +61,24 @@ default Result<String> save(
throw new NotImplementedException("接口未实现");
}

@Operation(summary = "更新用户信息", description = "更新用户信息到数据库")
@ApiResponse(responseCode = "200", description = "更新成功")
@PostMapping("/update/{id}")
default Result<String> update(
@Parameter(description = "用户id",required = true,example = "1220708537638920191")
@PathVariable("id") Long userId,
@Parameter(description = "用户信息",required = true)
@Valid @RequestBody UserDTO userDTO) {
throw new NotImplementedException("接口未实现");
}

@Operation(summary = "删除用户信息", description = "删除用户信息到数据库")
@ApiResponse(responseCode = "200", description = "删除成功")
@DeleteMapping("/delete/{id}")
default Result<String> delete(
@Parameter(description = "用户id",required = true,example = "1220708537638920191")
@NotNull @PathVariable("id") Long userId) {
throw new NotImplementedException("接口未实现");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,88 @@
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.apache.commons.lang3.StringUtils;
import org.hzz.domain.common.PageQuery;
import org.hzz.domain.common.PageResult;
import org.hzz.domain.common.Result;
import org.hzz.domain.dto.UserDTO;
import org.hzz.domain.dto.UserQueryDTO;
import org.hzz.domain.vo.UserVO;
import org.hzz.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@RestController
public class UserController implements UserApi {

@Autowired
private UserService userService;

@Override
public Result<String> save(UserDTO userDTO) {
int save = userService.save(userDTO);
if(save == 1){
return Result.success("保存用户成功");
}
throw new RuntimeException("保存用户失败");
}



@PostMapping("/update/{id}")
@Override
public Result<String> update(
@PathVariable("id") Long userId,
@RequestBody UserDTO userDTO) {

int updateCount = userService.update(userId, userDTO);
if(updateCount == 0){
throw new RuntimeException("更新用户失败");
}
return Result.success("更新用户成功");
}

@DeleteMapping("/delete/{id}")
@Override
public Result<String> delete(@PathVariable("id") Long userId) {

int deleteCount = userService.delete(userId);
if(deleteCount == 0){
throw new RuntimeException("删除用户失败");
}
return Result.success("删除用户成功");
}

@GetMapping("/query")
public Result<PageResult> query(Integer pageNum, Integer pageSize,
UserQueryDTO userQueryDTO) {

return Result.success(new PageResult());
}

@GetMapping("/hello")
public Result<String> hello() {
return Result.success("hello111");
}


@GetMapping("/exception")
public Result<String> exception() {
throw new RuntimeException("测试异常");
@Override
public Result<PageResult<List<UserVO>>> query(Integer pageNum, Integer pageSize,
UserQueryDTO userQueryDTO) {
PageQuery<UserQueryDTO> pageQuery = new PageQuery<>(pageNum, pageSize, userQueryDTO);
PageResult<List<UserDTO>> userDTOPageResult = userService.query(pageQuery);

PageResult<List<UserVO>> userVOPageResult = new PageResult<>();
BeanUtils.copyProperties(userDTOPageResult,userVOPageResult);

List<UserVO> userVOList = Optional.ofNullable(userDTOPageResult.getData())
.orElse(Collections.emptyList())
.stream()
.map(userDTO -> {
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userDTO, userVO);
return userVO;
})
.collect(Collectors.toList());

userVOPageResult.setData(userVOList);
return Result.success(userVOPageResult);
}

@Override
public Result<UserVO> queryById(String userId) {
UserDTO userDTO = userService.queryById(Long.parseLong(userId));
UserVO userVO = new UserVO();
userVO.setId(userDTO.getId());
userVO.setName(userDTO.getUsername());
userVO.setPassword(userDTO.getPassword());
userVO.setPassword(StringUtils.repeat("*",userDTO.getPassword().length()));
return Result.success(userVO);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package org.hzz.domain.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageQuery<T> {


@Schema(description = "当前页(默认为1)",example = "1")
private Integer pageNum = 1;

@Schema(description = "每页行数(默认为20)",example = "20")
private Integer pageSize = 20;

/**
* 动态查询
*/
@Schema(description = "动态查询")
private T query;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hzz.domain.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
Expand All @@ -8,26 +9,31 @@ public class PageResult<T> {
/**
* 当前页
*/
@Schema(description = "当前页",example = "1")
private Integer pageNo;

/**
* 每页行数
*/
@Schema(description = "每页行数",example = "10")
private Integer pageSize;

/**
* 总记录
*/
@Schema(description = "总记录",example = "100")
private Long total;

/**
* 总页数
*/
@Schema(description = "总页数",example = "10")
private Long pageNum;

/**
* 内容
*/
@Schema(description = "内容")
private T data;

}
11 changes: 9 additions & 2 deletions ddd/multi-layers/src/main/java/org/hzz/domain/dto/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
Expand All @@ -15,20 +16,26 @@
@AllArgsConstructor
@Builder
public class UserDTO {
@Schema(description = "用户id",example = "1220708537638920191")
private Long id;

@Schema(description = "用户名",example = "Q10Viking")
@NotBlank(message = "用户名不能为空")
private String username;

@Schema(description = "用户密码",example = "123456")
@NotBlank(message = "密码不能为空")
private String password;

@Schema(description = "用户年龄",example = "[email protected]")
@Email(message = "邮箱格式不正确")
@NotNull(message = "邮箱不能为空")
private String email;

@Schema(description = "用户年龄",example = "18")
private Integer age;

@Schema(description = "用户手机号",example = "17801054400")
private String phone;

@Schema(description = "创建时间",example = "2020-01-23 12:23:34")
private Date created;
}
Loading

0 comments on commit 41d0266

Please sign in to comment.