From b7e1b19b42097d559f21bb867fbe9b192061d36e Mon Sep 17 00:00:00 2001 From: nawal-ahmad Date: Sat, 23 Oct 2021 21:24:11 +0300 Subject: [PATCH 1/7] signup-login-profile --- build.gradle | 8 +- .../com/example/init/controllers/User.java | 65 ++++++++ .../example/init/models/ApplicationUser.java | 144 ++++++++++++++++++ .../ApplicationUserRepository.java | 10 ++ .../init/security/UserDetailsServiceImpl.java | 25 +++ .../init/security/WebSecurityConfig.java | 32 ++++ src/main/resources/application.properties | 23 ++- src/main/resources/templates/login.html | 29 ++++ src/main/resources/templates/profile.html | 18 +++ src/main/resources/templates/signup.html | 45 ++++++ 10 files changed, 389 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/example/init/controllers/User.java create mode 100644 src/main/java/com/example/init/models/ApplicationUser.java create mode 100644 src/main/java/com/example/init/repositories/ApplicationUserRepository.java create mode 100644 src/main/java/com/example/init/security/UserDetailsServiceImpl.java create mode 100644 src/main/java/com/example/init/security/WebSecurityConfig.java create mode 100644 src/main/resources/templates/login.html create mode 100644 src/main/resources/templates/profile.html create mode 100644 src/main/resources/templates/signup.html diff --git a/build.gradle b/build.gradle index 71840fc..8a1b3cf 100644 --- a/build.gradle +++ b/build.gradle @@ -13,16 +13,16 @@ repositories { } dependencies { - // implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - // implementation 'org.springframework.boot:spring-boot-starter-security' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-websocket' - // implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' + implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'org.postgresql:postgresql' testImplementation 'org.springframework.boot:spring-boot-starter-test' - // testImplementation 'org.springframework.security:spring-security-test' + testImplementation 'org.springframework.security:spring-security-test' } test { diff --git a/src/main/java/com/example/init/controllers/User.java b/src/main/java/com/example/init/controllers/User.java new file mode 100644 index 0000000..413b164 --- /dev/null +++ b/src/main/java/com/example/init/controllers/User.java @@ -0,0 +1,65 @@ +package com.example.init.controllers; + +import antlr.BaseAST; +import com.example.init.models.ApplicationUser; +import com.example.init.repositories.ApplicationUserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.view.RedirectView; + +import java.security.Principal; +import java.util.ArrayList; + +@Controller +public class User { + + @Autowired + BCryptPasswordEncoder bCryptPasswordEncoder; + + @Autowired + ApplicationUserRepository applicationUserRepository; + + @GetMapping("/signup") + public String getSignUpPage() { + return "signup"; + } + + @GetMapping("/login") + public String getSignInPage() { + return "login"; + } + + @PostMapping("/signup") + public RedirectView attemptSignUp(@ModelAttribute ApplicationUser user) { + ApplicationUser newUser = new ApplicationUser(user.getEmail(),user.getUsername(), + bCryptPasswordEncoder.encode(user.getPassword()), + user.getFirstName(), user.getLastName(), user.getDateOfBirth(), user.getBio()); + applicationUserRepository.save(newUser); + Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>()); + SecurityContextHolder.getContext().setAuthentication(authentication); + return new RedirectView("/login"); + } + + @PostMapping("/login") + public RedirectView loginResponse(@ModelAttribute ApplicationUser user, Model model) { + model.addAttribute("username", applicationUserRepository.findByUsername(user.getUsername())); + return new RedirectView("profile"); + } + + @GetMapping("/profile") + public String getUserProfile(Principal principal, Model model) { + ApplicationUser user = applicationUserRepository.findByUsername(principal.getName()); + model.addAttribute("username", principal.getName()); + model.addAttribute("userProfile", user); + return "profile"; + } + +} diff --git a/src/main/java/com/example/init/models/ApplicationUser.java b/src/main/java/com/example/init/models/ApplicationUser.java new file mode 100644 index 0000000..7f4ca18 --- /dev/null +++ b/src/main/java/com/example/init/models/ApplicationUser.java @@ -0,0 +1,144 @@ +package com.example.init.models; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import javax.persistence.*; +import java.util.Collection; + +@Entity +public class ApplicationUser implements UserDetails { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + private Long id; + + @Column(unique = true) + private String email; + + @Column(unique = true) + private String username; + + private String password; + private String firstName; + private String lastName; + private String dateOfBirth; + private String bio; + + public ApplicationUser() { + } + + public ApplicationUser(Long id, String email, String username, String password, String firstName, String lastName, String dateOfBirth, String bio) { + this.id = id; + this.email = email; + this.username = username; + this.password = password; + this.firstName = firstName; + this.lastName = lastName; + this.dateOfBirth = dateOfBirth; + this.bio = bio; + } + + public ApplicationUser(String email, String username, String password, String firstName, String lastName, String dateOfBirth, String bio) { + this.email = email; + this.username = username; + this.password = password; + this.firstName = firstName; + this.lastName = lastName; + this.dateOfBirth = dateOfBirth; + this.bio = bio; + } + + + @Override + public Collection getAuthorities() { + return null; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public boolean isAccountNonExpired() { + return false; + } + + @Override + public boolean isAccountNonLocked() { + return false; + } + + @Override + public boolean isCredentialsNonExpired() { + return false; + } + + @Override + public boolean isEnabled() { + return false; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(String dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + + public String getBio() { + return bio; + } + + public void setBio(String bio) { + this.bio = bio; + } + +} diff --git a/src/main/java/com/example/init/repositories/ApplicationUserRepository.java b/src/main/java/com/example/init/repositories/ApplicationUserRepository.java new file mode 100644 index 0000000..b2dba6f --- /dev/null +++ b/src/main/java/com/example/init/repositories/ApplicationUserRepository.java @@ -0,0 +1,10 @@ +package com.example.init.repositories; + +import com.example.init.models.ApplicationUser; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ApplicationUserRepository extends JpaRepository { + public ApplicationUser findByUsername(String username); +} diff --git a/src/main/java/com/example/init/security/UserDetailsServiceImpl.java b/src/main/java/com/example/init/security/UserDetailsServiceImpl.java new file mode 100644 index 0000000..c6b5e57 --- /dev/null +++ b/src/main/java/com/example/init/security/UserDetailsServiceImpl.java @@ -0,0 +1,25 @@ +package com.example.init.security; + +import com.example.init.models.ApplicationUser; +import com.example.init.repositories.ApplicationUserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service +public class UserDetailsServiceImpl implements UserDetailsService { + + @Autowired + ApplicationUserRepository applicationUserRepository; + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + ApplicationUser applicationUser = applicationUserRepository.findByUsername(username); + if (applicationUser == null) { + throw new UsernameNotFoundException((username + " not found!")); + } + return applicationUser; + } +} diff --git a/src/main/java/com/example/init/security/WebSecurityConfig.java b/src/main/java/com/example/init/security/WebSecurityConfig.java new file mode 100644 index 0000000..d29a49e --- /dev/null +++ b/src/main/java/com/example/init/security/WebSecurityConfig.java @@ -0,0 +1,32 @@ +package com.example.init.security; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Autowired + UserDetailsServiceImpl userDetailsService; + + @Bean + public BCryptPasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.cors().disable().csrf().disable().authorizeRequests().antMatchers( "/login", "/signup","/").permitAll().antMatchers("/adminpanel").hasAuthority("ROLE_ADMIN").anyRequest().authenticated().and().formLogin().loginPage("/login").loginProcessingUrl("/perform_login").defaultSuccessUrl("/", true).failureUrl("/error").and().logout().logoutUrl("/perform_logout"); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f03a366..30c62a3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,18 @@ +spring.datasource.hikari.connectionTimeout=20000 +spring.datasource.hikari.maximumPoolSize=5 spring.sql.init.platform=postgres -spring.datasource.url=jdbc:postgresql://localhost:5432/coders -spring.datasource.username=abdelqader -spring.datasource.password=0000 -spring.jpa.database=postgresql -spring.jpa.generate-ddl=true -spring.jpa.hibernate.ddl-auto=update + +spring.datasource.url=jdbc:postgresql://localhost:5433/init +spring.datasource.username=postgres +spring.datasource.password=1234 + +spring.jpa.hibernate.ddl-auto=create-drop +spring.datasource.initialization-mode=always + +spring.jpa.show-sql=true +spring.jpa.generate-ddl=false +spring.jpa.open-in-view=false +spring.jpa.properties.hibernate.show_sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.type=trace +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 0000000..2ee5460 --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,29 @@ + + + + + login + + + +
+ +
+ Invalid username and password. +
+
+ You have been logged out. +
+ +
+ + +
+
+ + +
+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html new file mode 100644 index 0000000..18157f4 --- /dev/null +++ b/src/main/resources/templates/profile.html @@ -0,0 +1,18 @@ + + + + + user ptofile + + + +
+ ProfilePicture +
+
+

+

+

+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/signup.html b/src/main/resources/templates/signup.html new file mode 100644 index 0000000..29ce50c --- /dev/null +++ b/src/main/resources/templates/signup.html @@ -0,0 +1,45 @@ + + + + + signup + + + +
+ Sign up +
+
+ + +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ + \ No newline at end of file From 9820bf9dfbb69154027eda9f4561dedae29ca382 Mon Sep 17 00:00:00 2001 From: nawal-ahmad Date: Sun, 24 Oct 2021 10:06:10 +0300 Subject: [PATCH 2/7] signup-signin-profile --- .../com/example/init/controllers/User.java | 6 +++++ .../ApplicationUserRepository.java | 1 + .../init/security/WebSecurityConfig.java | 22 ++++++++++++++++++- src/main/resources/templates/login.html | 2 +- src/main/resources/templates/profile.html | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/init/controllers/User.java b/src/main/java/com/example/init/controllers/User.java index 413b164..c870e2e 100644 --- a/src/main/java/com/example/init/controllers/User.java +++ b/src/main/java/com/example/init/controllers/User.java @@ -7,6 +7,8 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -17,6 +19,7 @@ import java.security.Principal; import java.util.ArrayList; +import java.util.Optional; @Controller public class User { @@ -51,9 +54,12 @@ public RedirectView attemptSignUp(@ModelAttribute ApplicationUser user) { @PostMapping("/login") public RedirectView loginResponse(@ModelAttribute ApplicationUser user, Model model) { model.addAttribute("username", applicationUserRepository.findByUsername(user.getUsername())); + model.addAttribute("email", applicationUserRepository.findByEmail(user.getEmail())); +// if (user.isEmpty()) user = applicationUserRepository.findByUsername(email); return new RedirectView("profile"); } + @GetMapping("/profile") public String getUserProfile(Principal principal, Model model) { ApplicationUser user = applicationUserRepository.findByUsername(principal.getName()); diff --git a/src/main/java/com/example/init/repositories/ApplicationUserRepository.java b/src/main/java/com/example/init/repositories/ApplicationUserRepository.java index b2dba6f..bba47bc 100644 --- a/src/main/java/com/example/init/repositories/ApplicationUserRepository.java +++ b/src/main/java/com/example/init/repositories/ApplicationUserRepository.java @@ -7,4 +7,5 @@ @Repository public interface ApplicationUserRepository extends JpaRepository { public ApplicationUser findByUsername(String username); + public ApplicationUser findByEmail(String email); } diff --git a/src/main/java/com/example/init/security/WebSecurityConfig.java b/src/main/java/com/example/init/security/WebSecurityConfig.java index d29a49e..f72fb63 100644 --- a/src/main/java/com/example/init/security/WebSecurityConfig.java +++ b/src/main/java/com/example/init/security/WebSecurityConfig.java @@ -27,6 +27,26 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception { @Override protected void configure(HttpSecurity http) throws Exception { - http.cors().disable().csrf().disable().authorizeRequests().antMatchers( "/login", "/signup","/").permitAll().antMatchers("/adminpanel").hasAuthority("ROLE_ADMIN").anyRequest().authenticated().and().formLogin().loginPage("/login").loginProcessingUrl("/perform_login").defaultSuccessUrl("/", true).failureUrl("/error").and().logout().logoutUrl("/perform_logout"); + http + .cors() + .disable(). + csrf() + .disable() + .authorizeRequests() + .antMatchers( "/login", "/signup","/") + .permitAll().antMatchers("/adminpanel") + .hasAuthority("ROLE_ADMIN") + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .usernameParameter("email") + .permitAll() + .loginProcessingUrl("/perform_login") + .defaultSuccessUrl("/", true) + .failureUrl("/error") + .and() + .logout() + .logoutUrl("/perform_logout"); } } diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index 2ee5460..1fbc38b 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -16,7 +16,7 @@
- +
diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 18157f4..d701c06 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -10,6 +10,7 @@ ProfilePicture
+

From 680942d0f7673904edca84bb0817fa2038aee1c8 Mon Sep 17 00:00:00 2001 From: nawal-ahmad Date: Sun, 24 Oct 2021 12:24:54 +0300 Subject: [PATCH 3/7] post --- .../com/example/init/controllers/User.java | 19 ++++-- .../example/init/models/ApplicationUser.java | 61 ++++++++--------- .../java/com/example/init/models/Post.java | 67 +++++++++++++++++++ .../init/repositories/PostRepository.java | 9 +++ src/main/resources/templates/profile.html | 31 ++++++++- 5 files changed, 149 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/example/init/models/Post.java create mode 100644 src/main/java/com/example/init/repositories/PostRepository.java diff --git a/src/main/java/com/example/init/controllers/User.java b/src/main/java/com/example/init/controllers/User.java index c870e2e..28058d2 100644 --- a/src/main/java/com/example/init/controllers/User.java +++ b/src/main/java/com/example/init/controllers/User.java @@ -2,7 +2,9 @@ import antlr.BaseAST; import com.example.init.models.ApplicationUser; +import com.example.init.models.Post; import com.example.init.repositories.ApplicationUserRepository; +import com.example.init.repositories.PostRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -16,10 +18,8 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.view.RedirectView; - import java.security.Principal; import java.util.ArrayList; -import java.util.Optional; @Controller public class User { @@ -30,6 +30,9 @@ public class User { @Autowired ApplicationUserRepository applicationUserRepository; + @Autowired + PostRepository postRepository; + @GetMapping("/signup") public String getSignUpPage() { return "signup"; @@ -42,9 +45,8 @@ public String getSignInPage() { @PostMapping("/signup") public RedirectView attemptSignUp(@ModelAttribute ApplicationUser user) { - ApplicationUser newUser = new ApplicationUser(user.getEmail(),user.getUsername(), - bCryptPasswordEncoder.encode(user.getPassword()), - user.getFirstName(), user.getLastName(), user.getDateOfBirth(), user.getBio()); + ApplicationUser newUser = new ApplicationUser(user.getEmail(),bCryptPasswordEncoder.encode(user.getUsername()),user.getPassword(), + user.getFirstName(),user.getLastName(),user.getDateOfBirth(),user.getBio()); applicationUserRepository.save(newUser); Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>()); SecurityContextHolder.getContext().setAuthentication(authentication); @@ -68,4 +70,11 @@ public String getUserProfile(Principal principal, Model model) { return "profile"; } + @PostMapping("/addPost") + public RedirectView addPost(Principal principle, String body) { + ApplicationUser newUser = applicationUserRepository.findByUsername(principle.getName()); + Post post = new Post(newUser, body); + postRepository.save(post); + return new RedirectView("/profile"); + } } diff --git a/src/main/java/com/example/init/models/ApplicationUser.java b/src/main/java/com/example/init/models/ApplicationUser.java index 7f4ca18..6b7d7c9 100644 --- a/src/main/java/com/example/init/models/ApplicationUser.java +++ b/src/main/java/com/example/init/models/ApplicationUser.java @@ -2,9 +2,9 @@ import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; - import javax.persistence.*; import java.util.Collection; +import java.util.List; @Entity public class ApplicationUser implements UserDetails { @@ -18,29 +18,20 @@ public class ApplicationUser implements UserDetails { @Column(unique = true) private String username; - private String password; private String firstName; private String lastName; private String dateOfBirth; private String bio; - public ApplicationUser() { - } + @OneToMany (mappedBy = "applicationUser", fetch = FetchType.EAGER) + private List posts; - public ApplicationUser(Long id, String email, String username, String password, String firstName, String lastName, String dateOfBirth, String bio) { - this.id = id; - this.email = email; - this.username = username; - this.password = password; - this.firstName = firstName; - this.lastName = lastName; - this.dateOfBirth = dateOfBirth; - this.bio = bio; + public ApplicationUser() { } public ApplicationUser(String email, String username, String password, String firstName, String lastName, String dateOfBirth, String bio) { - this.email = email; + this.id = id; this.username = username; this.password = password; this.firstName = firstName; @@ -49,22 +40,6 @@ public ApplicationUser(String email, String username, String password, String fi this.bio = bio; } - - @Override - public Collection getAuthorities() { - return null; - } - - @Override - public String getPassword() { - return password; - } - - @Override - public String getUsername() { - return username; - } - @Override public boolean isAccountNonExpired() { return false; @@ -85,6 +60,15 @@ public boolean isEnabled() { return false; } + public void setUsername(String username) { + this.username = username; + } + + @Override + public Collection getAuthorities() { + return null; + } + public Long getId() { return id; } @@ -101,8 +85,14 @@ public void setEmail(String email) { this.email = email; } - public void setUsername(String username) { - this.username = username; + @Override + public String getUsername() { + return username; + } + + @Override + public String getPassword() { + return password; } public void setPassword(String password) { @@ -141,4 +131,11 @@ public void setBio(String bio) { this.bio = bio; } + public List getPosts() { + return posts; + } + + public void setPosts(List posts) { + this.posts = posts; + } } diff --git a/src/main/java/com/example/init/models/Post.java b/src/main/java/com/example/init/models/Post.java new file mode 100644 index 0000000..1be4400 --- /dev/null +++ b/src/main/java/com/example/init/models/Post.java @@ -0,0 +1,67 @@ +package com.example.init.models; + +import com.fasterxml.jackson.annotation.JsonFormat; +import org.hibernate.annotations.CreationTimestamp; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.*; +import java.time.LocalDateTime; + +@Entity +public class Post { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + private Long id; + + @ManyToOne() + @JoinColumn(name = "application_user_id") + private ApplicationUser applicationUser; + + private String body; + + @CreationTimestamp + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) + @JsonFormat(pattern = "yyyy-MM-dd||HH:mm:ss") + private LocalDateTime createdAt; + + public Post() { + } + + public Post(ApplicationUser applicationUser, String body ) { + this.applicationUser = applicationUser; + this.body = body; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public ApplicationUser getApplicationUser() { + return applicationUser; + } + + public void setApplicationUser(ApplicationUser applicationUser) { + this.applicationUser = applicationUser; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public LocalDateTime getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(LocalDateTime createdAt) { + this.createdAt = createdAt; + } +} diff --git a/src/main/java/com/example/init/repositories/PostRepository.java b/src/main/java/com/example/init/repositories/PostRepository.java new file mode 100644 index 0000000..a2d4053 --- /dev/null +++ b/src/main/java/com/example/init/repositories/PostRepository.java @@ -0,0 +1,9 @@ +package com.example.init.repositories; + +import com.example.init.models.Post; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PostRepository extends JpaRepository { +} diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index d701c06..006e555 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -2,7 +2,7 @@ - user ptofile + user profile @@ -15,5 +15,34 @@
+ +
+
+ +
+
+
+ + +
+ +
+
+
+ +
+
+ +Posts + +
+
+
+

+

+
+
+
+ \ No newline at end of file From 97234e24c76608272da9806d2d9ef8e6c651fe74 Mon Sep 17 00:00:00 2001 From: nawal-ahmad Date: Sun, 24 Oct 2021 13:24:20 +0300 Subject: [PATCH 4/7] add post --- src/main/java/com/example/init/controllers/User.java | 2 +- src/main/java/com/example/init/models/ApplicationUser.java | 2 +- src/main/java/com/example/init/models/Post.java | 6 ++++-- src/main/resources/application.properties | 2 +- src/main/resources/templates/profile.html | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/init/controllers/User.java b/src/main/java/com/example/init/controllers/User.java index 28058d2..954e07c 100644 --- a/src/main/java/com/example/init/controllers/User.java +++ b/src/main/java/com/example/init/controllers/User.java @@ -45,7 +45,7 @@ public String getSignInPage() { @PostMapping("/signup") public RedirectView attemptSignUp(@ModelAttribute ApplicationUser user) { - ApplicationUser newUser = new ApplicationUser(user.getEmail(),bCryptPasswordEncoder.encode(user.getUsername()),user.getPassword(), + ApplicationUser newUser = new ApplicationUser(user.getEmail(),user.getUsername(),bCryptPasswordEncoder.encode(user.getPassword()), user.getFirstName(),user.getLastName(),user.getDateOfBirth(),user.getBio()); applicationUserRepository.save(newUser); Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>()); diff --git a/src/main/java/com/example/init/models/ApplicationUser.java b/src/main/java/com/example/init/models/ApplicationUser.java index 6b7d7c9..c8d5581 100644 --- a/src/main/java/com/example/init/models/ApplicationUser.java +++ b/src/main/java/com/example/init/models/ApplicationUser.java @@ -31,7 +31,7 @@ public ApplicationUser() { } public ApplicationUser(String email, String username, String password, String firstName, String lastName, String dateOfBirth, String bio) { - this.id = id; + this.email=email; this.username = username; this.password = password; this.firstName = firstName; diff --git a/src/main/java/com/example/init/models/Post.java b/src/main/java/com/example/init/models/Post.java index 1be4400..ec2fd70 100644 --- a/src/main/java/com/example/init/models/Post.java +++ b/src/main/java/com/example/init/models/Post.java @@ -14,7 +14,8 @@ public class Post { @Column(name = "id", nullable = false) private Long id; - @ManyToOne() + + @ManyToOne @JoinColumn(name = "application_user_id") private ApplicationUser applicationUser; @@ -22,7 +23,7 @@ public class Post { @CreationTimestamp @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) - @JsonFormat(pattern = "yyyy-MM-dd||HH:mm:ss") +// @JsonFormat(pattern = "yyyy-MM-dd||HH:mm:ss") private LocalDateTime createdAt; public Post() { @@ -64,4 +65,5 @@ public LocalDateTime getCreatedAt() { public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 30c62a3..61bd96f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,7 +6,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5433/init spring.datasource.username=postgres spring.datasource.password=1234 -spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=create spring.datasource.initialization-mode=always spring.jpa.show-sql=true diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 006e555..8e0f5f7 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -9,7 +9,7 @@
ProfilePicture
-
+

From daf22407160635367d8777636fe37a77cbda475d Mon Sep 17 00:00:00 2001 From: Heba1998 Date: Sun, 24 Oct 2021 15:22:02 +0300 Subject: [PATCH 5/7] first try --- build.gradle | 1 + quiz.json | 1 + .../com/example/init/InitApplication.java | 31 +++++++++++++++++-- src/main/resources/application.properties | 4 +-- 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 quiz.json diff --git a/build.gradle b/build.gradle index 71840fc..4925ba1 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ dependencies { runtimeOnly 'org.postgresql:postgresql' testImplementation 'org.springframework.boot:spring-boot-starter-test' // testImplementation 'org.springframework.security:spring-security-test' + implementation 'com.google.code.gson:gson:2.8.8' } test { diff --git a/quiz.json b/quiz.json new file mode 100644 index 0000000..2f5acc5 --- /dev/null +++ b/quiz.json @@ -0,0 +1 @@ +[{"id":711,"question":"Are arrays supported in shell scripts?","description":null,"answers":{"answer_a":"True","answer_b":"False","answer_c":"Yes but only under certain conditions","answer_d":null,"answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"true","answer_b_correct":"false","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":null,"explanation":null,"tip":null,"tags":[{"name":"BASH"},{"name":"Linux"}],"category":"Linux","difficulty":"Easy"},{"id":943,"question":"What does PVC stand for?","description":null,"answers":{"answer_a":"Pod Volume Claim","answer_b":"Persistent Volume Container","answer_c":"Persistent Volume Claim","answer_d":"Pod Volume Container","answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"false","answer_c_correct":"true","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":null,"explanation":null,"tip":null,"tags":[{"name":"Docker"}],"category":"Docker","difficulty":"Medium"},{"id":339,"question":"Which of the following are not Numeric column types?","description":null,"answers":{"answer_a":"BIGINT","answer_b":"LARGEINT","answer_c":"SMALLINT","answer_d":"DOUBLE","answer_e":"DECIMAL","answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"true","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":"answer_a","explanation":null,"tip":null,"tags":[{"name":"MySQL"}],"category":"SQL","difficulty":"Hard"},{"id":662,"question":"Which command can give first found difference between two file after comparing?","description":null,"answers":{"answer_a":"diff","answer_b":"cmp","answer_c":"Common","answer_d":"stat","answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"true","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":"answer_a","explanation":null,"tip":null,"tags":[{"name":"BASH"}],"category":"Linux","difficulty":"Easy"},{"id":548,"question":"Which of the following is used to add multiple line comments in PHP ?","description":null,"answers":{"answer_a":"\/\/","answer_b":"\/* *\/","answer_c":"{{ }}","answer_d":"{\/ \\}","answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"true","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":"answer_a","explanation":null,"tip":null,"tags":[{"name":"HTML"}],"category":"Code","difficulty":"Medium"}][{"id":711,"question":"Are arrays supported in shell scripts?","description":null,"answers":{"answer_a":"True","answer_b":"False","answer_c":"Yes but only under certain conditions","answer_d":null,"answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"true","answer_b_correct":"false","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":null,"explanation":null,"tip":null,"tags":[{"name":"BASH"},{"name":"Linux"}],"category":"Linux","difficulty":"Easy"},{"id":943,"question":"What does PVC stand for?","description":null,"answers":{"answer_a":"Pod Volume Claim","answer_b":"Persistent Volume Container","answer_c":"Persistent Volume Claim","answer_d":"Pod Volume Container","answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"false","answer_c_correct":"true","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":null,"explanation":null,"tip":null,"tags":[{"name":"Docker"}],"category":"Docker","difficulty":"Medium"},{"id":339,"question":"Which of the following are not Numeric column types?","description":null,"answers":{"answer_a":"BIGINT","answer_b":"LARGEINT","answer_c":"SMALLINT","answer_d":"DOUBLE","answer_e":"DECIMAL","answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"true","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":"answer_a","explanation":null,"tip":null,"tags":[{"name":"MySQL"}],"category":"SQL","difficulty":"Hard"},{"id":662,"question":"Which command can give first found difference between two file after comparing?","description":null,"answers":{"answer_a":"diff","answer_b":"cmp","answer_c":"Common","answer_d":"stat","answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"true","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":"answer_a","explanation":null,"tip":null,"tags":[{"name":"BASH"}],"category":"Linux","difficulty":"Easy"},{"id":548,"question":"Which of the following is used to add multiple line comments in PHP ?","description":null,"answers":{"answer_a":"\/\/","answer_b":"\/* *\/","answer_c":"{{ }}","answer_d":"{\/ \\}","answer_e":null,"answer_f":null},"multiple_correct_answers":"false","correct_answers":{"answer_a_correct":"false","answer_b_correct":"true","answer_c_correct":"false","answer_d_correct":"false","answer_e_correct":"false","answer_f_correct":"false"},"correct_answer":"answer_a","explanation":null,"tip":null,"tags":[{"name":"HTML"}],"category":"Code","difficulty":"Medium"}] \ No newline at end of file diff --git a/src/main/java/com/example/init/InitApplication.java b/src/main/java/com/example/init/InitApplication.java index 365f755..006710e 100644 --- a/src/main/java/com/example/init/InitApplication.java +++ b/src/main/java/com/example/init/InitApplication.java @@ -1,13 +1,40 @@ package com.example.init; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import java.io.*; +import java.lang.reflect.Type; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.List; +import java.util.Set; + @SpringBootApplication public class InitApplication { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { SpringApplication.run(InitApplication.class, args); - } + StringBuilder createdLine = new StringBuilder(); + String url = "https://quizapi.io/api/v1/questions?apiKey=phkL2Z69NDCrImLlfnLoElQkoF3StoJlQrAeKvkf&limit=5&css"; + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + + connection.setConnectTimeout(5000); + connection.setReadTimeout(5000); + + InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream()); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + + String data = bufferedReader.readLine(); + createdLine = new StringBuilder(data); + System.out.println(data); + createdLine.append(data); + bufferedReader.close(); + FileWriter fileToWrite = new FileWriter("quiz.json"); + fileToWrite.write(createdLine.toString()); + fileToWrite.close(); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f03a366..90a30c2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ spring.sql.init.platform=postgres spring.datasource.url=jdbc:postgresql://localhost:5432/coders -spring.datasource.username=abdelqader -spring.datasource.password=0000 +spring.datasource.username=postgres +spring.datasource.password=1234 spring.jpa.database=postgresql spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=update From 505504577c2ee5a99c9c69f4b8c982d1fa6b1640 Mon Sep 17 00:00:00 2001 From: abdelqader-alomari Date: Sun, 24 Oct 2021 15:23:30 +0300 Subject: [PATCH 6/7] back to main --- api.json | 98 +++++++++++++++++++ recentquotes.json | 1 + .../com/example/init/controllers/Home.java | 6 ++ src/main/resources/templates/home.html | 8 ++ 4 files changed, 113 insertions(+) create mode 100644 api.json create mode 100644 recentquotes.json diff --git a/api.json b/api.json new file mode 100644 index 0000000..fa9b858 --- /dev/null +++ b/api.json @@ -0,0 +1,98 @@ +[ + { + "id": 849, + "question": "How to remove the docker container with container id mentioned in the command.", + "description": null, + "answers": { + "answer_a": "$ docker rm -rf 9b6343d3b5a0", + "answer_b": "$ docker -rm rf 9b6343d3b5a0", + "answer_c": "$ docker rm 9b6343d3b5a0", + "answer_d": null, + "answer_e": null, + "answer_f": null + }, + "multiple_correct_answers": "false", + "correct_answers": { + "answer_a_correct": "false", + "answer_b_correct": "false", + "answer_c_correct": "true", + "answer_d_correct": "false", + "answer_e_correct": "false", + "answer_f_correct": "false" + }, + "correct_answer": "answer_a", + "explanation": null, + "tip": null, + "tags": [ + { + "name": "Docker" + } + ], + "category": "Docker", + "difficulty": "Easy" + }, + { + "id": 26, + "question": "What is a daemon?", + "description": null, + "answers": { + "answer_a": "It is a generic name for e-mail servers on Linux. The most famous one is mailer-daemon", + "answer_b": "It is a program that keeps running on the background after it is called, answering to requests done by users and other programs.", + "answer_c": "It is an antivirus for Linux.", + "answer_d": "It is the generic name for any Linux server.", + "answer_e": null, + "answer_f": null + }, + "multiple_correct_answers": "false", + "correct_answers": { + "answer_a_correct": "false", + "answer_b_correct": "true", + "answer_c_correct": "false", + "answer_d_correct": "false", + "answer_e_correct": "false", + "answer_f_correct": "false" + }, + "correct_answer": "answer_b", + "explanation": null, + "tip": null, + "tags": [ + { + "name": "Linux" + } + ], + "category": "Linux", + "difficulty": "Medium" + }, + { + "id": 627, + "question": "How can we set infinite execution time for a PHP script?", + "description": null, + "answers": { + "answer_a": "Add set_time_limit() at the beginning of a script", + "answer_b": "Add set_time_limit(infinity) at the beginning of a script", + "answer_c": "Add set_time_limit(0) at the beginning of a script", + "answer_d": null, + "answer_e": null, + "answer_f": null + }, + "multiple_correct_answers": "false", + "correct_answers": { + "answer_a_correct": "false", + "answer_b_correct": "false", + "answer_c_correct": "true", + "answer_d_correct": "false", + "answer_e_correct": "false", + "answer_f_correct": "false" + }, + "correct_answer": "answer_a", + "explanation": null, + "tip": null, + "tags": [ + { + "name": "PHP" + } + ], + "category": "Code", + "difficulty": "Medium" + } + ] \ No newline at end of file diff --git a/recentquotes.json b/recentquotes.json new file mode 100644 index 0000000..fa6c1bc --- /dev/null +++ b/recentquotes.json @@ -0,0 +1 @@ +{"quoteText":"A lot of people give up just before theyre about to make it. You know you never know when that next obstacle is going to be the last one. ", "quoteAuthor":"Chuck Norris", "senderName":"", "senderLink":"", "quoteLink":"http://forismatic.com/en/2bc6cf0fd5/"} \ No newline at end of file diff --git a/src/main/java/com/example/init/controllers/Home.java b/src/main/java/com/example/init/controllers/Home.java index 6adc840..b34f5e3 100644 --- a/src/main/java/com/example/init/controllers/Home.java +++ b/src/main/java/com/example/init/controllers/Home.java @@ -10,4 +10,10 @@ public class Home { public String home() { return "home"; } + + @GetMapping("/index") + public String index() { + return "index"; + } + } diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index 033f5e1..6b510d5 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -8,5 +8,13 @@

Hello World

+ From 0c065c9264263b1ba84af46ee43edb690ccadd2c Mon Sep 17 00:00:00 2001 From: abdelqader-alomari Date: Sun, 24 Oct 2021 15:31:49 +0300 Subject: [PATCH 7/7] any --- build.gradle | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 71840fc..b66f0b1 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ repositories { } dependencies { - // implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' @@ -23,6 +23,8 @@ dependencies { runtimeOnly 'org.postgresql:postgresql' testImplementation 'org.springframework.boot:spring-boot-starter-test' // testImplementation 'org.springframework.security:spring-security-test' + implementation 'com.google.code.gson:gson:2.8.8' + } test {