mirror of https://github.com/halo-dev/halo
Add support for link group and its sort (#2105)
* fix issue 1905. * fix issue 1905. * follow suggestions. * Update src/main/java/run/halo/app/repository/LinkRepository.java Co-authored-by: John Niang <johnniang@fastmail.com> * Withdraw irrelevant modification. * Withdraw irrelevant modification. * Withdraw irrelevant modification. Co-authored-by: John Niang <johnniang@fastmail.com>pull/2120/head
parent
6d4ac6b79f
commit
c2e477fcab
|
@ -5,6 +5,8 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
|
|||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.web.SortDefault;
|
||||
|
@ -17,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import run.halo.app.model.dto.LinkDTO;
|
||||
import run.halo.app.model.dto.base.InputConverter;
|
||||
import run.halo.app.model.entity.Link;
|
||||
import run.halo.app.model.params.LinkParam;
|
||||
import run.halo.app.service.LinkService;
|
||||
|
@ -75,4 +78,26 @@ public class LinkController {
|
|||
public List<String> teams() {
|
||||
return linkService.listAllTeams();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the links in batch.
|
||||
*
|
||||
* <p>To realize the draggable sort approach for link priority,
|
||||
* a links batch update API is in demand.
|
||||
*
|
||||
* @param linkParams the modified links params.
|
||||
* @return the links after updated.
|
||||
*/
|
||||
@PutMapping("/batch")
|
||||
@ApiOperation("Updates links in batch")
|
||||
public List<LinkDTO> updateBatchBy(@RequestBody List<@Valid LinkParam> linkParams) {
|
||||
List<Link> links = linkParams
|
||||
.stream()
|
||||
.filter(linkParam -> Objects.nonNull(linkParam.getId()))
|
||||
.map(InputConverter::convertTo)
|
||||
.collect(Collectors.toList());
|
||||
return linkService.updateInBatch(links).stream()
|
||||
.map(link -> (LinkDTO) new LinkDTO().convertFrom(link))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ import run.halo.app.model.entity.Link;
|
|||
@Data
|
||||
public class LinkParam implements InputConverter<Link> {
|
||||
|
||||
private Integer id;
|
||||
|
||||
@NotBlank(message = "友情链接名称不能为空")
|
||||
@Size(max = 255, message = "友情链接名称的字符长度不能超过 {max}")
|
||||
private String name;
|
||||
|
|
|
@ -17,7 +17,7 @@ public interface LinkRepository extends BaseRepository<Link, Integer> {
|
|||
*
|
||||
* @return a list of teams
|
||||
*/
|
||||
@Query(value = "select distinct a.team from Link a")
|
||||
@Query(value = "select a.team from Link a group by a.team order by max(a.priority) DESC")
|
||||
List<String> findAllTeams();
|
||||
|
||||
boolean existsByNameAndIdNot(String name, Integer id);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -61,7 +62,7 @@ public class LinkServiceImpl extends AbstractCrudService<Link, Integer> implemen
|
|||
ServiceUtils.convertToListMap(teams, links, LinkDTO::getTeam);
|
||||
|
||||
List<LinkTeamVO> result = new LinkedList<>();
|
||||
|
||||
Map<LinkTeamVO, Integer> teamPriorities = new HashMap<>();
|
||||
// Wrap link team vo list
|
||||
teamLinkListMap.forEach((team, linkList) -> {
|
||||
// Build link team vo
|
||||
|
@ -69,13 +70,30 @@ public class LinkServiceImpl extends AbstractCrudService<Link, Integer> implemen
|
|||
linkTeamVO.setTeam(team);
|
||||
linkTeamVO.setLinks(linkList);
|
||||
|
||||
teamPriorities.put(linkTeamVO, getTeamPriority(linkTeamVO));
|
||||
// Add it to result
|
||||
result.add(linkTeamVO);
|
||||
});
|
||||
|
||||
result.sort((a, b) -> teamPriorities.get(b) - teamPriorities.get(a));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the priority of a link team, which is the maximum priority of its link members.
|
||||
*
|
||||
* @param linkTeam A team of links.
|
||||
* @return the priority of a link team.
|
||||
*/
|
||||
private Integer getTeamPriority(LinkTeamVO linkTeam) {
|
||||
return linkTeam.getLinks().stream()
|
||||
.mapToInt(LinkDTO::getPriority)
|
||||
.max()
|
||||
.orElse(-1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull List<LinkTeamVO> listTeamVosByRandom(@NonNull Sort sort) {
|
||||
Assert.notNull(sort, "Sort info must not be null");
|
||||
|
|
Loading…
Reference in New Issue