Add json view to DTOs.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -25,5 +25,5 @@ nbdist/
|
||||
|
||||
### Angular ###
|
||||
src/main/resources/static/
|
||||
src/main/ts/node_modules/
|
||||
**/node_modules/
|
||||
node/
|
||||
5
pom.xml
5
pom.xml
@@ -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>
|
||||
|
||||
@@ -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")
|
||||
|
||||
6
src/main/java/org/codiki/core/entities/dto/View.java
Executable file
6
src/main/java/org/codiki/core/entities/dto/View.java
Executable file
@@ -0,0 +1,6 @@
|
||||
package org.codiki.core.entities.dto;
|
||||
|
||||
public class View {
|
||||
public interface UserDTO {}
|
||||
public interface PostDTO {}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 {
|
||||
@@ -36,18 +39,23 @@ public class User implements Serializable {
|
||||
@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;
|
||||
|
||||
@@ -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 {
|
||||
@@ -52,6 +55,7 @@ public class PostController {
|
||||
.map(PostDTO::new).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@JsonView(View.PostDTO.class)
|
||||
@GetMapping("/{postKey}")
|
||||
public PostDTO getByKey(@PathVariable("postKey") final String pPostKey,
|
||||
final HttpServletResponse response) {
|
||||
@@ -62,6 +66,7 @@ public class PostController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@JsonView(View.PostDTO.class)
|
||||
@GetMapping("/{postKey}/source")
|
||||
public PostDTO getByKeyAndSource(@PathVariable("postKey")final String pPostKey,
|
||||
final HttpServletResponse response) {
|
||||
@@ -77,32 +82,36 @@ 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);
|
||||
}
|
||||
@@ -110,6 +119,7 @@ public class PostController {
|
||||
return result;
|
||||
}
|
||||
|
||||
@JsonView(View.PostDTO.class)
|
||||
@PostMapping("/preview")
|
||||
public PostDTO preview(@RequestBody final PostDTO pPost) {
|
||||
final PostDTO result = new PostDTO();
|
||||
@@ -124,6 +134,7 @@ 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) {
|
||||
|
||||
Reference in New Issue
Block a user