@@ -10,7 +10,6 @@

@RestController
@RequestMapping("/api/comments")
//TODO swagger io annotations fo doc
public class CommentController {

@Autowired
@@ -22,7 +21,7 @@ public ResponseEntity<?> getAllComments() {
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getOneCommentById(@PathVariable Integer id) {
public ResponseEntity<?> getOneCommentById(@PathVariable Long id) {
return new ResponseEntity<>(commentService.findOneCommentById(id), HttpStatus.OK);
}

@@ -33,7 +32,7 @@ public ResponseEntity<?> updateReviewById(@RequestBody CommentDTO commentDTO) {
}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteCommentById(@PathVariable Integer id) {
public ResponseEntity<?> deleteCommentById(@PathVariable Long id) {
commentService.deleteCommentById(id);
return new ResponseEntity<>(id, HttpStatus.OK);
}
@@ -8,7 +8,6 @@
* Created by union on 01.06.16.
*/
@Controller
//TODO swagger io annotations fo doc
public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
@@ -23,7 +23,7 @@
@Api
@RestController
@RequestMapping("/api/posts")
//TODO swagger io annotations fo doc
//TODO swagger io annotations for generate doc
public class PostController {

@Autowired
@@ -62,11 +62,14 @@ public PageResource<Post> getPostPageResource(@RequestParam(required = false) In
})
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getOnePostById(
@ApiParam(value = "Post id", required = true) @PathVariable Integer id) {
@ApiParam(value = "Post id", required = true) @PathVariable Long id) {
return new ResponseEntity<>(postService.findOnePostById(id), HttpStatus.OK);
}

@ApiOperation(value = "Add new post", notes = "By authenticated users only.", position = 3)
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created")
})
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> addNewPost(
@ApiParam(value = "Created post object", required = true) @RequestBody PostDTO postDTO) {
@@ -77,13 +80,13 @@ public ResponseEntity<?> addNewPost(
post.setDate(LocalDate.now().toString());
post.setAuthor(postDTO.getAuthor());
postService.save(post);
return new ResponseEntity<>(postDTO.getTitle(), HttpStatus.OK);
return new ResponseEntity<>(postDTO.getTitle(), HttpStatus.CREATED);
}

@ApiOperation(value = "Delete post by id", notes = "By authenticated users only.", position = 4)
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deletePostById(
@ApiParam(value = "Post id", required = true) @PathVariable Integer id) {
@ApiParam(value = "Post id", required = true) @PathVariable Long id) {
postService.deletePostById(id);
return new ResponseEntity<>(id, HttpStatus.OK);
}
@@ -105,13 +108,14 @@ public ResponseEntity<?> deleteAllPosts() {

@ApiOperation(value = "Add new comment to post", notes = "By authenticated users only.", position = 7)
@RequestMapping(value = "/{id}/comments", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<?> addNewCommentToPost(
@ApiParam(value = "Post id", required = true) @PathVariable Integer id,
@ApiParam(value = "Post id", required = true) @PathVariable Long id,
@ApiParam(value = "Created comment object", required = true) @RequestBody CommentDTO commentDTO) {
Comment comment = new Comment();
comment.setAuthor(commentDTO.getAuthor());
comment.setReview(commentDTO.getReview());
comment.setDate(commentDTO.getDate());
comment.setDate(LocalDate.now().toString());
commentService.save(comment);

commentService.addNewCommentToPost(id, comment);
@@ -121,7 +125,7 @@ public ResponseEntity<?> addNewCommentToPost(
@ApiOperation(value = "Get all comments from post by id", notes = "By authenticated users only.", position = 8)
@RequestMapping(value = "/{id}/comments", method = RequestMethod.GET)
public ResponseEntity<?> getAllCommentsFromPost(
@ApiParam(value = "Post id", required = true) @PathVariable Integer id) {
@ApiParam(value = "Post id", required = true) @PathVariable Long id) {
return new ResponseEntity<>(commentService.findAllCommentsFromPostById(id), HttpStatus.OK);
}
}
@@ -14,7 +14,7 @@ public class Authority implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Long id;

@Column(name = "name")
private String name;
@@ -27,11 +27,11 @@ public Authority(String name) {
this.name = name;
}

public Integer getId() {
public Long getId() {
return id;
}

public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}

@@ -11,7 +11,7 @@ public class Comment implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Long id;

@Column(name = "author")
private String author;
@@ -23,7 +23,7 @@ public class Comment implements Serializable {
private String date;

@Column(name = "post_id")
private Integer postId;
private Long postId;

public Comment() {
}
@@ -34,11 +34,11 @@ public Comment(String author, String review, String date) {
this.date = date;
}

public Integer getId() {
public Long getId() {
return id;
}

public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}

@@ -66,11 +66,11 @@ public void setDate(String date) {
this.date = date;
}

public Integer getPostId() {
public Long getPostId() {
return postId;
}

public void setPostId(Integer postId) {
public void setPostId(Long postId) {
this.postId = postId;
}

@@ -12,7 +12,7 @@ public class Post implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Long id;

@Column(name = "title")
private String title;
@@ -44,11 +44,11 @@ public Post(String title, String subtitle, String content, String date, String a
this.author = author;
}

public Integer getId() {
public Long getId() {
return id;
}

public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}

@@ -22,7 +22,7 @@ public class User implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Long id;

@Column(name = "nickname", length = 32)
private String nickname;
@@ -54,11 +54,11 @@ public User(String nickname, String email, String login, String password, List<A
this.authorities = authorities;
}

public Integer getId() {
public Long getId() {
return id;
}

public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}

@@ -3,7 +3,7 @@
import java.io.Serializable;

public class CommentDTO implements Serializable {
private Integer id;
private Long id;
private String author;
private String review;
private String date;
@@ -12,19 +12,11 @@ public class CommentDTO implements Serializable {
public CommentDTO() {
}

public CommentDTO(Integer id, String author, String review, String date, Integer postId) {
this.id = id;
this.author = author;
this.review = review;
this.date = date;
this.postId = postId;
}

public Integer getId() {
public Long getId() {
return id;
}

public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}

@@ -6,7 +6,7 @@
import java.util.List;

public class PostDTO implements Serializable {
private Integer id;
private Long id;
private String title;
private String subtitle;
private String content;
@@ -17,30 +17,11 @@ public class PostDTO implements Serializable {
public PostDTO() {
}

public PostDTO(Integer id, String title, String subtitle, String content, String date, String author) {
this.id = id;
this.title = title;
this.subtitle = subtitle;
this.content = content;
this.date = date;
this.author = author;
}

public PostDTO(Integer id, String title, String subtitle, String content, String date, String author, List<Comment> commentList) {
this.id = id;
this.title = title;
this.subtitle = subtitle;
this.content = content;
this.date = date;
this.author = author;
this.commentList = commentList;
}

public Integer getId() {
public Long getId() {
return id;
}

public void setId(Integer id) {
public void setId(Long id) {
this.id = id;
}

@@ -12,12 +12,16 @@
public interface CommentRepository extends JpaRepository<Comment, Integer> {

@Query(value = "SELECT c FROM Comment c WHERE c.id = ?1")
Comment findOneCommentById(Integer id);
Comment findOneCommentById(Long id);

@Query(value = "SELECT c FROM Comment c WHERE c.postId = ?1")
List<Comment> findAllCommentsFromPostById(Integer id);
List<Comment> findAllCommentsFromPostById(Long id);

@Modifying
@Query(value = "UPDATE Comment c SET c.review = ?1 WHERE c.id = ?2")
void updateReviewById(String review, Integer id);
void updateReviewById(String review, Long id);

@Modifying
@Query(value = "DELETE FROM Comment c WHERE c.id = ?1")
void delete(Long id);
}
@@ -10,9 +10,13 @@
public interface PostRepository extends JpaRepository<Post, Integer> {

@Query(value = "SELECT p FROM Post p WHERE p.id = ?1")
Post findOnePostById(Integer id);
Post findOnePostById(Long id);

@Modifying(clearAutomatically = true)
@Query(value = "UPDATE Post p SET p.content = ?1 WHERE p.id = ?2")
void updateContentById(String content, Integer id);
void updateContentById(String content, Long id);

@Modifying
@Query(value = "DELETE FROM Post p WHERE p.id = ?1")
void delete(Long id);
}
@@ -13,12 +13,15 @@
public interface UserRepository extends JpaRepository<User, Integer> {

@Query(value = "SELECT u FROM User u WHERE u.id = ?1")
User findUserById(Integer id);
User findUserById(Long id);

@Modifying(clearAutomatically = true)
@Query(value = "UPDATE User u SET u.nickname = ?1 WHERE u.id = ?2")
void changeUserNickname(String nickname, Integer id);
void changeUserNickname(String nickname, Long id);

@Query(value = "SELECT u FROM User u WHERE u.nickname = ?1")
User findUserByNickname(String nickname);

@Query(value = "SELECT u FROM User u WHERE u.login = ?1")
User findUserByLogin(String login);
}
@@ -19,31 +19,31 @@ public class CommentService {
@Autowired
private CommentRepository commentRepository;

public Comment findOneCommentById(Long id) {
return commentRepository.findOneCommentById(id);
}

public List<Comment> findAll() {
return commentRepository.findAll();
}

public Comment findOneCommentById(Integer id) {
return commentRepository.findOneCommentById(id);
public List<Comment> findAllCommentsFromPostById(Long id) {
return commentRepository.findAllCommentsFromPostById(id);
}

public void save(Comment comment) {
commentRepository.save(comment);
}

public void deleteCommentById(Integer id) {
public void deleteCommentById(Long id) {
commentRepository.delete(id);
}

public void addNewCommentToPost(Integer id, Comment comment) {
public void addNewCommentToPost(Long id, Comment comment) {
comment.setPostId(id);
commentRepository.save(comment);
}

public List<Comment> findAllCommentsFromPostById(Integer id) {
return commentRepository.findAllCommentsFromPostById(id);
}

public void updateReviewById(CommentDTO commentDTO) {
commentRepository.updateReviewById(commentDTO.getReview(), commentDTO.getId());
}
@@ -0,0 +1,75 @@
package com.github.solairerove.blog.service;

import com.github.solairerove.blog.domain.Authority;
import com.github.solairerove.blog.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Created by union on 5/06/16.
*/
public class CustomUserDetailsService implements UserDetails {

private static final long serialVersionUID = 1L;
private Collection<? extends GrantedAuthority> authorities;
private String password;
private String login;

public CustomUserDetailsService(User user) {
this.authorities = translate(user.getAuthorities());
this.password = user.getPassword();
this.login = user.getLogin();
}

private Collection<? extends GrantedAuthority> translate(List<Authority> authorities) {
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
for (Authority authority : authorities) {
String name = authority.getName().toUpperCase();
if (!name.startsWith("ROLE_")) {
name = "ROLE_" + name;
}
grantedAuthorities.add(new SimpleGrantedAuthority(name));
}
return grantedAuthorities;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
}

@Override
public String getPassword() {
return password;
}

@Override
public String getUsername() {
return login;
}

@Override
public boolean isAccountNonExpired() {
return true;
}

@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
public boolean isCredentialsNonExpired() {
return true;
}

@Override
public boolean isEnabled() {
return true;
}
}
@@ -29,15 +29,15 @@ public Page<Post> findAll(Pageable pageable) {
return postRepository.findAll(pageable);
}

public Post findOnePostById(Integer id) {
public Post findOnePostById(Long id) {
return postRepository.findOnePostById(id);
}

public void save(Post post) {
postRepository.save(post);
}

public void deletePostById(Integer id) {
public void deletePostById(Long id) {
postRepository.delete(id);
}

@@ -1,38 +1,23 @@
package com.github.solairerove.blog.service;

import com.github.solairerove.blog.domain.User;
import com.github.solairerove.blog.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;

/**
* Created by vlad on 23.05.16.
* Created by union on 5/06/16.
*/
@Service
@Transactional
public class UserService {
@Component
public interface UserService {

@Autowired
private UserRepository userRepository;
void save(User user);

public void save(User user) {
userRepository.save(user);
}
User findUserById(Long id);

public User findUserById(Integer id) {
return userRepository.findUserById(id);
}
void changeUserNickname(String nickname, Long id);

public void changeUserNickname(String nickname, Integer id) {
userRepository.changeUserNickname(nickname, id);
}
User findUserByNickname(String nickname);

public User findUserByNickname(String nickname) {
return userRepository.findUserByNickname(nickname);
}
User findUserByLogin(String login);

public void deleteUserById(Integer id) {
userRepository.delete(id);
}
void deleteUserById(Integer id);
}
@@ -0,0 +1,43 @@
package com.github.solairerove.blog.service.impl;

import com.github.solairerove.blog.domain.User;
import com.github.solairerove.blog.repository.UserRepository;
import com.github.solairerove.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* Created by vlad on 23.05.16.
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

public void save(User user) {
userRepository.save(user);
}

public User findUserById(Long id) {
return userRepository.findUserById(id);
}

public void changeUserNickname(String nickname, Long id) {
userRepository.changeUserNickname(nickname, id);
}

public User findUserByNickname(String nickname) {
return userRepository.findUserByNickname(nickname);
}

public User findUserByLogin(String login) {
return userRepository.findUserByLogin(login);
}

public void deleteUserById(Integer id) {
userRepository.delete(id);
}
}
@@ -126,9 +126,9 @@ VALUES (
INSERT INTO `USER` (`id`, `nickname`, `email`, `login`, `password`)
VALUES (
1,
'JaVaDeV',
'javadev12@mail.com',
'voitelrulit',
'Govnov',
'govnov@gmail.com',
'user',
'strongpwd'
);

@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS `post` (
`id` INTEGER IDENTITY PRIMARY KEY,
`id` BIGINT IDENTITY PRIMARY KEY,
`title` VARCHAR(255),
`subtitle` VARCHAR(255),
`content` TEXT,
@@ -8,28 +8,28 @@ CREATE TABLE IF NOT EXISTS `post` (
);

CREATE TABLE IF NOT EXISTS `comment` (
`id` INTEGER IDENTITY PRIMARY KEY,
`id` BIGINT IDENTITY PRIMARY KEY,
`author` VARCHAR(255),
`review` VARCHAR(255),
`date` VARCHAR(255),
`post_id` INTEGER
`post_id` BIGINT
);

CREATE TABLE IF NOT EXISTS `user` (
`id` INTEGER IDENTITY PRIMARY KEY,
`id` BIGINT IDENTITY PRIMARY KEY,
`nickname` VARCHAR(255) UNIQUE,
`email` VARCHAR(255) UNIQUE,
`login` VARCHAR(255) UNIQUE,
`password` VARCHAR(255),
);

CREATE TABLE IF NOT EXISTS `authority` (
`id` INTEGER IDENTITY PRIMARY KEY,
`id` BIGINT IDENTITY PRIMARY KEY,
`name` VARCHAR(255),
);

CREATE TABLE IF NOT EXISTS `user_authority` (
`id` INTEGER IDENTITY PRIMARY KEY,
`user_id` INTEGER,
`role_id` INTEGER
`id` BIGINT IDENTITY PRIMARY KEY,
`user_id` BIGINT,
`role_id` BIGINT
);
@@ -95,6 +95,6 @@ public void addNewPost() throws Exception {
mvc.perform(MockMvcRequestBuilders.request(HttpMethod.POST, "/api/posts")
.content(objectMapper.writeValueAsString(saved))
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk());
.andExpect(MockMvcResultMatchers.status().isCreated());
}
}
@@ -3,6 +3,7 @@
import com.github.solairerove.blog.Application;
import com.github.solairerove.blog.domain.Authority;
import com.github.solairerove.blog.domain.User;
import com.github.solairerove.blog.service.UserService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;