Skip to content

Making camelcaseCh #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6deeccd
Log in working and Posts page loading.
pazzer Jun 2, 2025
135a5be
Fighting with git.
pazzer Jun 2, 2025
5cc2479
Tests passing.
pazzer Jun 2, 2025
e358171
Merge pull request #1 from jamesdismore/first_bugs
pazzer Jun 2, 2025
045ab91
fixed_db
jamesdismore Jun 3, 2025
bf49303
initial commit - starting html
maddyjm Jun 3, 2025
295c1a9
changed_database_added_to_post_model
jamesdismore Jun 3, 2025
b517375
added_seed_data_to_test
jamesdismore Jun 3, 2025
db4e01f
screened users, new users now diverted to registration page
maddyjm Jun 3, 2025
5125f96
continued working
maddyjm Jun 3, 2025
d5ff782
seed_data_working
jamesdismore Jun 3, 2025
021f8b1
post_and_user_models_working
jamesdismore Jun 3, 2025
5d7a8a3
friendship_and_comment_models_added
jamesdismore Jun 3, 2025
b7609f3
all_models_added
jamesdismore Jun 3, 2025
7aaf6c7
added_all_repos
jamesdismore Jun 3, 2025
1b3767d
attempting errors
maddyjm Jun 3, 2025
58e6e3a
error message shows - added dependancy
maddyjm Jun 3, 2025
8573b48
added_more_seed_data
jamesdismore Jun 4, 2025
722452c
fixed_seed_data
jamesdismore Jun 4, 2025
28dc283
Nav bar, templates, and routes
ItsProbablyAndy Jun 4, 2025
b558b3b
Added disabled links for home and settings
ItsProbablyAndy Jun 4, 2025
0193957
removing commented out code and adding explanations
maddyjm Jun 4, 2025
d2067b4
saving_to_change_branch
jamesdismore Jun 4, 2025
74879e7
html comments and cleaning
maddyjm Jun 4, 2025
dad822a
final changes
maddyjm Jun 4, 2025
2d52f5a
adding stuff so that if user is not saved in OUR database, they get r…
maddyjm Jun 4, 2025
5cfa806
made some changes so that a user can be greeted by their database inf…
maddyjm Jun 4, 2025
335e70e
final PUSH
maddyjm Jun 4, 2025
74def18
after_claire
jamesdismore Jun 4, 2025
9804588
james_merge_database_setup
jamesdismore Jun 4, 2025
698756c
Merge branch 'main' into new-user-page
jamesdismore Jun 4, 2025
d0e4733
Merge pull request #3 from jamesdismore/new-user-page
jamesdismore Jun 4, 2025
d5b6366
Added placeholder avatar
ItsProbablyAndy Jun 4, 2025
6d5fa84
merging changes between main and htmlnavbar
ItsProbablyAndy Jun 4, 2025
acae8e0
fixed_autowire_in_posts
jamesdismore Jun 4, 2025
a06dc78
fixing_users_post_merge
jamesdismore Jun 4, 2025
701c923
redirecting user not logged in, and also got rid of fake index page
maddyjm Jun 4, 2025
37c4ca0
Resolved local bugs; building successfully.
pazzer Jun 4, 2025
72dcc4c
Settings page loading, and changes taking effect.
pazzer Jun 5, 2025
b1c1152
MVP Settings page up and running.
pazzer Jun 5, 2025
e076f4a
Model fields now use camelCase, rather than snake_case.
pazzer Jun 5, 2025
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
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ dependency-reduced-pom.xml
.factorypath
.project
.settings/
application.yaml
application.yml
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
<properties>
<java.version>21</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -42,6 +48,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
Expand All @@ -66,6 +76,11 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -95,6 +110,11 @@
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
Binary file added src/main/java/com/.DS_Store
Binary file not shown.
Binary file added src/main/java/com/makersacademy/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package com.makersacademy.acebook.config;

import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.UserRepository;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.IOException;
import java.util.Optional;

import static org.springframework.security.config.Customizer.withDefaults;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

@Autowired
private UserRepository userRepository;

@Value("${okta.oauth2.issuer}")
private String issuer;
@Value("${okta.oauth2.client-id}")
Expand All @@ -31,7 +41,7 @@ public class SecurityConfiguration {
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/", "/images/**").permitAll()
.anyRequest().authenticated()
Expand All @@ -40,7 +50,15 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {
.successHandler(new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.sendRedirect("/users/after-login");
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
Optional<User> user = userRepository.findUserByUsername(username);
if (user.isEmpty()) {
response.sendRedirect("/users/newUser"); // Redirect if user is missing
} else {
response.sendRedirect("/users/after-login");
//^^ if user is not in OUR database, they get redirected
}
}
})
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
package com.makersacademy.acebook.controller;

import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.UserRepository;
import org.springframework.security.core.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Optional;

@Controller
public class HomeController {
@RequestMapping(value = "/")
public RedirectView index() {
return new RedirectView("/posts");

@Autowired
UserRepository userRepository;

@ModelAttribute("user")
Optional<User> findUser(Authentication authentication) {
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
return userRepository.findUserByUsername(username);
}

// Routes ------

@GetMapping(value = "/")
public String index(@ModelAttribute("user") Optional<User> user) {
return user.
map(_user -> "redirect:/users/" + _user.getUsername())
.orElse("redirect:/users/newUser");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// STOP PEOPLE BEING SNEAKY IF NOT REGISTERED
/*
import com.makersacademy.acebook.repository.UserRepository;
import org.springframework.security.core.Authentication; <- replaces the tomcat one
@Autowired
UserRepository userRepository;

public String exampleMethod(Authentication authentication)
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
Optional<User> user = userRepository.findUserByUsername(username);
if (user.isEmpty()) {
return "redirect:/users/newUser";
}
return "whateverhtmlyouwant"
*/

package com.makersacademy.acebook.controller;

import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.UserRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Optional;


@Controller
public class NavigationController {

@Autowired
UserRepository userRepository;

@ModelAttribute("user")
public Optional<User> getUser(Authentication authentication) {
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
return userRepository.findUserByUsername(username);
}


// Routes ------

@GetMapping("/profile")
public String profile(@ModelAttribute("user") Optional<User> user) {
return user.isEmpty() ? "redirect:/users/newUser" : "profile";
}

@GetMapping("/settings")
public String settings(@ModelAttribute("user") Optional<User> user) {
return user.isEmpty() ? "redirect:users/newUser" : "settings";
}

@PostMapping("/settings")
public String settings(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "settings";
} else {
userRepository.save(user);
return "redirect:/posts";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
package com.makersacademy.acebook.controller;

import com.makersacademy.acebook.model.Post;
import com.makersacademy.acebook.model.User;

import com.makersacademy.acebook.repository.UserRepository;
import com.makersacademy.acebook.repository.PostRepository;
import org.springframework.security.core.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

@Controller
public class PostsController {

@Autowired
PostRepository repository;

@Autowired
UserRepository userRepository;



@GetMapping("/posts")
public String index(Model model) {
public String index(Model model, Authentication authentication) {
DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
String username = (String) principal.getAttributes().get("email");
// code above to get email from the authenticator
Optional<User> user = userRepository.findUserByUsername(username);
// ^^ optional user, theoretical

if (user.isEmpty()) {
return "redirect:/users/newUser"; // Redirect if not registered
}
// ^^ if the user is not saved in our database, they get redirected to the registration page

Long userId = user.get().getId(); // getting id from database - checking that id is connected
String email = user.get().getUsername(); // getting email in same way
// public String index(Model model, @AuthenticationPrincipal OAuth2User principal) {
// String userName = principal.getAttribute("email");
// Optional<User> user = userRepository.findUserByUsername(userName);
// long id = user.map(User::getId).orElse(999999999999L);
// model.addAttribute("userID",id);

Iterable<Post> posts = repository.findAll();
model.addAttribute("posts", posts);
model.addAttribute("post", new Post());
return "posts/index";

// code below to get userId and email from database
model.addAttribute("userId", userId);
model.addAttribute("email", email);

return "index";
}

@PostMapping("/posts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

import com.makersacademy.acebook.model.User;
import com.makersacademy.acebook.repository.UserRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@RestController
import java.util.Optional;

@Controller
@RequestMapping
public class UsersController {
@Autowired
UserRepository userRepository;
Expand All @@ -21,10 +28,33 @@ public RedirectView afterLogin() {
.getPrincipal();

String username = (String) principal.getAttributes().get("email");
userRepository
.findUserByUsername(username)
.orElseGet(() -> userRepository.save(new User(username)));
Optional<User> user = userRepository.findUserByUsername(username);
if (user.isPresent()){
return new RedirectView("/posts"); // redirect to posts if registered
} else {
return new RedirectView("/users/newUser"); // redirect to registration if missing
}

}
@GetMapping("/users/newUser")
public String afterSignUp(@ModelAttribute("our_user") User user) {
DefaultOidcUser principal = (DefaultOidcUser) SecurityContextHolder
.getContext()
.getAuthentication()
.getPrincipal();

String username = (String) principal.getAttributes().get("email");
user.setUsername(username); // automatically fills in username on new user field, perhaps make this not allowed to change? once we add FN and LN
return "newUser";

return new RedirectView("/posts");
}
@PostMapping("/users/newUser")
public String saveNewUser(@Valid @ModelAttribute("our_user") User user, BindingResult result) {
if (result.hasErrors()) { // error messages come from class constraints (needs dependency)
return "newUser"; //stays on page
} else {
userRepository.save(user); // this saves user to database, eventually fill out parameters as database changes to include first name, last name, dob etc
return "redirect:/posts"; // redirects to posts
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/makersacademy/acebook/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.makersacademy.acebook.model;


import jakarta.persistence.*;
import lombok.Data;

import java.sql.Timestamp;
import java.time.Instant;

@Data
@Entity
@Table(name = "comments")
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String content;

@Column(name = "user_id")
private int userId;

@Column(name = "post_id")
private int postId;

@Column(name = "comment_timestamp")
private Timestamp commentTimestamp;

public Comment(String content, int userId, int postId){
this.content = content;
this.userId = userId;
this.postId = postId;
this.commentTimestamp = Timestamp.from(Instant.now());
}
}
Loading