Complete list latest journals and journal creation api

pull/146/head
johnniang 2019-04-25 10:57:15 +08:00
parent e854626366
commit 25a8d663db
9 changed files with 174 additions and 17 deletions

View File

@ -5,7 +5,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
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.enums.CommentStatus;
import run.halo.app.model.params.CommentParam;
@ -57,29 +57,26 @@ public class CommentController {
@PostMapping
@ApiOperation("Creates a comment (new or reply)")
public CommentDTO createBy(@RequestBody CommentParam commentParam) {
return new CommentDTO().convertFrom(commentService.createBy(commentParam));
public BaseCommentDTO createBy(@RequestBody CommentParam commentParam) {
Comment createdComment = commentService.createBy(commentParam);
return commentService.convertTo(createdComment);
}
@PutMapping("{commentId:\\d+}/status/{status}")
@ApiOperation("Updates comment status")
public CommentDTO updateStatusBy(@PathVariable("commentId") Long commentId,
@PathVariable("status") CommentStatus status) {
public BaseCommentDTO updateStatusBy(@PathVariable("commentId") Long commentId,
@PathVariable("status") CommentStatus status) {
// Update comment status
Comment updatedComment = commentService.updateStatus(commentId, status);
return new CommentDTO().convertFrom(updatedComment);
return commentService.convertTo(updatedComment);
}
@DeleteMapping("{commentId:\\d+}")
@ApiOperation("Deletes comment permanently and recursively")
public CommentDTO deleteBy(@PathVariable("commentId") Long commentId) {
// Get comment by id
Comment comment = commentService.getById(commentId);
public BaseCommentDTO deleteBy(@PathVariable("commentId") Long commentId) {
Comment deletedComment = commentService.removeById(commentId);
// Remove it
commentService.remove(comment);
return new CommentDTO().convertFrom(comment);
return commentService.convertTo(deletedComment);
}
}

View File

@ -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);
}
}

View File

@ -10,8 +10,8 @@ import lombok.ToString;
* @author johnniang
*/
@Data
@ToString
@EqualsAndHashCode
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class CommentDTO extends BaseCommentDTO {
}

View File

@ -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;
}

View File

@ -1,6 +1,8 @@
package run.halo.app.service;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.params.JournalParam;
import run.halo.app.service.base.BaseCommentService;
/**
@ -11,4 +13,13 @@ import run.halo.app.service.base.BaseCommentService;
*/
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);
}

View File

@ -4,6 +4,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.dto.BaseCommentDTO;
import run.halo.app.model.entity.BaseComment;
import run.halo.app.model.enums.CommentStatus;
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.List;
import java.util.Map;
import java.util.function.Function;
/**
* Base comment service interface.
@ -115,4 +115,22 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
*/
@NonNull
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);
}

View File

@ -18,6 +18,7 @@ import run.halo.app.event.comment.CommentNewEvent;
import run.halo.app.event.comment.CommentPassEvent;
import run.halo.app.event.comment.CommentReplyEvent;
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.enums.CommentStatus;
import run.halo.app.model.params.CommentQuery;
@ -38,6 +39,7 @@ import run.halo.app.utils.ServletUtils;
import javax.persistence.criteria.Predicate;
import java.util.*;
import java.util.stream.Collectors;
/**
* Base comment service implementation.
@ -281,6 +283,23 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
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
private Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
Assert.notNull(commentQuery, "Comment query must not be null");

View File

@ -59,7 +59,7 @@ public class CommentServiceImpl extends BaseCommentServiceImpl<Comment> implemen
if (authentication != null) {
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.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
}

View File

@ -1,12 +1,20 @@
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.stereotype.Service;
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.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.OptionService;
import run.halo.app.utils.ValidationUtils;
/**
* Journal service implementation.
@ -26,4 +34,27 @@ public class JournalServiceImpl extends BaseCommentServiceImpl<Journal> implemen
super(journalRepository, postRepository, optionService, eventPublisher);
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());
}
}