图书接口编写
This commit is contained in:
parent
2a35ec2152
commit
af67f97cb7
|
@ -57,6 +57,18 @@ public class RedisCache {
|
|||
}
|
||||
return value;
|
||||
}
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
Object value = redisTemplate.opsForValue().get(key);
|
||||
if (value == null) {
|
||||
return null; // or throw an exception, depending on your requirement
|
||||
}
|
||||
if (clazz.isInstance(value)) {
|
||||
return clazz.cast(value);
|
||||
} else {
|
||||
throw new IllegalStateException("Cached value is not of expected type " + clazz);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Object get(String key) {
|
||||
return get(key, NOT_EXPIRE);
|
||||
|
|
|
@ -16,4 +16,5 @@ auth:
|
|||
- /maku/t_book/list/search
|
||||
- /maku/t_book/{id}
|
||||
- /user/login
|
||||
- /maku/t_shopping_trolley/aaa
|
||||
|
||||
|
|
|
@ -2,11 +2,14 @@ package net.maku.maku.controller;
|
|||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.AllArgsConstructor;
|
||||
import net.maku.framework.common.cache.RedisCache;
|
||||
import net.maku.framework.common.utils.PageResult;
|
||||
import net.maku.framework.common.utils.Result;
|
||||
import net.maku.maku.convert.TBookCommentConvert;
|
||||
import net.maku.maku.entity.TBookCommentEntity;
|
||||
import net.maku.maku.entity.TUserEntity;
|
||||
import net.maku.maku.service.TBookCommentService;
|
||||
import net.maku.maku.query.TBookCommentQuery;
|
||||
import net.maku.maku.vo.TBookCommentVO;
|
||||
|
@ -15,6 +18,11 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -29,6 +37,27 @@ import java.util.List;
|
|||
@AllArgsConstructor
|
||||
public class TBookCommentController {
|
||||
private final TBookCommentService tBookCommentService;
|
||||
private final RedisCache redisCache;
|
||||
@PostMapping("/comment")
|
||||
public Result setshoppingtrolly(HttpServletRequest request, @RequestBody TBookCommentVO vo) {
|
||||
// 首先验证token的有效性,然后从Redis中获取用户信息
|
||||
String token = request.getHeader("token");
|
||||
if(token==null || token.isEmpty()){
|
||||
return Result.error("token is error");
|
||||
}
|
||||
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
|
||||
Integer userId=tUserEntity.getId();
|
||||
LocalDateTime currentTime = LocalDateTime.now();
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
|
||||
//DATE没有时区二localDateTime有时区,为了将 LocalDateTime 转换为 Date,你需要指定一个时区,因为 Date 是基于UTC的
|
||||
// 将LocalDateTime转换为ZonedDateTime,然后转换为Instant,最后转换为Date
|
||||
// ZonedDateTime zdt = currentTime.atZone(zoneId);
|
||||
// Date date = Date.from(zdt.toInstant());
|
||||
// tBookCollectionService.save(userId,bookId,date);
|
||||
return Result.ok("加入购物车成功");
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("page")
|
||||
@Operation(summary = "分页")
|
||||
|
|
|
@ -3,11 +3,16 @@ package net.maku.maku.controller;
|
|||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.AllArgsConstructor;
|
||||
import net.maku.framework.common.cache.RedisCache;
|
||||
import net.maku.framework.common.utils.PageResult;
|
||||
import net.maku.framework.common.utils.Result;
|
||||
import net.maku.maku.convert.TBookCollectionConvert;
|
||||
import net.maku.maku.convert.TBookConvert;
|
||||
import net.maku.maku.entity.TBookEntity;
|
||||
import net.maku.maku.entity.TUserEntity;
|
||||
import net.maku.maku.service.TBookCollectionService;
|
||||
import net.maku.maku.service.TBookService;
|
||||
import net.maku.maku.query.TBookQuery;
|
||||
import net.maku.maku.vo.TBookVO;
|
||||
|
@ -16,6 +21,11 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -30,6 +40,8 @@ import java.util.List;
|
|||
@AllArgsConstructor
|
||||
public class TBookController {
|
||||
private final TBookService tBookService;
|
||||
private final RedisCache redisCache;
|
||||
private final TBookCollectionService tBookCollectionService;
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "列表")
|
||||
|
@ -56,6 +68,28 @@ public class TBookController {
|
|||
return Result.ok(entity);
|
||||
}
|
||||
|
||||
@GetMapping("/collection")
|
||||
public Result setbookcollection(HttpServletRequest request, @RequestParam Integer bookId) {
|
||||
// 首先验证token的有效性,然后从Redis中获取用户信息
|
||||
String token = request.getHeader("token");
|
||||
if(token==null || token.isEmpty()){
|
||||
return Result.error("token is error");
|
||||
}
|
||||
|
||||
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
|
||||
Integer userId=tUserEntity.getId();
|
||||
LocalDateTime currentTime = LocalDateTime.now();
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
|
||||
//DATE没有时区二localDateTime有时区,为了将 LocalDateTime 转换为 Date,你需要指定一个时区,因为 Date 是基于UTC的
|
||||
// 将LocalDateTime转换为ZonedDateTime,然后转换为Instant,最后转换为Date
|
||||
ZonedDateTime zdt = currentTime.atZone(zoneId);
|
||||
Date date = Date.from(zdt.toInstant());
|
||||
tBookCollectionService.save(userId,bookId,date);
|
||||
return Result.ok("收藏图书成功");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("page")
|
||||
|
@ -63,7 +97,6 @@ public class TBookController {
|
|||
@PreAuthorize("hasAuthority('maku:t_book:page')")
|
||||
public Result<PageResult<TBookVO>> page(@ParameterObject @Valid TBookQuery query){
|
||||
PageResult<TBookVO> page = tBookService.page(query);
|
||||
|
||||
return Result.ok(page);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,14 @@ package net.maku.maku.controller;
|
|||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.AllArgsConstructor;
|
||||
import net.maku.framework.common.cache.RedisCache;
|
||||
import net.maku.framework.common.utils.PageResult;
|
||||
import net.maku.framework.common.utils.Result;
|
||||
import net.maku.maku.convert.TShoppingTrolleyConvert;
|
||||
import net.maku.maku.entity.TShoppingTrolleyEntity;
|
||||
import net.maku.maku.entity.TUserEntity;
|
||||
import net.maku.maku.service.TShoppingTrolleyService;
|
||||
import net.maku.maku.query.TShoppingTrolleyQuery;
|
||||
import net.maku.maku.vo.TShoppingTrolleyVO;
|
||||
|
@ -15,6 +18,11 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -28,7 +36,30 @@ import java.util.List;
|
|||
@Tag(name="")
|
||||
@AllArgsConstructor
|
||||
public class TShoppingTrolleyController {
|
||||
private final RedisCache redisCache;
|
||||
private final TShoppingTrolleyService tShoppingTrolleyService;
|
||||
@GetMapping("/aaa")
|
||||
public Result setshoppingtrolly(HttpServletRequest request, @RequestParam Integer bookId) {
|
||||
// 首先验证token的有效性,然后从Redis中获取用户信息
|
||||
String token = request.getHeader("token");
|
||||
if(token==null || token.isEmpty()){
|
||||
return Result.error("token is error");
|
||||
}
|
||||
|
||||
TUserEntity tUserEntity = (TUserEntity) redisCache.get(token);
|
||||
Integer userId=tUserEntity.getId();
|
||||
LocalDateTime currentTime = LocalDateTime.now();
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
|
||||
//DATE没有时区二localDateTime有时区,为了将 LocalDateTime 转换为 Date,你需要指定一个时区,因为 Date 是基于UTC的
|
||||
// 将LocalDateTime转换为ZonedDateTime,然后转换为Instant,最后转换为Date
|
||||
ZonedDateTime zdt = currentTime.atZone(zoneId);
|
||||
Date date = Date.from(zdt.toInstant());
|
||||
tShoppingTrolleyService.save(userId,bookId,date);
|
||||
return Result.ok("加入购物车成功");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("page")
|
||||
@Operation(summary = "分页")
|
||||
|
@ -41,7 +72,7 @@ public class TShoppingTrolleyController {
|
|||
|
||||
@GetMapping("{id}")
|
||||
@Operation(summary = "信息")
|
||||
@PreAuthorize("hasAuthority('maku:t_shopping_trolley:info')")
|
||||
// @PreAuthorize("hasAuthority('maku:t_shopping_trolley:info')")
|
||||
public Result<TShoppingTrolleyVO> get(@PathVariable("id") Long id){
|
||||
TShoppingTrolleyEntity entity = tShoppingTrolleyService.getById(id);
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package net.maku.maku.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import java.util.Date;
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ import net.maku.maku.vo.TBookCollectionVO;
|
|||
import net.maku.maku.query.TBookCollectionQuery;
|
||||
import net.maku.maku.entity.TBookCollectionEntity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -23,4 +25,6 @@ public interface TBookCollectionService extends BaseService<TBookCollectionEntit
|
|||
void update(TBookCollectionVO vo);
|
||||
|
||||
void delete(List<Long> idList);
|
||||
|
||||
void save(Integer userId, Integer bookId, Date currentTime);
|
||||
}
|
|
@ -6,6 +6,7 @@ import net.maku.maku.vo.TShoppingTrolleyVO;
|
|||
import net.maku.maku.query.TShoppingTrolleyQuery;
|
||||
import net.maku.maku.entity.TShoppingTrolleyEntity;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -23,4 +24,6 @@ public interface TShoppingTrolleyService extends BaseService<TShoppingTrolleyEnt
|
|||
void update(TShoppingTrolleyVO vo);
|
||||
|
||||
void delete(List<Long> idList);
|
||||
|
||||
void save(Integer userId, Integer bookId, Date date);
|
||||
}
|
|
@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import net.maku.framework.common.utils.PageResult;
|
||||
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
|
||||
import net.maku.maku.convert.TBookCollectionConvert;
|
||||
|
@ -16,6 +17,8 @@ import net.maku.maku.service.TBookCollectionService;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -62,4 +65,20 @@ public class TBookCollectionServiceImpl extends BaseServiceImpl<TBookCollectionD
|
|||
removeByIds(idList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Integer userId, Integer bookId, Date currentTime) {
|
||||
TBookCollectionEntity entity = new TBookCollectionEntity();
|
||||
entity.setUserId(userId);
|
||||
entity.setBookId(bookId);
|
||||
entity.setCreateTime(currentTime);
|
||||
// 可以根据需要设置其它字段,例如自增id字段和lastModifiedTime字段(如果需要)
|
||||
|
||||
// 假设lastModifiedTime可以为空,可以根据需要设置
|
||||
|
||||
|
||||
// 使用 MyBatis Plus 的 save 方法保存实体
|
||||
this.save(entity);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
|
|||
import net.maku.framework.common.utils.PageResult;
|
||||
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
|
||||
import net.maku.maku.convert.TShoppingTrolleyConvert;
|
||||
import net.maku.maku.entity.TBookCollectionEntity;
|
||||
import net.maku.maku.entity.TShoppingTrolleyEntity;
|
||||
import net.maku.maku.query.TShoppingTrolleyQuery;
|
||||
import net.maku.maku.vo.TShoppingTrolleyVO;
|
||||
|
@ -16,6 +17,7 @@ import net.maku.maku.service.TShoppingTrolleyService;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -62,4 +64,18 @@ public class TShoppingTrolleyServiceImpl extends BaseServiceImpl<TShoppingTrolle
|
|||
removeByIds(idList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Integer userId, Integer bookId, Date currentTime) {
|
||||
TShoppingTrolleyEntity entity = new TShoppingTrolleyEntity();
|
||||
entity.setUserId(userId);
|
||||
entity.setBookId(bookId);
|
||||
entity.setCreateTime(currentTime);
|
||||
// 可以根据需要设置其它字段,例如自增id字段和lastModifiedTime字段(如果需要)
|
||||
|
||||
// 假设lastModifiedTime可以为空,可以根据需要设置
|
||||
|
||||
// 使用 MyBatis Plus 的 save 方法保存实体
|
||||
this.save(entity);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user