提交 e0d1ec82 作者: 方治民

feat: 新增 UserExtension 示例

上级 a1e02477
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.domain.user;
import com.yiring.auth.domain.user.User;
import com.yiring.common.domain.BasicEntity;
import java.io.Serial;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.Comment;
/**
* 用户扩展
*
* @author Jim
* @version 0.1
* 2022/7/13 10:59
*/
@Getter
@Setter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "SYS_USER_EXTENSION")
@Comment("用户扩展表")
public class UserExtension extends BasicEntity implements Serializable {
@Serial
private static final long serialVersionUID = -1157047754883351972L;
@Comment("用户")
@OneToOne
@JoinColumn(nullable = false, unique = true)
User user;
@Comment("性别")
Integer gender;
@Comment("年龄")
Integer age;
public UserExtension(User user) {
this.user = user;
}
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.domain.user;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author Jim
* @version 0.1
* 2022/7/13 11:11
*/
@Repository
public interface UserExtensionRepository extends JpaRepository<UserExtension, Serializable> {}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.vo.user;
import com.yiring.common.jackson.MappingSerialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serial;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.FieldDefaults;
/**
* @author Jim
* @version 0.1
* 2022/7/13 11:36
*/
@ApiModel("UserExtensionVo")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class UserExtensionVo implements Serializable {
@Serial
private static final long serialVersionUID = -2251567849918281906L;
@ApiModelProperty(value = "性别", example = "男")
@MappingSerialize(mapping = "0:女,1:男")
Integer gender;
@ApiModelProperty(value = "年龄", example = "18")
Integer age;
}
/* (C) 2021 YiRing, Inc. */ /* (C) 2021 YiRing, Inc. */
package com.yiring.app.web.example; package com.yiring.app.web.example;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSupport; import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.yiring.app.constant.Code; import com.yiring.app.constant.Code;
import com.yiring.app.domain.TestTable; import com.yiring.app.domain.TestTable;
import com.yiring.app.domain.user.UserExtension;
import com.yiring.app.domain.user.UserExtensionRepository;
import com.yiring.app.exception.CodeException; import com.yiring.app.exception.CodeException;
import com.yiring.app.mapper.TestTableMapper; import com.yiring.app.mapper.TestTableMapper;
import com.yiring.app.vo.user.UserExtensionVo;
import com.yiring.auth.annotation.AuthIgnore;
import com.yiring.auth.domain.user.User;
import com.yiring.auth.util.Auths;
import com.yiring.common.core.Result; import com.yiring.common.core.Result;
import com.yiring.common.core.Status;
import com.yiring.common.param.PageParam; import com.yiring.common.param.PageParam;
import com.yiring.common.util.Commons;
import com.yiring.common.util.FileUtils; import com.yiring.common.util.FileUtils;
import com.yiring.common.vo.PageVo; import com.yiring.common.vo.PageVo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Example;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -38,8 +50,12 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -38,8 +50,12 @@ import org.springframework.web.bind.annotation.RestController;
@Api(tags = "示例", description = "Example") @Api(tags = "示例", description = "Example")
@RequestMapping("/example/") @RequestMapping("/example/")
@RestController @RestController
@RequiredArgsConstructor
public class ExampleController { public class ExampleController {
final Auths auths;
final UserExtensionRepository userExtensionRepository;
String text = "😎 Hello World"; String text = "😎 Hello World";
@GetMapping @GetMapping
...@@ -64,6 +80,7 @@ public class ExampleController { ...@@ -64,6 +80,7 @@ public class ExampleController {
return Result.ok(vo); return Result.ok(vo);
} }
@AuthIgnore
@SneakyThrows @SneakyThrows
@ApiOperation(value = "download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) @ApiOperation(value = "download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@GetMapping("download") @GetMapping("download")
...@@ -78,4 +95,19 @@ public class ExampleController { ...@@ -78,4 +95,19 @@ public class ExampleController {
List<TestTable> tests = SpringUtil.getBean(TestTableMapper.class).selectList(null); List<TestTable> tests = SpringUtil.getBean(TestTableMapper.class).selectList(null);
return Result.ok(tests.stream().map(TestTable::getName).reduce((a, b) -> a + "," + b).orElse("")); return Result.ok(tests.stream().map(TestTable::getName).reduce((a, b) -> a + "," + b).orElse(""));
} }
@SaCheckLogin
@ApiOperation("查询用户属性")
@GetMapping("findUserExtensionInfo")
public Result<UserExtensionVo> findUserExtensionInfo() {
User user = auths.getLoginUser();
Optional<UserExtension> optional = userExtensionRepository.findOne(Example.of(new UserExtension(user)));
if (optional.isEmpty()) {
return Result.no(Status.NOT_FOUND);
}
UserExtension ext = optional.get();
UserExtensionVo vo = Commons.transform(ext, UserExtensionVo.class);
return Result.ok(vo);
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论