Add json view to DTOs.

This commit is contained in:
2019-01-24 22:07:43 +01:00
parent 55940c5fe7
commit f4a2fea869
8 changed files with 85 additions and 33 deletions

4
.gitignore vendored
View File

@@ -25,5 +25,5 @@ nbdist/
### Angular ###
src/main/resources/static/
src/main/ts/node_modules/
node/
**/node_modules/
node/

View File

@@ -48,6 +48,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mindrot/jbcrypt -->
<dependency>
<groupId>org.mindrot</groupId>

View File

@@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletResponse;
import org.codiki.core.entities.dto.PasswordWrapperDTO;
import org.codiki.core.entities.dto.UserDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.entities.persistence.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
@@ -21,15 +22,18 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
@RestController
@RequestMapping("/api/account")
public class AccountController {
@Autowired
private AccountService accountService;
@JsonView(View.UserDTO.class)
@PostMapping("/login")
public UserDTO login(@RequestBody final User pUser) throws BadCredentialsException {
return new UserDTO(accountService.authenticate(pUser));
public User login(@RequestBody final User pUser) throws BadCredentialsException {
return accountService.authenticate(pUser);
}
@GetMapping("/logout")

View File

@@ -0,0 +1,6 @@
package org.codiki.core.entities.dto;
public class View {
public interface UserDTO {}
public interface PostDTO {}
}

View File

@@ -19,10 +19,13 @@ import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.codiki.core.entities.dto.PostDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.utils.DateUtils;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import com.fasterxml.jackson.annotation.JsonView;
@Entity
@Table(name="post")
public class Post implements Serializable {
@@ -38,19 +41,25 @@ public class Post implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonView(View.PostDTO.class)
// This annotation serves to fetch the attribute after an insert into db
@Generated(GenerationTime.ALWAYS)
private String key;
@JsonView(View.PostDTO.class)
private String title;
@JsonView(View.PostDTO.class)
private String text;
@JsonView(View.PostDTO.class)
@Column(length = 250)
private String description;
@JsonView(View.PostDTO.class)
private String image;
@JsonView(View.PostDTO.class)
@Column(name = "creation_date")
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@@ -58,10 +67,12 @@ public class Post implements Serializable {
/* ******************* */
/* Relations */
/* ******************* */
@JsonView(View.PostDTO.class)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "creator_id")
private User author;
@JsonView(View.PostDTO.class)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;

View File

@@ -8,15 +8,21 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.codiki.core.entities.dto.View;
import com.fasterxml.jackson.annotation.JsonView;
@Entity
@Table(name="role")
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
@JsonView({View.UserDTO.class})
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonView({View.UserDTO.class})
private String name;
public Long getId() {

View File

@@ -20,9 +20,12 @@ import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.codiki.core.entities.dto.View;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import com.fasterxml.jackson.annotation.JsonView;
@Entity
@Table(name="`user`")
public class User implements Serializable {
@@ -35,19 +38,24 @@ public class User implements Serializable {
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="user_id_seq")
@SequenceGenerator(name="user_id_seq", sequenceName="user_id_seq", allocationSize=1)
private Long id;
@JsonView({View.UserDTO.class, View.PostDTO.class})
// This annotation serves to fetch the attribute after an insert into db
@Generated(GenerationTime.ALWAYS)
private String key;
@JsonView({View.UserDTO.class, View.PostDTO.class})
private String name;
@JsonView({View.UserDTO.class, View.PostDTO.class})
private String email;
private String password;
@JsonView({View.UserDTO.class, View.PostDTO.class})
private String image;
@JsonView({View.UserDTO.class, View.PostDTO.class})
@Column(name = "inscription_date")
@Temporal(TemporalType.TIMESTAMP)
private Date inscriptionDate;
@@ -55,6 +63,7 @@ public class User implements Serializable {
/* ******************* */
/* Relations */
/* ******************* */
@JsonView({View.UserDTO.class})
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
private Role role;

View File

@@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codiki.core.entities.dto.PostDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.entities.persistence.Post;
import org.codiki.core.entities.persistence.User;
import org.codiki.core.repositories.PostRepository;
@@ -28,6 +29,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
@RestController
@RequestMapping("/api/posts")
public class PostController {
@@ -51,7 +54,8 @@ public class PostController {
return StreamSupport.stream(postRepository.findAll().spliterator(), false)
.map(PostDTO::new).collect(Collectors.toList());
}
@JsonView(View.PostDTO.class)
@GetMapping("/{postKey}")
public PostDTO getByKey(@PathVariable("postKey") final String pPostKey,
final HttpServletResponse response) {
@@ -61,7 +65,8 @@ public class PostController {
}
return result;
}
@JsonView(View.PostDTO.class)
@GetMapping("/{postKey}/source")
public PostDTO getByKeyAndSource(@PathVariable("postKey")final String pPostKey,
final HttpServletResponse response) {
@@ -76,40 +81,45 @@ public class PostController {
return result;
}
@JsonView(View.PostDTO.class)
@GetMapping("/last")
public List<PostDTO> getLast() {
return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME)).stream()
.map(PostDTO::new).collect(Collectors.toList());
public List<Post> getLast() {
return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME));
}
@JsonView(View.PostDTO.class)
@GetMapping("/search/{searchCriteria}")
public List<PostDTO> search(@PathVariable("searchCriteria") final String pSearchCriteria) {
return postService.search(pSearchCriteria);
}
@JsonView(View.PostDTO.class)
@GetMapping("/byCategory/{categoryId}")
public List<PostDTO> getByCategory(@PathVariable("categoryId") final Long pCategoryId) {
return postRepository.getByCategoryId(pCategoryId).stream()
.map(PostDTO::new).collect(Collectors.toList());
}
@JsonView(View.PostDTO.class)
@GetMapping("/myPosts")
public List<PostDTO> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
public List<Post> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
final Principal pPrincipal) throws IOException {
List<PostDTO> result = new LinkedList<>();
List<Post> result = new LinkedList<>();
final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
if(connectedUser.isPresent()) {
result = postRepository.getByCreator(connectedUser.get().getId())
.stream().map(PostDTO::new).collect(Collectors.toList());
// result = postRepository.getByCreator(connectedUser.get().getId())
// .stream().map(PostDTO::new).collect(Collectors.toList());
result = postRepository.getByCreator(connectedUser.get().getId());
} else {
pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
return result;
}
@JsonView(View.PostDTO.class)
@PostMapping("/preview")
public PostDTO preview(@RequestBody final PostDTO pPost) {
final PostDTO result = new PostDTO();
@@ -123,7 +133,8 @@ public class PostController {
return result;
}
@JsonView(View.PostDTO.class)
@PostMapping("/")
public PostDTO insert(@RequestBody final PostDTO pPost, final HttpServletRequest pRequest,
final HttpServletResponse pResponse, final Principal pPrincipal) {