Skip to content
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
3 changes: 3 additions & 0 deletions demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea/
.gradle/
gradle/
.DS_STORE
target/
784 changes: 618 additions & 166 deletions demo/.idea/workspace.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public List<NotesDto> getAllNotesByUserId(@PathVariable (value = "userId") Long
//Create a new Note
@PostMapping("/{userId}/notes")
public NotesDto createNote(@PathVariable (value = "userId") Long userId, @Valid @RequestBody NotesDto notesDto) throws ParseException {
return noteService.createNote1(userId, notesDto);
return noteService.createNote(userId, notesDto);
}

//Get a Single Note
@GetMapping("/{userId}/notes/{noteId}")
public NotesDto getNoteByUserIdAndNoteId(@PathVariable(value = "userId") Long userId, @PathVariable(value = "noteId") Long noteId) throws ParseException {
return noteService.getNoteByUserAndNoteId(userId, noteId);
return noteService.getNoteByUserIdAndNoteId(userId, noteId);
}

//Update a Note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class ApplicationUserDto {
private Long id;
private Date createdAt;
private String username;
private Date updatedAt;
private Date createdAt;

public void setId(Long id) {
this.id = id;
Expand Down
22 changes: 18 additions & 4 deletions demo/src/main/java/com/example/easynotes/dto/DtoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@
import com.example.easynotes.model.ApplicationUser;
import com.example.easynotes.model.Note;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;

@Service
public class DtoManager {

private final ModelMapper modelMapper;

@Autowired
private final ModelMapper modelMapper = new ModelMapper();
public DtoManager(ModelMapper modelMapper) {
this.modelMapper = modelMapper;
}

public Note convertToEntity(NotesDto notesDto) throws ParseException {
Note note = modelMapper.map(notesDto, Note.class);
return note;
}

public NotesDto convertToDto(Note note){
NotesDto noteDto = modelMapper.map(note, NotesDto.class);
return noteDto;
public NotesDto convertFromNoteToNotesDto(Note note){
TypeMap<Note, NotesDto> typeMap = modelMapper.getTypeMap(Note.class, NotesDto.class);
if (typeMap == null) {
TypeMap<Note, NotesDto> typeMap1 = modelMapper.createTypeMap(Note.class, NotesDto.class);
typeMap1.includeBase(Note.class, NotesDto.class);
typeMap = typeMap1;
}

return typeMap.map(note);
}

public ApplicationUser convertToEntity(ApplicationUserDto userDto) throws ParseException {
Expand Down
25 changes: 21 additions & 4 deletions demo/src/main/java/com/example/easynotes/dto/NotesDto.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.example.easynotes.dto;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;

public class NotesDto {
@Autowired
private ModelMapper modelMapper;

private Long noteId;

Expand All @@ -15,6 +12,26 @@ public class NotesDto {

private ApplicationUserDto user;

private Date updatedAt;

private Date createdAt;

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}

public Long getNoteId() {
return noteId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
package com.example.easynotes.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "users")
public class ApplicationUser extends AuditModel {
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class ApplicationUser implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(unique = true)
private String username;

@NotNull
private String password;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false, updatable = false)
@CreatedDate
private Date createdAt;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
@LastModifiedDate
private Date updatedAt;

public Date getCreatedAt() {
return this.createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}

public ApplicationUser() { }

public Long getId() {
Expand Down
42 changes: 0 additions & 42 deletions demo/src/main/java/com/example/easynotes/model/AuditModel.java

This file was deleted.

42 changes: 37 additions & 5 deletions demo/src/main/java/com/example/easynotes/model/Note.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
package com.example.easynotes.model;

import com.example.easynotes.dto.ApplicationUserDto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "notes")
public class Note extends AuditModel {
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Note implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long noteId;

@NotNull
@Lob
private String title;

@NotNull
@Lob
private String content;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
private ApplicationUser user;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false, updatable = false)
@CreatedDate
private Date createdAt;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
@LastModifiedDate
private Date updatedAt;

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public Date getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}

public Long getNoteId() {
return noteId;
Expand All @@ -40,7 +72,7 @@ public void setUser(ApplicationUser user) {
this.user = user;
}

protected Note() { }
public Note() { }

public String getTitle() {
return title;
Expand Down
24 changes: 21 additions & 3 deletions demo/src/main/java/com/example/easynotes/security/WebSecurity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,35 @@ public WebSecurity(UserDetailsServiceImpl userDetailsService, BCryptPasswordEnco
}

protected void configure(HttpSecurity httpSecurity) throws Exception{
httpSecurity.cors().and().csrf().disable().authorizeRequests().antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll().anyRequest().authenticated().and().addFilter(new JWTAuthenticationFilter(authenticationManager())).addFilter(new JWTAuthorizationFilter(authenticationManager())).sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST, SIGN_UP_URL)
.permitAll()
.anyRequest()
.authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(authenticationManager()))
.addFilter(new JWTAuthorizationFilter(authenticationManager()))
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
authenticationManagerBuilder
.userDetailsService(userDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}

@Bean
CorsConfigurationSource corsConfigurationSource(){
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
source
.registerCorsConfiguration("/**", new CorsConfiguration()
.applyPermitDefaultValues());
return source;
}
}
Loading