Enhance ApiJournalController

pull/137/head
johnniang 2019-03-04 22:53:27 +08:00
parent c48970a034
commit 06e5c18cc7
6 changed files with 61 additions and 19 deletions

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.model.params;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.base.AbstractInputConverter; import cc.ryanc.halo.model.dto.base.AbstractInputConverter;
import cc.ryanc.halo.model.enums.PostTypeEnum;
import cc.ryanc.halo.utils.MarkdownUtils; import cc.ryanc.halo.utils.MarkdownUtils;
import lombok.Data; import lombok.Data;
@ -31,9 +32,12 @@ public class JournalParam extends AbstractInputConverter<Post> {
@Override @Override
public Post convertTo() { public Post convertTo() {
Post post = super.convertTo(); Post post = new Post();
post.setPostTitle(title);
post.setPostContentMd(content); post.setPostContentMd(content);
post.setPostContent(MarkdownUtils.renderMarkdown(content)); post.setPostContent(MarkdownUtils.renderMarkdown(content));
post.setPostSource(source);
post.setPostType(PostTypeEnum.POST_TYPE_JOURNAL.getDesc());
return post; return post;
} }
} }

View File

@ -295,6 +295,10 @@ public class HaloUtils {
* @param password password * @param password password
*/ */
public static void configMail(String smtpHost, String userName, String password) { public static void configMail(String smtpHost, String userName, String password) {
Assert.hasText(smtpHost, "SMTP host config must not be blank");
Assert.hasText(userName, "Email username must not be blank");
Assert.hasText(password, "Email password must not be blank");
final Properties properties = OhMyEmail.defaultConfig(false); final Properties properties = OhMyEmail.defaultConfig(false);
properties.setProperty("mail.smtp.host", smtpHost); properties.setProperty("mail.smtp.host", smtpHost);
OhMyEmail.config(properties, userName, password); OhMyEmail.config(properties, userName, password);

View File

@ -240,6 +240,7 @@ public class AdminController extends BaseController {
session.setAttribute(RESET_PASSWORD_SESSION_KEY, code); session.setAttribute(RESET_PASSWORD_SESSION_KEY, code);
return JsonResult.success("邮件发送成功,请登录您的邮箱进行下一步操作"); return JsonResult.success("邮件发送成功,请登录您的邮箱进行下一步操作");
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to send password email", e);
return JsonResult.fail("邮件发送失败,请确定已经配置好了发信服务器信息"); return JsonResult.fail("邮件发送失败,请确定已经配置好了发信服务器信息");
} }
} }

View File

@ -1,15 +1,10 @@
package cc.ryanc.halo.web.controller.api; package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.enums.PostTypeEnum;
import cc.ryanc.halo.model.params.JournalParam; import cc.ryanc.halo.model.params.JournalParam;
import cc.ryanc.halo.model.support.JsonResult;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.MarkdownUtils; import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** /**
* <pre> * <pre>
@ -20,11 +15,14 @@ import org.springframework.web.bind.annotation.RestController;
* @date : 2019/03/04 * @date : 2019/03/04
*/ */
@RestController @RestController
@RequestMapping(value = "/api/journal") @RequestMapping(value = "/api/journals")
public class ApiJournalController { public class ApiJournalController {
@Autowired private final PostService postService;
private PostService postService;
public ApiJournalController(PostService postService) {
this.postService = postService;
}
/** /**
* *
@ -32,13 +30,13 @@ public class ApiJournalController {
* @param journalParam journalParam * @param journalParam journalParam
* @return JsonResult * @return JsonResult
*/ */
@PostMapping(value = "/save") @PostMapping
public JsonResult save(JournalParam journalParam) { @ResponseStatus(HttpStatus.CREATED)
Post post = new Post(); public Post save(@RequestBody JournalParam journalParam) {
post.setPostContentMd(MarkdownUtils.renderMarkdown(journalParam.getContent())); // TODO need to validate token
post.setPostSource(journalParam.getSource());
post.setPostType(PostTypeEnum.POST_TYPE_JOURNAL.getDesc()); Post post = journalParam.convertTo();
post = postService.create(post);
return JsonResult.success("ok"); return postService.create(post);
} }
} }

View File

@ -8,7 +8,9 @@ import cc.ryanc.halo.utils.ValidationUtils;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
@ -69,6 +71,23 @@ public class ControllerExceptionHandler {
return jsonResult; return jsonResult;
} }
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public JsonResult handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
JsonResult jsonResult = handleBaseException(e);
jsonResult.setCode(HttpStatus.BAD_REQUEST.value());
return jsonResult;
}
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public JsonResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
JsonResult jsonResult = handleBaseException(e);
jsonResult.setCode(HttpStatus.BAD_REQUEST.value());
jsonResult.setMsg("Required request body is missing");
return jsonResult;
}
@ExceptionHandler(NoHandlerFoundException.class) @ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY) @ResponseStatus(HttpStatus.BAD_GATEWAY)
public JsonResult handleNoHandlerFoundException(NoHandlerFoundException e) { public JsonResult handleNoHandlerFoundException(NoHandlerFoundException e) {

View File

@ -30,6 +30,9 @@ public class BeanUtilsTest {
assertEquals(a.getB(), b.getB()); assertEquals(a.getB(), b.getB());
assertNull(b.getC()); assertNull(b.getC());
TestD d = new TestD(a);
TestE e = BeanUtils.transformFrom(d, TestE.class);
assertEquals(d.getA().getA(), e.getA().getA());
} }
@Test @Test
@ -95,4 +98,17 @@ public class BeanUtilsTest {
private Integer b; private Integer b;
} }
@Data
@NoArgsConstructor
@AllArgsConstructor
static class TestD {
private TestA a;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
static class TestE {
private TestA a;
}
} }