Skip to content
Snippets Groups Projects
Commit 7a53207e authored by Beth Davies's avatar Beth Davies
Browse files

Change database methods and re-work repositories and controllers

parent ac396d67
No related branches found
No related tags found
No related merge requests found
Showing with 201 additions and 23 deletions
package polish_community_group_11.polish_community.profile.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
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.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import polish_community_group_11.polish_community.profile.models.Profile;
import polish_community_group_11.polish_community.profile.services.ProfileService;
import polish_community_group_11.polish_community.register.models.User;
import polish_community_group_11.polish_community.register.services.UserService;
@Controller
public class ProfileController {
@Autowired
private UserService userService;
@Autowired
private ProfileService profileService;
@GetMapping("/profile")
public ModelAndView profile(Authentication authentication) {
ModelAndView modelAndView = new ModelAndView("profile/profilePage");
String username = authentication.getName();
User user = userService.findByEmail(username);
Profile profile = profileService.getProfile(user.getId());
modelAndView.addObject("profile", profile);
return modelAndView;
}
@PostMapping("/update")
public String updateProfile(@ModelAttribute Profile profile, Authentication authentication) {
String username = authentication.getName();
User user = userService.findByEmail(username);
profile.setUserId(user.getId());
profileService.updateProfile(profile);
return "redirect:/profile";
}
}
package polish_community_group_11.polish_community.profile.repositories;public class ProfileRepositoryImpl {
package polish_community_group_11.polish_community.profile.repositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import polish_community_group_11.polish_community.feed.models.FeedImpl;
import polish_community_group_11.polish_community.profile.models.Profile;
@Repository
public class ProfileRepositoryImpl implements ProfileRepository {
private final JdbcTemplate jdbcTemplate;
private final RowMapper<Profile> rowMapper;
public ProfileRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.rowMapper = (rs, rowNum) -> new Profile(
rs.getInt("user_id"),
rs.getString("fullName"),
rs.getString("email"),
rs.getString("profile_picture"),
rs.getDate("dob").toLocalDate(),
rs.getString("bio"),
rs.getString("phone_number")
);
}
@Override
public void addProfile(Profile profile) {
String sql = "INSERT INTO user_profile (user_id) VALUES (?)";
jdbcTemplate.update(sql, profile.getUserId());
}
@Override
public Profile getProfile(Integer id) {
String sql = "SELECT u.id as user_id, " +
"u.fullname, u.email, u.dob, " +
"up.profile_picture, up.phone_number, up.bio " +
"FROM users u LEFT JOIN user_profile up ON " +
"u.id = up.user_id WHERE u.id = ?";
return jdbcTemplate.queryForObject(sql, rowMapper, id);
}
@Override
public void updateProfile(Profile profile) {
String sql = "UPDATE user_profile SET profile_picture = ?," +
" bio = ?, phone_number = ? WHERE user_id = ?";
jdbcTemplate.update(sql, profile.getProfilePicture(), profile.getBio(), profile.getPhoneNumber(), profile.getUserId());
}
};
package polish_community_group_11.polish_community.profile.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import polish_community_group_11.polish_community.profile.models.Profile;
import polish_community_group_11.polish_community.profile.repositories.ProfileRepository;
@Service
public class ProfileService {
private ProfileRepository profileRepository;
@Autowired
public ProfileService(ProfileRepository profileRepository) {
this.profileRepository = profileRepository;
}
public void addProfile(Profile profile) {profileRepository.addProfile(profile);}
public Profile getProfile(Integer userId) {
return profileRepository.getProfile(userId);
}
public void updateProfile(Profile profile) {
profileRepository.updateProfile(profile);
}
}
......@@ -4,6 +4,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import polish_community_group_11.polish_community.profile.models.Profile;
import polish_community_group_11.polish_community.profile.services.ProfileService;
import polish_community_group_11.polish_community.register.models.User;
import polish_community_group_11.polish_community.register.models.Role;
import polish_community_group_11.polish_community.register.services.UserService;
......@@ -20,6 +22,9 @@ public class RegisterController {
@Autowired
private RoleService roleService;
@Autowired
private ProfileService profileService;
// displaying the registration form using get request and ModelAndView
@GetMapping("/register")
public ModelAndView showRegistrationForm() {
......@@ -49,6 +54,10 @@ public class RegisterController {
// save user to the database
userService.saveUser(user);
// Profile newProfile = new Profile();
// newProfile.setUserId(user.getId());
// profileService.addProfile(newProfile);
// redirect to the login page
return "redirect:/login";
}
......
......@@ -103,11 +103,19 @@ INSERT INTO users (email, password, fullname, dob, role_id, organization) VALUES
("user10@gmail.com", "ChangeMe#1", "David Anderson", "1992-12-13", 2, "EcoGreen Enterprises");
INSERT INTO users (email, password, fullname, dob, role_id)
VALUES ('user@email.com', 'Abcdef1!', 'Jane Doe', '200-01-01', 1 );
-- insert the user and role id's of the created roles and user
INSERT INTO user_roles (user_id, role_id)
VALUES (1, 1);
# ALTER TABLE user_profile ADD CONSTRAINT foreign key polish_community.user_profile.user_profile_ibfk_1
# FOREIGN KEY (user_id) REFERENCES polish_community.users.id (id)
# ON UPDATE CASCADE;
CREATE TRIGGER UMustHaveProfile AFTER INSERT ON users FOR EACH ROW BEGIN INSERT INTO user_profile (user_id) VALUES (NEW.id);
-- Insert posts
INSERT INTO feed (post_image_url, post_title, post_description, post_time, user_id) VALUES
('https://example.com/image1.jpg', 'Post 1', 'Description for post 1', '2024-12-07', 1),
......
-- create schema for categories
drop table if exists information;
drop table if exists categories;
-- drop tables at the start of a session
DROP TABLE IF EXISTS information;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS event;
DROP TABLE IF EXISTS news;
DROP TABLE IF EXISTS post_likes;
DROP TABLE IF EXISTS feed_tags;
DROP TABLE IF EXISTS comment;
DROP TABLE IF EXISTS feed;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS user_profile;
DROP TABLE IF EXISTS user_roles;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS roles;
create table if not exists categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
......@@ -31,7 +42,7 @@ create table if not exists information
-- create schema for event
drop table if exists event;
create table if not exists event(
event_id int primary key auto_increment,
......@@ -65,13 +76,13 @@ create table if not exists news
-- drop tables at the start of a session
drop table if exists post_likes;
drop table if exists feed_tags;
drop table if exists feed;
drop table if exists tags;
DROP TABLE IF EXISTS user_roles;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS roles;
# drop table if exists post_likes;
# drop table if exists feed_tags;
# drop table if exists feed;
# drop table if exists tags;
# DROP TABLE IF EXISTS user_roles;
# DROP TABLE IF EXISTS users;
# DROP TABLE IF EXISTS roles;
-- schema for roles
......@@ -139,9 +150,12 @@ create table if not exists post_likes (
constraint unique_post_user unique (post_id, user_id)
) engine = InnoDB;
);
DROP TABLE IF EXISTS user_profile;
CREATE TABLE IF NOT EXISTS user_profile (
)
\ No newline at end of file
user_id INT PRIMARY KEY NOT NULL,
profile_picture TEXT,
bio TEXT,
phone_number VARCHAR(20),
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) engine = InnoDB;
.profile-container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.profile-picture {
width: 150px;
height: 150px;
border-radius: 50%;
object-fit: cover;
}
.profile-info {
margin-top: 20px;
}
button {
margin-top: 20px;
}
#editForm {
margin-top: 20px;
}
#editForm input,
#editForm textarea {
display: block;
width: 100%;
margin-bottom: 10px;
padding: 5px;
}
\ No newline at end of file
......@@ -6,12 +6,12 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
<link rel="stylesheet" href="/profile/profilePage.css">
<link rel="stylesheet" href="/profile/profile.css">
</head>
<body>
<section class="profile-container">
<section class="profile-container" layout:fragment="content">
<h3>Profile Page</h3>
<img src="${profile.profilePicture}" alt="Profile Picture" class="profile-picture">
<img th:src="${profile.profilePicture}" alt="Profile Picture" class="profile-picture">
<div class="profile-info">
<p><strong>Full Name:</strong> </p>
<p><strong>Email:</strong> </p>
......
package polish_community_group_11.polish_community.profile.controllers;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ProfileControllerTest {
@Test
void profile() {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment