Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog fix : Fix create blog with security current login user #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,42 @@ public String createPost(CreatePost request, Model model, Principal principal){

log.info(">>> create post start ...");
Post post = new Post();

String authorName = principal.getName();

List<Author> authors = authorService.getAllAuthors();
List<String> names = authors.stream().map(x -> x.getName()).collect(Collectors.toList());
Author author = new Author();
author.setId(request.getId());

if(!names.contains(authorName)){
//author.setId(request.getId()); // TODO : automate setup is (from name)
author.setName(principal.getName()); // set author name from spring security principal
authorService.saveAuthor(author);
}

author = authorService.getByName(authorName);

BeanUtils.copyProperties(request, post);
post.setId(postService.getTotalPost() + 1);
post.setAuthorId(author.getId());
post.setDateTime(LocalDateTime.now());
post.setSynopsis(PostUtil.getSynopsis(request.getContent()));
post.setAuthor(author);
//post.setAuthor(author);
post.setDateTime(LocalDateTime.now());
//post.setAuthorId(authorId);
log.info(">>> request = " + request);
log.info(">>> post = " + post);
log.info(">>> author = " + author);
log.info(">>>> create post end ...");
postService.savePost(post);
List<Author> authors = authorService.getAllAuthors();
List<Long> ids = authors.stream().map(x -> x.getId()).collect(Collectors.toList());
if (!ids.contains(author.getId())){
authorService.saveAuthor(author);
}

// List<Author> authors = authorService.getAllAuthors();
// //List<Long> ids = authors.stream().map(x -> x.getId()).collect(Collectors.toList());
// List<String> names = authors.stream().map(x -> x.getName()).collect(Collectors.toList());
// if (!names.contains(author.getName())){
// authorService.saveAuthor(author);
// }

model.addAttribute("user", principal.getName());
return "success";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Author {
private Date updateTime;

@Column
@OneToMany(mappedBy = "author")
private List<Post> posts;
//@OneToMany(mappedBy = "author_id")
//private List<Post> posts;
private String posts;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public class Post implements Serializable {
@Column(length = 150)
private String synopsis;

@ManyToOne
@JoinColumn(name = "author_id")
private Author author;
//@ManyToOne
//@JoinColumn(name = "author_id")
private Long authorId;
//private Author author;

@Column
@Convert(converter = DateTimeUtil.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class CreatePost {
private String title;
private String content;
private String synopsis;
private String author;
private Long authorId;
private LocalDateTime dateTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ContextEventListener implements ApplicationListener<ContextRefreshe

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

Arrays.stream(postFiles).forEach(postFile -> {

Optional<String> postFileNameOpt = Optional.ofNullable(postFile.getFilename());
Expand Down Expand Up @@ -63,7 +64,7 @@ public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
try{
System.out.println("Post with ID: " + id + " does not exist. Creating post...");
post.setTitle(title);
post.setAuthor(author);
post.setAuthorId(author.getId());
//post.setAuthorId(author.getId());
post.setContent(htmlContent);
post.setSynopsis(getSynopsisFromHtmlContent(htmlContent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ public interface AuthorMapper {

public Author getById(@Param("id") long id);

public Author getByName(@Param("name") String name);

public List<Author> getAllAuthors();

public int getAuthorCount();

// @Insert("INSERT INTO authors(`id`,`email`,`name`,`url`) values(#{id}, #{email}, #{name}, #{url})")
// @Options(useGeneratedKeys = true, keyProperty = "id")
public void insertAuthor(Author author);
public void insertAuthor(@Param("author") Author author);

public void updateAuthor(Author author);
public void updateAuthor(@Param("author") Author author);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
SELECT * FROM authors where id = #{id}
</select>

<!-- public Author getByName(@Param("name") String name); -->
<select id="getByName=" resultType="com.yen.mdblog.entity.Po.Author" parameterType="String">
SELECT * FROM authors where name = #{name}
</select>

<!-- public List<Author> getAllAuthors(); -->
<select id="getAllAuthors" resultType="com.yen.mdblog.entity.Po.Author">
SELECT * FROM authors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public interface PostMapper {
//@Insert("INSERT INTO posts(`id`,`title`,`content`,`synopsis`,`author_id`, `dateTime`) values(#{id}, #{title}, #{content}, #{synopsis}, #{author_id}, #{dateTime})")
// @Insert("INSERT INTO posts(`id`,`title`,`content`,`synopsis`,`author_id`, `date_time`) values(#{id}, #{title}, #{content}, #{synopsis}, 1, #{dateTime})")
// @Options(useGeneratedKeys = true, keyProperty = "id")
public void insertPost(Post post);
public void insertPost(@Param("post") Post post);

public void updatePost(Post post);
public void updatePost(@Param("post") Post post);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- public void insertPost(Post post); -->
<insert id="insertPost">
INSERT INTO posts(`id`,`title`,`content`,`synopsis`,`author_id`, `date_time`)
values(#{id}, #{title}, #{content}, #{synopsis}, 1, #{dateTime})
values(#{id}, #{title}, #{content}, #{synopsis}, #{authorId}, #{dateTime})
</insert>

<!-- public void updatePost(Post post); -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface AuthorService {

Author getById(Long id);

Author getByName(String name);

List<Author> getAllAuthors();

void saveAuthor(Author author);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,31 @@ public class AuthorServiceImpl implements AuthorService {

@Override
public Author getById(Long id) {

return authorMapper.getById(id);
}

@Override
public Author getByName(String name) {

return authorMapper.getByName(name);
}

@Override
public List<Author> getAllAuthors() {

return authorMapper.getAllAuthors();
}

@Override
public void saveAuthor(Author author) {

authorMapper.insertAuthor(author);
}

@Override
public void updateAuthor(Author author) {

authorMapper.updateAuthor(author);
}

Expand Down
19 changes: 11 additions & 8 deletions springBootBlog/src/main/java/com/yen/mdblog/util/AuthorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@

public class AuthorUtil {
public static Author bootstrapAuthor(AuthorRepository authorRepository) {

Optional<Author> authorOpt = authorRepository.findById(1L);
if (authorOpt.isPresent()) {
return authorOpt.get();
} else {
Author newAuthor = new Author();
newAuthor.setName("yen");
newAuthor.setEmail("[email protected]");
newAuthor.setUrl("yen.xx.yy");
newAuthor.setCreateTime(new Date());
newAuthor.setUpdateTime(new Date());
authorRepository.save(newAuthor);
return newAuthor;

Author author1 = new Author();
author1.setName("yen");
author1.setEmail("[email protected]");
author1.setUrl("yen.xx.yy");
author1.setCreateTime(new Date());
author1.setUpdateTime(new Date());
authorRepository.save(author1);

return author1;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.yen.mdblog.mapper;

import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.service.AuthorService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootTest
public class TestAuthorMapper {

@Autowired
AuthorService authorService;

@Test
public void TestGetAllId(){
List<Author> authors = authorService.getAllAuthors();
//Integer[] authorId = (Integer[]) authors.stream().map(x -> x.getId()).toArray();
List<Long> ids = authors.stream().map(x -> x.getId()).collect(Collectors.toList());

System.out.println(">>> authors = " + authors);
System.out.println(">>> ids = " + ids);
//System.out.println(">>> authorId = " + authorId);
System.out.println();

authors.stream().forEach(x->{System.out.println(x);});
authors.stream().forEach(x->{System.out.println(x.getName());});
}

}
//package com.yen.mdblog.mapper;
//
//import com.yen.mdblog.entity.Po.Author;
//import com.yen.mdblog.service.AuthorService;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@SpringBootTest
//public class TestAuthorMapper {
//
// @Autowired
// AuthorService authorService;
//
// @Test
// public void TestGetAllId(){
// List<Author> authors = authorService.getAllAuthors();
// //Integer[] authorId = (Integer[]) authors.stream().map(x -> x.getId()).toArray();
// List<Long> ids = authors.stream().map(x -> x.getId()).collect(Collectors.toList());
//
// System.out.println(">>> authors = " + authors);
// System.out.println(">>> ids = " + ids);
// //System.out.println(">>> authorId = " + authorId);
// System.out.println();
//
// authors.stream().forEach(x->{System.out.println(x);});
// authors.stream().forEach(x->{System.out.println(x.getName());});
// }
//
//}