mirror of https://github.com/halo-dev/halo
Complete link creation api
parent
bf795f3225
commit
f36f101f08
|
@ -37,13 +37,13 @@ public class Link extends BaseEntity {
|
|||
/**
|
||||
* Link website address.
|
||||
*/
|
||||
@Column(name = "url", columnDefinition = "varchar(255) not null")
|
||||
@Column(name = "url", columnDefinition = "varchar(1023) not null")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* Website logo.
|
||||
*/
|
||||
@Column(name = "logo", columnDefinition = "varchar(255) default ''")
|
||||
@Column(name = "logo", columnDefinition = "varchar(1023) default ''")
|
||||
private String logo;
|
||||
|
||||
/**
|
||||
|
@ -62,6 +62,19 @@ public class Link extends BaseEntity {
|
|||
@Override
|
||||
public void prePersist() {
|
||||
super.prePersist();
|
||||
|
||||
id = null;
|
||||
|
||||
if (logo == null) {
|
||||
logo = "";
|
||||
}
|
||||
|
||||
if (description == null) {
|
||||
description = "";
|
||||
}
|
||||
|
||||
if (team == null) {
|
||||
team = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package cc.ryanc.halo.model.params;
|
||||
|
||||
import cc.ryanc.halo.model.dto.base.InputConverter;
|
||||
import cc.ryanc.halo.model.entity.Link;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* Link param.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 4/3/19
|
||||
*/
|
||||
@Data
|
||||
public class LinkParam implements InputConverter<Link> {
|
||||
|
||||
@NotBlank(message = "Link name must not be blank")
|
||||
@Size(max = 255, message = "Length of link name must not be more than {max}")
|
||||
private String name;
|
||||
|
||||
@NotBlank(message = "Link url must not be blank")
|
||||
@Size(max = 1023, message = "Length of link url must not be more than {max}")
|
||||
@URL(message = "Link url format is incorrect")
|
||||
private String url;
|
||||
|
||||
@Size(max = 1023, message = "Length of link logo must not be more than {max}")
|
||||
private String logo;
|
||||
|
||||
@Size(max = 255, message = "Length of link description must not be more than {max}")
|
||||
private String description;
|
||||
|
||||
@Size(max = 255, message = "Length of link team must not be more than {max}")
|
||||
private String team;
|
||||
|
||||
}
|
|
@ -1,8 +1,9 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.dto.LinkOutputDTO;
|
||||
import cc.ryanc.halo.model.dto.LinkTeamOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Link;
|
||||
import cc.ryanc.halo.model.params.LinkParam;
|
||||
import cc.ryanc.halo.model.vo.LinkTeamVO;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
@ -22,12 +23,40 @@ public interface LinkService extends CrudService<Link, Integer> {
|
|||
* @param sort sort
|
||||
* @return all links
|
||||
*/
|
||||
@NonNull
|
||||
List<LinkOutputDTO> listDtos(@NonNull Sort sort);
|
||||
|
||||
/**
|
||||
* List link by group
|
||||
*
|
||||
* @return List<LinkTeamOutputDTO>
|
||||
* @return a list of link team vo
|
||||
*/
|
||||
List<LinkTeamOutputDTO> listTeam();
|
||||
@NonNull
|
||||
List<LinkTeamVO> listTeamVos();
|
||||
|
||||
/**
|
||||
* Lists link team vos.
|
||||
*
|
||||
* @param sort must not be null
|
||||
* @return a list of link team vo
|
||||
*/
|
||||
@NonNull
|
||||
List<LinkTeamVO> listTeamVos(@NonNull Sort sort);
|
||||
|
||||
/**
|
||||
* Creates link by link param.
|
||||
*
|
||||
* @param linkParam must not be null
|
||||
* @return create link
|
||||
*/
|
||||
@NonNull
|
||||
Link createBy(@NonNull LinkParam linkParam);
|
||||
|
||||
/**
|
||||
* Exists by link name.
|
||||
*
|
||||
* @param name must not be blank
|
||||
* @return true if exists; false otherwise
|
||||
*/
|
||||
boolean existByName(String name);
|
||||
}
|
||||
|
|
|
@ -1,16 +1,23 @@
|
|||
package cc.ryanc.halo.service.impl;
|
||||
|
||||
import cc.ryanc.halo.exception.AlreadyExistsException;
|
||||
import cc.ryanc.halo.model.dto.LinkOutputDTO;
|
||||
import cc.ryanc.halo.model.dto.LinkTeamOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Link;
|
||||
import cc.ryanc.halo.model.params.LinkParam;
|
||||
import cc.ryanc.halo.model.vo.LinkTeamVO;
|
||||
import cc.ryanc.halo.repository.LinkRepository;
|
||||
import cc.ryanc.halo.service.LinkService;
|
||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||
import cc.ryanc.halo.utils.ServiceUtils;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
|
@ -39,17 +46,79 @@ public class LinkServiceImpl extends AbstractCrudService<Link, Integer> implemen
|
|||
public List<LinkOutputDTO> listDtos(Sort sort) {
|
||||
Assert.notNull(sort, "Sort info must not be null");
|
||||
|
||||
return listAll(sort).stream().map(link -> (LinkOutputDTO) new LinkOutputDTO().convertFrom(link)).collect(Collectors.toList());
|
||||
return convertTo(listAll(sort));
|
||||
}
|
||||
|
||||
/**
|
||||
* List link by group
|
||||
*
|
||||
* @return List<LinkTeamOutputDTO>
|
||||
* @return List<LinkTeamVO>
|
||||
*/
|
||||
@Override
|
||||
public List<LinkTeamOutputDTO> listTeam() {
|
||||
public List<LinkTeamVO> listTeamVos() {
|
||||
// TODO list team
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LinkTeamVO> listTeamVos(Sort sort) {
|
||||
Assert.notNull(sort, "Sort info must not be null");
|
||||
|
||||
// List all links
|
||||
List<LinkOutputDTO> links = listDtos(sort);
|
||||
|
||||
// Get teams
|
||||
Set<String> teams = ServiceUtils.fetchProperty(links, LinkOutputDTO::getTeam);
|
||||
|
||||
// Convert to team link list map (Key: team, value: link list)
|
||||
Map<String, List<LinkOutputDTO>> teamLinkListMap = ServiceUtils.convertToListMap(teams, links, LinkOutputDTO::getTeam);
|
||||
|
||||
List<LinkTeamVO> result = new LinkedList<>();
|
||||
|
||||
// Wrap link team vo list
|
||||
teamLinkListMap.forEach((team, linkList) -> {
|
||||
// Build link team vo
|
||||
LinkTeamVO linkTeamVO = new LinkTeamVO();
|
||||
linkTeamVO.setTeam(team);
|
||||
linkTeamVO.setLinks(linkList);
|
||||
|
||||
// Add it to result
|
||||
result.add(linkTeamVO);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Link createBy(LinkParam linkParam) {
|
||||
Assert.notNull(linkParam, "Link param must not be null");
|
||||
|
||||
// Check the name
|
||||
boolean exist = existByName(linkParam.getName());
|
||||
|
||||
if (exist) {
|
||||
throw new AlreadyExistsException("Link name " + linkParam.getName() + " has already existed").setErrorData(linkParam.getName());
|
||||
}
|
||||
|
||||
return create(linkParam.convertTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existByName(String name) {
|
||||
Assert.hasText(name, "Link name must not be blank");
|
||||
Link link = new Link();
|
||||
link.setName(name);
|
||||
|
||||
return linkRepository.exists(Example.of(link));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private List<LinkOutputDTO> convertTo(@Nullable List<Link> links) {
|
||||
if (CollectionUtils.isEmpty(links)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return links.stream().map(link -> new LinkOutputDTO().<LinkOutputDTO>convertFrom(link))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
package cc.ryanc.halo.web.controller.admin.api;
|
||||
|
||||
import cc.ryanc.halo.model.dto.LinkOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Link;
|
||||
import cc.ryanc.halo.model.params.LinkParam;
|
||||
import cc.ryanc.halo.service.LinkService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.web.SortDefault;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -16,7 +19,7 @@ import java.util.List;
|
|||
* @date : 2019/3/21
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = "/admin/api/links")
|
||||
@RequestMapping("/admin/api/links")
|
||||
public class LinkController {
|
||||
|
||||
private final LinkService linkService;
|
||||
|
@ -48,6 +51,12 @@ public class LinkController {
|
|||
return new LinkOutputDTO().convertFrom(linkService.getById(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public LinkOutputDTO createBy(@RequestBody @Valid LinkParam linkParam) {
|
||||
Link link = linkService.createBy(linkParam);
|
||||
return new LinkOutputDTO().convertFrom(link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete link by id.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue