Skip to content

User model #176

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 12 commits into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.22.0</version> <!-- Use the latest version available -->
<scope>test</scope> <!-- Ensure Mockito is only included in test scope -->
</dependency>



</dependencies>



</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home", "/css/**").permitAll()
.antMatchers("/posts").hasRole("USER")
.antMatchers("/users").permitAll()
.and().formLogin();
.and().formLogin().defaultSuccessUrl("/posts", true);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.makersacademy.acebook.controller;

import com.makersacademy.acebook.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class HomeController {

@RequestMapping(value = "/")
public RedirectView index() {
return new RedirectView("/posts");
public String index(Model model) {
model.addAttribute("user", new User());
return "home"; // Ensure this matches the name of your Thymeleaf template
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;
import java.util.Date;


@Controller
public class PostsController {
Expand All @@ -26,7 +30,15 @@ public String index(Model model) {

@PostMapping("/posts")
public RedirectView create(@ModelAttribute Post post) {
post.setCreatedAt(new Date());

repository.save(post);
return new RedirectView("/posts");
}

@PostMapping("/posts/{id}/delete")
public RedirectView delete(@PathVariable Long id){
repository.deleteById(id);
return new RedirectView("/posts");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import com.makersacademy.acebook.repository.AuthoritiesRepository;
import com.makersacademy.acebook.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
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;


@Controller
public class UsersController {

Expand All @@ -20,7 +23,7 @@ public class UsersController {
@Autowired
AuthoritiesRepository authoritiesRepository;

@GetMapping("/users/new")
@GetMapping("/register")
public String signup(Model model) {
model.addAttribute("user", new User());
return "users/new";
Expand All @@ -33,4 +36,35 @@ public RedirectView signup(@ModelAttribute User user) {
authoritiesRepository.save(authority);
return new RedirectView("/login");
}

@GetMapping("/profile")
public String showProfile(Model model) {
User currentUser = getCurrentUser();
model.addAttribute("user", currentUser);
return "users/profile";
}

@PostMapping("/profile")
public RedirectView updateProfile(@ModelAttribute User user) {
User currentUser = getCurrentUser();
currentUser.setMobileNumber(user.getMobileNumber());
currentUser.setEmailAddress(user.getEmailAddress());
currentUser.setGender(user.getGender());
currentUser.setCountry(user.getCountry());
currentUser.setLanguage(user.getLanguage());
userRepository.save(currentUser);
return new RedirectView("/profile");
}

private User getCurrentUser() {
// Assuming you're using Spring Security to manage user authentication
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username;
if (principal instanceof UserDetails) {
username = ((UserDetails) principal).getUsername();
} else {
username = principal.toString();
}
return userRepository.findByUsername(username);
}
}
26 changes: 10 additions & 16 deletions src/main/java/com/makersacademy/acebook/model/Post.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package com.makersacademy.acebook.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GenerationType;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import javax.persistence.*;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "POSTS")
public class Post {

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

public Post() {}

public Post(String content) {
this.content = content;
}
public String getContent() { return this.content; }
public void setContent(String content) { this.content = content; }

private String title;
private String content;
private Date createdAt;
}
40 changes: 11 additions & 29 deletions src/main/java/com/makersacademy/acebook/model/User.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.makersacademy.acebook.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GenerationType;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static java.lang.Boolean.TRUE;
import javax.persistence.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "USERS")
public class User {
Expand All @@ -19,26 +17,10 @@ public class User {
private Long id;
private String username;
private String password;
private boolean enabled;

public User() {
this.enabled = TRUE;
}

public User(String username, String password) {
this.username = username;
this.password = password;
this.enabled = TRUE;
}

public User(String username, String password, boolean enabled) {
this.username = username;
this.password = password;
this.enabled = enabled;
}

public String getUsername() { return this.username; }
public String getPassword() { return this.password; }
public void setUsername(String username) { this.username = username; }
public void setPassword(String password) { this.password = password; }
private boolean enabled = true;
private String mobileNumber;
private String emailAddress;
private String gender;
private String country;
private String language;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

public interface UserRepository extends CrudRepository<User, Long> {

User findByUsername(String username);
}
4 changes: 3 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ spring.profiles.active=dev
spring.data.rest.base-path=/api
spring.datasource.platform=postgres
spring.jpa.hibernate.ddl-auto=validate
logging.level.org.springframework.web: DEBUG
logging.level.org.springframework.web: DEBUG#
spring.datasource.username=
spring.datasource.password=
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE posts
ADD COLUMN title varchar(100);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE posts
ADD COLUMN user_id int,
ADD CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE
;
2 changes: 2 additions & 0 deletions src/main/resources/db/migration/V5__fixed_user_id_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE posts
ALTER COLUMN user_id TYPE bigint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE posts
ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
6 changes: 6 additions & 0 deletions src/main/resources/db/migration/V7__alter_user_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE USERS
ADD COLUMN mobile_number VARCHAR(15),
ADD COLUMN email_address VARCHAR(255),
ADD COLUMN gender VARCHAR(10),
ADD COLUMN country VARCHAR(50),
ADD COLUMN language VARCHAR(50);
Loading