mirror of https://github.com/halo-dev/halo
parent
868efecdce
commit
e58d8fbe73
|
@ -54,9 +54,8 @@ public class LinkController {
|
|||
@ApiOperation("Updates a link")
|
||||
public LinkDTO updateBy(@PathVariable("id") Integer id,
|
||||
@RequestBody @Valid LinkParam linkParam) {
|
||||
Link link = linkService.getById(id);
|
||||
linkParam.update(link);
|
||||
return new LinkDTO().convertFrom(linkService.update(link));
|
||||
Link link = linkService.updateBy(id, linkParam);
|
||||
return new LinkDTO().convertFrom(link);
|
||||
}
|
||||
|
||||
@DeleteMapping("{id:\\d+}")
|
||||
|
|
|
@ -20,4 +20,8 @@ public interface LinkRepository extends BaseRepository<Link, Integer> {
|
|||
*/
|
||||
@Query(value = "select distinct a.team from Link a")
|
||||
List<String> findAllTeams();
|
||||
|
||||
boolean existsByNameAndIdNot(String name, Integer id);
|
||||
|
||||
boolean existsByUrlAndIdNot(String url, Integer id);
|
||||
}
|
||||
|
|
|
@ -55,6 +55,17 @@ public interface LinkService extends CrudService<Link, Integer> {
|
|||
@NonNull
|
||||
Link createBy(@NonNull LinkParam linkParam);
|
||||
|
||||
/**
|
||||
* Updates link by link param.
|
||||
*
|
||||
*
|
||||
* @param id must not be null
|
||||
* @param linkParam must not be null
|
||||
* @return updated link
|
||||
*/
|
||||
@NonNull
|
||||
Link updateBy(Integer id, @NonNull LinkParam linkParam);
|
||||
|
||||
/**
|
||||
* Exists by link name.
|
||||
*
|
||||
|
@ -63,6 +74,14 @@ public interface LinkService extends CrudService<Link, Integer> {
|
|||
*/
|
||||
boolean existByName(String name);
|
||||
|
||||
/**
|
||||
* Exists by link url.
|
||||
*
|
||||
* @param url must not be blank
|
||||
* @return true if exists; false otherwise
|
||||
*/
|
||||
boolean existByUrl(String url);
|
||||
|
||||
/**
|
||||
* List all link teams.
|
||||
*
|
||||
|
|
|
@ -101,9 +101,39 @@ public class LinkServiceImpl extends AbstractCrudService<Link, Integer> implemen
|
|||
throw new AlreadyExistsException("友情链接 " + linkParam.getName() + " 已存在").setErrorData(linkParam.getName());
|
||||
}
|
||||
|
||||
// Check the url
|
||||
exist = existByUrl(linkParam.getUrl());
|
||||
|
||||
if (exist) {
|
||||
throw new AlreadyExistsException("友情链接 " + linkParam.getUrl() + " 已存在").setErrorData(linkParam.getUrl());
|
||||
}
|
||||
|
||||
return create(linkParam.convertTo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Link updateBy(Integer id, @NotNull LinkParam linkParam) {
|
||||
Assert.notNull(id, "Id must not be null");
|
||||
Assert.notNull(linkParam, "Link param must not be null");
|
||||
|
||||
// Check the name
|
||||
boolean exist = linkRepository.existsByNameAndIdNot(linkParam.getName(), id);
|
||||
if (exist) {
|
||||
throw new AlreadyExistsException("友情链接 " + linkParam.getName() + " 已存在").setErrorData(linkParam.getName());
|
||||
}
|
||||
|
||||
// Check the url
|
||||
exist = linkRepository.existsByUrlAndIdNot(linkParam.getUrl(), id);
|
||||
if (exist) {
|
||||
throw new AlreadyExistsException("友情链接 " + linkParam.getUrl() + " 已存在").setErrorData(linkParam.getUrl());
|
||||
}
|
||||
|
||||
Link link = getById(id);
|
||||
linkParam.update(link);
|
||||
|
||||
return update(link);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existByName(String name) {
|
||||
Assert.hasText(name, "Link name must not be blank");
|
||||
|
@ -113,6 +143,15 @@ public class LinkServiceImpl extends AbstractCrudService<Link, Integer> implemen
|
|||
return linkRepository.exists(Example.of(link));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existByUrl(String url) {
|
||||
Assert.hasText(url, "Link url must not be blank");
|
||||
Link link = new Link();
|
||||
link.setUrl(url);
|
||||
|
||||
return linkRepository.exists(Example.of(link));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listAllTeams() {
|
||||
return linkRepository.findAllTeams();
|
||||
|
|
Loading…
Reference in New Issue