1
This commit is contained in:
parent
1507a67630
commit
cd236638c4
18
pom.xml
18
pom.xml
@ -54,6 +54,14 @@
|
||||
<artifactId>sa-token-redis-jackson</artifactId>
|
||||
<version>1.40.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
@ -68,11 +76,6 @@
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@ -95,6 +98,11 @@
|
||||
<version>3.0.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>annotationProcessor</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,37 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.service.LikeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
public class LikeController {
|
||||
private final LikeService likeService;
|
||||
|
||||
@Autowired
|
||||
public LikeController(LikeService likeService) {
|
||||
this.likeService = likeService;
|
||||
}
|
||||
|
||||
@PostMapping("/{targetId}")
|
||||
public void like(@PathVariable("targetId") String targetId, @RequestHeader("userId") String userId) {
|
||||
likeService.like(targetId, userId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{targetId}")
|
||||
public void unlike(@PathVariable("targetId") String targetId, @RequestHeader("userId") String userId) {
|
||||
likeService.unlike(targetId, userId);
|
||||
}
|
||||
|
||||
@GetMapping("/{targetId}/hasLiked")
|
||||
public boolean hasLiked(@PathVariable("targetId") String targetId, @RequestHeader("userId") String userId) {
|
||||
return likeService.hasLiked(targetId, userId);
|
||||
}
|
||||
|
||||
@GetMapping("/{targetId}/count")
|
||||
public long getLikeCount(@PathVariable("targetId") String targetId) {
|
||||
return likeService.getLikeCount(targetId);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.common.ResponseBean;
|
||||
import com.example.demo.mapper.ArticleMapper;
|
||||
import com.example.demo.mapper.NewsMapper;
|
||||
import com.example.demo.model.Article;
|
||||
import com.example.demo.model.News;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
|
||||
public class NewsController {
|
||||
@Resource
|
||||
private NewsMapper newsMapper;
|
||||
|
||||
|
||||
|
||||
@PostMapping
|
||||
public ResponseBean<Object> addUser(@RequestBody News news) {
|
||||
int insert = newsMapper.insert(news);
|
||||
return ResponseBean.success(insert);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public News get(@RequestParam String title) {
|
||||
return (News) newsMapper.selectById(title);
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping
|
||||
public ResponseBean<Object> delete(@RequestParam String title) {
|
||||
int insert = newsMapper.deleteById(title);
|
||||
return ResponseBean.success(insert);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ResponseBean<Object> updateUser(@RequestBody News news) {
|
||||
|
||||
int update = newsMapper.updateById(news);
|
||||
return ResponseBean.success(update);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.demo.model.Article;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface NewsMapper extends BaseMapper {
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.example.demo.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@TableName("news")
|
||||
@Setter
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class News {
|
||||
private String title;
|
||||
private String content;
|
||||
private String author;
|
||||
private String date;
|
||||
}
|
36
src/main/java/com/example/demo/service/LikeService.java
Normal file
36
src/main/java/com/example/demo/service/LikeService.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.example.demo.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class LikeService {
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
public LikeService(RedisTemplate<String, Object> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
public void like(String targetId, String userId) {
|
||||
// 使用集合记录点赞用户,防止重复点赞
|
||||
redisTemplate.opsForSet().add(targetId + ":likes", userId);
|
||||
// 点赞数自增,使用字符串存储点赞数,方便后续展示
|
||||
redisTemplate.opsForValue().increment(targetId + ":likeCount", 1);
|
||||
}
|
||||
public void unlike(String targetId, String userId) {
|
||||
redisTemplate.opsForSet().remove(targetId + ":likes", userId);
|
||||
redisTemplate.opsForValue().decrement(targetId + ":likeCount", 1);
|
||||
}
|
||||
public boolean hasLiked(String targetId, String userId) {
|
||||
return redisTemplate.opsForSet().isMember(targetId + ":likes", userId);
|
||||
}
|
||||
public long getLikeCount(String targetId) {
|
||||
Long count = (Long) redisTemplate.opsForValue().get(targetId + ":likeCount");
|
||||
return count == null? 0 : count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user