mirror of https://github.com/halo-dev/halo
Complete list latest journals and journal creation api
parent
e854626366
commit
25a8d663db
|
@ -5,7 +5,7 @@ import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.web.PageableDefault;
|
import org.springframework.data.web.PageableDefault;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import run.halo.app.model.dto.CommentDTO;
|
import run.halo.app.model.dto.BaseCommentDTO;
|
||||||
import run.halo.app.model.entity.Comment;
|
import run.halo.app.model.entity.Comment;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
import run.halo.app.model.params.CommentParam;
|
import run.halo.app.model.params.CommentParam;
|
||||||
|
@ -57,29 +57,26 @@ public class CommentController {
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@ApiOperation("Creates a comment (new or reply)")
|
@ApiOperation("Creates a comment (new or reply)")
|
||||||
public CommentDTO createBy(@RequestBody CommentParam commentParam) {
|
public BaseCommentDTO createBy(@RequestBody CommentParam commentParam) {
|
||||||
return new CommentDTO().convertFrom(commentService.createBy(commentParam));
|
Comment createdComment = commentService.createBy(commentParam);
|
||||||
|
return commentService.convertTo(createdComment);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("{commentId:\\d+}/status/{status}")
|
@PutMapping("{commentId:\\d+}/status/{status}")
|
||||||
@ApiOperation("Updates comment status")
|
@ApiOperation("Updates comment status")
|
||||||
public CommentDTO updateStatusBy(@PathVariable("commentId") Long commentId,
|
public BaseCommentDTO updateStatusBy(@PathVariable("commentId") Long commentId,
|
||||||
@PathVariable("status") CommentStatus status) {
|
@PathVariable("status") CommentStatus status) {
|
||||||
// Update comment status
|
// Update comment status
|
||||||
Comment updatedComment = commentService.updateStatus(commentId, status);
|
Comment updatedComment = commentService.updateStatus(commentId, status);
|
||||||
|
|
||||||
return new CommentDTO().convertFrom(updatedComment);
|
return commentService.convertTo(updatedComment);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("{commentId:\\d+}")
|
@DeleteMapping("{commentId:\\d+}")
|
||||||
@ApiOperation("Deletes comment permanently and recursively")
|
@ApiOperation("Deletes comment permanently and recursively")
|
||||||
public CommentDTO deleteBy(@PathVariable("commentId") Long commentId) {
|
public BaseCommentDTO deleteBy(@PathVariable("commentId") Long commentId) {
|
||||||
// Get comment by id
|
Comment deletedComment = commentService.removeById(commentId);
|
||||||
Comment comment = commentService.getById(commentId);
|
|
||||||
|
|
||||||
// Remove it
|
return commentService.convertTo(deletedComment);
|
||||||
commentService.remove(comment);
|
|
||||||
|
|
||||||
return new CommentDTO().convertFrom(comment);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package run.halo.app.controller.admin.api;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import run.halo.app.model.dto.BaseCommentDTO;
|
||||||
|
import run.halo.app.model.entity.Journal;
|
||||||
|
import run.halo.app.model.params.JournalParam;
|
||||||
|
import run.halo.app.service.JournalService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Journal controller.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 19-4-25
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/admin/journals")
|
||||||
|
public class JournalController {
|
||||||
|
|
||||||
|
private final JournalService journalService;
|
||||||
|
|
||||||
|
public JournalController(JournalService journalService) {
|
||||||
|
this.journalService = journalService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("latest")
|
||||||
|
@ApiOperation("Gets latest journals")
|
||||||
|
public List<BaseCommentDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
||||||
|
List<Journal> journals = journalService.pageLatest(top).getContent();
|
||||||
|
return journalService.convertTo(journals);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ApiOperation("Creates a journal")
|
||||||
|
public BaseCommentDTO createBy(@RequestBody JournalParam journalParam) {
|
||||||
|
Journal createdJournal = journalService.createBy(journalParam);
|
||||||
|
return journalService.convertTo(createdJournal);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -10,8 +10,8 @@ import lombok.ToString;
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@ToString
|
@ToString(callSuper = true)
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode(callSuper = true)
|
||||||
public class CommentDTO extends BaseCommentDTO {
|
public class CommentDTO extends BaseCommentDTO {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package run.halo.app.model.params;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import run.halo.app.model.dto.base.InputConverter;
|
||||||
|
import run.halo.app.model.entity.Journal;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Journal param.
|
||||||
|
*
|
||||||
|
* @author johnniang
|
||||||
|
* @date 19-4-25
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class JournalParam implements InputConverter<Journal> {
|
||||||
|
|
||||||
|
@NotBlank(message = "Author name must not be blank")
|
||||||
|
@Size(max = 50, message = "Length of comment author name must not be more than {max}")
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
@NotBlank(message = "Email must not be blank")
|
||||||
|
@Email(message = "Email's format is incorrect")
|
||||||
|
@Size(max = 255, message = "Length of comment email must not be more than {max}")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Size(max = 127, message = "Length of comment author url must not be more than {max}")
|
||||||
|
private String authorUrl;
|
||||||
|
|
||||||
|
@NotBlank(message = "Content must not be blank")
|
||||||
|
@Size(max = 511, message = "Length of comment content must not be more than {max}")
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@Min(value = 0, message = "Parent id must not be less than {value}")
|
||||||
|
private Long parentId = 0L;
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
package run.halo.app.service;
|
package run.halo.app.service;
|
||||||
|
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import run.halo.app.model.entity.Journal;
|
import run.halo.app.model.entity.Journal;
|
||||||
|
import run.halo.app.model.params.JournalParam;
|
||||||
import run.halo.app.service.base.BaseCommentService;
|
import run.halo.app.service.base.BaseCommentService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,4 +13,13 @@ import run.halo.app.service.base.BaseCommentService;
|
||||||
*/
|
*/
|
||||||
public interface JournalService extends BaseCommentService<Journal> {
|
public interface JournalService extends BaseCommentService<Journal> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a journal.
|
||||||
|
*
|
||||||
|
* @param journalParam journal param must not be null
|
||||||
|
* @return created journal
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Journal createBy(@NonNull JournalParam journalParam);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import run.halo.app.model.dto.BaseCommentDTO;
|
||||||
import run.halo.app.model.entity.BaseComment;
|
import run.halo.app.model.entity.BaseComment;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
import run.halo.app.model.params.CommentQuery;
|
import run.halo.app.model.params.CommentQuery;
|
||||||
|
@ -13,7 +14,6 @@ import run.halo.app.model.vo.BaseCommentWithParentVO;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base comment service interface.
|
* Base comment service interface.
|
||||||
|
@ -115,4 +115,22 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
COMMENT updateStatus(@NonNull Long commentId, @NonNull CommentStatus status);
|
COMMENT updateStatus(@NonNull Long commentId, @NonNull CommentStatus status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts to base comment dto.
|
||||||
|
*
|
||||||
|
* @param comment comment must not be null
|
||||||
|
* @return base comment dto
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
BaseCommentDTO convertTo(@NonNull COMMENT comment);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts to base comment dto list.
|
||||||
|
*
|
||||||
|
* @param comments comment list must not be null
|
||||||
|
* @return a list of base comment dto
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
List<BaseCommentDTO> convertTo(@NonNull List<COMMENT> comments);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import run.halo.app.event.comment.CommentNewEvent;
|
||||||
import run.halo.app.event.comment.CommentPassEvent;
|
import run.halo.app.event.comment.CommentPassEvent;
|
||||||
import run.halo.app.event.comment.CommentReplyEvent;
|
import run.halo.app.event.comment.CommentReplyEvent;
|
||||||
import run.halo.app.exception.NotFoundException;
|
import run.halo.app.exception.NotFoundException;
|
||||||
|
import run.halo.app.model.dto.BaseCommentDTO;
|
||||||
import run.halo.app.model.entity.BaseComment;
|
import run.halo.app.model.entity.BaseComment;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
import run.halo.app.model.params.CommentQuery;
|
import run.halo.app.model.params.CommentQuery;
|
||||||
|
@ -38,6 +39,7 @@ import run.halo.app.utils.ServletUtils;
|
||||||
|
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base comment service implementation.
|
* Base comment service implementation.
|
||||||
|
@ -281,6 +283,23 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
return updatedComment;
|
return updatedComment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<BaseCommentDTO> convertTo(List<COMMENT> comments) {
|
||||||
|
if (CollectionUtils.isEmpty(comments)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return comments.stream()
|
||||||
|
.map(this::convertTo)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseCommentDTO convertTo(COMMENT comment) {
|
||||||
|
Assert.notNull(comment, "Comment must not be null");
|
||||||
|
|
||||||
|
return new BaseCommentDTO().convertFrom(comment);
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
|
private Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
|
||||||
Assert.notNull(commentQuery, "Comment query must not be null");
|
Assert.notNull(commentQuery, "Comment query must not be null");
|
||||||
|
|
|
@ -59,7 +59,7 @@ public class CommentServiceImpl extends BaseCommentServiceImpl<Comment> implemen
|
||||||
|
|
||||||
if (authentication != null) {
|
if (authentication != null) {
|
||||||
User user = authentication.getDetail().getUser();
|
User user = authentication.getDetail().getUser();
|
||||||
commentParam.setAuthor(StringUtils.isEmpty(user.getNickname()) ? user.getUsername() : user.getNickname());
|
commentParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
|
||||||
commentParam.setEmail(user.getEmail());
|
commentParam.setEmail(user.getEmail());
|
||||||
commentParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
|
commentParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
package run.halo.app.service.impl;
|
package run.halo.app.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import run.halo.app.model.entity.Journal;
|
import run.halo.app.model.entity.Journal;
|
||||||
|
import run.halo.app.model.entity.User;
|
||||||
|
import run.halo.app.model.params.JournalParam;
|
||||||
|
import run.halo.app.model.properties.BlogProperties;
|
||||||
import run.halo.app.repository.JournalRepository;
|
import run.halo.app.repository.JournalRepository;
|
||||||
import run.halo.app.repository.PostRepository;
|
import run.halo.app.repository.PostRepository;
|
||||||
|
import run.halo.app.security.authentication.Authentication;
|
||||||
|
import run.halo.app.security.context.SecurityContextHolder;
|
||||||
import run.halo.app.service.JournalService;
|
import run.halo.app.service.JournalService;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
|
import run.halo.app.utils.ValidationUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Journal service implementation.
|
* Journal service implementation.
|
||||||
|
@ -26,4 +34,27 @@ public class JournalServiceImpl extends BaseCommentServiceImpl<Journal> implemen
|
||||||
super(journalRepository, postRepository, optionService, eventPublisher);
|
super(journalRepository, postRepository, optionService, eventPublisher);
|
||||||
this.journalRepository = journalRepository;
|
this.journalRepository = journalRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Journal createBy(JournalParam journalParam) {
|
||||||
|
Assert.notNull(journalParam, "Journal param must not be null");
|
||||||
|
|
||||||
|
// Check user login status
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication != null) {
|
||||||
|
// Get user detail
|
||||||
|
User user = authentication.getDetail().getUser();
|
||||||
|
// Set some default value
|
||||||
|
journalParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
|
||||||
|
journalParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
|
||||||
|
journalParam.setEmail(user.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate the journal param
|
||||||
|
ValidationUtils.validate(journalParam);
|
||||||
|
|
||||||
|
// Convert, create and return
|
||||||
|
return createBy(journalParam.convertTo());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue