提交 90907e65 authored 作者: Whispa's avatar Whispa

commit commit

上级 c5554d33
...@@ -9,7 +9,9 @@ import com.example.afrishop_v3.util.IdUtil; ...@@ -9,7 +9,9 @@ import com.example.afrishop_v3.util.IdUtil;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;
@RestController @RestController
@RequestMapping("/search") @RequestMapping("/search")
...@@ -26,6 +28,26 @@ public class SearchController { ...@@ -26,6 +28,26 @@ public class SearchController {
@PostMapping("/save") @PostMapping("/save")
public Result save(@RequestBody TbCfSearch tbCfSearch) { public Result save(@RequestBody TbCfSearch tbCfSearch) {
if( tbCfSearch == null)
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Empty body !");
String searchKeywords = tbCfSearch.getSearchKeywords();
if( searchKeywords == null || searchKeywords.trim().isEmpty())
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Empty search keyword !");
searchKeywords = searchKeywords.trim();
Optional<TbCfSearch> optional = repository.findFirstBySearchKeywords(searchKeywords);
tbCfSearch = optional.orElse(tbCfSearch);
tbCfSearch.setCreateTime(new Date());
tbCfSearch.setId(IdUtil.createIdbyUUID()); tbCfSearch.setId(IdUtil.createIdbyUUID());
tbCfSearch.setUserId(user.userId()); tbCfSearch.setUserId(user.userId());
...@@ -37,7 +59,7 @@ public class SearchController { ...@@ -37,7 +59,7 @@ public class SearchController {
@GetMapping("/getRecords") @GetMapping("/getRecords")
public Result getRecords() { public Result getRecords() {
List<TbCfSearch> list = repository.getAllByUserId(user.userId()); List<TbCfSearch> list = repository.getAllByUserIdOrderByCreateTimeDesc(user.userId());
return new Result<>(list); return new Result<>(list);
} }
......
...@@ -7,6 +7,7 @@ import lombok.Setter; ...@@ -7,6 +7,7 @@ import lombok.Setter;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import java.util.Date;
@Entity @Entity
@Getter @Getter
...@@ -31,6 +32,9 @@ public class TbCfSearch { ...@@ -31,6 +32,9 @@ public class TbCfSearch {
*/ */
private String userId; private String userId;
private Date createTime;
/** /**
* 设置:搜索ID * 设置:搜索ID
*/ */
...@@ -38,6 +42,10 @@ public class TbCfSearch { ...@@ -38,6 +42,10 @@ public class TbCfSearch {
this.id = id; this.id = id;
} }
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
/** /**
* 获取:搜索ID * 获取:搜索ID
*/ */
......
...@@ -7,13 +7,16 @@ import org.springframework.data.repository.PagingAndSortingRepository; ...@@ -7,13 +7,16 @@ import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Optional;
public interface TbCfSearchRepository extends PagingAndSortingRepository<TbCfSearch,String> { public interface TbCfSearchRepository extends PagingAndSortingRepository<TbCfSearch,String> {
List<TbCfSearch> getAllByUserId(String userId); List<TbCfSearch> getAllByUserIdOrderByCreateTimeDesc(String userId);
@Transactional @Transactional
void deleteAllByUserId(String userId); void deleteAllByUserId(String userId);
Optional<TbCfSearch> findFirstBySearchKeywords(String searchKeywords);
@Transactional @Transactional
void deleteAllByIdInAndUserId(String[] ids,String userId); void deleteAllByIdInAndUserId(String[] ids,String userId);
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论