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 ### ### Angular ###
src/main/resources/static/ src/main/resources/static/
src/main/ts/node_modules/ **/node_modules/
node/ node/

View File

@@ -48,6 +48,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </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 --> <!-- https://mvnrepository.com/artifact/org.mindrot/jbcrypt -->
<dependency> <dependency>
<groupId>org.mindrot</groupId> <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.PasswordWrapperDTO;
import org.codiki.core.entities.dto.UserDTO; import org.codiki.core.entities.dto.UserDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.entities.persistence.User; import org.codiki.core.entities.persistence.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
@RestController @RestController
@RequestMapping("/api/account") @RequestMapping("/api/account")
public class AccountController { public class AccountController {
@Autowired @Autowired
private AccountService accountService; private AccountService accountService;
@JsonView(View.UserDTO.class)
@PostMapping("/login") @PostMapping("/login")
public UserDTO login(@RequestBody final User pUser) throws BadCredentialsException { public User login(@RequestBody final User pUser) throws BadCredentialsException {
return new UserDTO(accountService.authenticate(pUser)); return accountService.authenticate(pUser);
} }
@GetMapping("/logout") @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 javax.persistence.TemporalType;
import org.codiki.core.entities.dto.PostDTO; import org.codiki.core.entities.dto.PostDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.utils.DateUtils; import org.codiki.core.utils.DateUtils;
import org.hibernate.annotations.Generated; import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime; import org.hibernate.annotations.GenerationTime;
import com.fasterxml.jackson.annotation.JsonView;
@Entity @Entity
@Table(name="post") @Table(name="post")
public class Post implements Serializable { public class Post implements Serializable {
@@ -38,19 +41,25 @@ public class Post implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@JsonView(View.PostDTO.class)
// This annotation serves to fetch the attribute after an insert into db // This annotation serves to fetch the attribute after an insert into db
@Generated(GenerationTime.ALWAYS) @Generated(GenerationTime.ALWAYS)
private String key; private String key;
@JsonView(View.PostDTO.class)
private String title; private String title;
@JsonView(View.PostDTO.class)
private String text; private String text;
@JsonView(View.PostDTO.class)
@Column(length = 250) @Column(length = 250)
private String description; private String description;
@JsonView(View.PostDTO.class)
private String image; private String image;
@JsonView(View.PostDTO.class)
@Column(name = "creation_date") @Column(name = "creation_date")
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date creationDate; private Date creationDate;
@@ -58,10 +67,12 @@ public class Post implements Serializable {
/* ******************* */ /* ******************* */
/* Relations */ /* Relations */
/* ******************* */ /* ******************* */
@JsonView(View.PostDTO.class)
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "creator_id") @JoinColumn(name = "creator_id")
private User author; private User author;
@JsonView(View.PostDTO.class)
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id") @JoinColumn(name = "category_id")
private Category category; private Category category;

View File

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

View File

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

View File

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