diff --git a/src/main/java/run/halo/app/controller/admin/api/LinkController.java b/src/main/java/run/halo/app/controller/admin/api/LinkController.java index 2ae82277d..3993099ee 100644 --- a/src/main/java/run/halo/app/controller/admin/api/LinkController.java +++ b/src/main/java/run/halo/app/controller/admin/api/LinkController.java @@ -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 teams() { return linkService.listAllTeams(); } + + /** + * Update the links in batch. + * + *

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 updateBatchBy(@RequestBody List<@Valid LinkParam> linkParams) { + List 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()); + } } diff --git a/src/main/java/run/halo/app/model/params/LinkParam.java b/src/main/java/run/halo/app/model/params/LinkParam.java index 716d8fec1..ff283b53d 100644 --- a/src/main/java/run/halo/app/model/params/LinkParam.java +++ b/src/main/java/run/halo/app/model/params/LinkParam.java @@ -17,6 +17,8 @@ import run.halo.app.model.entity.Link; @Data public class LinkParam implements InputConverter { + private Integer id; + @NotBlank(message = "友情链接名称不能为空") @Size(max = 255, message = "友情链接名称的字符长度不能超过 {max}") private String name; diff --git a/src/main/java/run/halo/app/repository/LinkRepository.java b/src/main/java/run/halo/app/repository/LinkRepository.java index 37f9f9828..cbd1536bd 100755 --- a/src/main/java/run/halo/app/repository/LinkRepository.java +++ b/src/main/java/run/halo/app/repository/LinkRepository.java @@ -17,7 +17,7 @@ public interface LinkRepository extends BaseRepository { * * @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 findAllTeams(); boolean existsByNameAndIdNot(String name, Integer id); diff --git a/src/main/java/run/halo/app/service/impl/LinkServiceImpl.java b/src/main/java/run/halo/app/service/impl/LinkServiceImpl.java index bb916be3f..e6bc05b4c 100644 --- a/src/main/java/run/halo/app/service/impl/LinkServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/LinkServiceImpl.java @@ -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 implemen ServiceUtils.convertToListMap(teams, links, LinkDTO::getTeam); List result = new LinkedList<>(); - + Map 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 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 listTeamVosByRandom(@NonNull Sort sort) { Assert.notNull(sort, "Sort info must not be null");