Skip to content

Commit

Permalink
move authorRegister biz logic to service, fix controller, layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 6, 2023
1 parent c04018b commit be67751
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Author>(authorList, PageConst.PAGE_SIZE.getSize());
model.addAttribute("pageInfo", pageInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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;
import org.springframework.ui.Model;
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")
Expand All @@ -20,6 +19,9 @@ public class AuthorRegisterController {
@Autowired
AuthorMapper authorMapper;

@Autowired
AuthorService authorService;

@GetMapping("/create")
public String register(Model model, Principal principal){

Expand All @@ -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";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface AuthorService {

List<Author> getAllAuthors();

void saveAuthor(Author author);
Boolean createAuthor(String name, String email);

void updateAuthor(Author author);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,9 +36,23 @@ public List<Author> 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
Expand Down

0 comments on commit be67751

Please sign in to comment.