diff --git a/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorEditController.java b/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorEditController.java index 9546a0ef7..fe7f24114 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorEditController.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorEditController.java @@ -41,7 +41,6 @@ public String preEdit(Model model, Principal principal) { try{ // add blogs for editing blogs at admin-age PageHelper.startPage(PageConst.PAGE_NUM.getSize(), PageConst.PAGE_SIZE.getSize()); - //authorList = DataUtil.iterable2List(authorRepository.findAll()); authorList = authorService.getAllAuthors(); pageInfo = new PageInfo(authorList, PageConst.PAGE_SIZE.getSize()); model.addAttribute("pageInfo", pageInfo); diff --git a/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorRegisterController.java b/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorRegisterController.java index a41ff9c42..8899bcb7d 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorRegisterController.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/controller/AuthorRegisterController.java @@ -1,8 +1,8 @@ package com.yen.mdblog.controller; -import com.yen.mdblog.entity.Po.Author; import com.yen.mdblog.entity.Vo.CreateAuthor; import com.yen.mdblog.mapper.AuthorMapper; +import com.yen.mdblog.service.AuthorService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;;import java.security.Principal; -import java.util.Date; @Controller @RequestMapping("/register") @@ -20,6 +19,9 @@ public class AuthorRegisterController { @Autowired AuthorMapper authorMapper; + @Autowired + AuthorService authorService; + @GetMapping("/create") public String register(Model model, Principal principal){ @@ -31,25 +33,14 @@ public String register(Model model, Principal principal){ @RequestMapping(value="/create", method= RequestMethod.POST) public String createUser(CreateAuthor request){ - log.info("create new user start ..."); - try{ - Author author = new Author(); - String name = request.getName(); - String email = request.getEmail(); - int id = authorMapper.getAuthorCount() + 1; - author.setId(id); - author.setName(name); - author.setEmail(email); - author.setCreateTime(new Date()); - author.setUpdateTime(new Date()); - log.info("author = " + author.toString()); - authorMapper.insertAuthor(author); + String name = request.getName(); + String email = request.getEmail(); + Boolean createAuthorSuccess = authorService.createAuthor(name, email); + if (createAuthorSuccess){ log.info("create new user OK ..."); return "author/success_register"; - }catch (Exception e){ - String errorMsg = "create new user failed ..." + e; - log.error(errorMsg); - return errorMsg; + }else{ + return "create new user failed"; } } diff --git a/springBootBlog/src/main/java/com/yen/mdblog/controller/PostController.java b/springBootBlog/src/main/java/com/yen/mdblog/controller/PostController.java index cb9e1ae4c..13457240b 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/controller/PostController.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/controller/PostController.java @@ -8,6 +8,7 @@ import com.yen.mdblog.entity.Po.Post; import com.yen.mdblog.entity.Vo.CreateComment; import com.yen.mdblog.entity.Vo.CreatePost; +import com.yen.mdblog.mapper.AuthorMapper; import com.yen.mdblog.mapper.PostMapper; import com.yen.mdblog.repository.PostRepository; import com.yen.mdblog.service.AuthorService; @@ -55,6 +56,9 @@ public class PostController { @Autowired CommentService commentService; + @Autowired + AuthorMapper authorMapper; + @GetMapping("/all") public String getPaginatedPosts( @RequestParam(value = "pageNum", defaultValue = "0") int pageNum, @@ -146,7 +150,7 @@ public String createPost(CreatePost request, Model model, Principal principal){ author.setCreateTime(new Date()); author.setUpdateTime(new Date()); author.setName(authorName); - authorService.saveAuthor(author); + authorMapper.insertAuthor(author); Integer id = authorService.getByName(authorName).getId(); author.setId(id); }else{ diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/AuthorService.java b/springBootBlog/src/main/java/com/yen/mdblog/service/AuthorService.java index bee502dcd..cf6c57fe9 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/AuthorService.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/AuthorService.java @@ -12,7 +12,7 @@ public interface AuthorService { List getAllAuthors(); - void saveAuthor(Author author); + Boolean createAuthor(String name, String email); void updateAuthor(Author author); } diff --git a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/AuthorServiceImpl.java b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/AuthorServiceImpl.java index e08d43da5..b3111e22e 100644 --- a/springBootBlog/src/main/java/com/yen/mdblog/service/impl/AuthorServiceImpl.java +++ b/springBootBlog/src/main/java/com/yen/mdblog/service/impl/AuthorServiceImpl.java @@ -3,12 +3,15 @@ import com.yen.mdblog.entity.Po.Author; import com.yen.mdblog.mapper.AuthorMapper; import com.yen.mdblog.service.AuthorService; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.List; @Service +@Log4j2 public class AuthorServiceImpl implements AuthorService { @Autowired @@ -33,9 +36,23 @@ public List getAllAuthors() { } @Override - public void saveAuthor(Author author) { - - authorMapper.insertAuthor(author); + public Boolean createAuthor(String name, String email) { + try{ + Author newAuthor = new Author(); + int id = authorMapper.getAuthorCount() + 1; + newAuthor.setId(id); + newAuthor.setName(name); + newAuthor.setEmail(email); + newAuthor.setCreateTime(new Date()); + newAuthor.setUpdateTime(new Date()); + log.info("save author = " + newAuthor); + authorMapper.insertAuthor(newAuthor); + log.info("create new user OK ..."); + return true; + }catch (Exception e){ + log.error("createAuthor failed : " + e); + return false; + } } @Override