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

Dev v1 niuchang #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 更新日志

### 2023-2-10
1. 初始化项目,采用Srpingboot+H2来实现简单的CRUD,API满足restful规范
2. 添加UserService单元测试
3. 封装统一返回参数JsonResult

由于是简单的demo,所以在代码可维护的前提下,没有直接采用maven分模块的形式来分层,而是采用包的形式

启动入口是UserApplication的main方法

### 2023-2-11
1. 添加CHANGE_LOG.md
2. 添加TokenService,说明终端用户的访问控制
3. 添加logback-spring.xml
4. 添加FriendsController描述如何维护关注和取消关注API等
5. 在FriendsController添加如何获取附近的朋友API
6. 添加QUICK_START.md,介绍项目启动及API描述
72 changes: 72 additions & 0 deletions QUICK_START.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 快速开始

### 项目启动
1. 找到UserApplication.java
2. 运行main

## API介绍
### UserController
1. 查询用户API

| URI | GET /api/user/get/{id} |
| ---- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":{"id":"xxx",//user ID<br />"name":"test",//user name<br />"dob":"",//date of birth<br />"address":"",//user address<br />"description":"",//user description<br />"createdAt":""//user created date}<br />} |

2. 新增用户API

| URI | POST /api/user/create |
| ---- |------------------------------------------------------------------------------------------------------------------|
| 入参名称 | name // user name<br /> dob // date of birth<br /> address // user address<br /> description // user description |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

3. 修改用户API

| URI | PUT /api/user/update |
|------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID<br /> name // user name<br /> dob // date of birth<br /> address // user address<br /> description // user description |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

4. 删除用户API

| URI | DELETE /api/user/delete |
|------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

### FriendsController
1. 关注API

| URI | POST /api/friends/follow |
|--------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID <br /> followedUserId // followed user ID |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

2. 取消关注API

| URI | POST /api/friends/follow/cancel |
|---------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID <br /> followedUserId // followed user ID |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

3. 查询关注我的人API

| URI | GET /api/friends/fans |
|-------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

4. 查询我关注的人API

| URI | GET /api/friends/followers |
|-------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | id // user ID |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

5. 查询我附近的朋友API

| URI | POST /api/friends/follow/cancel |
|-------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| 入参名称 | name // user name |
| 返回结果 | {<br />"code":0,<br />"msg":"",<br />"data":""<br />} |

85 changes: 85 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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>

<!-- 理论上不应该直接依赖这个原生的3方包,应该是内部封装过的,方便版本控制和特性扩展 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<!-- 应用名及版本 -->
<groupId>com.wiredcraft</groupId>
<artifactId>test-backend-java</artifactId>
<version>1.0-SNAPSHOT</version>

<name>test-backend-java</name>

<description>Demo project</description>

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

<dependencies>

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

<!-- 数据库依赖 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<!-- 单元测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
16 changes: 16 additions & 0 deletions src/main/java/com/wiredcraft/test/user/UserApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.wiredcraft.test.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Boot Starter
*/
@SpringBootApplication
public class UserApplication {

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

}
12 changes: 12 additions & 0 deletions src/main/java/com/wiredcraft/test/user/auth/TokenService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wiredcraft.test.user.auth;

/**
* 用于终端用户访问,一般设计成token+3方扫码形式
* 用户通过3方扫码登录后拿到token,再通过角色访问控制来限制资源的访问
*
* 企业级控制一般需要前端和后端配合来实现控制,设计如下
* 资源级别:菜单,页面,按钮
* 角色和资源绑定,用户和角色绑定
*/
public class TokenService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.wiredcraft.test.user.controller;

import com.wiredcraft.test.user.model.vo.UserVO;
import com.wiredcraft.test.user.service.FriendsService;
import com.wiredcraft.test.user.tool.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Friends API
*/
@RestController
@RequestMapping("/api/friends")
public class FriendsController {

@Autowired
private FriendsService friendsService;

/**
* 关注
*
* @param userId 当前用户
* @param followedUserId 被关注人
* @return
*/
@PostMapping("/follow")
public JsonResult<Void> follow(@RequestParam int userId, @RequestParam int followedUserId) {
friendsService.follow(followedUserId, userId);
return JsonResult.success();
}

/**
* 取消关注
*
* @param userId 当前用户
* @param followUsereId 被关注的人
* @return
*/
@PostMapping("/follow/cancel")
public JsonResult<Void> cancelFollow(@RequestParam int userId, @RequestParam int followUsereId) {
friendsService.cannelFollow(followUsereId, userId);
return JsonResult.success();
}

/**
* 关注我的人
*
* @param userId
* @return
*/
@GetMapping("/fans")
public JsonResult<List<UserVO>> fans(@RequestParam int userId) {
List<UserVO> fans = friendsService.fans(userId);
return JsonResult.success(fans);
}

/**
* 我关注的人
*
* @param userId
* @return
*/
@GetMapping("/followers")
public JsonResult<List<UserVO>> followers(@RequestParam int userId) {
List<UserVO> followers = friendsService.fans(userId);
return JsonResult.success(followers);
}

/**
* 关注我的数量
*
* @param userId
* @return
*/
@GetMapping("/fans/count")
public JsonResult<Integer> countFans(@RequestParam int userId) {
return JsonResult.success();
}

/**
* 我关注的数量
*
* @param userId
* @return
*/
@GetMapping("/followers/count")
public JsonResult<Integer> countFollowers(@RequestParam int userId) {
return JsonResult.success();
}

/**
* 获取附近的朋友
*
* @param name 用户名
* @return
*/
@GetMapping("/nearby")
public JsonResult<List<UserVO>> nearbyFriends(@RequestParam String name) {

// 1 根据name查询坐标,查用户表

// 2 查询我的朋友的id,查关系表

// 3 半径内符合条件的朋友,查用户表,条件是坐标范围和朋友ID集合

return JsonResult.success();
}

}
Loading